continue refactor project

This commit is contained in:
2023-09-16 22:38:21 +08:00
parent 1ddeeb3b68
commit 3c8266e7dd
8 changed files with 215 additions and 60 deletions

View File

@ -1,5 +1,8 @@
#pragma once
#include "XTypes.hpp"
#include "../CK2/CKContext.hpp"
#include "../CK2/MgrImpls/CKObjectManager.hpp"
#include "../CK2/ObjImpls/CKObject.hpp"
#include <type_traits>
namespace LibCmo::XContainer {
@ -46,4 +49,84 @@ namespace LibCmo::XContainer {
}
template<class _Ty>
constexpr bool GeneralXArrayCheck_TypeCheck() {
return std::is_same_v<_Ty, CK2::CK_ID> || std::is_same_v<_Ty, CK2::ObjImpls::CKObject*>;
}
template<class _Ty, bool _IsPre>
bool GeneralXArrayCheck_ItemCheck(const _Ty& item, CK2::CKContext* ctx) {
static_assert(GeneralXArrayCheck_TypeCheck<_Ty>());
if (ctx == nullptr) return false;
if constexpr (_IsPre) {
CK2::ObjImpls::CKObject* obj = nullptr;
if constexpr (std::is_same_v<_Ty, CK2::CK_ID>) {
obj = ctx->GetObject(item);
if (obj == nullptr) return false;
} else {
obj = item;
}
if (EnumsHelper::Has(obj->GetObjectFlags(), CK2::CK_OBJECT_FLAGS::CK_OBJECT_TOBEDELETED)) return false;
} else {
CK2::MgrImpls::CKObjectManager* objmgr = ctx->GetObjectManager();
if constexpr (std::is_same_v<_Ty, CK2::CK_ID>) {
if (!objmgr->IsObjectSafe(item)) return false;
} else {
if (!objmgr->IsObjectPointerSafe(item)) return false;
}
}
return true;
}
namespace NSXObjectArray {
void PreDeletedCheck(XObjectArray& objarray, CK2::CKContext* ctx) {
if (ctx == nullptr) return;
std::erase_if(objarray, [ctx](const CK2::CK_ID& item) -> bool {
return GeneralXArrayCheck_ItemCheck<CK2::CK_ID, true>(item, ctx);
});
}
void PostDeletedCheck(XObjectArray& objarray, CK2::CKContext* ctx) {
if (ctx == nullptr) return;
std::erase_if(objarray, [ctx](const CK2::CK_ID& item) -> bool {
return GeneralXArrayCheck_ItemCheck<CK2::CK_ID, false>(item, ctx);
});
}
}
namespace NSXObjectPointerArray {
void PreDeletedCheck(XObjectPointerArray& objarray, CK2::CKContext* ctx) {
if (ctx == nullptr) return;
std::erase_if(objarray, [ctx](CK2::ObjImpls::CKObject* const& item) -> bool {
return GeneralXArrayCheck_ItemCheck<CK2::ObjImpls::CKObject*, true>(item, ctx);
});
}
void PostDeletedCheck(XObjectPointerArray& objarray, CK2::CKContext* ctx) {
if (ctx == nullptr) return;
std::erase_if(objarray, [ctx](CK2::ObjImpls::CKObject* const& item) -> bool {
return GeneralXArrayCheck_ItemCheck<CK2::ObjImpls::CKObject*, false>(item, ctx);
});
}
void PreDeletedCheck(XList<CK2::ObjImpls::CKObject*>& objarray, CK2::CKContext* ctx) {
if (ctx == nullptr) return;
std::erase_if(objarray, [ctx](CK2::ObjImpls::CKObject* const& item) -> bool {
return GeneralXArrayCheck_ItemCheck<CK2::ObjImpls::CKObject*, true>(item, ctx);
});
}
void PostDeletedCheck(XList<CK2::ObjImpls::CKObject*>& objarray, CK2::CKContext* ctx) {
if (ctx == nullptr) return;
std::erase_if(objarray, [ctx](CK2::ObjImpls::CKObject* const& item) -> bool {
return GeneralXArrayCheck_ItemCheck<CK2::ObjImpls::CKObject*, false>(item, ctx);
});
}
}
}

View File

@ -4,6 +4,7 @@
#include <string>
#include <vector>
#include <unordered_map>
#include <list>
/**
* @brief The X container part of LibCmo.
@ -34,6 +35,13 @@ namespace LibCmo::XContainer {
*/
template<typename T>
using XArray = std::vector<T>;
/**
* @brief Double-linked list.
* @tparam T Element Type.
*/
template<typename T>
using XList = std::list<T>;
/**
@brief Container class for CKObject Id's.
@ -105,5 +113,54 @@ namespace LibCmo::XContainer {
}
namespace NSXObjectArray {
/**
* @brief Check Object ID validation and remove invalid IDs before deletion.
* @param objarray
* @param ctx
*/
void PreDeletedCheck(XObjectArray& objarray, CK2::CKContext* ctx);
/**
* @brief Check Object ID validation and remove invalid IDs after deletion.
* @param objarray
* @param ctx
*/
void PostDeletedCheck(XObjectArray& objarray, CK2::CKContext* ctx);
}
namespace NSXObjectPointerArray {
/**
* @brief Check Object pointer validation and remove invalid pointers before deletion.
* @param objarray
* @param ctx
*/
void PreDeletedCheck(XObjectPointerArray& objarray, CK2::CKContext* ctx);
/**
* @brief Check Object pointer validation and remove invalid pointers after deletion.
* @param objarray
* @param ctx
* @remark The performance of this function is extremely low. Use it carefully.
*/
void PostDeletedCheck(XObjectPointerArray& objarray, CK2::CKContext* ctx);
/**
* @brief Check Object pointer validation and remove invalid pointers before deletion.
* @param objarray
* @param ctx
*/
void PreDeletedCheck(XList<CK2::ObjImpls::CKObject*>& objarray, CK2::CKContext* ctx);
/**
* @brief Check Object pointer validation and remove invalid pointers after deletion.
* @param objarray
* @param ctx
* @remark The performance of this function is extremely low. Use it carefully.
*/
void PostDeletedCheck(XList<CK2::ObjImpls::CKObject*>& objarray, CK2::CKContext* ctx);
}
}