Polygon Crucher SDK - Documentation
Documentation
Loading...
Searching...
No Matches
CustomData.h
Go to the documentation of this file.
1//! @file CustomData.h
2//! @brief CCustomData class is a handly class for storing any kind of data
3//!
4//////////////////////////////////////////////////////////////////////
5
6#if !defined(AFX_CUSTOMDATA_H__5D9D5366_1E68_11D3_A381_EA02A046C52E__INCLUDED_)
7#define AFX_CUSTOMDATA_H__5D9D5366_1E68_11D3_A381_EA02A046C52E__INCLUDED_
8
9#ifdef _MSC_VER
10#pragma once
11#endif // _MSC_VER
12
13#include "XThreadSync.h"
14
15BEGIN_MOOTOOLS_NAMESPACE
16
18
19// Contains the information which is stored on files
20// (No pointer reference because the size differs from x32 to x64)
21typedef struct CustomPrivateDataInfo
22{
23 unsigned int id;
24 unsigned int size;
25 unsigned int kindof;
26} CustomPrivateDataInfo;
27
28typedef struct CustomPrivateData
29{
30 CustomPrivateDataInfo info;
31 unsigned int beginOfData;
33
34#define CUSTOMDATA_NODATA 0
35#define CUSTOMDATA_UNDEFINED MAKE_CUSTOM_ID(' ', ' ', ' ', ' ') // Only for compatibility with previous CCustomData storage
36#define CUSTOMDATA_ANSI_TEXT MAKE_CUSTOM_ID('A', 'N', 'S', 'I')
37#define CUSTOMDATA_UTF8_TEXT MAKE_CUSTOM_ID('U', 'T', 'F', '8')
38#define CUSTOMDATA_UNICODE_TEXT MAKE_CUSTOM_ID('U', 'N', 'I', 'C') // Unicode xStringUtf16 only
39#define CUSTOMDATA_BOOL MAKE_CUSTOM_ID('B', 'O', 'O', 'L')
40#define CUSTOMDATA_CHAR MAKE_CUSTOM_ID('C', 'H', 'A', 'R')
41#define CUSTOMDATA_UCHAR MAKE_CUSTOM_ID('U', 'C', 'H', 'R')
42#define CUSTOMDATA_FLOAT MAKE_CUSTOM_ID('F', 'L', 'O', 'A')
43#define CUSTOMDATA_DOUBLE MAKE_CUSTOM_ID('D', 'O', 'U', 'B')
44#define CUSTOMDATA_BIN MAKE_CUSTOM_ID(' ', 'B', 'I', 'N')
45#define CUSTOMDATA_OLDCUSTDATA MAKE_CUSTOM_ID('C', 'D', 'A', 'T')
46#define CUSTOMDATA_CUSTDATA MAKE_CUSTOM_ID('C', 'D', 'A', '2')
47#define CUSTOMDATA_UINT MAKE_CUSTOM_ID('U', 'I', 'N', 'T')
48#define CUSTOMDATA_INT MAKE_CUSTOM_ID(' ', 'I', 'N', 'T')
49#define CUSTOMDATA_LONG MAKE_CUSTOM_ID('L', 'O', 'N', 'G') // 4 bits
50#define CUSTOMDATA_ULONG MAKE_CUSTOM_ID('U', 'L', 'O', 'N') // 4 bits
51#define CUSTOMDATA_LONGINT MAKE_CUSTOM_ID('L', 'L', 'O', 'N') // 8 bits
52#define CUSTOMDATA_ULONGINT MAKE_CUSTOM_ID('U', 'L', 'L', 'O') // 8 bits
53#define CUSTOMDATA_ANSI_STRINGS MAKE_CUSTOM_ID('A', 'S', 'T', 'R')
54#define CUSTOMDATA_UTF8_STRINGS MAKE_CUSTOM_ID('U', '8', 'T', 'R')
55#define CUSTOMDATA_UNICODE_STRINGS MAKE_CUSTOM_ID('U', 'S', 'T', 'R')
56#define CUSTOMDATA_PTR MAKE_CUSTOM_ID('P', 'T', 'R', ' ')
57#define CUSTOMDATA_PTR64 MAKE_CUSTOM_ID('P', 'T', 'R', '6')
58#define CUSTOMDATA_ARRAY MAKE_CUSTOM_ID('A', 'R', 'R', 'A')
59#define CUSTOMDATA_UTCTIME MAKE_CUSTOM_ID('T', 'I', 'M', 'E') // UTC time
60#define CUSTOMDATA_XTIME MAKE_CUSTOM_ID('T', 'I', 'M', 'X') // xTime
61
62#ifdef __64BITS__
63#define PREFERENCE_DATA_ID MAKE_CUSTOM_ID('S', 'E', '6', '4')
64#else
65#define PREFERENCE_DATA_ID MAKE_CUSTOM_ID('S', 'E', 'T', 'T')
66#endif
67
68//! @enum CUSTOMDATA_COPY_FLAGS
69//! Flags for controling the way custom data are copied
71{
72 CUSTOMDATA_COPY_DEFAULT = 0x00, //!< Same has operator =
73 CUSTOMDATA_COPY_NEWID = 0x01, //!< Copy only id from source that are not in destination
74 CUSTOMDATA_COPY_KEEP_CONTENT = 0x02,//!< Keep content of destination when copying
76
77class CCustomData;
78
79//! @class CCustomDataClass
80//! @brief CCustomDataClass can be derived to use CCustomData::GetCustomClass / CCustomData::SetCustomClass
82{
83 friend CCustomData;
84
85protected:
86 virtual void InitData() = 0;
87 virtual bool SetData(CCustomData& data) const = 0;
88 virtual bool GetData(const CCustomData& data) = 0;
89};
90
91
92//! @class CCustomData
93//! @brief CCustomData is a handly class for storing any kind of data.
94//! @par
95//! Each data is defined by its identifier. This identifier can be constructed using the #MAKE_CUSTOM_ID macro.\n
96//! Using your own identifiers, you can associate your own data to any classes that give access to a CCustomData object (which is the case of many classes in the SDK)\n
97//! These data follow the classes and you can retrieve them after different operations that might occur on the classes data.
98//! @par
99//! A data store in CCustomData has a type. If you use CCustomData::SetString with an id and use that's id to retrieve an int using CCustomData::GetInt, you'll get nothing.\n
100//! This is why, when some data are store in CCustomData, you'll find the type of the data.\n
101//! For example, #FBX_IO_OPTION_PASSWORD defines in #io3dmgr.h specifies that the data is a CXString. You must use CCustomData::SetString to define the fbx password through the CSceneImportOptions / CSceneExportOptions.
102//! @par
103//! The CCustomData data can be read/save through the CCustomData::Serialize process\n
104//! CCustomData::LoadFromPrefs / CCustomData::SaveToPrefs allow to load/store the data on disk
105class DLL_TOOLSFUNCTION CCustomData
106{
107private:
108 // To be deleted, as it dangerous to use
109 CCustomData *operator=(const CCustomData *) const
110 {
111 XASSERT(0);
112 return NULL;
113 }
114
115 inline CustomPrivateData *GetNewChunkEntry() const;
116 inline CustomPrivateData *GetNextChunk(CustomPrivateData *parsedata) const;
117 inline CustomPrivateData *GetLastChunk() const;
118
120 CCustomDataHashID *id2offset;
121 void UpdateOffsetMap(unsigned int defaultSize = 0);
122
123protected:
124 enum _stringmode
125 {
126 STRING_DEFAULT = 0x00,
127 STRING_UNICODE = 0x01,
128 STRING_ANSI = 0x02,
129 } stringmode;
130
131 mutable CXCriticalSection threadlock;
132 CustomPrivateData *data;
133 unsigned int cursize, totalsize;
134
135 void ConvertPreviousData(CCustomData& data, CustomPrivateDataOld* olddata, unsigned int totalsize) const;
136#ifndef MOOTOOLS_NO_ARCHIVE_SUPPORT
137 void ReadPreviousVersion(CXArchive& archive);
138#endif // !MOOTOOLS_NO_ARCHIVE_SUPPORT
139
140
141 bool ReplaceCustom(unsigned int id, const void *ptr, unsigned int size, unsigned int kindof);
142 void SetCustom(unsigned int id, const void *ptr, unsigned int size, unsigned int kindof);
143 unsigned int GetCustom(unsigned int id, void *ptr, unsigned int size, unsigned int kindof) const;
144
145 void SetStrings(unsigned int id, const void *strings, bool array);
146 int GetStrings(unsigned int id, void *strings, bool array) const;
147
148public:
149 unsigned int CopyID(const CCustomData *, unsigned int firstid, ...);
150 void Copy(const CCustomData *, unsigned int copyFlags);
151 CCustomData *Copy() const;
152 CCustomData& operator=(const CCustomData& refdata);
153 bool IsEqual(const CCustomData& data) const;
154
155 // id can be any value (0 is allowed)
156 void SetBinary(unsigned int id, const void *ptr, unsigned int size);
157 void SetCustomData(unsigned int id, const CCustomData& data);
158 void SetCustomClass(unsigned int id, const CCustomDataClass& value);
159 void SetFloat(unsigned int id, float value);
160 void SetDouble(unsigned int id, double value);
161 void SetChar(unsigned int id, char value);
162 void SetUChar(unsigned int id, unsigned char value);
163 void SetBool(unsigned int id, BOOL value);
164 void SetPtr(unsigned int id, const void *ptr);
165 void SetInt(unsigned int id, int value);
166 void SetUInt(unsigned int id, unsigned int value);
167 void SetLong(unsigned int id, long value); // warning: long is stored using 4 bytes whatever the OS size of long
168 void SetULong(unsigned int id, unsigned long value); // warning: long is stored using 4 bytes whatever the OS size of long
169 void SetSizet(unsigned int id, SIZET value);
170 void SetLongInt(unsigned int id, longint value);
171 void SetULongInt(unsigned int id, longuint value);
172 bool SetArray(unsigned int id, const void *ptr, unsigned int elementCount, unsigned int sizeOfElement);
173 void SetString(unsigned int id, const CXString& string);
174 void SetStringA(unsigned int id, const CXStringA& string);
175 void SetStringW(unsigned int id, const CXStringW& string);
176#ifdef MOOTOOLS_MFC_PRODUCT_BUILD
177 void SetStringList(unsigned int id, const CStringList& strings);
178#endif
179 void SetStringArray(unsigned int id, const CXStringArray& strings);
180 void SetUTCTime(unsigned int id, const CXTime& time); // CXTime is converted to UTC time
181 void SetXTime(unsigned int id, const CXTime& time); // store CXTime value (without conversion and with full time precision)
182
183 void AddToCustomData(unsigned int id, const CCustomData& newdata, BOOL copyNewIDOnly);
184
185 bool HasData() const;
186 bool HasData(unsigned int id) const;
187 unsigned int GetDataKindOf(unsigned int id) const;
188 bool ChangeID(unsigned int srcid, unsigned int newid);
189
190 // Allocate/Free a block of memory that can be used with Attach methods
191 static void *Alloc(unsigned int size); // Allocate memory for CCustomData. Alloc uses xmalloc.
192 static void Free(void *data); // Free CCustomData memory. Free uses xfree
193 void FreeExtra();
194
195 void *Detach(unsigned int& size);
196 void Attach(void *data, unsigned int size); // Attach a memory block (must have been allocated with Alloc (or xmalloc)
197 const void *GetData(unsigned int& totalsize) const;
198 void SetData(const void *data, unsigned int totalsize);
199
200 unsigned int GetDataSize(unsigned int id) const;
201 const void *GetCustomPtr(unsigned int id, unsigned int& size) const; // Return directly the data associated to the ID. GetCustomPtr requires to lock the custom data to prevent change of the data in an external thread.
202 unsigned int GetChecksum(unsigned int id) const;
203
204 bool GetBinary(unsigned int id, void *ptr, unsigned int size) const;
205 bool GetCustomData(unsigned int id, CCustomData& data) const;
206 bool GetCustomClass(unsigned int id, CCustomDataClass& data) const;
207 bool GetFloat(unsigned int id, float& value, float defaultvalue) const;
208 bool GetDouble(unsigned int id, double& value, double defaultvalue) const;
209 bool GetBool(unsigned int id, bool& value, bool defaultvalue) const;
210 bool GetBool(unsigned int id, BOOL& value, BOOL defaultvalue) const;
211 bool GetChar(unsigned int id, char& value, char defaultvalue) const;
212 bool GetUChar(unsigned int id, unsigned char& value, unsigned char defaultvalue) const;
213 bool GetPtr(unsigned int id, void *&value) const;
214 void *GetPtr(unsigned int id) const;
215 bool GetInt(unsigned int id, int& value, int defaultvalue) const;
216 bool GetUInt(unsigned int id, unsigned int& value, unsigned int defaultvalue) const;
217 bool GetLong(unsigned int id, long& value, long defaultvalue) const; // warning: long is stored using 4 bytes whatever the OS size of long
218 bool GetLongInt(unsigned int id, longint& value, longint defaultvalue) const;
219 bool GetSizet(unsigned int id, SIZET& value, SIZET defaultvalue) const; // 32 / 64 bits independent. SIZET is stored as a unsigned long long. return defaultvalue if too big on 32 bits system
220 bool GetULong(unsigned int id, unsigned long& value, unsigned long defaultvalue) const; // warning: long is stored using 4 bytes whatever the OS size of long
221 bool GetULongInt(unsigned int id, longuint& value, longuint defaultvalue) const;
222 const void *GetArray(unsigned int id, unsigned int& elementCount, unsigned int sizeOfElement, CXSingleLock *safeThreadGuard) const; // Release array must be called if the return data is not null (can be also called when null)
223 bool GetString(unsigned int id, CXString& string, const CXString& = CXString()) const;
224 bool GetStringA(unsigned int id, CXStringA& string, const CXStringA& = CXStringA()) const;
225 bool GetStringW(unsigned int id, CXStringW& string, const CXStringW& = CXStringW()) const;
226#ifdef MOOTOOLS_MFC_PRODUCT_BUILD
227 int GetStringList(unsigned int id, CStringList &strings) const;
228#endif
229 int GetStringArray(unsigned int id, CXStringArray& strings) const;
230 bool GetUTCTime(unsigned int id, CXTime& time, const CXTime& = CXTime::GetCurrentTime().GetTime()) const; // The time is initialized from UTC time (_time_t) which is cross platform. Comparison with CXTime possible if CXTime also initialized with UTC time
231 bool GetXTime(unsigned int id, CXTime& time, const CXTime & = CXTime::GetCurrentTime().GetTime()) const; // The time is initialized from xTime which allows comparison if CXTime object is initialized standardly with full time precision
232
233 void RemoveAll();
234 bool RemoveCustom(unsigned int id);
235
236 unsigned int GetIDCount() const;
237 unsigned int GetIDByIndex(unsigned int index, unsigned int& kindof) const;
238
239 CXString DumpCustomData(bool tabify, int level = 0) const;
240
241#ifndef MOOTOOLS_NO_ARCHIVE_SUPPORT
242 virtual void Serialize(CXArchive& archive);
243 virtual bool SerializeFromData( const void *rawdata, fileuint size);
244 virtual void *SerializeToData(fileuint& size) const; // Use CCustomData::Free to free the returned memory
245 static bool SerializeFromData(CCustomData& data, const void *rawdata, fileuint size);
246#else // !MOOTOOLS_NO_ARCHIVE_SUPPORT
247 static bool SerializeFromData(CCustomData& data, const void *rawdata, fileuint size);
248#endif
249
250 // the following are not supported (return false) if CCUSTOMDATA_NO_PREFS_SUPPORT is defined
251 bool SaveToPrefs(const CXString& filename, unsigned int paramName1, unsigned int paramName2) const;
252 bool LoadFromPrefs(const CXString filename, unsigned int paramName1, unsigned int paramName2, bool copyPreviousVersion = false);
253
254#ifdef _DEBUG
255 void Dump();
256#endif
257
258 CCustomData();
259 CCustomData(const CCustomData& data);
260 virtual ~CCustomData();
261};
262
263END_MOOTOOLS_NAMESPACE
264
265#endif // !defined(AFX_CUSTOMDATA_H__5D9D5366_1E68_11D3_A381_EA02A046C52E__INCLUDED_)
CUSTOMDATA_COPY_FLAGS
Definition CustomData.h:71
@ CUSTOMDATA_COPY_NEWID
Copy only id from source that are not in destination.
Definition CustomData.h:73
@ CUSTOMDATA_COPY_KEEP_CONTENT
Keep content of destination when copying.
Definition CustomData.h:74
@ CUSTOMDATA_COPY_DEFAULT
Same has operator =.
Definition CustomData.h:72
CXTString< wchar_t > CXStringW
CXStringA is an unicode wchar_t string. Cf. CXTString.
Definition XString.h:120
CXTString< char > CXStringA
CXStringA is an ansi / utf8 char string. Cf. CXTString.
Definition XString.h:119
CXTString< TCHAR > CXString
CXString depend on the target OS. Could be CXStringW (Windows) or CXStringA (Linux / Macos)
Definition XString.h:118
The class defines an x, y, z 3D point which can use int, float or double.
Definition 3DPoint.h:27
CCustomDataClass can be derived to use CCustomData::GetCustomClass / CCustomData::SetCustomClass.
Definition CustomData.h:82
CCustomData is a handly class for storing any kind of data.
Definition CustomData.h:106
Definition XArchive.h:17
Definition XThreadSync.h:20
Definition XThreadSync.h:38
CXStringArray implement an array of CXString.
Definition XStringArray.h:25
Definition XTime.h:18
Definition CustomData.h:29