From f93e3bd9bcf1232c5b9120e6d07ddf8da3bbb890 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Tue, 19 Aug 2014 23:31:24 +0200 Subject: [PATCH] [DEV] std::map to etk::Hash due to a test error --- ejson/Object.cpp | 128 +++++++++++++++++++---------------------------- ejson/Object.h | 40 +++------------ 2 files changed, 59 insertions(+), 109 deletions(-) diff --git a/ejson/Object.cpp b/ejson/Object.cpp index 708c26b..498b1a0 100644 --- a/ejson/Object.cpp +++ b/ejson/Object.cpp @@ -20,12 +20,12 @@ #define __class__ "Object" void ejson::Object::clear() { - for(auto &it : m_value) { - if (it.second == NULL) { + for (int32_t iii=0; iiisecond; + for (int32_t iii=0; iiiisString()) { ejson::String* tmp2 = tmp->toString(); - if (tmp2 != NULL) { + if (tmp2 != nullptr) { if( tmp2->get().size()>25 - || it->first.size()>25) { + || m_value.getKey(iii).size()>25) { oneLine=false; break; } @@ -239,17 +239,15 @@ bool ejson::Object::iGenerate(std::string& _data, size_t _indent) const { } else { _data += "{\n"; } - for(auto it = m_value.begin(); - it != m_value.end(); - ++it) { + for (int32_t iii=0; iiifirst; + _data += m_value.getKey(iii); _data += "\": "; - it->second->iGenerate(_data, _indent+1); - if (it != m_value.end()) { + m_value.getValue(iii)->iGenerate(_data, _indent+1); + if (iiisecond; + return m_value[_name]; } const ejson::Value* ejson::Object::get(const std::string& _name) const { - auto it = m_value.find(_name); - if (it == m_value.end()) { + if (false == m_value.exist(_name)) { return NULL; } - return it->second; + return m_value[_name]; } ejson::Object* ejson::Object::getObject(const std::string& _name) { - auto it = m_value.find(_name); - if (it == m_value.end()) { + ejson::Value* tmp = get(_name); + if (NULL == tmp) { return NULL; } - if (NULL == it->second) { - return NULL; - } - return dynamic_cast(it->second); + return dynamic_cast(tmp); } const ejson::Object* ejson::Object::getObject(const std::string& _name) const { - auto it = m_value.find(_name); - if (it == m_value.end()) { + const ejson::Value* tmp = get(_name); + if (NULL == tmp) { return NULL; } - if (NULL == it->second) { - return NULL; - } - return dynamic_cast(it->second); + return dynamic_cast(tmp); } ejson::Array* ejson::Object::getArray(const std::string& _name) { - auto it = m_value.find(_name); - if (it == m_value.end()) { + ejson::Value* tmp = get(_name); + if (NULL == tmp) { return NULL; } - if (NULL == it->second) { - return NULL; - } - return dynamic_cast(it->second); + return dynamic_cast(tmp); } const ejson::Array* ejson::Object::getArray(const std::string& _name) const { - auto it = m_value.find(_name); - if (it == m_value.end()) { + const ejson::Value* tmp = get(_name); + if (NULL == tmp) { return NULL; } - if (NULL == it->second) { - return NULL; - } - return dynamic_cast(it->second); + return dynamic_cast(tmp); } ejson::Null* ejson::Object::getNull(const std::string& _name) { - auto it = m_value.find(_name); - if (it == m_value.end()) { + ejson::Value* tmp = get(_name); + if (NULL == tmp) { return NULL; } - if (NULL == it->second) { - return NULL; - } - return dynamic_cast(it->second); + return dynamic_cast(tmp); } const ejson::Null* ejson::Object::getNull(const std::string& _name) const { - auto it = m_value.find(_name); - if (it == m_value.end()) { + const ejson::Value* tmp = get(_name); + if (NULL == tmp) { return NULL; } - if (NULL == it->second) { - return NULL; - } - return dynamic_cast(it->second); + return dynamic_cast(tmp); } ejson::String* ejson::Object::getString(const std::string& _name) { - auto it = m_value.find(_name); - if (it == m_value.end()) { + ejson::Value* tmp = get(_name); + if (NULL == tmp) { return NULL; } - if (NULL == it->second) { - return NULL; - } - return dynamic_cast(it->second); + return dynamic_cast(tmp); } const ejson::String* ejson::Object::getString(const std::string& _name) const { - auto it = m_value.find(_name); - if (it == m_value.end()) { + const ejson::Value* tmp = get(_name); + if (NULL == tmp) { return NULL; } - if (NULL == it->second) { - return NULL; - } - return dynamic_cast(it->second); + return dynamic_cast(tmp); } const std::string& ejson::Object::getStringValue(const std::string& _name) const { @@ -446,13 +418,13 @@ bool ejson::Object::add(const std::string& _name, ejson::Value* _value) { if (_name.size() == 0) { return false; } - auto it = m_value.find(_name); - if (it != m_value.end()) { - delete(it->second); - it->second = _value; + if (m_value.exist(_name)) { + ejson::Value* tmp = m_value[_name]; + delete(tmp); + m_value[_name] = _value; return true; } - m_value.insert(std::pair(_name, _value)); + m_value.add(_name, _value); return true; } @@ -498,11 +470,13 @@ ejson::Value* ejson::Object::duplicate() const { JSON_ERROR("Allocation error ..."); return NULL; } - for(auto it = m_value.begin(); it != m_value.end(); ++it) { - if (it->second == NULL) { + for (int32_t iii=0; iiiadd(it->first, it->second->duplicate()); + output->add(key, val->duplicate()); } return output; } diff --git a/ejson/Object.h b/ejson/Object.h index 704489c..148a124 100755 --- a/ejson/Object.h +++ b/ejson/Object.h @@ -10,7 +10,7 @@ #define __ETK_JSON_OBJECT_H__ #include -#include +#include #include #include @@ -26,7 +26,7 @@ namespace ejson { */ virtual ~Object() { }; protected: - std::map m_value; //!< value of the node (for element this is the name, for text it is the inside text ...) + etk::Hash m_value; //!< value of the node (for element this is the name, for text it is the inside text ...) public: // TODO : add direct id access.... /** @@ -57,11 +57,7 @@ namespace ejson { * @return a vector of all name (key). */ std::vector getKeys() const { - std::vector keys; - for (auto &it : m_value) { - keys.push_back(it.first); - } - return keys; + return m_value.getKeys(); } /** * @brief get the number of sub element in the current one @@ -76,33 +72,19 @@ namespace ejson { * @return NULL if the element does not exist. */ ejson::Value* get(size_t _id) { - size_t id = 0; - for(auto &it : m_value) { - if (id == _id) { - return it.second; - } - id++; - } - return NULL; + return m_value[_id]; }; //! @previous const ejson::Value* get(size_t _id) const{ - size_t id = 0; - for(auto &it : m_value) { - if (id == _id) { - return it.second; - } - id++; - } - return NULL; + return m_value[_id]; }; //! @previous ejson::Value* operator[] (size_t _id) { - return get(_id); + return m_value[_id]; } //! @previous const ejson::Value* operator[] (size_t _id) const { - return get(_id); + return m_value[_id]; } /** * @brief Get the element name (key). @@ -110,13 +92,7 @@ namespace ejson { * @return The name (key). */ std::string getKey(size_t _id) const { - size_t id = 0; - for(auto it = m_value.begin(); it != m_value.end(); ++it, ++id) { - if (id == _id) { - return it->first; - } - } - return NULL; + return m_value.getKey(_id); } /** * @brief get the sub element with his name (Casted as Object if it is possible)