diff --git a/CMakeLists.txt b/CMakeLists.txt index 12b3d27..983f2b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,18 +28,35 @@ include_directories(.) #Create src file list set(src_files ejson/debug.cpp + ejson/debug.h ejson/ejson.cpp + ejson/ejson.h ejson/Array.cpp + ejson/Array.h ejson/Boolean.cpp + ejson/Boolean.h ejson/Null.cpp + ejson/Null.h ejson/Number.cpp + ejson/Number.h ejson/String.cpp + ejson/String.h ejson/Object.cpp + ejson/Object.h ejson/Value.cpp + ejson/Value.h ) add_definitions( -DDEBUG_LEVEL=3 ) add_definitions( -DDEBUG=1 ) +if (APPLE) + add_definitions( -D__TARGET_OS__MacOs ) +elseif (UNIX) + add_definitions( -D__TARGET_OS__Linux ) +elseif (WIN32) + add_definitions( -D__TARGET_OS__Windows ) +endif () + include_directories(${etk_SOURCE_DIR}) include_directories(${linearmath_SOURCE_DIR}/bullet/src/) diff --git a/ejson/Array.h b/ejson/Array.h old mode 100644 new mode 100755 index fdc63d3..00eda2f --- a/ejson/Array.h +++ b/ejson/Array.h @@ -10,8 +10,6 @@ #define __ETK_JSON_ARRAY_H__ #include -#include -#include #include namespace ejson { @@ -165,16 +163,6 @@ namespace ejson { public: // herited function : virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc); virtual bool iGenerate(std::string& _data, size_t _indent) const; - virtual enum nodeType getType(void) const { - return typeArray; - }; - virtual ejson::Array* toArray(void) { - return this; - }; - //! @previous - virtual const ejson::Array* toArray(void) const { - return this; - }; virtual void clear(void); virtual bool transfertIn(ejson::Value* _obj); virtual ejson::Value* duplicate(void) const; diff --git a/ejson/Boolean.h b/ejson/Boolean.h old mode 100644 new mode 100755 index 8440ec2..d742e51 --- a/ejson/Boolean.h +++ b/ejson/Boolean.h @@ -10,8 +10,6 @@ #define __ETK_JSON_BOOLEAN_H__ #include -#include -#include #include namespace ejson { @@ -50,16 +48,6 @@ namespace ejson { public: // herited function : virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc); virtual bool iGenerate(std::string& _data, size_t _indent) const; - virtual enum nodeType getType(void) const { - return typeString; - }; - virtual ejson::Boolean* toBoolean(void) { - return this; - }; - //! @previous - virtual const ejson::Boolean* toBoolean(void) const{ - return this; - }; virtual bool transfertIn(ejson::Value* _obj); virtual ejson::Value* duplicate(void) const; }; diff --git a/ejson/Null.h b/ejson/Null.h old mode 100644 new mode 100755 index 44d2b57..12d68e0 --- a/ejson/Null.h +++ b/ejson/Null.h @@ -10,8 +10,6 @@ #define __ETK_JSON_NULL_H__ #include -#include -#include #include namespace ejson { @@ -28,16 +26,6 @@ namespace ejson { public: // herited function : virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc); virtual bool iGenerate(std::string& _data, size_t _indent) const; - virtual enum nodeType getType(void) const { - return typeString; - }; - virtual ejson::Null* toNull(void) { - return this; - }; - //! @previous - virtual const ejson::Null* toNull(void) const{ - return this; - }; virtual bool transfertIn(ejson::Value* _obj); virtual ejson::Value* duplicate(void) const; }; diff --git a/ejson/Number.h b/ejson/Number.h old mode 100644 new mode 100755 index 760f497..038229a --- a/ejson/Number.h +++ b/ejson/Number.h @@ -10,8 +10,6 @@ #define __ETK_JSON_NUMBER_H__ #include -#include -#include #include namespace ejson { @@ -62,16 +60,6 @@ namespace ejson { public: // herited function : virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc); virtual bool iGenerate(std::string& _data, size_t _indent) const; - virtual enum nodeType getType(void) const { - return typeString; - }; - virtual ejson::Number* toNumber(void) { - return this; - }; - //! @previous - virtual const ejson::Number* toNumber(void) const{ - return this; - }; virtual bool transfertIn(ejson::Value* _obj); virtual ejson::Value* duplicate(void) const; }; diff --git a/ejson/Object.cpp b/ejson/Object.cpp index 1e48765..3edfd26 100644 --- a/ejson/Object.cpp +++ b/ejson/Object.cpp @@ -20,12 +20,12 @@ #define __class__ "Object" void ejson::Object::clear(void) { - for (int32_t iii=0; iiisecond; if (tmp == NULL) { continue; } @@ -224,9 +224,9 @@ bool ejson::Object::iGenerate(std::string& _data, size_t _indent) const { } if (true == tmp->isString()) { ejson::String* tmp2 = tmp->toString(); - if (NULL!=tmp2) { + if (tmp2 != NULL) { if( tmp2->get().size()>25 - || m_value.getKey(iii).size()>25) { + || it->first.size()>25) { oneLine=false; break; } @@ -239,15 +239,17 @@ bool ejson::Object::iGenerate(std::string& _data, size_t _indent) const { } else { _data += "{\n"; } - for (int32_t iii=0; iiifirst; _data += "\": "; - m_value.getValue(iii)->iGenerate(_data, _indent+1); - if (iiisecond->iGenerate(_data, _indent+1); + if (it != m_value.end()) { _data += ","; } if (true == oneLine) { @@ -264,85 +266,111 @@ bool ejson::Object::iGenerate(std::string& _data, size_t _indent) const { } bool ejson::Object::exist(const std::string& _name) const { - return m_value.exist(_name); + return m_value.find(_name) != m_value.end(); } ejson::Value* ejson::Object::get(const std::string& _name) { - if (false == m_value.exist(_name)) { + auto it = m_value.find(_name); + if (it == m_value.end()) { return NULL; } - return m_value[_name]; + return it->second; } const ejson::Value* ejson::Object::get(const std::string& _name) const { - if (false == m_value.exist(_name)) { + auto it = m_value.find(_name); + if (it == m_value.end()) { return NULL; } - return m_value[_name]; + return it->second; } ejson::Object* ejson::Object::getObject(const std::string& _name) { - ejson::Value* tmp = get(_name); - if (NULL == tmp) { + auto it = m_value.find(_name); + if (it == m_value.end()) { return NULL; } - return tmp->toObject(); + if (NULL == it->second) { + return NULL; + } + return dynamic_cast(it->second); } const ejson::Object* ejson::Object::getObject(const std::string& _name) const { - const ejson::Value* tmp = get(_name); - if (NULL == tmp) { + auto it = m_value.find(_name); + if (it == m_value.end()) { return NULL; } - return tmp->toObject(); + if (NULL == it->second) { + return NULL; + } + return dynamic_cast(it->second); } ejson::Array* ejson::Object::getArray(const std::string& _name) { - ejson::Value* tmp = get(_name); - if (NULL == tmp) { + auto it = m_value.find(_name); + if (it == m_value.end()) { return NULL; } - return tmp->toArray(); + if (NULL == it->second) { + return NULL; + } + return dynamic_cast(it->second); } const ejson::Array* ejson::Object::getArray(const std::string& _name) const { - const ejson::Value* tmp = get(_name); - if (NULL == tmp) { + auto it = m_value.find(_name); + if (it == m_value.end()) { return NULL; } - return tmp->toArray(); + if (NULL == it->second) { + return NULL; + } + return dynamic_cast(it->second); } ejson::Null* ejson::Object::getNull(const std::string& _name) { - ejson::Value* tmp = get(_name); - if (NULL == tmp) { + auto it = m_value.find(_name); + if (it == m_value.end()) { return NULL; } - return tmp->toNull(); + if (NULL == it->second) { + return NULL; + } + return dynamic_cast(it->second); } const ejson::Null* ejson::Object::getNull(const std::string& _name) const { - const ejson::Value* tmp = get(_name); - if (NULL == tmp) { + auto it = m_value.find(_name); + if (it == m_value.end()) { return NULL; } - return tmp->toNull(); + if (NULL == it->second) { + return NULL; + } + return dynamic_cast(it->second); } ejson::String* ejson::Object::getString(const std::string& _name) { - ejson::Value* tmp = get(_name); - if (NULL == tmp) { + auto it = m_value.find(_name); + if (it == m_value.end()) { return NULL; } - return tmp->toString(); + if (NULL == it->second) { + return NULL; + } + return dynamic_cast(it->second); } const ejson::String* ejson::Object::getString(const std::string& _name) const { - const ejson::Value* tmp = get(_name); - if (NULL == tmp) { + auto it = m_value.find(_name); + if (it == m_value.end()) { return NULL; } - return tmp->toString(); + if (NULL == it->second) { + return NULL; + } + return dynamic_cast(it->second); } const std::string& ejson::Object::getStringValue(const std::string& _name) const { @@ -418,13 +446,13 @@ bool ejson::Object::add(const std::string& _name, ejson::Value* _value) { if (_name.size() == 0) { return false; } - if (m_value.exist(_name)) { - ejson::Value* tmp = m_value[_name]; - delete(tmp); - m_value[_name] = _value; + auto it = m_value.find(_name); + if (it != m_value.end()) { + delete(it->second); + it->second = _value; return true; } - m_value.add(_name, _value); + m_value.insert(std::pair(_name, _value)); return true; } @@ -470,13 +498,11 @@ ejson::Value* ejson::Object::duplicate(void) const { JSON_ERROR("Allocation error ..."); return NULL; } - for (int32_t iii=0; iiisecond == NULL) { continue; } - output->add(key, val->duplicate()); + output->add(it->first, it->second->duplicate()); } return output; } diff --git a/ejson/Object.h b/ejson/Object.h old mode 100644 new mode 100755 index 8a8a17f..e593f37 --- a/ejson/Object.h +++ b/ejson/Object.h @@ -10,9 +10,8 @@ #define __ETK_JSON_OBJECT_H__ #include -#include -#include -#include +#include +#include #include namespace ejson { @@ -27,7 +26,7 @@ namespace ejson { */ virtual ~Object(void) { }; protected: - etk::Hash m_value; //!< value of the node (for element this is the name, for text it is the inside text ...) + std::map 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.... /** @@ -52,12 +51,17 @@ namespace ejson { const ejson::Value* operator[] (const std::string& _name) const { return get(_name); } + public: /** * @brief Get all the element name (keys). * @return a vector of all name (key). */ std::vector getKeys(void) const { - return m_value.getKeys(); + std::vector keys; + for (auto &it : m_value) { + keys.push_back(it.first); + } + return keys; } /** * @brief get the number of sub element in the current one @@ -72,19 +76,31 @@ namespace ejson { * @return NULL if the element does not exist. */ ejson::Value* get(size_t _id) { - return m_value[_id]; + size_t id = 0; + for(auto &it : m_value) { + if (id == _id) { + return it.second; + } + } + return NULL; }; //! @previous const ejson::Value* get(size_t _id) const{ - return m_value[_id]; + size_t id = 0; + for(auto &it : m_value) { + if (id == _id) { + return it.second; + } + } + return NULL; }; //! @previous ejson::Value* operator[] (size_t _id) { - return m_value[_id]; + return get(_id); } //! @previous const ejson::Value* operator[] (size_t _id) const { - return m_value[_id]; + return get(_id); } /** * @brief Get the element name (key). @@ -92,7 +108,13 @@ namespace ejson { * @return The name (key). */ std::string getKey(size_t _id) const { - return m_value.getKey(_id); + size_t id = 0; + for(auto it = m_value.begin(); it != m_value.end(); ++it, ++id) { + if (id == _id) { + return it->first; + } + } + return NULL; } /** * @brief get the sub element with his name (Casted as Object if it is possible) @@ -207,16 +229,6 @@ namespace ejson { public: // herited function : virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc); virtual bool iGenerate(std::string& _data, size_t _indent) const; - virtual enum nodeType getType(void) const { - return typeObject; - }; - virtual ejson::Object* toObject(void) { - return this; - }; - //! @previous - virtual const ejson::Object* toObject(void) const{ - return this; - }; virtual void clear(void); virtual bool transfertIn(ejson::Value* _obj); virtual ejson::Value* duplicate(void) const; diff --git a/ejson/String.h b/ejson/String.h old mode 100644 new mode 100755 index 0e7e10d..5bc06ad --- a/ejson/String.h +++ b/ejson/String.h @@ -10,8 +10,6 @@ #define __ETK_JSON_STRING_H__ #include -#include -#include #include namespace ejson { @@ -48,16 +46,6 @@ namespace ejson { public: // herited function : virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc); virtual bool iGenerate(std::string& _data, size_t _indent) const; - virtual enum nodeType getType(void) const { - return typeString; - }; - virtual ejson::String* toString(void) { - return this; - }; - //! @previous - virtual const ejson::String* toString(void) const { - return this; - }; virtual bool transfertIn(ejson::Value* _obj); virtual ejson::Value* duplicate(void) const; }; diff --git a/ejson/Value.cpp b/ejson/Value.cpp index c2788db..e51447a 100644 --- a/ejson/Value.cpp +++ b/ejson/Value.cpp @@ -123,3 +123,46 @@ bool ejson::Value::checkNumber(char32_t _val) const { return false; } + +ejson::Document* ejson::Value::toDocument(void) { + return dynamic_cast(this); +}; +const ejson::Document* ejson::Value::toDocument(void) const { + return dynamic_cast(this); +}; +ejson::Array* ejson::Value::toArray(void) { + return dynamic_cast(this); +}; +const ejson::Array* ejson::Value::toArray(void) const{ + return dynamic_cast(this); +}; +ejson::Object* ejson::Value::toObject(void) { + return dynamic_cast(this); +}; +const ejson::Object* ejson::Value::toObject(void) const{ + return dynamic_cast(this); +}; +ejson::String* ejson::Value::toString(void) { + return dynamic_cast(this); +}; +const ejson::String* ejson::Value::toString(void) const{ + return dynamic_cast(this); +}; +ejson::Number* ejson::Value::toNumber(void) { + return dynamic_cast(this); +}; +const ejson::Number* ejson::Value::toNumber(void) const{ + return dynamic_cast(this); +}; +ejson::Boolean* ejson::Value::toBoolean(void) { + return dynamic_cast(this); +}; +const ejson::Boolean* ejson::Value::toBoolean(void) const{ + return dynamic_cast(this); +}; +ejson::Null* ejson::Value::toNull(void) { + return dynamic_cast(this); +}; +const ejson::Null* ejson::Value::toNull(void) const{ + return dynamic_cast(this); +}; \ No newline at end of file diff --git a/ejson/Value.h b/ejson/Value.h old mode 100644 new mode 100755 index 4724c55..2f2507d --- a/ejson/Value.h +++ b/ejson/Value.h @@ -10,8 +10,6 @@ #define __ETK_JSON_VALUE_H__ #include -#include -#include namespace ejson { //#define ENABLE_DISPLAY_PARSED_ELEMENT @@ -32,18 +30,6 @@ namespace ejson { class Null; class Number; class String; - - enum nodeType{ - typeUnknow, //!< might be an error ... - typeValue, //!< XXXXXX:* - typeDocument, //!< all the file main access - typeArray, //!< [...] - typeString, //!< the "" - typeNumber, //!< the -1565.21515 - typeBoolean, //!< the true and false - typeNull, //!< the null element - typeObject, //!< the { ... } - }; //! @not-in-doc class filePos { private: @@ -145,14 +131,6 @@ namespace ejson { * @return false if an error occured. */ virtual bool iGenerate(std::string& _data, size_t _indent) const = 0; - public: - /** - * @brief get the node type. - * @return the type of the Node. - */ - virtual enum nodeType getType(void) const { - return typeValue; - }; protected: /** * @brief add indentation of the string input. @@ -189,139 +167,111 @@ namespace ejson { * @brief Cast the element in a Value if it is possible. * @return pointer on the class or NULL. */ - virtual ejson::Value* toValue(void) { + ejson::Value* toValue(void) { return this; }; //! @previous - virtual const ejson::Value* toValue(void) const { + const ejson::Value* toValue(void) const { return this; }; /** * @brief Cast the element in a Document if it is possible. * @return pointer on the class or NULL. */ - virtual ejson::Document* toDocument(void) { - return NULL; - }; + ejson::Document* toDocument(void); //! @previous - virtual const ejson::Document* toDocument(void) const { - return NULL; - }; + const ejson::Document* toDocument(void) const; /** * @brief Cast the element in a Array if it is possible. * @return pointer on the class or NULL. */ - virtual ejson::Array* toArray(void) { - return NULL; - }; + ejson::Array* toArray(void); //! @previous - virtual const ejson::Array* toArray(void) const{ - return NULL; - }; + const ejson::Array* toArray(void) const; /** * @brief Cast the element in a Object if it is possible. * @return pointer on the class or NULL. */ - virtual ejson::Object* toObject(void) { - return NULL; - }; + ejson::Object* toObject(void); //! @previous - virtual const ejson::Object* toObject(void) const{ - return NULL; - }; + const ejson::Object* toObject(void) const; /** * @brief Cast the element in a String if it is possible. * @return pointer on the class or NULL. */ - virtual ejson::String* toString(void) { - return NULL; - }; + ejson::String* toString(void); //! @previous - virtual const ejson::String* toString(void) const{ - return NULL; - }; + const ejson::String* toString(void) const; /** * @brief Cast the element in a Number if it is possible. * @return pointer on the class or NULL. */ - virtual ejson::Number* toNumber(void) { - return NULL; - }; + ejson::Number* toNumber(void); //! @previous - virtual const ejson::Number* toNumber(void) const{ - return NULL; - }; + const ejson::Number* toNumber(void) const; /** * @brief Cast the element in a Boolean if it is possible. * @return pointer on the class or NULL. */ - virtual ejson::Boolean* toBoolean(void) { - return NULL; - }; + ejson::Boolean* toBoolean(void); //! @previous - virtual const ejson::Boolean* toBoolean(void) const{ - return NULL; - }; + const ejson::Boolean* toBoolean(void) const; /** * @brief Cast the element in a Null if it is possible. * @return pointer on the class or NULL. */ - virtual ejson::Null* toNull(void) { - return NULL; - }; + ejson::Null* toNull(void); //! @previous - virtual const ejson::Null* toNull(void) const{ - return NULL; - }; + const ejson::Null* toNull(void) const; /** * @brief check if the node is a ejson::Document * @return true if the node is a ejson::Document */ bool isDocument(void) const { - return getType() == ejson::typeDocument; + return toDocument() != NULL; }; /** * @brief check if the node is a ejson::Array * @return true if the node is a ejson::Array */ bool isArray(void) const { - return getType() == ejson::typeArray; + return toArray() != NULL; }; /** * @brief check if the node is a ejson::Object * @return true if the node is a ejson::Object */ bool isObject(void) const { - return getType() == ejson::typeObject; + return toObject() != NULL; }; /** * @brief check if the node is a ejson::String * @return true if the node is a ejson::String */ bool isString(void) const { - return getType() == ejson::typeString; + return toString() != NULL; }; /** * @brief check if the node is a ejson::Number * @return true if the node is a ejson::Number */ bool isNumber(void) const { - return getType() == ejson::typeNumber; + return toNumber() != NULL; }; /** * @brief check if the node is a ejson::Boolean * @return true if the node is a ejson::Boolean */ bool isBoolean(void) const { - return getType() == ejson::typeBoolean; + return toBoolean() != NULL; }; /** * @brief check if the node is a ejson::Null * @return true if the node is a ejson::Null */ bool isNull(void) const { - return getType() == ejson::typeNull; + return toNull() != NULL; }; /** @@ -353,5 +303,12 @@ namespace ejson { }; }; +#include +#include +#include +#include +#include +#include + #endif diff --git a/ejson/ejson.h b/ejson/ejson.h old mode 100644 new mode 100755 index 3fdbc0a..12dc197 --- a/ejson/ejson.h +++ b/ejson/ejson.h @@ -76,17 +76,8 @@ namespace ejson { void createError(const std::string& _data, size_t _pos, const ejson::filePos& _filePos, const std::string& _comment); void displayError(void); public: // herited function: - virtual enum nodeType getType(void) const { - return typeDocument; - }; virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc); virtual bool iGenerate(std::string& _data, size_t _indent) const; - virtual ejson::Document* toDocument(void) { - return this; - }; - virtual const ejson::Document* toDocument(void) const { - return this; - }; }; };