3DCoat Core API
The 3DCoat API documentation.
Loading...
Searching...
No Matches
TagsList.h
1#pragma once
2
13class BinStream;
14class BaseClass;
15
20class APICALL TagsList
21{
22public:
23 TagsList();
24 ~TagsList();
25 TagsList(const char* QuoteName);
26 //new/delete are using pool, so this is fast
27 void operator delete(void* ptr, size_t size);
28 void* operator new(size_t size);
29
31 int to_int () const;
33 float to_float() const;
35 bool to_bool() const;
37 const char* to_string() const;
39 float to_float();
41 int get_data(void* buffer, int maxlength);
42
44 static comms::cStr& LastTagFile();
45 static comms::cStr& LastTag();
46 static comms::cStr& defTab();
47
49 int GetTagsFromString(const char* XMLSource);
51 int PutTagsIntoString(comms::cStr* To, int shift = 0, bool fast = false);
53 int GetTagsFromJsonString(const char* jsonSource);
55 int PutTagsIntoJsonString(comms::cStr* Source, int shift = 0, bool treatAllAsText = false);
57 bool ReadTagsFromFile(const char* FilePath);
59 void WriteTagsToFile(const char* FilePath, bool OnlyChanged = false);
60
62 void Assign(int v);
64 void Assign(const char* s);
65 // get/set parent for this tag
66 void SetParent(TagsList* Parent);
67 TagsList* GetParent() const;
68
72 TagsList* AddSubTag(const char* quotename, const char* body = "");
74 TagsList* AddSubTag(const char* quotename, int body);
75 TagsList* AddSubTag(const char* quotename, DWORD body);
76 TagsList* AddSubTag(const char* quotename, short body);
77 TagsList* AddSubTag(const char* quotename, WORD body);
78 TagsList* AddSubTag(const char* quotename, char body);
80 TagsList* AddSubTag(const char* quotename, float body);
82 TagsList* AddSubTag(const char* quotename, void* data, int Length);
86 TagsList& operator << (const char* string);
87 TagsList& operator << (int value);
88 TagsList& operator << (bool value);
89 TagsList& operator << (float value);
90 TagsList& operator << (const comms::cVec2& value);
91 TagsList& operator << (const comms::cVec3& value);
92 TagsList& operator << (const comms::cVec4& value);
93 TagsList& operator << (const comms::cMat3& value);
94 TagsList& operator << (const comms::cMat4& value);
95 TagsList& operator << (BaseClass* BC);
98 template <class X>
99 TagsList& operator << (const cList<X>& array);
113 template <typename X, typename TYPE>
114 void StoreField(const char* tagname, const cList<X>& array, TYPE X::* member);
115
117 void operator >> (comms::cStr& string_value);
118 void operator >> (int& value);
119 void operator >> (float& value);
120 void operator >> (bool& value);
121 void operator >> (comms::cVec2& value);
122 void operator >> (comms::cVec3& value);
123 void operator >> (comms::cVec4& value);
124 void operator >> (comms::cMat3& value);
125 void operator >> (comms::cMat4& value);
126 void operator >> (BaseClass* BC);
128 template <class X>
129 void operator >> (cList<X>& array);
132 template <typename X, typename TYPE>
133 void RestoreField(cList<X>& array, TYPE X::* member);
134
135
137 void bin_ToBS(BinStream& BS);
139 void text_ToBS(BinStream& BS);
141 bool FromBS(BinStream& BS);
142
143 comms::cStr& body();
144
146 TagsList* GetSubTag(int Index);
149 TagsList* elem(int Index);
150 TagsList* operator[](int Index);
152 TagsList* GetSubTag(const char* SubQuoteName);
153 int GetSubTagIndex(const char* SubQuoteName);
156 TagsList* elem(const char* SubQuoteName);
157 TagsList* operator[](const char* SubQuoteName);
161 int length();
162
163 comms::cStr string(const char* SubQuoteName);
164 int integer(const char* SubQuoteName, int Default = 0);
165 float floating(const char* SubQuoteName, float Default = 0.0f);
166 bool boolean(const char* SubQuoteName, bool Default = false);
167
169 void RemoveSubTag(int idx);
171 void RemoveSubTag(const char* id);
174
176 const char* GetTagName();
178 void SetTagName(const char* Name);
180 void FreeTag();
182 void operator = (TagsList& src);
184 static const char* GetConstantLocation(const char* str);
185 static int& GetConstantLocationValueRef(const char* str);
187 static const char* GetConstantSubstringLocation(const char* str, int Len, int** value = nullptr);
188
189 void setAsArray(bool isArray);
190 bool isArray() const;
191private:
192 const char* _tagname;
193 const char* _tag(const char* tag);
194 comms::cList<TagsList*> SubTags;
195 comms::cStr TagBody;
196 TagsList* _parent;
197 bool _isArray;
198
199};
200
201template <class X>
203 array.Free();
204 if (this) {
205 const char* s = body().ToCharPtr();
206 if (body().Length()) {
207 cPtrDiff num = *((int*)s);
208 cPtrDiff sz = *((int*)(s + 4));
209 if (sz == sizeof(X)) {
210 X val;
211 array.Add(val, num);
212 memcpy(array.ToPtr(), s + 8, num * sz);
213 }
214 }
215 }
216}
217
218template <typename X, typename TYPE>
219void TagsList::RestoreField(cList<X>& array, TYPE X::* member) {
220 if (this) {
221 const char* s = body().ToCharPtr();
222 if (body().Length()) {
223 cPtrDiff num = *((int*)s);
224 cPtrDiff sz = *((int*)(s + 4));
225 if (sz == sizeof(TYPE)) {
226 TYPE val;
227 if (array.Count() < num) {
228 X ref;
229 array.Add(ref, num - array.Count());
230 }
231 TYPE* TL = (TYPE*)(s + 8);
232 for (int i = 0; i < num; i++) {
233 array[i].*member = *TL;
234 TL++;
235 }
236 }
237 }
238 }
239}
240
241template <class X>
243 TagsList* T = AddSubTag(LastTag());
244 if (array.Count()) {
245 T->body().SetLength(4 + 4 + array.Count() * sizeof(X));
246 char* s = T->body().ToNonConstCharPtr();
247 *((int*)s) = array.Count();
248 *((int*)(s + 4)) = sizeof(X);
249 memcpy(s + 8, array.ToPtr(), cPtrDiff(array.Count()) * sizeof(X));
250 }
251 LastTag().Clear();
252 return *T;
253}
254
255template <typename X, typename TYPE>
256void TagsList::StoreField(const char* tagname, const cList<X>& array, TYPE X::* member) {
257 TagsList* T = AddSubTag(tagname);
258 if (array.Count()) {
259 T->body().SetLength(4 + 4 + array.Count() * sizeof(TYPE));
260 char* s = T->body().ToNonConstCharPtr();
261 *((int*)s) = array.Count();
262 *((int*)(s + 4)) = sizeof(TYPE);
263 TYPE* TL = (TYPE*)(s + 8);
264 for (int i = 0; i < array.Count();i++) {
265 *TL = array[i].*member;
266 TL++;
267 }
268 }
269}
270
271void ToBase64(BYTE* buf, int Len, comms::cStr& dest);
272void ToBase64(BYTE* buf, int Len, std::string& dest);
273int ReadBase64(const char* src, BYTE* buf, int MaxLen);
274
275TagsList* new_TagsList();
276inline TagsList* new_TagsList(const char* s){
277 TagsList* x=new_TagsList();
278 x->SetTagName(s);
279 return x;
280}
281void delete_TagsList(TagsList* x);
282inline void TagsList::operator delete(void* ptr, size_t size) {
283 delete_TagsList((TagsList*)ptr);
284}
285inline void* TagsList::operator new(size_t size) {
286 return new_TagsList();
287}
288
289inline comms::cStr& TagsList::body() {
290 return TagBody;
291}
292
293inline comms::cStr TagsList::string(const char* SubQuoteName) {
294 TagsList* T = elem(SubQuoteName);
295 if (T) {
296 return T->body();
297 }
298 return comms::cStr::Empty;
299}
300
301inline int TagsList::integer(const char* SubQuoteName, int Default) {
302 TagsList* T = elem(SubQuoteName);
303 if (T) {
304 return T->to_int();
305 }
306 return Default;
307}
308
309inline float TagsList::floating(const char* SubQuoteName, float Default) {
310 TagsList* T = elem(SubQuoteName);
311 if (T) {
312 return T->to_float();
313 }
314 return Default;
315}
316
317inline bool TagsList::boolean(const char* SubQuoteName, bool Default) {
318 TagsList* T = elem(SubQuoteName);
319 if (T) {
320 return T->to_bool();
321 }
322 return Default;
323}
324
325inline void TagsList::setAsArray(bool isArray) {
326 this->_isArray = isArray;
327}
328
329inline bool TagsList::isArray() const {
330 return _isArray;
331}
332
333
Use this class for build a class for UI or serialization. see class_reg.h for details about the class...
Definition BaseClass.h:94
This class intended to load/save the class derived from the BaseClass as XML/JSON....
Definition TagsList.h:21
int PutTagsIntoJsonString(comms::cStr *Source, int shift=0, bool treatAllAsText=false)
convert tags structure to the string
float to_float()
get value of the tag as float
TagsList * AddSubTag(const char *quotename, float body)
Add new tag with the floating point value.
void Assign(int v)
assign int value to the tag value
TagsList * AddSubTag(TagsList *SubQ)
Add already allocated tag to the tags list within this tag.
static const char * GetConstantSubstringLocation(const char *str, int Len, int **value=nullptr)
This function returns constant location for the input sub - string (max length = Len)....
void bin_ToBS(BinStream &BS)
save to the binary stream in the binary form, see the implementation for the data format specificatio...
int GetTagsFromJsonString(const char *jsonSource)
convert string to the tags structure
TagsList * elem(const char *SubQuoteName)
void Assign(const char *s)
assign string value to the tag
static const char * GetConstantLocation(const char *str)
This function returns constant location for the input string. Even if the input string will be destro...
void FreeTag()
free all memory associated with this tag
int GetTagsFromString(const char *XMLSource)
convert string to the tags structure
const char * GetTagName()
returns this tag name
static comms::cStr & LastTagFile()
last saved/loaded file with tags
void StoreField(const char *tagname, const cList< X > &array, TYPE X::*member)
Definition TagsList.h:256
TagsList * operator[](int Index)
Should return pointer (not reference) because of optimization error in clang/LLVM.
TagsList * GetSubTag(const char *SubQuoteName)
Get sub-tag by name.
bool ReadTagsFromFile(const char *FilePath)
read tags from file
bool to_bool() const
convert tag value to bool
TagsList * elem(int Index)
int GetSubTagsCount()
Get amount of sub-tags.
bool FromBS(BinStream &BS)
load in binary or text form
TagsList * GetSubTag(int Index)
Get sub-tag by index.
TagsList * AddSubTag(const char *quotename, const char *body="")
Add new tag to the tags list within this tag.
void operator>>(comms::cStr &string_value)
read values from the tags list
void RemoveSubTag(const char *id)
Remove sub-tag by index.
int PutTagsIntoString(comms::cStr *To, int shift=0, bool fast=false)
convert tags structure to the string
TagsList * AddSubTag(const char *quotename, int body)
Add new tag with integer value to the tags list within this tag.
int to_int() const
convert tag value to int
TagsList * operator[](const char *SubQuoteName)
Should return pointer (not reference) because of optimization error in clang/LLVM.
void WriteTagsToFile(const char *FilePath, bool OnlyChanged=false)
save tags to file
float to_float() const
convert tag value to float
const char * to_string() const
get value of the tag
void text_ToBS(BinStream &BS)
save to the binary stream in the textual (XML) form
TagsList & operator<<(const char *string)
void RestoreField(cList< X > &array, TYPE X::*member)
Definition TagsList.h:219
int length()
Get amount of sub-tags, same as previous, js-like.
void RemoveSubTag(int idx)
Remove sub-tag by index.
int get_data(void *buffer, int maxlength)
get value of the tag as the data array, returns the actual data length
TagsList * AddSubTag(const char *quotename, void *data, int Length)
Add new tag with the data array.
TagsList * RemoveAndGetSubTag(int idx)
Remove and return tag by index. The return value will not be destroyed, you should do it later by you...
void SetTagName(const char *Name)
set name for the current tag. If this tag was already used somewhere, the memory will not be allocate...
The array template, refer it as coat::list <...> if you are using the Core API.
Definition cList.h:136