libcmo21/LibCmo/XContainer/XTypes.hpp

264 lines
10 KiB
C++
Raw Normal View History

2023-08-22 15:30:26 +08:00
#pragma once
2023-09-16 18:31:25 +08:00
#include "../CK2/CKTypes.hpp"
2023-08-22 15:30:26 +08:00
#include <string>
#include <vector>
#include <unordered_map>
2023-09-16 22:38:21 +08:00
#include <list>
2023-08-22 15:30:26 +08:00
/**
* @brief The X container part of LibCmo.
* These classes are prefixed with X in original Virtools SDK.
*/
namespace LibCmo::XContainer {
/**
2024-08-19 10:43:30 +08:00
* @brief The representation of an UTF8 string (an array of UTF8 character ended by a '\0').
* @remarks
* \li This class now is implemented by \c std::u8string.
2023-08-22 15:30:26 +08:00
*/
using XString = std::u8string;
2023-08-22 15:30:26 +08:00
/**
2024-08-19 10:43:30 +08:00
* @brief The representation of a set of bit flags (memory optimized to reduce occupied size).
* @remarks
* \li This class is implemented by specialized \c std::vector<bool>.
* \li This class define a set of bit flags that may be treated as a virtual array but are stored in an efficient manner.
* \li The class has methods to set, clear and return the i-th bit, resize the array, etc.
2023-08-22 15:30:26 +08:00
*/
using XBitArray = std::vector<bool>;
/**
2024-08-19 10:43:30 +08:00
* @brief The representation of an array.
* @tparam T Element type.
* @remarks
* \li This class now use \c std::vector<T>.
* \li XSArray now redirect to this. Because XSArray behaves exactly like the XArray,
* but it only occupies less memory. This feature also can be done by executing \c std::vector<T>::shrink_to_fit().
* \li XClassArray now redirect to this. Because it is designed to hold structure or class
* which have something specific to do on the construction, deletion or recopy.
* However this feature can be resolved by writing proper ctor, copy ctor, move ctor and dtor.
2023-08-22 15:30:26 +08:00
*/
template<typename T>
using XArray = std::vector<T>;
2023-09-16 22:38:21 +08:00
/**
2024-08-19 10:43:30 +08:00
* @brief The representation of an double-linked list.
* @tparam T Element type.
2023-09-16 22:38:21 +08:00
*/
template<typename T>
using XList = std::list<T>;
2023-08-22 15:30:26 +08:00
/**
2024-08-19 10:43:30 +08:00
* @brief The representation of an CKObject ID array.
* @remarks
* \li This class is implemented as \c XArray<CK2::CK_ID>
* \li Supports for Check, Load, Save, Add, Remove, Find functions in the Object CK_ID array.
* \li XSObjectArray now redirect to this. See the relation between XArray and XSArray for more reason.
* \li CKObjectArray now redirect to this according to its requirements (use pointer or use ID).
* @see XObjectPointerArray
* @todo Add implementations of functions introduced in remarks to the patch namespace.
2023-08-22 15:30:26 +08:00
*/
using XObjectArray = XArray<CK2::CK_ID>;
/**
2024-08-19 10:43:30 +08:00
* @brief The representation of an CKObject pointer array.
* @remarks
* \li This class is implemented as \c XArray<CK2::ObjImpls::CKObject*>
* \li Exactly same as XObjectArray class, but stores pointers, not IDs (more efficient to retrieve the objects).
* \li Supports for Check, Load, Save, Add, Remove, Find functions in the CKObject pointer array.
* \li XSObjectPointerArray now redirect to this. See the relation between XArray and XSArray for more reason.
* \li CKObjectArray now redirect to this according to its requirements (use pointer or use ID).
* @see XObjectArray
* @todo Add implementations of functions introduced in remarks to the patch namespace.
2023-08-22 15:30:26 +08:00
*/
using XObjectPointerArray = XArray<CK2::ObjImpls::CKObject*>;
2023-08-22 15:30:26 +08:00
/**
2024-08-19 10:43:30 +08:00
* @brief The representation of an hash table container.
* @tparam K The type of the key
* @tparam T The type of element to insert
* @tparam H The hash function to hash the key
* @tparam Eq The equal function to the key
* @remarks
* \li This class now is implemented by \c std::unordered_map<K,T,H,Eq>.
* \li XNHashTable now redirect to this. Because it seems like the old version of XHashTable.
* \li XSHashTable now redirect to this. See the relation between XArray and XSArray for more reason.
2023-08-22 15:30:26 +08:00
*/
template<class K, class T, class H = std::hash<K>, class Eq = std::equal_to<K>>
using XHashTable = std::unordered_map<K, T, H, Eq>;
2023-09-16 18:31:25 +08:00
// ========== Patch Section ==========
2024-08-19 10:43:30 +08:00
/**
* @brief The patch namespace for XBitArray
* @details This namespace provides XBitArray functions which presented in original Virtools SDK,
* because XBitArray use standard library container as its implementation.
*/
2023-09-16 18:31:25 +08:00
namespace NSXBitArray {
/**
* @brief Resize XBitArray to the new size. Initial value is false.
2024-08-19 10:43:30 +08:00
* @param[in] ba The XBitArray to be resized.
* @param[in] newsize New size (the count of bit flags, \b not the space in byte).
*/
void Resize(XBitArray& ba, CKDWORD newsize);
/**
2024-08-19 10:43:30 +08:00
* @brief Merge 2 XBitArray like performing <TT>thisba | thatba</TT> for each flags.
* @details
* This XBitArray will be resized at least equal with merged XBitArray if its size is lower than merged XBitArray
2024-08-19 10:43:30 +08:00
* @param[in] thisba The merging XBitArray.
* If this XBitArray will be resized at least equal with merged XBitArray before merging.
* @param[in] thatba Other XBitArray will be merged.
*/
void Or(XBitArray& thisba, const XBitArray& thatba);
2023-09-16 18:31:25 +08:00
/**
* @brief Check whether bit flag is set.
2024-08-19 10:43:30 +08:00
* @param[in] ba The XBitArray for checking
* @param[in] n The position for checking.
* @return True if set. False if not set or out of range.
*/
bool IsSet(const XBitArray& ba, CKDWORD n);
/**
* @brief Set specified position to true in XBitArray. Auto resize if no space to set.
2024-08-19 10:43:30 +08:00
* @param[in] ba The XBitArray for setting.
* @param[in] n The position for setting.
*/
void Set(XBitArray& ba, CKDWORD n);
/**
2024-08-19 10:43:30 +08:00
* @brief Unset specified position to true in XBitArray. Do nothing if out of range.
* @param[in] ba The XBitArray for unsetting.
* @param[in] n The position for unsetting.
*/
void Unset(XBitArray& ba, CKDWORD n);
/**
2024-08-19 10:43:30 +08:00
* @brief Find the n-th bit which is set(1) bit from the lowest position.
* @param[in] ba The The XBitArray for finding.
* @param[in] n N-th
* @param[out] got The position we found.
* @return True if we found, otherwise false.
2023-09-16 18:31:25 +08:00
*/
bool GetSetBitPosition(const XBitArray& ba, CKDWORD n, CKDWORD& got);
/**
2024-08-19 10:43:30 +08:00
* @brief Find the n-th bit which is unset(0) bit from the lowest position.
* @param[in] ba The The XBitArray for finding.
* @param[in] n N-th
* @param[out] got The position we found.
* @return True if we found, otherwise false.
2023-09-16 18:31:25 +08:00
*/
bool GetUnsetBitPosition(const XBitArray& ba, CKDWORD n, CKDWORD& got);
}
2024-08-19 10:43:30 +08:00
/**
* @brief The patch namespace for XString
* @details This namespace provides XString functions which presented in original Virtools SDK,
* because XString use standard library container as its implementation.
*/
2023-09-16 18:31:25 +08:00
namespace NSXString {
/**
2024-08-19 10:43:30 +08:00
* @brief Return CKSTRING representation of XString.
* @param[in] strl The XString need to be output.
* @return Return the CKSTRING representation of XString.
* If XString is blank, return nullptr instead.
2023-09-16 18:31:25 +08:00
*/
CKSTRING ToCKSTRING(const XString& strl);
/**
2024-08-19 10:43:30 +08:00
* @brief Copy CKSTRING representation to XString.
* @param[in] strl The string dest.
* @param[in] s The CKSTRING need to be copied. nullptr will clear string dest.
2023-09-16 18:31:25 +08:00
*/
void FromCKSTRING(XString& strl, CKSTRING s);
}
2024-08-19 10:43:30 +08:00
/**
* @brief The patch namespace for XObjectArray
* @details This namespace provides XObjectArray functions which presented in original Virtools SDK,
* because XObjectArray use standard library container as its implementation.
*/
2023-09-16 22:38:21 +08:00
namespace NSXObjectArray {
/**
* @brief Check Object ID validation and remove invalid IDs before deletion.
2024-08-19 10:43:30 +08:00
* @param[in] objarray The array to be checked.
* @param[in] ctx Associated CKContext.
2023-09-16 22:38:21 +08:00
*/
void PreDeletedCheck(XObjectArray& objarray, CK2::CKContext* ctx);
/**
* @brief Check Object ID validation and remove invalid IDs after deletion.
2024-08-19 10:43:30 +08:00
* @param[in] objarray The array to be checked.
* @param[in] ctx Associated CKContext.
* @warning The performance of this function is extremely bad. Use this carefully.
2023-09-16 22:38:21 +08:00
*/
void PostDeletedCheck(XObjectArray& objarray, CK2::CKContext* ctx);
/**
* @brief Check Object pointer validation and remove invalid pointers before deletion.
2024-08-19 10:43:30 +08:00
* @param[in] objarray The array to be checked.
* @param[in] ctx Associated CKContext.
*/
void PreDeletedCheck(XList<CK2::CK_ID>& objarray, CK2::CKContext* ctx);
/**
* @brief Check Object pointer validation and remove invalid pointers after deletion.
2024-08-19 10:43:30 +08:00
* @param[in] objarray The array to be checked.
* @param[in] ctx Associated CKContext.
* @warning The performance of this function is extremely bad. Use this carefully.
*/
void PostDeletedCheck(XList<CK2::CK_ID>& objarray, CK2::CKContext* ctx);
2023-09-16 22:38:21 +08:00
}
2024-08-19 10:43:30 +08:00
/**
* @brief The patch namespace for XObjectPointer
* @details This namespace provides XObjectPointer functions which presented in original Virtools SDK,
* because XObjectPointer use standard library container as its implementation.
*/
2023-09-16 22:38:21 +08:00
namespace NSXObjectPointerArray {
2023-09-19 22:32:07 +08:00
/**
2023-09-20 10:49:32 +08:00
* @brief Inserts the object at the end of the array, if it is not yet present.
2024-08-19 10:43:30 +08:00
* @param[in] objarray The array for adding.
* @param[in] obj The CKObject to be added.
2023-09-20 10:49:32 +08:00
* @return True if the object was already present, false otherwise
2023-09-19 22:32:07 +08:00
*/
bool AddIfNotHere(XObjectPointerArray& objarray, CK2::ObjImpls::CKObject* obj);
2023-09-19 22:32:07 +08:00
2023-09-16 22:38:21 +08:00
/**
* @brief Check Object pointer validation and remove invalid pointers before deletion.
2024-08-19 10:43:30 +08:00
* @param objarray The array to be checked.
* @param ctx Associated CKContext.
2023-09-16 22:38:21 +08:00
*/
void PreDeletedCheck(XObjectPointerArray& objarray, CK2::CKContext* ctx);
/**
* @brief Check Object pointer validation and remove invalid pointers after deletion.
2024-08-19 10:43:30 +08:00
* @param objarray The array to be checked.
* @param ctx Associated CKContext.
* @warning The performance of this function is extremely bad. Use this carefully.
2023-09-16 22:38:21 +08:00
*/
void PostDeletedCheck(XObjectPointerArray& objarray, CK2::CKContext* ctx);
/**
* @brief Check Object pointer validation and remove invalid pointers before deletion.
2024-08-19 10:43:30 +08:00
* @param objarray The array to be checked.
* @param ctx Associated CKContext.
2023-09-16 22:38:21 +08:00
*/
void PreDeletedCheck(XList<CK2::ObjImpls::CKObject*>& objarray, CK2::CKContext* ctx);
/**
* @brief Check Object pointer validation and remove invalid pointers after deletion.
2024-08-19 10:43:30 +08:00
* @param objarray The array to be checked.
* @param ctx Associated CKContext.
* @warning The performance of this function is extremely bad. Use this carefully.
2023-09-16 22:38:21 +08:00
*/
void PostDeletedCheck(XList<CK2::ObjImpls::CKObject*>& objarray, CK2::CKContext* ctx);
}
2023-08-22 15:30:26 +08:00
}