all prepare work done

This commit is contained in:
yyc12345 2023-02-09 17:16:58 +08:00
parent 7fe2bd8f5c
commit 6bda076a1d
4 changed files with 76 additions and 18 deletions

View File

@ -2,9 +2,28 @@
#include <cinttypes>
#include <cstdint>
#include <cstdarg>
#include <type_traits>
namespace LibCmo {
namespace EnumHelper {
template<typename TEnum>
inline TEnum FlagEnumAdd(TEnum e, ...) {
TEnum result = e;
va_list argptr;
va_start(argptr, e);
result = static_cast<TEnum>(static_cast<std::underlying_type_t<TEnum>>(result) | static_cast<std::underlying_type_t<TEnum>>(va_arg(argptr, TEnum)));
va_end(argptr);
return result;
}
template<typename TEnum>
inline bool FlagEnumHas(TEnum e, TEnum probe) {
return static_cast<bool>(static_cast<std::underlying_type_t<TEnum>>(e) & static_cast<std::underlying_type_t<TEnum>>(probe));
}
}
using CK_ID = uint32_t;
enum class CK_CLASSID : uint32_t {

View File

@ -8,6 +8,8 @@ namespace Unvirt {
#pragma region universal enum name
const char c_InvalidEnumName[] = "[undefined]";
namespace EnumDesc {
const EnumDescPairArray<LibCmo::CK_FILE_WRITEMODE> CK_FILE_WRITEMODE{
{ LibCmo::CK_FILE_WRITEMODE::CKFILE_UNCOMPRESSED, "CKFILE_UNCOMPRESSED" },
@ -36,18 +38,6 @@ namespace Unvirt {
};
}
template<typename TEnum>
void GetEnumName(const EnumDescPairArray<TEnum> desc, std::string& strl, TEnum val) {
}
template<typename TEnum>
void GetFlagEnumName(const EnumDescPairArray<TEnum> desc, std::string& strl, TEnum val) {
}
#pragma endregion
#pragma region class id and ck error
@ -116,24 +106,26 @@ namespace Unvirt {
};
void GetCkErrorName(std::string& strl, LibCmo::CKERROR err) {
strl.clear();
const std::array<const char*, 2>* pErrDesc = GetEnumData<LibCmo::CKERROR, std::array<const char*, 2>>(
_CkErrorData, err
);
if (pErrDesc != nullptr) {
strl = pErrDesc->front();
} else {
strl = c_InvalidEnumName;
}
}
void GetCkErrorDescription(std::string& strl, LibCmo::CKERROR err) {
strl.clear();
const std::array<const char*, 2>* pErrDesc = GetEnumData<LibCmo::CKERROR, std::array<const char*, 2>>(
_CkErrorData, err
);
if (pErrDesc != nullptr) {
strl = pErrDesc->back();
} else {
strl = c_InvalidEnumName;
}
}
@ -209,27 +201,30 @@ namespace Unvirt {
};
void GetClassIdName(std::string& strl, LibCmo::CK_CLASSID cls) {
strl.clear();
const std::vector<const char*>* pHierarchy = GetEnumData<LibCmo::CK_CLASSID, std::vector<const char*>>(
_CkClassHierarchy, cls
);
if (pHierarchy != nullptr) {
strl = pHierarchy->back();
} else {
strl = c_InvalidEnumName;
}
}
void GetClassIdHierarchy(std::string& strl, LibCmo::CK_CLASSID cls) {
strl.clear();
const std::vector<const char*>* pHierarchy = GetEnumData<LibCmo::CK_CLASSID, std::vector<const char*>>(
_CkClassHierarchy, cls
);
if (pHierarchy != nullptr) {
strl.clear();
for (auto it = pHierarchy->begin(); it != pHierarchy->end(); ++it) {
if (it != pHierarchy->begin()) strl += " -> ";
strl += (*it);
}
} else {
strl = c_InvalidEnumName;
}
}

View File

@ -7,6 +7,10 @@
namespace Unvirt {
namespace AccessibleValue {
extern const char c_InvalidEnumName[];
#pragma region universal enum name
template<typename TEnum>
using EnumDescPairArray = std::vector<std::pair<TEnum, const char*>>;
@ -17,9 +21,40 @@ namespace Unvirt {
}
template<typename TEnum>
void GetEnumName(const EnumDescPairArray<TEnum> desc, std::string& strl, TEnum val);
void GetEnumName(const EnumDescPairArray<TEnum>& desc, std::string& strl, TEnum val) {
for (auto it = desc.begin(); it != desc.end(); ++it) {
if ((*it).first == val) {
strl = (*it).second;
return;
}
}
strl = c_InvalidEnumName;
}
template<typename TEnum>
void GetFlagEnumName(const EnumDescPairArray<TEnum> desc, std::string& strl, TEnum val);
void GetFlagEnumName(const EnumDescPairArray<TEnum>& desc, std::string& strl, TEnum val) {
strl.clear();
for (auto it = desc.begin(); it != desc.end(); ++it) {
// if it have exacelt same entry, return directly
if ((*it).first == val) {
strl = (*it).second;
return;
}
// check flag match
if (LibCmo::EnumHelper::FlagEnumHas(val, (*it).first)) {
// matched, add it
if (strl.size() != 0u) strl += ", ";
strl += (*it).second;
}
}
if (strl.size() == 0u) {
// nothing was gotten. set to undefined
strl = c_InvalidEnumName;
} // otherwise return directly
}
#pragma endregion
void GetClassIdName(std::string& strl, LibCmo::CK_CLASSID cls);
void GetCkErrorName(std::string& strl, LibCmo::CKERROR err);

View File

@ -7,5 +7,14 @@ int main(int argc, char* argv[]) {
printf("%s\n", test.c_str());
Unvirt::AccessibleValue::GetCkErrorDescription(test, LibCmo::CKERROR::CKERR_OBSOLETEVIRTOOLS);
printf("%s\n", test.c_str());
Unvirt::AccessibleValue::GetEnumName<LibCmo::CK_FO_OPTIONS>(Unvirt::AccessibleValue::EnumDesc::CK_FO_OPTIONS, test, LibCmo::CK_FO_OPTIONS::CK_FO_RENAMEOBJECT);
printf("%s\n", test.c_str());
Unvirt::AccessibleValue::GetFlagEnumName<LibCmo::CK_LOAD_FLAGS>(Unvirt::AccessibleValue::EnumDesc::CK_LOAD_FLAGS, test, LibCmo::CK_LOAD_FLAGS::CK_LOAD_DEFAULT);
printf("%s\n", test.c_str());
auto v = LibCmo::EnumHelper::FlagEnumAdd(LibCmo::CK_LOAD_FLAGS::CK_LOAD_ANIMATION, LibCmo::CK_LOAD_FLAGS::CK_LOAD_ASCHARACTER);
Unvirt::AccessibleValue::GetFlagEnumName<LibCmo::CK_LOAD_FLAGS>(Unvirt::AccessibleValue::EnumDesc::CK_LOAD_FLAGS, test, v);
printf("%s\n", test.c_str());
return 0;
}