|
| 1 | +#pragma once |
| 2 | +#include "qd/base/base.h" |
| 3 | +#include "qd/stl/ref_ptr.h" |
| 4 | +#include "qd/stl/string.h" |
| 5 | +#include "qd/debug/exception.h" |
| 6 | + |
| 7 | + |
| 8 | +namespace qd { |
| 9 | +class INodeElement; |
| 10 | + |
| 11 | +struct INodeIterator : public qd::RefCounted { |
| 12 | + ref_ptr<INodeElement> m_pCurrent; |
| 13 | + using Ptr = qd::ref_ptr<INodeIterator>; |
| 14 | + |
| 15 | +public: |
| 16 | + ref_ptr<INodeElement>& get() { return m_pCurrent; } |
| 17 | + void setCurrent(const ref_ptr<INodeElement>& Current) { m_pCurrent = Current; } |
| 18 | + |
| 19 | + virtual ~INodeIterator() override = default; |
| 20 | + |
| 21 | +}; // INodeIterator |
| 22 | +////////////////////////////////////////////////////////////////////////// |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +// It's base interface for getting data from hierarchical data sources like XML, JSON, etc |
| 27 | +// read-only Interface |
| 28 | +class INodeElement : public qd::RefCounted |
| 29 | +{ |
| 30 | + using TThis = INodeElement; |
| 31 | +public: |
| 32 | + using Ptr = qd::ref_ptr<INodeElement>; |
| 33 | + |
| 34 | + virtual qtd::string getName() { return ""; } |
| 35 | + |
| 36 | + virtual int getLine() { return -1; } |
| 37 | + virtual int getColumn() { return -1; } |
| 38 | + |
| 39 | + virtual bool getStrTry(const qtd::string_view& /*pKey*/, qtd::string& /*outVal*/) { return false; } |
| 40 | + virtual bool getIntValue(const qtd::string_view& /*pKey*/, int& /*outVal*/) { return false; } |
| 41 | + virtual bool getU32Try(const qtd::string_view& /*pKey*/, uint32_t* /*outVal*/ = nullptr) { return false; } |
| 42 | + virtual bool getFloatValue(const qtd::string_view& /*pKey*/, float& /*outVal*/) { return false; } |
| 43 | + virtual bool getBoolValue(const qtd::string_view& /*pKey*/, bool& /*outVal*/) { return false; } |
| 44 | + virtual bool getPointValue(const qtd::string_view& /*pKey*/, int& /*outX*/, int& /*outY*/) { return false; } |
| 45 | + virtual bool getVectorValue(const qtd::string_view& /*pKey*/, float& x, float& y) { return false; } |
| 46 | + |
| 47 | + template<class TString> |
| 48 | + qtd::string getStrDef(const qtd::string_view& key, const TString& defVal) { |
| 49 | + qtd::string tmpVal; |
| 50 | + if (this->getStrTry(key, tmpVal)) |
| 51 | + return tmpVal; |
| 52 | + return defVal; |
| 53 | + } |
| 54 | + |
| 55 | + template<class TString> |
| 56 | + bool getStrTry(const qtd::string_view& pKey, TString* outVal) { |
| 57 | + qtd::string tmpVal; |
| 58 | + if (this->getStrTry(pKey, tmpVal)) { |
| 59 | + if (outVal) |
| 60 | + outVal->assign(tmpVal.c_str(), tmpVal.size()); |
| 61 | + return true; |
| 62 | + } |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + inline int getIntValue(const qtd::string_view& pKey) { |
| 68 | + int Val; |
| 69 | + if (!getIntValue(pKey, Val)) |
| 70 | + G_THROW_OR_DO(Exception("Node can't parse value for Attr:\"%s\"", pKey), return 0); |
| 71 | + return Val; |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | + inline uint32_t getU32Def(const qtd::string_view& key, uint32_t defVal) { |
| 76 | + uint32_t Val; |
| 77 | + if (getU32Try(key, &Val)) |
| 78 | + return Val; |
| 79 | + return defVal; |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + inline float getFloatValue(const qtd::string_view& pKey) { |
| 84 | + float Val; |
| 85 | + if (!getFloatValue(pKey, Val)) |
| 86 | + G_THROW_OR_DO(Exception("Node can't parse value for Attr:\"%s\"", pKey), return 0.f); |
| 87 | + return Val; |
| 88 | + } |
| 89 | + |
| 90 | + inline bool getBoolValue(const qtd::string_view& pKey) { |
| 91 | + bool Val; |
| 92 | + if (!getBoolValue(pKey, Val)) |
| 93 | + G_THROW_OR_DO(Exception("Node can't parse value for Attr:\"%s\"", pKey), return false); |
| 94 | + return Val; |
| 95 | + } |
| 96 | + |
| 97 | + inline qtd::string getStrTry(const qtd::string_view& pKey) { |
| 98 | + qtd::string Res; |
| 99 | + if (!getStrTry(pKey, Res)) |
| 100 | + return qtd::string(); |
| 101 | + return Res; |
| 102 | + } |
| 103 | + |
| 104 | + virtual qtd::string getNodeText() { return qtd::string(); } |
| 105 | + |
| 106 | + // Keys access by Index |
| 107 | + // Get Num Keys |
| 108 | + virtual int getNumKeys() const { return 0; } |
| 109 | + // GET Key By Index |
| 110 | + virtual qtd::string getKeyByInd(int /*nKey*/) { return qtd::string(); } |
| 111 | + |
| 112 | + // = HAS KEY |
| 113 | + virtual bool isKeyExists(const qtd::string_view& /*pKey*/) { return false; } |
| 114 | + |
| 115 | + // Childs By indexe |
| 116 | + virtual int getNumChilds() const { return 0; } |
| 117 | + |
| 118 | + virtual INodeElement::Ptr getChildByInd(int /*i*/) { return nullptr; } |
| 119 | + INodeElement::Ptr getChildByInd(int i) const { return const_cast<TThis*>(this)->getChildByInd(i); } |
| 120 | + |
| 121 | + // Finds child node by name |
| 122 | + virtual INodeElement::Ptr findChild(const qtd::string_view& /*key*/) { return nullptr; } |
| 123 | + INodeElement::Ptr findChild(const qtd::string_view& key) const { return const_cast<TThis*>(this)->findChild(key); } |
| 124 | + |
| 125 | + // Finds child by his attributes NAME and VALUE in this Attribute |
| 126 | + virtual INodeElement::Ptr findChildByKeyVal(const qtd::string_view& /*pKeyName*/, const qtd::string_view& /*pKeyValue*/) { return nullptr; } |
| 127 | + |
| 128 | + // Iteratable API |
| 129 | + virtual qd::INodeIterator::Ptr itBegin() { return nullptr; } |
| 130 | + virtual void itNext(qd::INodeIterator* /*pIt*/) {} |
| 131 | + virtual bool itEnd(qd::INodeIterator* /*pIt*/) { return true; } |
| 132 | + |
| 133 | +}; // class INodeElement |
| 134 | +////////////////////////////////////////////////////////////////////////// |
| 135 | + |
| 136 | + |
| 137 | +////////////////////////////////////////////////////////////////////////// |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | +/// // |
| 142 | +/// Read value from Simple CString |
| 143 | +/// |
| 144 | +class CNodeString : public qd::INodeElement |
| 145 | +{ |
| 146 | + typedef CNodeString TThis; |
| 147 | + qtd::string m_Name; |
| 148 | + qtd::string m_Value; |
| 149 | + |
| 150 | +public: |
| 151 | + CNodeString(const qtd::string& Value = qtd::string(), const qtd::string& _Name = qtd::string()) |
| 152 | + : m_Value(Value) |
| 153 | + , m_Name(_Name) {} |
| 154 | + |
| 155 | + TThis* setName(const qtd::string& v) { |
| 156 | + m_Name = v; |
| 157 | + return this; |
| 158 | + } |
| 159 | + TThis* setValue(const qtd::string& v) { |
| 160 | + m_Value = v; |
| 161 | + return this; |
| 162 | + } |
| 163 | + virtual qtd::string getName() override { return m_Name; } |
| 164 | + virtual int getLine() override { return 0; } |
| 165 | + virtual int getColumn() override { return 0; } |
| 166 | + |
| 167 | + virtual qtd::string getNodeText() override { return m_Value; } |
| 168 | + |
| 169 | + virtual bool getStrTry(const qtd::string_view&, qtd::string& Value) override { |
| 170 | + Value = m_Value; |
| 171 | + return true; |
| 172 | + } |
| 173 | + // TODO: |
| 174 | + //virtual bool getIntValue(const qtd::string_view&, int& Value) override { return m_Value.parseInt(Value); } |
| 175 | + //virtual bool getUIntValue(const qtd::string_view&, uint32_t& Value) override { return m_Value.parseUInt(Value); } |
| 176 | + //virtual bool getFloatValue(const qtd::string_view&, float& Value) override { return m_Value.parseFloat(Value); } |
| 177 | + //virtual bool getBoolValue(const qtd::string_view&, bool& Value) override { return m_Value.parseBool(Value); } |
| 178 | + |
| 179 | + virtual int getNumChilds() const override { return 0; } |
| 180 | + virtual int getNumKeys() const override { return 0; } |
| 181 | + virtual qtd::string getKeyByInd(int /*i*/) override { return ""; } |
| 182 | + virtual bool isKeyExists(const qtd::string_view& /*pAttr*/) override { return false; } |
| 183 | + virtual ref_ptr<INodeElement> getChildByInd(int /*i*/) override { return nullptr; } |
| 184 | + virtual ref_ptr<INodeElement> findChild(const qtd::string_view& /*pName*/) override { return nullptr; } |
| 185 | + |
| 186 | +}; // class CNodeCmdArgs |
| 187 | +////////////////////////////////////////////////////////////////////////// |
| 188 | + |
| 189 | + |
| 190 | + |
| 191 | +}; // namespace qd |
0 commit comments