Fix incomplete type

This commit is contained in:
Leonid Stryzhevskyi 2024-04-30 00:35:23 +03:00
parent 249fc4f137
commit 87af860824
3 changed files with 53 additions and 53 deletions

View File

@ -29,9 +29,9 @@
namespace oatpp { namespace data { namespace mapping { 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); auto it = m_map.find(key);
if(it == m_map.end()) { if(it == m_map.end()) {
auto& result = m_map[key]; auto& result = m_map[key];
@ -41,25 +41,25 @@ Tree& Tree::Map::operator [] (const oatpp::String& key) {
return it->second; 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); auto it = m_map.find(key);
if(it == m_map.end()) { 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; return it->second;
} }
std::pair<oatpp::String, std::reference_wrapper<Tree>> Tree::Map::operator [] (v_uint64 index) { std::pair<oatpp::String, std::reference_wrapper<Tree>> TreeMap::operator [] (v_uint64 index) {
auto& key = m_order.at(index); auto& key = m_order.at(index);
return {key, m_map[key]}; return {key, m_map[key]};
} }
std::pair<oatpp::String, std::reference_wrapper<const Tree>> Tree::Map::operator [] (v_uint64 index) const { std::pair<oatpp::String, std::reference_wrapper<const Tree>> TreeMap::operator [] (v_uint64 index) const {
auto& key = m_order.at(index); auto& key = m_order.at(index);
return {key, m_map.at(key)}; return {key, m_map.at(key)};
} }
v_uint64 Tree::Map::size() const { v_uint64 TreeMap::size() const {
return m_map.size(); return m_map.size();
} }
@ -201,7 +201,7 @@ void Tree::deleteValueObject() {
break; break;
} }
case Type::MAP: { case Type::MAP: {
auto data = reinterpret_cast<Map *>(m_data); auto data = reinterpret_cast<TreeMap *>(m_data);
delete data; delete data;
break; break;
} }
@ -276,7 +276,7 @@ void Tree::setCopy(const Tree& other) {
case Type::STRING: { case Type::STRING: {
auto otherData = reinterpret_cast<oatpp::String *>(other.m_data); auto otherData = reinterpret_cast<oatpp::String *>(other.m_data);
if(otherData == nullptr) { 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); auto ptr = new oatpp::String(*otherData);
m_data = reinterpret_cast<LARGEST_TYPE>(ptr); m_data = reinterpret_cast<LARGEST_TYPE>(ptr);
@ -285,25 +285,25 @@ void Tree::setCopy(const Tree& other) {
case Type::VECTOR: { case Type::VECTOR: {
auto otherData = reinterpret_cast<std::vector<Tree> *>(other.m_data); auto otherData = reinterpret_cast<std::vector<Tree> *>(other.m_data);
if(otherData == nullptr) { 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<Tree>(*otherData); auto ptr = new std::vector<Tree>(*otherData);
m_data = reinterpret_cast<LARGEST_TYPE>(ptr); m_data = reinterpret_cast<LARGEST_TYPE>(ptr);
break; break;
} }
case Type::MAP: { case Type::MAP: {
auto otherData = reinterpret_cast<Map *>(other.m_data); auto otherData = reinterpret_cast<TreeMap *>(other.m_data);
if(otherData == nullptr) { 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<LARGEST_TYPE>(ptr); m_data = reinterpret_cast<LARGEST_TYPE>(ptr);
break; break;
} }
case Type::PAIRS: { case Type::PAIRS: {
auto otherData = reinterpret_cast<std::vector<std::pair<oatpp::String, Tree>> *>(other.m_data); auto otherData = reinterpret_cast<std::vector<std::pair<oatpp::String, Tree>> *>(other.m_data);
if(otherData == nullptr) { 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<std::pair<oatpp::String, Tree>>(*otherData); auto ptr = new std::vector<std::pair<oatpp::String, Tree>>(*otherData);
m_data = reinterpret_cast<LARGEST_TYPE>(ptr); m_data = reinterpret_cast<LARGEST_TYPE>(ptr);
@ -376,10 +376,10 @@ void Tree::setVector(v_uint64 size) {
m_data = reinterpret_cast<LARGEST_TYPE>(data); m_data = reinterpret_cast<LARGEST_TYPE>(data);
} }
void Tree::setMap(const Map& value) { void Tree::setMap(const TreeMap& value) {
deleteValueObject(); deleteValueObject();
m_type = Type::MAP; m_type = Type::MAP;
auto data = new Map(value); auto data = new TreeMap(value);
m_data = reinterpret_cast<LARGEST_TYPE>(data); m_data = reinterpret_cast<LARGEST_TYPE>(data);
} }
@ -531,7 +531,7 @@ bool Tree::isIntPrimitive() const {
v_int64 Tree::getInteger() const { v_int64 Tree::getInteger() const {
if(m_type != Type::INTEGER) { 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; v_int64 result;
std::memcpy (&result, &m_data, sizeof(v_float64)); std::memcpy (&result, &m_data, sizeof(v_float64));
@ -540,7 +540,7 @@ v_int64 Tree::getInteger() const {
v_float64 Tree::getFloat() const { v_float64 Tree::getFloat() const {
if(m_type != Type::FLOAT) { 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; v_float64 result;
std::memcpy (&result, &m_data, sizeof(v_float64)); std::memcpy (&result, &m_data, sizeof(v_float64));
@ -549,7 +549,7 @@ v_float64 Tree::getFloat() const {
const oatpp::String& Tree::getString() const { const oatpp::String& Tree::getString() const {
if(m_type != Type::STRING) { 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<const oatpp::String*>(m_data); auto data = reinterpret_cast<const oatpp::String*>(m_data);
return *data; return *data;
@ -557,23 +557,23 @@ const oatpp::String& Tree::getString() const {
const std::vector<Tree>& Tree::getVector() const { const std::vector<Tree>& Tree::getVector() const {
if(m_type != Type::VECTOR) { 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<const std::vector<Tree>*>(m_data); auto data = reinterpret_cast<const std::vector<Tree>*>(m_data);
return *data; return *data;
} }
const Tree::Map& Tree::getMap() const { const TreeMap& Tree::getMap() const {
if(m_type != Type::MAP) { 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<const Map*>(m_data); auto data = reinterpret_cast<const TreeMap*>(m_data);
return *data; return *data;
} }
const std::vector<std::pair<oatpp::String, Tree>>& Tree::getPairs() const { const std::vector<std::pair<oatpp::String, Tree>>& Tree::getPairs() const {
if(m_type != Type::PAIRS) { 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<const std::vector<std::pair<oatpp::String, Tree>>*>(m_data); auto data = reinterpret_cast<const std::vector<std::pair<oatpp::String, Tree>>*>(m_data);
return *data; return *data;
@ -584,20 +584,20 @@ std::vector<Tree>& Tree::getVector() {
setVector({}); setVector({});
} }
if(m_type != Type::VECTOR) { 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<std::vector<Tree>*>(m_data); auto data = reinterpret_cast<std::vector<Tree>*>(m_data);
return *data; return *data;
} }
Tree::Map& Tree::getMap() { TreeMap& Tree::getMap() {
if(m_type == Type::UNDEFINED) { if(m_type == Type::UNDEFINED) {
setMap({}); setMap({});
} }
if(m_type != Type::MAP) { 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<Map*>(m_data); auto data = reinterpret_cast<TreeMap*>(m_data);
return *data; return *data;
} }
@ -606,7 +606,7 @@ std::vector<std::pair<oatpp::String, Tree>>& Tree::getPairs() {
setPairs({}); setPairs({});
} }
if(m_type != Type::MAP) { 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<std::vector<std::pair<oatpp::String, Tree>>*>(m_data); auto data = reinterpret_cast<std::vector<std::pair<oatpp::String, Tree>>*>(m_data);
return *data; return *data;

View File

@ -29,6 +29,8 @@
namespace oatpp { namespace data { namespace mapping { namespace oatpp { namespace data { namespace mapping {
class TreeMap;
class Tree { class Tree {
public: public:
@ -67,24 +69,6 @@ public:
struct NodePrimitiveType { struct NodePrimitiveType {
}; };
public:
class Map {
private:
std::unordered_map<oatpp::String, Tree> m_map;
std::vector<oatpp::String> m_order;
public:
Tree& operator [] (const oatpp::String& key);
const Tree& operator [] (const oatpp::String& key) const;
std::pair<oatpp::String, std::reference_wrapper<Tree>> operator [] (v_uint64 index);
std::pair<oatpp::String, std::reference_wrapper<const Tree>> operator [] (v_uint64 index) const;
v_uint64 size() const;
};
public: public:
class Attributes { 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<typename T> template<typename T>
T getValue() const { T getValue() const {
if(m_type != NodePrimitiveType<T>::type) { if(m_type != NodePrimitiveType<T>::type) {
throw std::runtime_error(std::string("[oatpp::data::Tree::getValue()]: NOT a ") + NodePrimitiveType<T>::name); throw std::runtime_error(std::string("[oatpp::data::mapping::Tree::getValue()]: NOT a ") + NodePrimitiveType<T>::name);
} }
T result; T result;
std::memcpy (&result, &m_data, sizeof(T)); std::memcpy (&result, &m_data, sizeof(T));
@ -222,7 +206,7 @@ public:
void setString(const oatpp::String& value); void setString(const oatpp::String& value);
void setVector(const std::vector<Tree>& value); void setVector(const std::vector<Tree>& value);
void setVector(v_uint64 size); void setVector(v_uint64 size);
void setMap(const Map& value); void setMap(const TreeMap& value);
void setPairs(const std::vector<std::pair<oatpp::String, Tree>>& value); void setPairs(const std::vector<std::pair<oatpp::String, Tree>>& value);
bool isNull() const; bool isNull() const;
@ -239,11 +223,11 @@ public:
const oatpp::String& getString() const; const oatpp::String& getString() const;
const std::vector<Tree>& getVector() const; const std::vector<Tree>& getVector() const;
const Map& getMap() const; const TreeMap& getMap() const;
const std::vector<std::pair<oatpp::String, Tree>>& getPairs() const; const std::vector<std::pair<oatpp::String, Tree>>& getPairs() const;
std::vector<Tree>& getVector(); std::vector<Tree>& getVector();
Map& getMap(); TreeMap& getMap();
std::vector<std::pair<oatpp::String, Tree>>& getPairs(); std::vector<std::pair<oatpp::String, Tree>>& getPairs();
Attributes& attributes(); Attributes& attributes();
@ -253,6 +237,22 @@ public:
}; };
class TreeMap {
private:
std::unordered_map<oatpp::String, Tree> m_map;
std::vector<oatpp::String> m_order;
public:
Tree& operator [] (const oatpp::String& key);
const Tree& operator [] (const oatpp::String& key) const;
std::pair<oatpp::String, std::reference_wrapper<Tree>> operator [] (v_uint64 index);
std::pair<oatpp::String, std::reference_wrapper<const Tree>> operator [] (v_uint64 index) const;
v_uint64 size() const;
};
////////////////////////////////////////////////////// //////////////////////////////////////////////////////
// Tree::NodePrimitiveType // Tree::NodePrimitiveType

View File

@ -132,7 +132,7 @@ void TreeTest::onRun() {
} }
{ {
Tree::Map originalMap; TreeMap originalMap;
for(v_uint32 i = 0; i < 10; i ++) { for(v_uint32 i = 0; i < 10; i ++) {
originalMap["node_" + utils::Conversion::int32ToStr(static_cast<v_int32>(i))].setValue(i); originalMap["node_" + utils::Conversion::int32ToStr(static_cast<v_int32>(i))].setValue(i);
} }