2023-08-22 15:30:26 +08:00
# pragma once
2023-09-16 18:31:25 +08:00
# include "CKTypes.hpp"
# include "../XContainer/XTypes.hpp"
# include <functional>
2024-08-21 17:57:35 +08:00
# include <memory>
2023-08-22 15:30:26 +08:00
namespace LibCmo : : CK2 {
// ========== Compression utilities ==========
/**
* @ brief Compress a buffer
2024-08-21 17:57:35 +08:00
* @ param [ in ] Data A pointer to the buffer to compress . nullptr is not allowed .
2023-08-22 15:30:26 +08:00
* @ param [ in ] size Size of the source buffer .
* @ param [ out ] NewSize A reference that will be filled with the size of the compressed buffer . 0 if failed .
2024-08-21 17:57:35 +08:00
* @ param [ in ] compressionlevel 0 - 9 Greater level smaller result size .
2023-08-22 15:30:26 +08:00
* @ return
* A pointer to the compressed buffer . nullptr if failed .
2024-08-21 17:57:35 +08:00
* The return pointer should be freed by \ c delete [ ] manually .
* @ remarks
2023-08-22 15:30:26 +08:00
* The size of allocated return value may greater than the passed value of NewSize .
* NewSize only indicate the size of the part storing useful data in return value .
2024-08-21 17:57:35 +08:00
* @ exception LogicException Raised if given buffer is nullptr and size is not equal to zero .
* @ see CKUnPackData ( ) , CKComputeDataCRC ( )
2023-08-22 15:30:26 +08:00
*/
2023-08-28 21:21:40 +08:00
void * CKPackData ( const void * Data , CKDWORD size , CKDWORD & NewSize , CKINT compressionlevel ) ;
2023-08-22 15:30:26 +08:00
/**
* @ brief Decompress a buffer
* @ param [ in ] DestSize Expected size of the decompressed buffer .
2024-08-21 17:57:35 +08:00
* @ param [ in ] SrcBuffer Compressed buffer . nullptr is not allowed .
2023-08-22 15:30:26 +08:00
* @ param [ in ] SrcSize Size of the compressed buffer .
* @ return
* A pointer to the decompressed buffer or nullptr if there was a error .
2024-08-21 17:57:35 +08:00
* The return pointer should be freed by \ c delete [ ] manually .
* @ exception LogicException Raised if given buffer is nullptr and size is not equal to zero .
* @ see CKPackData ( ) , CKComputeDataCRC ( )
2023-08-22 15:30:26 +08:00
*/
2023-08-28 21:21:40 +08:00
void * CKUnPackData ( CKDWORD DestSize , const void * SrcBuffer , CKDWORD SrcSize ) ;
2023-08-22 15:30:26 +08:00
/**
* @ brief Computes a CRC for a buffer .
2024-08-21 17:57:35 +08:00
* @ param [ in ] data A pointer to the buffer to create a CRC for . nullptr is not allowed .
2023-08-22 15:30:26 +08:00
* @ param [ in ] size Size of the source buffer .
* @ param [ in ] PreviousCRC
* The first time a CRC is computed this value should be 0 ,
* but it can be use to compute a single CRC for a several buffers
* by using the currently computed CRC for previous buffers in this value .
* @ return CRC of the buffer .
2024-08-21 17:57:35 +08:00
* @ exception LogicException Raised if given buffer is nullptr and size is not equal to zero .
* @ see CKPackData ( ) , CKUnPackData ( )
2023-08-22 15:30:26 +08:00
*/
2023-08-29 14:00:34 +08:00
CKDWORD CKComputeDataCRC ( const void * data , CKDWORD size , CKDWORD PreviousCRC = 0 ) ;
2023-09-04 22:58:53 +08:00
// ========== String Utilities ==========
2023-09-07 21:57:48 +08:00
/**
2024-08-21 17:57:35 +08:00
* @ brief Check whether two string is equal . Case senstive .
* @ param [ in ] str1 First string . nullptr is allowed but not suggested .
* @ param [ in ] str2 Second string . nullptr is allowed but not suggested .
* @ return True if two string is equal , otherwise false .
* @ remarks
* nullptr string is not equal to any other string .
* However two nullptr string is equal .
* @ see CKStrEqualI ( )
2023-09-07 21:57:48 +08:00
*/
2023-09-04 22:58:53 +08:00
bool CKStrEqual ( CKSTRING str1 , CKSTRING str2 ) ;
2023-09-07 21:57:48 +08:00
/**
2024-08-21 17:57:35 +08:00
* @ brief Check whther two string is equal . Case insenstive .
* @ param [ in ] str1 First string . nullptr is allowed but not suggested .
* @ param [ in ] str2 Second string . nullptr is allowed but not suggested .
* @ return True if two string is equal , otherwise false .
* @ remarks
* nullptr string is not equal to any other string .
* However two nullptr string is equal .
* @ see CKStrEqual ( )
2023-09-07 21:57:48 +08:00
*/
bool CKStrEqualI ( CKSTRING str1 , CKSTRING str2 ) ;
/**
* @ brief Check whether string is empty
2024-08-21 17:57:35 +08:00
* @ param [ in ] strl String for checking . nullptr is allowed but not suggested .
* @ return True if string is empty , otherwise false .
* @ remarks nullptr string is seen as empty string .
2023-09-07 21:57:48 +08:00
*/
bool CKStrEmpty ( CKSTRING strl ) ;
2024-08-22 10:57:53 +08:00
/**
* @ brief Get the length of given string .
* @ param [ in ] strl String for getting length . nullptr is allowed but not suggested .
* @ return String length in UTF8 code unit .
* @ remarks nullptr string will return 0 instead .
* @ exception RuntimeException Raised if the length of string exceed the maximum value of CKDWORD .
*/
CKDWORD CKStrLen ( CKSTRING strl ) ;
2023-09-04 22:58:53 +08:00
2023-09-16 18:31:25 +08:00
// ========== Class registration utilities ==========
2023-08-29 14:00:34 +08:00
2024-08-21 17:57:35 +08:00
/// @brief Function pointer which do extra stuff when registry this class.
2023-09-17 12:39:21 +08:00
using CKClassRegisterFct = std : : function < void ( ) > ;
2024-08-21 17:57:35 +08:00
/// @brief Function pointer which create ObjImpls::CKObject pointer by given CKContext, CK_ID and name.
2023-09-16 18:31:25 +08:00
using CKClassCreationFct = std : : function < ObjImpls : : CKObject * ( CKContext * , CK_ID , CKSTRING ) > ;
2024-08-21 17:57:35 +08:00
/// @brief Function pointer which free given ObjImpls::CKObject pointer.
2023-09-16 18:31:25 +08:00
using CKClassReleaseFct = std : : function < void ( CKContext * , ObjImpls : : CKObject * ) > ;
2024-08-21 17:57:35 +08:00
/// @brief Function pointer which return the name of class.
2023-09-16 18:31:25 +08:00
using CKClassNameFct = std : : function < CKSTRING ( ) > ;
//using CKClassDependenciesFct = std::function<CKSTRING(CKINT, CKINT)>;
//using CKClassDependenciesCountFct = std::function<CKINT(CKINT)>;
2024-08-21 17:57:35 +08:00
/**
* @ brief The representation of a registered class .
*/
2023-09-16 18:31:25 +08:00
struct CKClassDesc {
2024-08-21 17:57:35 +08:00
// Variables used when building class hierarchy table
2023-09-16 18:31:25 +08:00
bool IsValid ; /**< True if this CKClassDesc is a valid one. Because CK_CLASSID may not be consecutive. */
2024-08-21 17:57:35 +08:00
bool Done ; /**< Temporary variable indicating this item has been processed when creating table. */
// Variables initialized upon class registration
CK_CLASSID Self ; /**< Class identifier of self */
CK_CLASSID Parent ; /**< Class identifier of parent class */
CKClassRegisterFct RegisterFct ; /**< Function pointer which do extra stuff when registry this class. */
CKClassCreationFct CreationFct ; /**< Function pointer which create ObjImpls::CKObject pointer by given CKContext, CK_ID and name. */
CKClassReleaseFct ReleaseFct ; /**< Function pointer which free given ObjImpls::CKObject pointer. */
CKClassNameFct NameFct ; /**< Function pointer which return the name of class. */
2023-09-16 18:31:25 +08:00
//CKClassDependenciesFct DependsFct; // Pointer to Class dependencies function (Copy,delete,replace...)
//CKClassDependenciesCountFct DependsCountFct; // Pointer to Class dependencies Count function (Copy,delete,replace...)
//// Initialized by class specific registration function
//CKDWORD DefaultOptions; // Default options for this class
//CKDWORD DefaultCopyDependencies;
//CKDWORD DefaultDeleteDependencies;
//CKDWORD DefaultReplaceDependencies;
//CKDWORD DefaultSaveDependencies;
//CKGUID Parameter; // Associated parameter GUID
2024-08-21 17:57:35 +08:00
// Variables initialized after building class hierarchy table
CKINT DerivationLevel ; /**< How many parent level it has. 0 for CKObject, etc.. */
XContainer : : XBitArray Parents ; /**< Bit Mask of parents classes */
XContainer : : XBitArray Children ; /**< Bit Mask of children classes */
XContainer : : XBitArray ToBeNotify ; /**< User specified notify list, only for current class. If any deleted objects match class id in this XBitArray, notify the host of this XBitArray. */
XContainer : : XBitArray CommonToBeNotify ; /**< Same as ToBeNotify, but merging all parents' notify list. */
XContainer : : XBitArray ToNotify ; /**< The ClassID to notify when an object of this class is deleted (inverse of ToBeNotify) */
2023-09-16 18:31:25 +08:00
CKClassDesc ( ) :
IsValid ( false ) ,
Done ( false ) ,
Self ( CK_CLASSID : : CKCID_OBJECT ) , Parent ( CK_CLASSID : : CKCID_OBJECT ) ,
2023-09-17 12:39:21 +08:00
RegisterFct ( nullptr ) , CreationFct ( nullptr ) , ReleaseFct ( nullptr ) , NameFct ( nullptr ) ,
2023-09-16 18:31:25 +08:00
DerivationLevel ( 0 ) ,
2023-09-17 12:39:21 +08:00
Parents ( ) , Children ( ) , ToBeNotify ( ) , CommonToBeNotify ( )
2023-09-16 18:31:25 +08:00
{ }
2024-08-17 20:43:27 +08:00
YYCC_DEF_CLS_COPY_MOVE ( CKClassDesc ) ;
2023-09-16 18:31:25 +08:00
} ;
2023-08-23 16:04:58 +08:00
// ========== CKClass Registration ==========
2023-08-28 17:04:28 +08:00
2024-08-21 17:57:35 +08:00
/**
* @ brief Order that first class id will be notified when deleting object whose class id is second argument
* @ param [ in ] listener The id of class will be notified .
* @ param [ in ] listenTo The id of class which first argument interested in .
2024-08-27 11:25:53 +08:00
* @ remarks If one of given class ids is invalid , this function simply return and do nothing .
2024-08-21 17:57:35 +08:00
*/
2023-09-17 12:39:21 +08:00
void CKClassNeedNotificationFrom ( CK_CLASSID listener , CK_CLASSID listenTo ) ;
2024-08-21 17:57:35 +08:00
/**
* @ brief Get an usable class id for registration .
* @ details This function is usually used by plugin to registering classes .
* Because all embedded Virtools classes has constant class id .
* @ return An usable class id for registration .
*/
2023-09-01 12:19:06 +08:00
CK_CLASSID CKClassGetNewIdentifier ( ) ;
2024-08-21 17:57:35 +08:00
/**
* @ brief Register a new class .
* @ param [ in ] cid The id of this class .
* @ param [ in ] parentCid The id of parent class .
* @ param [ in ] regFct Pointer to class specific registration function which do extra stuff when registry this class .
* @ param [ in ] createFct Pointer to class instance creation function
* @ param [ in ] relFct Pointer to class instance release function
* @ param [ in ] nameFct Pointer to class name function
*/
2023-08-23 16:04:58 +08:00
void CKClassRegister ( CK_CLASSID cid , CK_CLASSID parentCid ,
2023-09-17 12:39:21 +08:00
CKClassRegisterFct regFct , CKClassCreationFct createFct , CKClassReleaseFct relFct , CKClassNameFct nameFct ) ;
2023-08-23 16:04:58 +08:00
// ========== Class Hierarchy Management ==========
2023-08-28 17:04:28 +08:00
2024-08-21 17:57:35 +08:00
/**
* @ brief Get total count of registered classes .
* @ return The total count of registered classes .
*/
2023-09-01 12:19:06 +08:00
CKDWORD CKGetClassCount ( ) ;
2024-08-21 17:57:35 +08:00
/**
* @ brief Get the class description struct by given class id .
* @ param [ in ] cid Class id for fetching .
* @ return The pointer to corresponding class description .
2024-08-27 11:25:53 +08:00
* If given class id is invalid , this function will return nullptr .
* According to this , caller can utilize this function to validate class id .
2024-08-21 17:57:35 +08:00
*/
2023-08-23 16:04:58 +08:00
const CKClassDesc * CKGetClassDesc ( CK_CLASSID cid ) ;
2024-08-21 17:57:35 +08:00
/**
* @ brief Get the name representation of given class id .
* @ param [ in ] cid Class id for fetching .
* @ return The name of given class id .
2024-08-27 11:25:53 +08:00
* If given class id is invalid , it return a predefined name .
2024-08-21 17:57:35 +08:00
*/
2023-08-23 16:04:58 +08:00
CKSTRING CKClassIDToString ( CK_CLASSID cid ) ;
2024-08-21 17:57:35 +08:00
/**
* @ brief Check whether first class is second class ' child class .
* @ param [ in ] child The id of first class assumed as child class .
* @ param [ in ] parent The id of second class assumed as parent class .
* @ return True if relation is satisfied , otherwise false .
2024-08-27 11:25:53 +08:00
* If one of given class ids is invalid , this function always return false .
2024-08-21 17:57:35 +08:00
*/
2023-08-29 14:00:34 +08:00
bool CKIsChildClassOf ( CK_CLASSID child , CK_CLASSID parent ) ;
2024-08-21 17:57:35 +08:00
/**
* @ brief Get the parent class id of given class id .
* @ param [ in ] child The id to class which need to find parent class .
* @ return The parent class id .
2024-08-27 11:25:53 +08:00
* If given class id is invalid , this function always return the class id of CKObject .
2024-08-21 17:57:35 +08:00
*/
2023-08-23 16:04:58 +08:00
CK_CLASSID CKGetParentClassID ( CK_CLASSID child ) ;
2024-08-21 17:57:35 +08:00
/**
* @ brief Get the cloest common parent of given two classes .
* @ param [ in ] cid1 The id of first class finding common parent .
* @ param [ in ] cid2 The id of second class finding common parent .
* @ return The cloest common parent class id .
*/
2023-08-23 16:04:58 +08:00
CK_CLASSID CKGetCommonParent ( CK_CLASSID cid1 , CK_CLASSID cid2 ) ;
2023-09-17 12:39:21 +08:00
/**
2024-08-21 17:57:35 +08:00
* @ brief Check whether object whose class id is first argument , need to be notified when the objects whose class id is second argument , is deleting .
* @ param [ in ] listener The class id of checking whether need to be notified .
* @ param [ in ] deletedObjCid The class id of deleting object .
* @ return True if it need to be notified , otherwise false .
2024-08-27 11:25:53 +08:00
* If the class id of checking is invalid , this function always return false .
2023-09-17 12:39:21 +08:00
*/
2024-08-21 17:57:35 +08:00
bool CKIsNeedNotify ( CK_CLASSID listener , CK_CLASSID deletedObjCid ) ; /**
2023-09-17 12:39:21 +08:00
/**
2024-08-21 17:57:35 +08:00
* @ brief Get all class ids need to be notified when objects whose class id included in \ c delObjCids are deleting .
* @ param [ in ] delObjCids The bit array representing class ids which need to be queried .
* @ return The queried bit array representing class ids need to be notified .
* @ see CKIsNeedNotify ( )
2023-09-17 12:39:21 +08:00
*/
XContainer : : XBitArray CKGetAllNotifyClassID ( const XContainer : : XBitArray & delObjCids ) ;
2024-08-21 17:57:35 +08:00
// ========== Initializations Functions ==========
2023-09-23 15:55:57 +08:00
2024-08-21 17:57:35 +08:00
/**
* @ brief Initialize CK engine .
* @ details You must call this function before anything .
* @ return Error code about initializing .
* @ see CKShutdown ( )
*/
2023-08-23 16:04:58 +08:00
CKERROR CKStartUp ( ) ;
2024-08-21 17:57:35 +08:00
/**
* @ brief Shutdown CK engine .
* @ details You must call this function after you have done all things .
* @ return Error code about shutdown .
* @ see CKStartUp ( )
*/
2023-08-23 16:04:58 +08:00
CKERROR CKShutdown ( ) ;
2023-08-22 15:30:26 +08:00
}