diff --git a/src/oatpp/data/mapping/Tree.cpp b/src/oatpp/data/mapping/Tree.cpp index 2320ed3f..7c049501 100644 --- a/src/oatpp/data/mapping/Tree.cpp +++ b/src/oatpp/data/mapping/Tree.cpp @@ -29,9 +29,9 @@ namespace oatpp { namespace data { namespace mapping { //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// -// Tree::Map +// TreeMap -Tree& Tree::Map::operator [] (const oatpp::String& key) { +Tree& TreeMap::operator [] (const oatpp::String& key) { auto it = m_map.find(key); if(it == m_map.end()) { auto& result = m_map[key]; @@ -41,25 +41,25 @@ Tree& Tree::Map::operator [] (const oatpp::String& key) { return it->second; } -const Tree& Tree::Map::operator [] (const oatpp::String& key) const { +const Tree& TreeMap::operator [] (const oatpp::String& key) const { auto it = m_map.find(key); if(it == m_map.end()) { - throw std::runtime_error("[oatpp::data::Tree::Map::operator[]]: const operator[] can't add items."); + throw std::runtime_error("[oatpp::data::mapping::Tree::TreeMap::operator[]]: const operator[] can't add items."); } return it->second; } -std::pair> Tree::Map::operator [] (v_uint64 index) { +std::pair> TreeMap::operator [] (v_uint64 index) { auto& key = m_order.at(index); return {key, m_map[key]}; } -std::pair> Tree::Map::operator [] (v_uint64 index) const { +std::pair> TreeMap::operator [] (v_uint64 index) const { auto& key = m_order.at(index); return {key, m_map.at(key)}; } -v_uint64 Tree::Map::size() const { +v_uint64 TreeMap::size() const { return m_map.size(); } @@ -201,7 +201,7 @@ void Tree::deleteValueObject() { break; } case Type::MAP: { - auto data = reinterpret_cast(m_data); + auto data = reinterpret_cast(m_data); delete data; break; } @@ -276,7 +276,7 @@ void Tree::setCopy(const Tree& other) { case Type::STRING: { auto otherData = reinterpret_cast(other.m_data); if(otherData == nullptr) { - throw std::runtime_error("[oatpp::data::Tree::setCopy()]: other.data is null, other.type is 'STRING'"); + throw std::runtime_error("[oatpp::data::mapping::Tree::setCopy()]: other.data is null, other.type is 'STRING'"); } auto ptr = new oatpp::String(*otherData); m_data = reinterpret_cast(ptr); @@ -285,25 +285,25 @@ void Tree::setCopy(const Tree& other) { case Type::VECTOR: { auto otherData = reinterpret_cast *>(other.m_data); if(otherData == nullptr) { - throw std::runtime_error("[oatpp::data::Tree::setCopy()]: other.data is null, other.type is 'VECTOR'"); + throw std::runtime_error("[oatpp::data::mapping::Tree::setCopy()]: other.data is null, other.type is 'VECTOR'"); } auto ptr = new std::vector(*otherData); m_data = reinterpret_cast(ptr); break; } case Type::MAP: { - auto otherData = reinterpret_cast(other.m_data); + auto otherData = reinterpret_cast(other.m_data); if(otherData == nullptr) { - throw std::runtime_error("[oatpp::data::Tree::setCopy()]: other.data is null, other.type is 'MAP'"); + throw std::runtime_error("[oatpp::data::mapping::Tree::setCopy()]: other.data is null, other.type is 'MAP'"); } - auto ptr = new Map(*otherData); + auto ptr = new TreeMap(*otherData); m_data = reinterpret_cast(ptr); break; } case Type::PAIRS: { auto otherData = reinterpret_cast> *>(other.m_data); if(otherData == nullptr) { - throw std::runtime_error("[oatpp::data::Tree::setCopy()]: other.data is null, other.type is 'PAIRS'"); + throw std::runtime_error("[oatpp::data::mapping::Tree::setCopy()]: other.data is null, other.type is 'PAIRS'"); } auto ptr = new std::vector>(*otherData); m_data = reinterpret_cast(ptr); @@ -376,10 +376,10 @@ void Tree::setVector(v_uint64 size) { m_data = reinterpret_cast(data); } -void Tree::setMap(const Map& value) { +void Tree::setMap(const TreeMap& value) { deleteValueObject(); m_type = Type::MAP; - auto data = new Map(value); + auto data = new TreeMap(value); m_data = reinterpret_cast(data); } @@ -531,7 +531,7 @@ bool Tree::isIntPrimitive() const { v_int64 Tree::getInteger() const { if(m_type != Type::INTEGER) { - throw std::runtime_error("[oatpp::data::Tree::getInteger()]: NOT an arbitrary INTEGER."); + throw std::runtime_error("[oatpp::data::mapping::Tree::getInteger()]: NOT an arbitrary INTEGER."); } v_int64 result; std::memcpy (&result, &m_data, sizeof(v_float64)); @@ -540,7 +540,7 @@ v_int64 Tree::getInteger() const { v_float64 Tree::getFloat() const { if(m_type != Type::FLOAT) { - throw std::runtime_error("[oatpp::data::Tree::getFloat()]: NOT an arbitrary FLOAT."); + throw std::runtime_error("[oatpp::data::mapping::Tree::getFloat()]: NOT an arbitrary FLOAT."); } v_float64 result; std::memcpy (&result, &m_data, sizeof(v_float64)); @@ -549,7 +549,7 @@ v_float64 Tree::getFloat() const { const oatpp::String& Tree::getString() const { if(m_type != Type::STRING) { - throw std::runtime_error("[oatpp::data::Tree::getString()]: NOT a STRING."); + throw std::runtime_error("[oatpp::data::mapping::Tree::getString()]: NOT a STRING."); } auto data = reinterpret_cast(m_data); return *data; @@ -557,23 +557,23 @@ const oatpp::String& Tree::getString() const { const std::vector& Tree::getVector() const { if(m_type != Type::VECTOR) { - throw std::runtime_error("[oatpp::data::Tree::getVector()]: NOT a VECTOR."); + throw std::runtime_error("[oatpp::data::mapping::Tree::getVector()]: NOT a VECTOR."); } auto data = reinterpret_cast*>(m_data); return *data; } -const Tree::Map& Tree::getMap() const { +const TreeMap& Tree::getMap() const { if(m_type != Type::MAP) { - throw std::runtime_error("[oatpp::data::Tree::getMap()]: NOT a MAP."); + throw std::runtime_error("[oatpp::data::mapping::Tree::getMap()]: NOT a MAP."); } - auto data = reinterpret_cast(m_data); + auto data = reinterpret_cast(m_data); return *data; } const std::vector>& Tree::getPairs() const { if(m_type != Type::PAIRS) { - throw std::runtime_error("[oatpp::data::Tree::getPairs()]: NOT a PAIRS."); + throw std::runtime_error("[oatpp::data::mapping::Tree::getPairs()]: NOT a PAIRS."); } auto data = reinterpret_cast>*>(m_data); return *data; @@ -584,20 +584,20 @@ std::vector& Tree::getVector() { setVector({}); } if(m_type != Type::VECTOR) { - throw std::runtime_error("[oatpp::data::Tree::getVector()]: NOT a VECTOR."); + throw std::runtime_error("[oatpp::data::mapping::Tree::getVector()]: NOT a VECTOR."); } auto data = reinterpret_cast*>(m_data); return *data; } -Tree::Map& Tree::getMap() { +TreeMap& Tree::getMap() { if(m_type == Type::UNDEFINED) { setMap({}); } if(m_type != Type::MAP) { - throw std::runtime_error("[oatpp::data::Tree::getMap()]: NOT a MAP."); + throw std::runtime_error("[oatpp::data::mapping::Tree::getMap()]: NOT a MAP."); } - auto data = reinterpret_cast(m_data); + auto data = reinterpret_cast(m_data); return *data; } @@ -606,7 +606,7 @@ std::vector>& Tree::getPairs() { setPairs({}); } if(m_type != Type::MAP) { - throw std::runtime_error("[oatpp::data::Tree::getMap()]: NOT a MAP."); + throw std::runtime_error("[oatpp::data::mapping::Tree::getMap()]: NOT a MAP."); } auto data = reinterpret_cast>*>(m_data); return *data; diff --git a/src/oatpp/data/mapping/Tree.hpp b/src/oatpp/data/mapping/Tree.hpp index 0ec0b511..f839a60b 100644 --- a/src/oatpp/data/mapping/Tree.hpp +++ b/src/oatpp/data/mapping/Tree.hpp @@ -29,6 +29,8 @@ namespace oatpp { namespace data { namespace mapping { +class TreeMap; + class Tree { public: @@ -67,24 +69,6 @@ public: struct NodePrimitiveType { }; -public: - - class Map { - private: - std::unordered_map m_map; - std::vector m_order; - public: - - Tree& operator [] (const oatpp::String& key); - const Tree& operator [] (const oatpp::String& key) const; - - std::pair> operator [] (v_uint64 index); - std::pair> operator [] (v_uint64 index) const; - - v_uint64 size() const; - - }; - public: class Attributes { @@ -178,7 +162,7 @@ public: } - throw std::runtime_error("[oatpp::data::Tree::operator T ()]: Value is NOT a Primitive type."); + throw std::runtime_error("[oatpp::data::mapping::Tree::operator T ()]: Value is NOT a Primitive type."); } @@ -206,7 +190,7 @@ public: template T getValue() const { if(m_type != NodePrimitiveType::type) { - throw std::runtime_error(std::string("[oatpp::data::Tree::getValue()]: NOT a ") + NodePrimitiveType::name); + throw std::runtime_error(std::string("[oatpp::data::mapping::Tree::getValue()]: NOT a ") + NodePrimitiveType::name); } T result; std::memcpy (&result, &m_data, sizeof(T)); @@ -222,7 +206,7 @@ public: void setString(const oatpp::String& value); void setVector(const std::vector& value); void setVector(v_uint64 size); - void setMap(const Map& value); + void setMap(const TreeMap& value); void setPairs(const std::vector>& value); bool isNull() const; @@ -239,11 +223,11 @@ public: const oatpp::String& getString() const; const std::vector& getVector() const; - const Map& getMap() const; + const TreeMap& getMap() const; const std::vector>& getPairs() const; std::vector& getVector(); - Map& getMap(); + TreeMap& getMap(); std::vector>& getPairs(); Attributes& attributes(); @@ -253,6 +237,22 @@ public: }; +class TreeMap { +private: + std::unordered_map m_map; + std::vector m_order; +public: + + Tree& operator [] (const oatpp::String& key); + const Tree& operator [] (const oatpp::String& key) const; + + std::pair> operator [] (v_uint64 index); + std::pair> operator [] (v_uint64 index) const; + + v_uint64 size() const; + +}; + ////////////////////////////////////////////////////// // Tree::NodePrimitiveType diff --git a/test/oatpp/data/mapping/TreeTest.cpp b/test/oatpp/data/mapping/TreeTest.cpp index 540ddceb..a6c7a585 100644 --- a/test/oatpp/data/mapping/TreeTest.cpp +++ b/test/oatpp/data/mapping/TreeTest.cpp @@ -132,7 +132,7 @@ void TreeTest::onRun() { } { - Tree::Map originalMap; + TreeMap originalMap; for(v_uint32 i = 0; i < 10; i ++) { originalMap["node_" + utils::Conversion::int32ToStr(static_cast(i))].setValue(i); }