14 #include "FileReader.hpp" 21 std::string base64_encode(
const atUint8* bytes_to_encode,
size_t in_len);
22 std::unique_ptr<atUint8[]> base64_decode(
const std::string& encoded_string);
24 void HandleYAMLParserError(yaml_parser_t* parser);
25 void HandleYAMLEmitterError(yaml_emitter_t* emitter);
29 std::string::const_iterator begin;
30 std::string::const_iterator end;
38 unsigned char* buffer,
size_t size,
size_t* size_read);
39 int YAMLStdStringWriter(std::string* str,
unsigned char* buffer,
size_t size);
43 yaml_node_type_t m_type;
44 std::string m_scalarString;
45 std::vector<std::unique_ptr<YAMLNode>> m_seqChildren;
46 std::vector<std::pair<std::string, std::unique_ptr<YAMLNode>>> m_mapChildren;
47 YAMLNode(yaml_node_type_t type) : m_type(type) {}
48 inline const YAMLNode* findMapChild(
const char* key)
const 50 for (
const auto& item : m_mapChildren)
51 if (!item.first.compare(key))
52 return item.second.get();
57 template <
typename RETURNTYPE>
58 RETURNTYPE NodeToVal(
const YAMLNode* node);
60 template <
typename INTYPE>
61 std::unique_ptr<YAMLNode> ValToNode(
const INTYPE& val);
62 template <
typename INTYPE>
63 std::unique_ptr<YAMLNode> ValToNode(
const INTYPE* val);
64 template <
typename INTYPE>
65 std::unique_ptr<YAMLNode> ValToNode(
const INTYPE& val,
size_t byteCount);
68 inline bool NodeToVal(
const YAMLNode* node)
70 char firstCh = tolower(node->m_scalarString[0]);
73 else if (firstCh ==
'f')
75 else if (isdigit(firstCh) && firstCh != 0)
81 inline std::unique_ptr<YAMLNode> ValToNode(
const bool& val)
84 ret->m_scalarString = val?
"True":
"False";
85 return std::unique_ptr<YAMLNode>(ret);
89 inline atInt8 NodeToVal(
const YAMLNode* node)
91 return strtol(node->m_scalarString.c_str(), NULL, 0);
95 inline std::unique_ptr<YAMLNode> ValToNode(
const atInt8& val)
98 snprintf(str, 32,
"0x%02X", val);
100 ret->m_scalarString = str;
101 return std::unique_ptr<YAMLNode>(ret);
105 inline atUint8 NodeToVal(
const YAMLNode* node)
107 return strtoul(node->m_scalarString.c_str(), NULL, 0);
111 inline std::unique_ptr<YAMLNode> ValToNode(
const atUint8& val)
114 snprintf(str, 32,
"0x%02X", val);
116 ret->m_scalarString = str;
117 return std::unique_ptr<YAMLNode>(ret);
121 inline atInt16 NodeToVal(
const YAMLNode* node)
123 return strtol(node->m_scalarString.c_str(), NULL, 0);
127 inline std::unique_ptr<YAMLNode> ValToNode(
const atInt16& val)
130 snprintf(str, 32,
"0x%04X", val);
132 ret->m_scalarString = str;
133 return std::unique_ptr<YAMLNode>(ret);
137 inline atUint16 NodeToVal(
const YAMLNode* node)
139 return strtoul(node->m_scalarString.c_str(), NULL, 0);
143 inline std::unique_ptr<YAMLNode> ValToNode(
const atUint16& val)
146 snprintf(str, 32,
"0x%04X", val);
148 ret->m_scalarString = str;
149 return std::unique_ptr<YAMLNode>(ret);
153 inline atInt32 NodeToVal(
const YAMLNode* node)
155 return strtol(node->m_scalarString.c_str(), NULL, 0);
159 inline std::unique_ptr<YAMLNode> ValToNode(
const atInt32& val)
162 snprintf(str, 32,
"0x%08X", val);
164 ret->m_scalarString = str;
165 return std::unique_ptr<YAMLNode>(ret);
169 inline atUint32 NodeToVal(
const YAMLNode* node)
171 return strtoul(node->m_scalarString.c_str(), NULL, 0);
175 inline std::unique_ptr<YAMLNode> ValToNode(
const atUint32& val)
178 snprintf(str, 32,
"0x%08X", val);
180 ret->m_scalarString = str;
181 return std::unique_ptr<YAMLNode>(ret);
185 inline atInt64 NodeToVal(
const YAMLNode* node)
188 return _strtoi64(node->m_scalarString.c_str(), NULL, 0);
190 return strtoq(node->m_scalarString.c_str(), NULL, 0);
195 inline std::unique_ptr<YAMLNode> ValToNode(
const atInt64& val)
198 snprintf(str, 32,
"0x%016" PRIX64, val);
200 ret->m_scalarString = str;
201 return std::unique_ptr<YAMLNode>(ret);
205 inline atUint64 NodeToVal(
const YAMLNode* node)
208 return _strtoui64(node->m_scalarString.c_str(), NULL, 0);
210 return strtouq(node->m_scalarString.c_str(), NULL, 0);
215 inline std::unique_ptr<YAMLNode> ValToNode(
const atUint64& val)
218 snprintf(str, 32,
"0x%016" PRIX64, val);
220 ret->m_scalarString = str;
221 return std::unique_ptr<YAMLNode>(ret);
225 inline float NodeToVal(
const YAMLNode* node)
227 return strtof(node->m_scalarString.c_str(), NULL);
231 inline std::unique_ptr<YAMLNode> ValToNode(
const float& val)
234 snprintf(str, 64,
"%f", val);
236 ret->m_scalarString = str;
237 return std::unique_ptr<YAMLNode>(ret);
241 inline double NodeToVal(
const YAMLNode* node)
243 return strtod(node->m_scalarString.c_str(), NULL);
247 inline std::unique_ptr<YAMLNode> ValToNode(
const double& val)
250 snprintf(str, 64,
"%f", val);
252 ret->m_scalarString = str;
253 return std::unique_ptr<YAMLNode>(ret);
256 template <
typename RETURNTYPE>
257 inline RETURNTYPE NodeToVec(
const YAMLNode* node)
259 RETURNTYPE retval = {};
260 auto it = node->m_seqChildren.begin();
262 i<4 && it != node->m_seqChildren.end();
266 if (snode->m_type == YAML_SCALAR_NODE)
268 if (std::is_same<RETURNTYPE, atVec2d>::value ||
269 std::is_same<RETURNTYPE, atVec3d>::value ||
270 std::is_same<RETURNTYPE, atVec4d>::value)
271 retval.vec[i] = NodeToVal<double>(snode);
273 retval.vec[i] = NodeToVal<float>(snode);
284 return NodeToVec<atVec2f>(node);
288 inline std::unique_ptr<YAMLNode> ValToNode(
const atVec2f& val)
291 ret->m_seqChildren.reserve(2);
292 for (
size_t i=0 ; i<2 ; ++i)
295 snprintf(str, 64,
"%f", val.vec[i]);
297 comp->m_scalarString = str;
298 ret->m_seqChildren.emplace_back(comp);
300 return std::unique_ptr<YAMLNode>(ret);
306 return NodeToVec<atVec3f>(node);
310 inline std::unique_ptr<YAMLNode> ValToNode(
const atVec3f& val)
313 ret->m_seqChildren.reserve(3);
314 for (
size_t i=0 ; i<3 ; ++i)
317 snprintf(str, 64,
"%f", val.vec[i]);
319 comp->m_scalarString = str;
320 ret->m_seqChildren.emplace_back(comp);
322 return std::unique_ptr<YAMLNode>(ret);
328 return NodeToVec<atVec4f>(node);
332 inline std::unique_ptr<YAMLNode> ValToNode(
const atVec4f& val)
335 ret->m_seqChildren.reserve(4);
336 for (
size_t i=0 ; i<4 ; ++i)
339 snprintf(str, 64,
"%f", val.vec[i]);
341 comp->m_scalarString = str;
342 ret->m_seqChildren.emplace_back(comp);
344 return std::unique_ptr<YAMLNode>(ret);
350 return NodeToVec<atVec2d>(node);
354 inline std::unique_ptr<YAMLNode> ValToNode(
const atVec2d& val)
357 ret->m_seqChildren.reserve(2);
358 for (
size_t i=0 ; i<2 ; ++i)
361 snprintf(str, 64,
"%f", val.vec[i]);
363 comp->m_scalarString = str;
364 ret->m_seqChildren.emplace_back(comp);
366 return std::unique_ptr<YAMLNode>(ret);
372 return NodeToVec<atVec3d>(node);
376 inline std::unique_ptr<YAMLNode> ValToNode(
const atVec3d& val)
379 ret->m_seqChildren.reserve(3);
380 for (
size_t i=0 ; i<3 ; ++i)
383 snprintf(str, 64,
"%f", val.vec[i]);
385 comp->m_scalarString = str;
386 ret->m_seqChildren.emplace_back(comp);
388 return std::unique_ptr<YAMLNode>(ret);
394 return NodeToVec<atVec4d>(node);
398 inline std::unique_ptr<YAMLNode> ValToNode(
const atVec4d& val)
401 ret->m_seqChildren.reserve(4);
402 for (
size_t i=0 ; i<4 ; ++i)
405 snprintf(str, 64,
"%f", val.vec[i]);
407 comp->m_scalarString = str;
408 ret->m_seqChildren.emplace_back(comp);
410 return std::unique_ptr<YAMLNode>(ret);
414 inline std::unique_ptr<atUint8[]> NodeToVal(
const YAMLNode* node)
416 return base64_decode(node->m_scalarString);
420 inline std::unique_ptr<YAMLNode> ValToNode(
const std::unique_ptr<atUint8[]>& val,
size_t byteCount)
423 ret->m_scalarString = base64_encode(val.get(), byteCount);
424 return std::unique_ptr<YAMLNode>(ret);
428 inline std::string NodeToVal(
const YAMLNode* node)
430 return node->m_scalarString;
434 inline std::unique_ptr<YAMLNode> ValToNode(
const std::string& val)
437 ret->m_scalarString = val;
438 return std::unique_ptr<YAMLNode>(ret);
442 inline std::unique_ptr<YAMLNode> ValToNode(
const char* val)
445 ret->m_scalarString = val;
446 return std::unique_ptr<YAMLNode>(ret);
450 inline std::wstring NodeToVal(
const YAMLNode* node)
453 retval.reserve(node->m_scalarString.length());
454 const utf8proc_uint8_t* buf =
reinterpret_cast<const utf8proc_uint8_t*
>(node->m_scalarString.c_str());
458 utf8proc_ssize_t len = utf8proc_iterate(buf, -1, &wc);
461 atWarning(
"invalid UTF-8 character while decoding");
465 retval += wchar_t(wc);
471 inline std::unique_ptr<YAMLNode> ValToNode(
const std::wstring& val)
474 ret->m_scalarString.reserve(val.length());
475 for (
wchar_t ch : val)
477 utf8proc_uint8_t mb[4];
478 utf8proc_ssize_t c = utf8proc_encode_char(utf8proc_int32_t(ch), mb);
481 atWarning(
"invalid UTF-8 character while encoding");
482 return std::unique_ptr<YAMLNode>(ret);
484 ret->m_scalarString.append(reinterpret_cast<char*>(mb), c);
486 return std::unique_ptr<YAMLNode>(ret);
490 inline std::unique_ptr<YAMLNode> ValToNode(
const wchar_t* val)
492 std::wstring wstr(val);
493 return ValToNode<const std::wstring&>(wstr);
498 std::unique_ptr<YAMLNode> m_rootNode;
499 std::vector<YAMLNode*> m_subStack;
500 std::vector<int> m_seqTrackerStack;
501 yaml_parser_t m_parser;
507 if (!yaml_parser_initialize(&m_parser))
509 HandleYAMLParserError(&m_parser);
515 yaml_parser_delete(&m_parser);
520 yaml_parser_delete(&m_parser);
521 if (!yaml_parser_initialize(&m_parser))
522 HandleYAMLParserError(&m_parser);
525 yaml_parser_t* getParser() {
return &m_parser;}
528 std::unique_ptr<YAMLNode> newRoot = ParseEvents(reader);
531 m_rootNode = std::move(newRoot);
533 m_subStack.push_back(m_rootNode.get());
534 m_seqTrackerStack.clear();
538 bool ClassTypeOperation(std::function<
bool(
const char* dnaType)> func);
539 bool ValidateClassType(
const char* expectedType);
541 inline const YAMLNode* getRootNode()
const {
return m_rootNode.get();}
542 inline const YAMLNode* getCurNode()
const {
return m_subStack.empty() ?
nullptr : m_subStack.back();}
543 std::unique_ptr<YAMLNode> releaseRootNode() {
return std::move(m_rootNode);}
545 bool enterSubRecord(
const char* name)
547 YAMLNode* curSub = m_subStack.back();
548 if (curSub->m_type == YAML_SEQUENCE_NODE)
550 int& seqIdx = m_seqTrackerStack.back();
551 m_subStack.push_back(curSub->m_seqChildren[seqIdx++].get());
552 if (m_subStack.back()->m_type == YAML_SEQUENCE_NODE)
553 m_seqTrackerStack.push_back(0);
556 for (
const auto& item : curSub->m_mapChildren)
558 if (!item.first.compare(name))
560 m_subStack.push_back(item.second.get());
561 if (m_subStack.back()->m_type == YAML_SEQUENCE_NODE)
562 m_seqTrackerStack.push_back(0);
569 void leaveSubRecord()
571 if (m_subStack.size() > 1)
573 if (m_subStack.back()->m_type == YAML_SEQUENCE_NODE)
574 m_seqTrackerStack.pop_back();
575 m_subStack.pop_back();
580 void enumerate(
const char* name, T& record)
582 enterSubRecord(name);
587 bool enterSubVector(
const char* name,
size_t& countOut)
589 YAMLNode* curSub = m_subStack.back();
590 if (!name && curSub->m_type == YAML_SEQUENCE_NODE)
592 m_subStack.push_back(curSub);
593 m_seqTrackerStack.push_back(0);
594 countOut = curSub->m_seqChildren.size();
599 for (
const auto& item : curSub->m_mapChildren)
601 if (!item.first.compare(name))
603 YAMLNode* nextSub = item.second.get();
604 if (nextSub->m_type == YAML_SEQUENCE_NODE)
606 countOut = nextSub->m_seqChildren.size();
610 atError(
"'%s' is not a vector field", name);
613 m_subStack.push_back(nextSub);
614 m_seqTrackerStack.push_back(0);
623 void leaveSubVector()
625 if (m_subStack.size() > 1)
627 m_subStack.pop_back();
628 m_seqTrackerStack.pop_back();
632 size_t enumerate(
const char* name, std::vector<T>& vector,
633 typename std::enable_if<!std::is_arithmetic<T>::value &&
634 !std::is_same<T, atVec2f>::value &&
635 !std::is_same<T, atVec3f>::value &&
636 !std::is_same<T, atVec4f>::value>::type* = 0)
639 enterSubVector(name, countOut);
641 vector.reserve(countOut);
642 for (
size_t i=0 ; i<countOut ; ++i)
644 vector.emplace_back();
645 enterSubRecord(
nullptr);
646 vector.back().read(*
this);
654 size_t enumerate(
const char* name, std::vector<T>& vector,
655 typename std::enable_if<std::is_arithmetic<T>::value ||
656 std::is_same<T, atVec2f>::value ||
657 std::is_same<T, atVec3f>::value ||
658 std::is_same<T, atVec4f>::value>::type* = 0)
661 enterSubVector(name, countOut);
663 vector.reserve(countOut);
664 for (
size_t i=0 ; i<countOut ; ++i)
665 vector.push_back(readVal<T>(name));
671 size_t enumerate(
const char* name, std::vector<T>& vector,
675 enterSubVector(name, countOut);
677 vector.reserve(countOut);
678 for (
size_t i=0 ; i<countOut ; ++i)
680 vector.emplace_back();
681 enterSubRecord(
nullptr);
682 readf(*
this, vector.back());
689 template <
typename RETURNTYPE>
690 RETURNTYPE readVal(
const char* name)
692 if (m_subStack.size())
694 const YAMLNode* mnode = m_subStack.back();
695 if (mnode->m_type == YAML_SCALAR_NODE)
697 return NodeToVal<RETURNTYPE>(mnode);
699 else if (mnode->m_type == YAML_SEQUENCE_NODE)
701 int& seqIdx = m_seqTrackerStack.back();
702 return NodeToVal<RETURNTYPE>(mnode->m_seqChildren[seqIdx++].get());
704 else if (mnode->m_type == YAML_MAPPING_NODE)
706 for (
const auto& item : mnode->m_mapChildren)
708 if (!item.first.compare(name))
710 return NodeToVal<RETURNTYPE>(item.second.get());
716 atWarning(
"Unable to find field '%s'; returning 0", name);
720 inline bool readBool(
const char* name)
722 return readVal<bool>(name);
725 inline atInt8 readByte(
const char* name)
727 return readVal<atInt8>(name);
730 inline atUint8 readUByte(
const char* name)
732 return readVal<atUint8>(name);
735 inline atInt16 readInt16(
const char* name)
737 return readVal<atInt16>(name);
740 inline atUint16 readUint16(
const char* name)
742 return readVal<atUint16>(name);
745 inline atInt32 readInt32(
const char* name)
747 return readVal<atInt32>(name);
750 inline atUint32 readUint32(
const char* name)
752 return readVal<atUint32>(name);
755 inline atInt64 readInt64(
const char* name)
757 return readVal<atInt64>(name);
760 inline atUint64 readUint64(
const char* name)
762 return readVal<atUint64>(name);
765 inline float readFloat(
const char* name)
767 return readVal<float>(name);
770 inline double readDouble(
const char* name)
772 return readVal<double>(name);
775 inline atVec2f readVec2f(
const char* name)
777 return readVal<atVec2f>(name);
780 inline atVec3f readVec3f(
const char* name)
782 return readVal<atVec3f>(name);
784 inline atVec4f readVec4f(
const char* name)
786 return readVal<atVec4f>(name);
789 inline atVec2d readVec2d(
const char* name)
791 return readVal<atVec2d>(name);
794 inline atVec3d readVec3d(
const char* name)
796 return readVal<atVec3d>(name);
799 inline atVec4d readVec4d(
const char* name)
801 return readVal<atVec4d>(name);
804 inline std::unique_ptr<atUint8[]> readUBytes(
const char* name)
806 return readVal<std::unique_ptr<atUint8[]>>(name);
809 inline std::string readString(
const char* name)
811 return readVal<std::string>(name);
814 inline std::wstring readWString(
const char* name)
816 return readVal<std::wstring>(name);
824 std::vector<YAMLNode*> m_subStack;
825 yaml_emitter_t m_emitter;
826 static bool RecursiveFinish(yaml_emitter_t* doc,
const YAMLNode& node);
828 YAMLDocWriter(
const char* classType) : m_rootNode(YAML_MAPPING_NODE)
830 if (!yaml_emitter_initialize(&m_emitter))
832 HandleYAMLEmitterError(&m_emitter);
835 yaml_emitter_set_unicode(&m_emitter,
true);
836 yaml_emitter_set_width(&m_emitter, -1);
838 m_subStack.emplace_back(&m_rootNode);
842 classVal->m_scalarString.assign(classType);
843 m_rootNode.m_mapChildren.emplace_back(
"DNAType", std::unique_ptr<YAMLNode>(classVal));
849 yaml_emitter_delete(&m_emitter);
852 yaml_emitter_t* getEmitter() {
return &m_emitter;}
856 inline YAMLNode* getCurNode()
const {
return m_subStack.empty() ?
nullptr : m_subStack.back();}
858 void enterSubRecord(
const char* name)
860 YAMLNode* curSub = m_subStack.back();
861 if (curSub->m_type != YAML_MAPPING_NODE &&
862 curSub->m_type != YAML_SEQUENCE_NODE)
865 if (curSub->m_type == YAML_MAPPING_NODE)
866 curSub->m_mapChildren.emplace_back(name?std::string(name):std::string(), std::unique_ptr<YAMLNode>(newNode));
867 else if (curSub->m_type == YAML_SEQUENCE_NODE)
868 curSub->m_seqChildren.emplace_back(newNode);
869 m_subStack.push_back(newNode);
872 void leaveSubRecord()
874 if (m_subStack.size() > 1)
876 YAMLNode* curSub = m_subStack.back();
878 if (curSub->m_mapChildren.size() == 1 &&
879 curSub->m_mapChildren[0].first.empty())
881 auto& item = curSub->m_mapChildren[0];
882 if (item.first.empty())
884 if (item.second->m_type == YAML_SCALAR_NODE)
886 curSub->m_type = YAML_SCALAR_NODE;
887 curSub->m_scalarString = std::move(item.second->m_scalarString);
888 curSub->m_mapChildren.clear();
890 else if (item.second->m_type == YAML_SEQUENCE_NODE)
892 curSub->m_type = YAML_SEQUENCE_NODE;
893 curSub->m_seqChildren = std::move(item.second->m_seqChildren);
894 curSub->m_mapChildren.clear();
898 m_subStack.pop_back();
903 void enumerate(
const char* name, T& record)
905 enterSubRecord(name);
910 void enterSubVector(
const char* name)
912 YAMLNode* curSub = m_subStack.back();
913 if (curSub->m_type != YAML_MAPPING_NODE &&
914 curSub->m_type != YAML_SEQUENCE_NODE)
917 if (curSub->m_type == YAML_MAPPING_NODE)
918 curSub->m_mapChildren.emplace_back(name?std::string(name):std::string(), std::unique_ptr<YAMLNode>(newNode));
919 else if (curSub->m_type == YAML_SEQUENCE_NODE)
920 curSub->m_seqChildren.emplace_back(newNode);
921 m_subStack.push_back(newNode);
924 void leaveSubVector()
926 if (m_subStack.size() > 1)
927 m_subStack.pop_back();
931 void enumerate(
const char* name,
const std::vector<T>& vector,
932 typename std::enable_if<!std::is_arithmetic<T>::value &&
933 !std::is_same<T, atVec2f>::value &&
934 !std::is_same<T, atVec3f>::value &&
935 !std::is_same<T, atVec4f>::value &&
936 !std::is_same<T, atVec2d>::value &&
937 !std::is_same<T, atVec3d>::value &&
938 !std::is_same<T, atVec4d>::value>::type* = 0)
940 enterSubVector(name);
941 for (
const T& item : vector)
943 enterSubRecord(
nullptr);
951 void enumerate(
const char* name,
const std::vector<T>& vector,
952 typename std::enable_if<std::is_arithmetic<T>::value ||
953 std::is_same<T, atVec2f>::value ||
954 std::is_same<T, atVec3f>::value ||
955 std::is_same<T, atVec4f>::value ||
956 std::is_same<T, atVec2d>::value ||
957 std::is_same<T, atVec3d>::value ||
958 std::is_same<T, atVec4d>::value>::type* = 0)
960 enterSubVector(name);
961 for (T item : vector)
962 writeVal<T>(
nullptr, item);
967 void enumerate(
const char* name,
const std::vector<T>& vector,
970 enterSubVector(name);
971 for (
const T& item : vector)
973 enterSubRecord(
nullptr);
980 template <
typename INTYPE>
981 void writeVal(
const char* name,
const INTYPE& val)
983 YAMLNode* curSub = m_subStack.back();
984 if (curSub->m_type == YAML_MAPPING_NODE)
985 curSub->m_mapChildren.emplace_back(name?name:std::string(), std::move(ValToNode(val)));
986 else if (curSub->m_type == YAML_SEQUENCE_NODE)
987 curSub->m_seqChildren.emplace_back(std::move(ValToNode(val)));
990 template <
typename INTYPE>
991 void writeVal(
const char* name,
const INTYPE& val,
size_t byteCount)
993 YAMLNode* curSub = m_subStack.back();
994 if (curSub->m_type == YAML_MAPPING_NODE)
995 curSub->m_mapChildren.emplace_back(name?name:std::string(), std::move(ValToNode(val, byteCount)));
996 else if (curSub->m_type == YAML_SEQUENCE_NODE)
997 curSub->m_seqChildren.emplace_back(std::move(ValToNode(val, byteCount)));
1000 inline void writeBool(
const char* name,
const bool& val)
1002 writeVal<bool>(name, val);
1005 inline void writeByte(
const char* name,
const atInt8& val)
1007 writeVal<atInt8>(name, val);
1010 inline void writeUByte(
const char* name,
const atUint8& val)
1012 writeVal<atUint8>(name, val);
1015 inline void writeInt16(
const char* name,
const atInt16& val)
1017 writeVal<atInt16>(name, val);
1020 inline void writeUint16(
const char* name,
const atUint16& val)
1022 writeVal<atUint16>(name, val);
1025 inline void writeInt32(
const char* name,
const atInt32& val)
1027 writeVal<atInt32>(name, val);
1030 inline void writeUint32(
const char* name,
const atUint32& val)
1032 writeVal<atUint32>(name, val);
1035 inline void writeInt64(
const char* name,
const atInt64& val)
1037 writeVal<atInt64>(name, val);
1040 inline void writeUint64(
const char* name,
const atUint64& val)
1042 writeVal<atUint64>(name, val);
1045 inline void writeFloat(
const char* name,
const float& val)
1047 writeVal<float>(name, val);
1050 inline void writeDouble(
const char* name,
const double& val)
1052 writeVal<double>(name, val);
1055 inline void writeVec2f(
const char* name,
const atVec2f& val)
1057 writeVal<atVec2f>(name, val);
1060 inline void writeVec3f(
const char* name,
const atVec3f& val)
1062 writeVal<atVec3f>(name, val);
1065 inline void writeVec4f(
const char* name,
const atVec4f& val)
1067 writeVal<atVec4f>(name, val);
1070 inline void writeVec2d(
const char* name,
const atVec2d& val)
1072 writeVal<atVec2d>(name, val);
1075 inline void writeVec3d(
const char* name,
const atVec3d& val)
1077 writeVal<atVec3d>(name, val);
1080 inline void writeVec4d(
const char* name,
const atVec4d& val)
1082 writeVal<atVec4d>(name, val);
1085 inline void writeUBytes(
const char* name,
const std::unique_ptr<atUint8[]>& val,
size_t byteCount)
1087 writeVal<const std::unique_ptr<atUint8[]>&>(name, val, byteCount);
1090 inline void writeString(
const char* name,
const std::string& val)
1092 writeVal<std::string>(name, val);
1095 inline void writeString(
const char* name,
const char* val)
1097 writeVal<const char*>(name, val);
1100 inline void writeWString(
const char* name,
const std::wstring& val)
1102 writeVal<std::wstring>(name, val);
1105 inline void writeWString(
const char* name,
const wchar_t* val)
1107 writeVal<const wchar_t*>(name, val);
1112 unsigned char* buffer,
size_t size,
size_t* size_read);
1115 unsigned char *buffer,
size_t size);
1119 template <
size_t sizeVar, Endian VE>
1122 template <atInt32 sizeVar, Endian VE>
1125 template <atInt32 sizeVar, Endian VE>
1128 template <atInt32 sizeVar, Endian VE>
1131 template <Endian DNAE>
1140 static const char* DNAType() {
return nullptr;}
1141 virtual const char* DNATypeV()
const {
return nullptr;}
1143 template <
size_t sizeVar>
1146 template <atInt32 sizeVar = -1>
1149 template <atInt32 sizeVar = -1, Endian VE = DNAE>
1152 template <atInt32 sizeVar = -1>
1155 std::string toYAMLString()
const 1160 yaml_emitter_set_output(docWriter.getEmitter(), (yaml_write_handler_t*)YAMLStdStringWriter, &res);
1161 yaml_emitter_set_unicode(docWriter.getEmitter(),
true);
1162 yaml_emitter_set_width(docWriter.getEmitter(), -1);
1165 if (!docWriter.finish(
nullptr))
1166 return std::string();
1171 bool fromYAMLString(
const std::string& str)
1175 yaml_parser_set_input(docReader.getParser(), (yaml_read_handler_t*)YAMLStdStringReader, &reader);
1176 if (!docReader.parse(
nullptr))
1182 template<
class DNASubtype>
1183 static bool ValidateFromYAMLString(
const std::string& str)
1187 yaml_parser_set_input(docReader.getParser(), (yaml_read_handler_t*)YAMLStdStringReader, &reader);
1188 bool retval = docReader.ValidateClassType(DNASubtype::DNAType());
1196 yaml_emitter_set_unicode(docWriter.getEmitter(),
true);
1197 yaml_emitter_set_width(docWriter.getEmitter(), -1);
1200 if (!docWriter.finish(&fout))
1209 if (!docReader.parse(&fin))
1215 template<
class DNASubtype>
1220 yaml_parser_set_input(reader.getParser(), (yaml_read_handler_t*)YAMLAthenaReader, &fin);
1221 bool retval = reader.ValidateClassType(DNASubtype::DNAType());
1222 fin.
seek(pos, athena::Begin);
1227 template <
size_t sizeVar, Endian VE>
1233 reset(
new atUint8[sizeVar]);
1242 return __isz + sizeVar;
1245 {*
this = reader.readUBytes(
nullptr);}
1247 {writer.writeUBytes(
nullptr, *
this, sizeVar);}
1250 template <atInt32 sizeVar, Endian VE>
1255 {this->assign(std::move(reader.
readString(sizeVar)));}
1259 {
return __isz + ((sizeVar<0)?(this->size()+1):sizeVar);}
1261 {this->assign(std::move(reader.readString(
nullptr)));}
1263 {writer.writeString(
nullptr, *
this);}
1264 std::string& operator=(
const std::string& __str)
1265 {
return this->assign(__str);}
1266 std::string& operator=(std::string&& __str)
1267 {this->swap(__str);
return *
this;}
1270 template <atInt32 sizeVar, Endian VE>
1276 reader.setEndian(VE);
1277 this->assign(std::move(reader.
readWString(sizeVar)));
1281 writer.setEndian(VE);
1285 {
return __isz + (((sizeVar<0)?(this->size()+1):sizeVar)*2);}
1287 {this->assign(std::move(reader.readWString(
nullptr)));}
1289 {writer.writeWString(
nullptr, *
this);}
1290 std::wstring& operator=(
const std::wstring& __str)
1291 {
return this->assign(__str);}
1292 std::wstring& operator=(std::wstring&& __str)
1293 {this->swap(__str);
return *
this;}
1296 template <atInt32 sizeVar, Endian VE>
1305 {
return __isz + (((sizeVar<0)?(this->size()+1):sizeVar)*2);}
1307 {this->assign(std::move(reader.readString(
nullptr)));}
1309 {writer.writeString(
nullptr, *
this);}
1310 std::string& operator=(
const std::string& __str)
1311 {
return this->assign(__str);}
1312 std::string& operator=(std::string&& __str)
1313 {this->swap(__str);
return *
this;}
1319 void read(athena::io::YAMLDocReader&); \ 1320 void write(athena::io::YAMLDocWriter&) const; \ 1321 static const char* DNAType(); \ 1322 const char* DNATypeV() const {return DNAType();} \ 1325 #define DECL_EXPLICIT_YAML \ 1326 void read(athena::io::YAMLDocReader&); \ 1327 void write(athena::io::YAMLDocWriter&) const; \ 1328 static const char* DNAType(); \ 1329 const char* DNATypeV() const {return DNAType();} \ 1334 #endif // DNAYAML_HPP
void read(IStreamReader &reader)
Common virtual read function for all DNA types.
void read(IStreamReader &reader)
Common virtual read function for all DNA types.
std::string readWStringAsString(atInt32 fixedLen=-1)
Reads a wide-char string (using endianness from setEndian), converts to UTF8 and advances the positio...
std::wstring readWString(atInt32 fixedLen=-1)
Reads a wstring and advances the position in the file.
void writeWString(const std::wstring &str, atInt32 fixedLen=-1)
Writes an wstring to the buffer and advances the buffer.
Base DNA class used against 'atdna'.
virtual void writeUBytes(const atUint8 *data, atUint64 len)=0
Writes the given buffer with the specified length, buffers can be bigger than the length however it's...
Concrete converting-wstring type used by DNA::WStringAsString.
The IStreamReader class defines a basic API for reading from streams, Implementors are provided with ...
Concrete buffer type used by DNA::Buffer.
void write(IStreamWriter &writer) const
Common virtual write function for all DNA types.
void write(IStreamWriter &writer) const
Common virtual write function for all DNA types.
void writeStringAsWString(const std::string &str, atInt32 fixedLen=-1)
Converts a UTF8 string to a wide-char string in the buffer and advances the buffer. It also swaps the bytes depending on the platform and Stream settings.
size_t binarySize(size_t __isz) const
Common virtual binary size computation for all DNA types.
Concrete wstring type used by DNA::WString.
void read(IStreamReader &reader)
Common virtual read function for all DNA types.
std::string readString(atInt32 fixedLen=-1)
Reads a string and advances the position in the file.
virtual atUint64 position() const =0
Returns the current position in the stream.
virtual void seek(atInt64 pos, SeekOrigin origin=SeekOrigin::Current)=0
Sets the buffers position relative to the specified position. It seeks relative to the current posit...
void read(IStreamReader &reader)
Common virtual read function for all DNA types.
size_t binarySize(size_t __isz) const
Common virtual binary size computation for all DNA types.
size_t binarySize(size_t __isz) const
Common virtual binary size computation for all DNA types.
void write(IStreamWriter &writer) const
Common virtual write function for all DNA types.
void writeString(const std::string &str, atInt32 fixedLen=-1)
Writes an string to the buffer and advances the buffer.
Concrete string type used by DNA::String.
virtual atUint64 readUBytesToBuf(void *buf, atUint64 len)=0
Attempts to read a fixed length of data into a pre-allocated buffer, this function is client defined ...
size_t binarySize(size_t __isz) const
Common virtual binary size computation for all DNA types.
void write(IStreamWriter &writer) const
Common virtual write function for all DNA types.