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
164 void RemoveSubTag(int idx);
166 void RemoveSubTag(const char* id);
169
171 const char* GetTagName();
173 void SetTagName(const char* Name);
175 void FreeTag();
177 void operator = (TagsList& src);
179 static const char* GetConstantLocation(const char* str);
180 static int& GetConstantLocationValueRef(const char* str);
182 static const char* GetConstantSubstringLocation(const char* str, int Len, int** value = nullptr);
183
184 void setAsArray(bool isArray);
185 bool isArray() const;
186private:
187 const char* _tagname;
188 const char* _tag(const char* tag);
189 comms::cList<TagsList*> SubTags;
190 comms::cStr TagBody;
191 TagsList* _parent;
192 bool _isArray;
193
194};
195
196template <class X>
198 array.Free();
199 if (this) {
200 const char* s = body().ToCharPtr();
201 if (body().Length()) {
202 cPtrDiff num = *((int*)s);
203 cPtrDiff sz = *((int*)(s + 4));
204 if (sz == sizeof(X)) {
205 X val;
206 array.Add(val, num);
207 memcpy(array.ToPtr(), s + 8, num * sz);
208 }
209 }
210 }
211}
212
213template <typename X, typename TYPE>
214void TagsList::RestoreField(cList<X>& array, TYPE X::* member) {
215 if (this) {
216 const char* s = body().ToCharPtr();
217 if (body().Length()) {
218 cPtrDiff num = *((int*)s);
219 cPtrDiff sz = *((int*)(s + 4));
220 if (sz == sizeof(TYPE)) {
221 TYPE val;
222 if (array.Count() < num) {
223 X ref;
224 array.Add(ref, num - array.Count());
225 }
226 TYPE* TL = (TYPE*)(s + 8);
227 for (int i = 0; i < num; i++) {
228 array[i].*member = *TL;
229 TL++;
230 }
231 }
232 }
233 }
234}
235
236template <class X>
238 TagsList* T = AddSubTag(LastTag());
239 if (array.Count()) {
240 T->body().SetLength(4 + 4 + array.Count() * sizeof(X));
241 char* s = T->body().ToNonConstCharPtr();
242 *((int*)s) = array.Count();
243 *((int*)(s + 4)) = sizeof(X);
244 memcpy(s + 8, array.ToPtr(), cPtrDiff(array.Count()) * sizeof(X));
245 }
246 LastTag().Clear();
247 return *T;
248}
249
250template <typename X, typename TYPE>
251void TagsList::StoreField(const char* tagname, const cList<X>& array, TYPE X::* member) {
252 TagsList* T = AddSubTag(tagname);
253 if (array.Count()) {
254 T->body().SetLength(4 + 4 + array.Count() * sizeof(TYPE));
255 char* s = T->body().ToNonConstCharPtr();
256 *((int*)s) = array.Count();
257 *((int*)(s + 4)) = sizeof(TYPE);
258 TYPE* TL = (TYPE*)(s + 8);
259 for (int i = 0; i < array.Count();i++) {
260 *TL = array[i].*member;
261 TL++;
262 }
263 }
264}
265
266void ToBase64(BYTE* buf, int Len, comms::cStr& dest);
267void ToBase64(BYTE* buf, int Len, std::string& dest);
268int ReadBase64(const char* src, BYTE* buf, int MaxLen);
269
270TagsList* new_TagsList();
271inline TagsList* new_TagsList(const char* s){
272 TagsList* x=new_TagsList();
273 x->SetTagName(s);
274 return x;
275}
276void delete_TagsList(TagsList* x);
277inline void TagsList::operator delete(void* ptr, size_t size) {
278 delete_TagsList((TagsList*)ptr);
279}
280inline void* TagsList::operator new(size_t size) {
281 return new_TagsList();
282}
283
284inline comms::cStr& TagsList::body() {
285 return TagBody;
286}
287
288inline void TagsList::setAsArray(bool isArray) {
289 this->_isArray = isArray;
290}
291
292inline bool TagsList::isArray() const {
293 return _isArray;
294}
295
296
Use this class for build a class for UI or serialization. see class_reg.h for details about the class...
Definition BaseClass.h:91
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:251
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:214
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:133