diff --git a/ejson/Array.cpp b/ejson/Array.cpp index 3e97441..a92430f 100644 --- a/ejson/Array.cpp +++ b/ejson/Array.cpp @@ -20,14 +20,11 @@ #define __class__ "Array" +std::shared_ptr ejson::Array::create() { + return std::shared_ptr(new ejson::Array()); +} + void ejson::Array::clear() { - for (size_t iii=0; iii tmpElement = ejson::Object::create(); + if (tmpElement == nullptr) { EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in object"); _pos=iii; return false; @@ -71,8 +68,8 @@ bool ejson::Array::iParse(const std::string& _data, size_t& _pos, ejson::filePos || _data[iii] == '\'') { // find a string: JSON_PARSE_ELEMENT("find String quoted"); - ejson::String * tmpElement = new ejson::String(); - if (NULL == tmpElement) { + std::shared_ptr tmpElement = ejson::String::create(); + if (tmpElement == nullptr) { EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in String"); _pos=iii; return false; @@ -82,8 +79,8 @@ bool ejson::Array::iParse(const std::string& _data, size_t& _pos, ejson::filePos } else if (_data[iii] == '[') { // find a list: JSON_PARSE_ELEMENT("find List"); - ejson::Array * tmpElement = new ejson::Array(); - if (NULL == tmpElement) { + std::shared_ptr tmpElement = ejson::Array::create(); + if (tmpElement == nullptr) { EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Array"); _pos=iii; return false; @@ -94,8 +91,8 @@ bool ejson::Array::iParse(const std::string& _data, size_t& _pos, ejson::filePos || _data[iii] == 't' ) { // find boolean: JSON_PARSE_ELEMENT("find Boolean"); - ejson::Boolean * tmpElement = new ejson::Boolean(); - if (NULL == tmpElement) { + std::shared_ptr tmpElement = ejson::Boolean::create(); + if (tmpElement == nullptr) { EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Boolean"); _pos=iii; return false; @@ -105,8 +102,8 @@ bool ejson::Array::iParse(const std::string& _data, size_t& _pos, ejson::filePos } else if( _data[iii] == 'n') { // find null: JSON_PARSE_ELEMENT("find Null"); - ejson::Null * tmpElement = new ejson::Null(); - if (NULL == tmpElement) { + std::shared_ptr tmpElement = ejson::Null::create(); + if (tmpElement == nullptr) { EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Boolean"); _pos=iii; return false; @@ -116,8 +113,8 @@ bool ejson::Array::iParse(const std::string& _data, size_t& _pos, ejson::filePos } else if(true == checkNumber(_data[iii])) { // find number: JSON_PARSE_ELEMENT("find Number"); - ejson::Number * tmpElement = new ejson::Number(); - if (NULL == tmpElement) { + std::shared_ptr tmpElement = ejson::Number::create(); + if (tmpElement == nullptr) { EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Boolean"); _pos=iii; return false; @@ -146,8 +143,8 @@ bool ejson::Array::iGenerate(std::string& _data, size_t _indent) const { oneLine=false; } else { for (size_t iii=0; iii tmp = m_value[iii]; + if (tmp == nullptr) { continue; } if (true == tmp->isObject()) { @@ -159,8 +156,8 @@ bool ejson::Array::iGenerate(std::string& _data, size_t _indent) const { break; } if (true == tmp->isString()) { - ejson::String* tmp2 = tmp->toString(); - if (NULL!=tmp2) { + std::shared_ptr tmp2 = tmp->toString(); + if (tmp2 != nullptr) { if(tmp2->get().size()>40) { oneLine=false; break; @@ -178,7 +175,7 @@ bool ejson::Array::iGenerate(std::string& _data, size_t _indent) const { if (false == oneLine) { addIndent(_data, _indent); } - if (NULL != m_value[iii]) { + if (m_value[iii] != nullptr) { m_value[iii]->iGenerate(_data, _indent+1); if (iii _element) { + if (_element == nullptr) { + JSON_ERROR("Request add on an nullptr pointer"); return false; } m_value.push_back(_element); @@ -207,29 +204,29 @@ bool ejson::Array::add(ejson::Value* _element) { } bool ejson::Array::addString(const std::string& _value) { - return add(new ejson::String(_value)); + return add(ejson::String::create(_value)); } bool ejson::Array::addNull() { - return add(new ejson::Null()); + return add(ejson::Null::create()); } bool ejson::Array::addBoolean(bool _value) { - return add(new ejson::Boolean(_value)); + return add(ejson::Boolean::create(_value)); } bool ejson::Array::addNumber(double _value) { - return add(new ejson::Number(_value)); + return add(ejson::Number::create(_value)); } -bool ejson::Array::transfertIn(ejson::Value* _obj) { - if (NULL == _obj) { - JSON_ERROR("Request transfer on an NULL pointer"); +bool ejson::Array::transfertIn(std::shared_ptr _obj) { + if (_obj == nullptr) { + JSON_ERROR("Request transfer on an nullptr pointer"); return false; } - ejson::Array* other = _obj->toArray(); - if (NULL == other) { + std::shared_ptr other = _obj->toArray(); + if (other == nullptr) { JSON_ERROR("Request transfer on an element that is not an array"); return false; } @@ -243,15 +240,15 @@ bool ejson::Array::transfertIn(ejson::Value* _obj) { } // TODO : Manage error ... -ejson::Value* ejson::Array::duplicate() const { - ejson::Array* output = new ejson::Array(); - if (NULL == output) { +std::shared_ptr ejson::Array::duplicate() const { + std::shared_ptr output = ejson::Array::create(); + if (output == nullptr) { JSON_ERROR("Allocation error ..."); - return NULL; + return nullptr; } for (size_t iii=0; iii val = m_value[iii]; + if (val == nullptr) { continue; } output->add(val->duplicate()); @@ -259,104 +256,104 @@ ejson::Value* ejson::Array::duplicate() const { return output; } -ejson::Object* ejson::Array::getObject(size_t _id) { - ejson::Value* tmpElement = m_value[_id]; - if (NULL == tmpElement) { - return NULL; +std::shared_ptr ejson::Array::getObject(size_t _id) { + std::shared_ptr tmpElement = m_value[_id]; + if (tmpElement == nullptr) { + return nullptr; } return tmpElement->toObject(); } -const ejson::Object* ejson::Array::getObject(size_t _id) const { - const ejson::Value* tmpElement = m_value[_id]; - if (NULL == tmpElement) { - return NULL; +const std::shared_ptr ejson::Array::getObject(size_t _id) const { + const std::shared_ptr tmpElement = m_value[_id]; + if (tmpElement == nullptr) { + return nullptr; } return tmpElement->toObject(); } -ejson::String* ejson::Array::getString(size_t _id) { - ejson::Value* tmpElement = m_value[_id]; - if (NULL == tmpElement) { - return NULL; +std::shared_ptr ejson::Array::getString(size_t _id) { + std::shared_ptr tmpElement = m_value[_id]; + if (tmpElement == nullptr) { + return nullptr; } return tmpElement->toString(); } -const ejson::String* ejson::Array::getString(size_t _id) const { - const ejson::Value* tmpElement = m_value[_id]; - if (NULL == tmpElement) { - return NULL; +const std::shared_ptr ejson::Array::getString(size_t _id) const { + const std::shared_ptr tmpElement = m_value[_id]; + if (tmpElement == nullptr) { + return nullptr; } return tmpElement->toString(); } -ejson::Array* ejson::Array::getArray(size_t _id) { - ejson::Value* tmpElement = m_value[_id]; - if (NULL == tmpElement) { - return NULL; +std::shared_ptr ejson::Array::getArray(size_t _id) { + std::shared_ptr tmpElement = m_value[_id]; + if (tmpElement == nullptr) { + return nullptr; } return tmpElement->toArray(); } -const ejson::Array* ejson::Array::getArray(size_t _id) const { - const ejson::Value* tmpElement = m_value[_id]; - if (NULL == tmpElement) { - return NULL; +const std::shared_ptr ejson::Array::getArray(size_t _id) const { + const std::shared_ptr tmpElement = m_value[_id]; + if (tmpElement == nullptr) { + return nullptr; } return tmpElement->toArray(); } -ejson::Null* ejson::Array::getNull(size_t _id) { - ejson::Value* tmpElement = m_value[_id]; - if (NULL == tmpElement) { - return NULL; +std::shared_ptr ejson::Array::getNull(size_t _id) { + std::shared_ptr tmpElement = m_value[_id]; + if (tmpElement == nullptr) { + return nullptr; } return tmpElement->toNull(); } -const ejson::Null* ejson::Array::getNull(size_t _id) const { - const ejson::Value* tmpElement = m_value[_id]; - if (NULL == tmpElement) { - return NULL; +const std::shared_ptr ejson::Array::getNull(size_t _id) const { + const std::shared_ptr tmpElement = m_value[_id]; + if (tmpElement == nullptr) { + return nullptr; } return tmpElement->toNull(); } -ejson::Number* ejson::Array::getNumber(size_t _id) { - ejson::Value* tmpElement = m_value[_id]; - if (NULL == tmpElement) { - return NULL; +std::shared_ptr ejson::Array::getNumber(size_t _id) { + std::shared_ptr tmpElement = m_value[_id]; + if (tmpElement == nullptr) { + return nullptr; } return tmpElement->toNumber(); } -const ejson::Number* ejson::Array::getNumber(size_t _id) const { - const ejson::Value* tmpElement = m_value[_id]; - if (NULL == tmpElement) { - return NULL; +const std::shared_ptr ejson::Array::getNumber(size_t _id) const { + const std::shared_ptr tmpElement = m_value[_id]; + if (tmpElement == nullptr) { + return nullptr; } return tmpElement->toNumber(); } -ejson::Boolean* ejson::Array::getBoolean(size_t _id) { - ejson::Value* tmpElement = m_value[_id]; - if (NULL == tmpElement) { - return NULL; +std::shared_ptr ejson::Array::getBoolean(size_t _id) { + std::shared_ptr tmpElement = m_value[_id]; + if (tmpElement == nullptr) { + return nullptr; } return tmpElement->toBoolean(); } -const ejson::Boolean* ejson::Array::getBoolean(size_t _id) const { - const ejson::Value* tmpElement = m_value[_id]; - if (NULL == tmpElement) { - return NULL; +const std::shared_ptr ejson::Array::getBoolean(size_t _id) const { + const std::shared_ptr tmpElement = m_value[_id]; + if (tmpElement == nullptr) { + return nullptr; } return tmpElement->toBoolean(); } std::string ejson::Array::getStringValue(size_t _id) { - ejson::String* tmpElement = getString(_id); - if (NULL == tmpElement) { + std::shared_ptr tmpElement = getString(_id); + if (tmpElement == nullptr) { return ""; } return tmpElement->get(); @@ -364,32 +361,32 @@ std::string ejson::Array::getStringValue(size_t _id) { const std::string& ejson::Array::getStringValue(size_t _id) const { static const std::string errorValue(""); - const ejson::String* tmpElement = getString(_id); - if (NULL == tmpElement) { + const std::shared_ptr tmpElement = getString(_id); + if (tmpElement == nullptr) { return errorValue; } return tmpElement->get(); } std::string ejson::Array::getStringValue(size_t _id, const std::string& _errorValue) const { - const ejson::String* tmpElement = getString(_id); - if (NULL == tmpElement) { + const std::shared_ptr tmpElement = getString(_id); + if (tmpElement == nullptr) { return _errorValue; } return tmpElement->get(); } double ejson::Array::getNumberValue(size_t _id, double _errorValue) const { - const ejson::Number* tmpElement = getNumber(_id); - if (NULL == tmpElement) { + const std::shared_ptr tmpElement = getNumber(_id); + if (tmpElement == nullptr) { return _errorValue; } return tmpElement->get(); } bool ejson::Array::getBooleanValue(size_t _id, bool _errorValue) const { - const ejson::Boolean* tmpElement = getBoolean(_id); - if (NULL == tmpElement) { + const std::shared_ptr tmpElement = getBoolean(_id); + if (tmpElement == nullptr) { return _errorValue; } return tmpElement->get(); diff --git a/ejson/Array.h b/ejson/Array.h index 063f5a3..1c92290 100644 --- a/ejson/Array.h +++ b/ejson/Array.h @@ -14,17 +14,19 @@ namespace ejson { class Array : public ejson::Value { - public: + protected: /** * @brief basic element of a xml structure */ Array() { }; + public: + static std::shared_ptr create(); /** * @brief destructor */ virtual ~Array() { }; private: - std::vector m_value; //!< vector of sub elements + std::vector> m_value; //!< vector of sub elements public: /** * @brief get the number of sub element in the current one @@ -36,39 +38,39 @@ namespace ejson { /** * @brief get the pointer on an element reference with his ID. * @param[in] _id Id of the element. - * @return NULL if the element does not exist. + * @return nullptr if the element does not exist. */ - ejson::Value* get(size_t _id) { + std::shared_ptr get(size_t _id) { return m_value[_id]; }; //! @previous - const ejson::Value* get(size_t _id) const{ + const std::shared_ptr get(size_t _id) const{ return m_value[_id]; }; //! @previous - ejson::Value* operator[] (size_t _id) { + std::shared_ptr operator[] (size_t _id) { return m_value[_id]; } //! @previous - const ejson::Value* operator[] (size_t _id) const { + const std::shared_ptr operator[] (size_t _id) const { return m_value[_id]; } /** * @brief get the pointer on an element reference with his ID (casted in Object if it is an object). * @param[in] _id Id of the element. - * @return NULL if the element does not exist. + * @return nullptr if the element does not exist. */ - ejson::Object* getObject(size_t _id); + std::shared_ptr getObject(size_t _id); //! @previous - const ejson::Object* getObject(size_t _id) const; + const std::shared_ptr getObject(size_t _id) const; /** * @brief get the pointer on an element reference with his ID (casted in String if it is an String). * @param[in] _id Id of the element. - * @return NULL if the element does not exist. + * @return nullptr if the element does not exist. */ - ejson::String* getString(size_t _id); + std::shared_ptr getString(size_t _id); //! @previous - const ejson::String* getString(size_t _id) const; + const std::shared_ptr getString(size_t _id) const; /** * @brief get the value of the string element (if not a string return "") * @param[in] _id Id of the element. @@ -87,27 +89,27 @@ namespace ejson { /** * @brief get the pointer on an element reference with his ID (casted in Array if it is an Array). * @param[in] _id Id of the element. - * @return NULL if the element does not exist. + * @return nullptr if the element does not exist. */ - ejson::Array* getArray(size_t _id); + std::shared_ptr getArray(size_t _id); //! @previous - const ejson::Array* getArray(size_t _id) const; + const std::shared_ptr getArray(size_t _id) const; /** * @brief get the pointer on an element reference with his ID (casted in Null if it is an Null). * @param[in] _id Id of the element. - * @return NULL if the element does not exist. + * @return nullptr if the element does not exist. */ - ejson::Null* getNull(size_t _id); + std::shared_ptr getNull(size_t _id); //! @previous - const ejson::Null* getNull(size_t _id) const; + const std::shared_ptr getNull(size_t _id) const; /** * @brief get the pointer on an element reference with his ID (casted in Number if it is an Number). * @param[in] _id Id of the element. - * @return NULL if the element does not exist. + * @return nullptr if the element does not exist. */ - ejson::Number* getNumber(size_t _id); + std::shared_ptr getNumber(size_t _id); //! @previous - const ejson::Number* getNumber(size_t _id) const; + const std::shared_ptr getNumber(size_t _id) const; /** * @brief get the value of the Number element * @param[in] _id Id of the element. @@ -118,11 +120,11 @@ namespace ejson { /** * @brief get the pointer on an element reference with his ID (casted in Boolean if it is an Boolean). * @param[in] _id Id of the element. - * @return NULL if the element does not exist. + * @return nullptr if the element does not exist. */ - ejson::Boolean* getBoolean(size_t _id); + std::shared_ptr getBoolean(size_t _id); //! @previous - const ejson::Boolean* getBoolean(size_t _id) const; + const std::shared_ptr getBoolean(size_t _id) const; /** * @brief get the value of the Boolean element * @param[in] _id Id of the element. @@ -135,7 +137,7 @@ namespace ejson { * @param[in] _element element to add. * @return false if an error occured. */ - bool add(ejson::Value* _element); + bool add(std::shared_ptr _element); /** * @brief add a string element in the Object (automatic creation) * @param[in] _value string value to add @@ -164,8 +166,8 @@ namespace ejson { 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 void clear(); - virtual bool transfertIn(ejson::Value* _obj); - virtual ejson::Value* duplicate() const; + virtual bool transfertIn(std::shared_ptr _obj); + virtual std::shared_ptr duplicate() const; }; }; diff --git a/ejson/Boolean.cpp b/ejson/Boolean.cpp index 9dc91dc..7e57d9f 100644 --- a/ejson/Boolean.cpp +++ b/ejson/Boolean.cpp @@ -13,6 +13,11 @@ #undef __class__ #define __class__ "Boolean" +std::shared_ptr ejson::Boolean::create(bool _value) { + return std::shared_ptr(new ejson::Boolean(_value)); +} + + bool ejson::Boolean::iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc) { JSON_PARSE_ELEMENT("start parse : 'Boolean' "); m_value=false; @@ -52,13 +57,13 @@ bool ejson::Boolean::iGenerate(std::string& _data, size_t _indent) const { } -bool ejson::Boolean::transfertIn(ejson::Value* _obj) { - if (NULL == _obj) { +bool ejson::Boolean::transfertIn(std::shared_ptr _obj) { + if (_obj == nullptr) { JSON_ERROR("Request transfer on an NULL pointer"); return false; } - ejson::Boolean* other = _obj->toBoolean(); - if (NULL == other) { + std::shared_ptr other = _obj->toBoolean(); + if (other == nullptr) { JSON_ERROR("Request transfer on an element that is not an Boolean"); return false; } @@ -68,11 +73,11 @@ bool ejson::Boolean::transfertIn(ejson::Value* _obj) { return true; } -ejson::Value* ejson::Boolean::duplicate() const { - ejson::Boolean* output = new ejson::Boolean(m_value); - if (NULL == output) { +std::shared_ptr ejson::Boolean::duplicate() const { + std::shared_ptr output = ejson::Boolean::create(m_value); + if (output == nullptr) { JSON_ERROR("Allocation error ..."); - return NULL; + return nullptr; } return output; } diff --git a/ejson/Boolean.h b/ejson/Boolean.h index 09bb2f7..1622689 100644 --- a/ejson/Boolean.h +++ b/ejson/Boolean.h @@ -14,7 +14,7 @@ namespace ejson { class Boolean : public ejson::Value { - public: + protected: /** * @brief basic element of a xml structure */ @@ -22,6 +22,8 @@ namespace ejson { m_value(_value) { }; + public: + static std::shared_ptr create(bool _value=false); /** * @brief destructor */ @@ -48,8 +50,8 @@ 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 bool transfertIn(ejson::Value* _obj); - virtual ejson::Value* duplicate() const; + virtual bool transfertIn(std::shared_ptr _obj); + virtual std::shared_ptr duplicate() const; }; }; diff --git a/ejson/Null.cpp b/ejson/Null.cpp index 97bf45e..0c73597 100644 --- a/ejson/Null.cpp +++ b/ejson/Null.cpp @@ -14,6 +14,11 @@ #undef __class__ #define __class__ "Null" +std::shared_ptr ejson::Null::create() { + return std::shared_ptr(new ejson::Null()); +} + + bool ejson::Null::iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc) { JSON_PARSE_ELEMENT("start parse : 'Null' "); if (_pos+3 >= _data.size()){ @@ -39,24 +44,24 @@ bool ejson::Null::iGenerate(std::string& _data, size_t _indent) const { } -bool ejson::Null::transfertIn(ejson::Value* _obj) { - if (NULL == _obj) { - JSON_ERROR("Request transfer on an NULL pointer"); +bool ejson::Null::transfertIn(std::shared_ptr _obj) { + if (_obj == nullptr) { + JSON_ERROR("Request transfer on an nullptr pointer"); return false; } - ejson::Null* other = _obj->toNull(); - if (NULL == other) { + std::shared_ptr other = _obj->toNull(); + if (other == nullptr) { JSON_ERROR("Request transfer on an element that is not an Null"); return false; } return true; } -ejson::Value* ejson::Null::duplicate() const { - ejson::Null* output = new ejson::Null(); - if (NULL == output) { +std::shared_ptr ejson::Null::duplicate() const { + std::shared_ptr output = ejson::Null::create(); + if (output == nullptr) { JSON_ERROR("Allocation error ..."); - return NULL; + return nullptr; } return output; } diff --git a/ejson/Null.h b/ejson/Null.h index 926b4cb..569ee26 100644 --- a/ejson/Null.h +++ b/ejson/Null.h @@ -14,11 +14,13 @@ namespace ejson { class Null : public ejson::Value { - public: + protected: /** * @brief basic element of a xml structure */ Null() { }; + public: + static std::shared_ptr create(); /** * @brief destructor */ @@ -26,8 +28,8 @@ 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 bool transfertIn(ejson::Value* _obj); - virtual ejson::Value* duplicate() const; + virtual bool transfertIn(std::shared_ptr _obj); + virtual std::shared_ptr duplicate() const; }; }; diff --git a/ejson/Number.cpp b/ejson/Number.cpp index 42ee8a6..36a2128 100644 --- a/ejson/Number.cpp +++ b/ejson/Number.cpp @@ -14,6 +14,10 @@ #undef __class__ #define __class__ "Number" +std::shared_ptr ejson::Number::create(double _value) { + return std::shared_ptr(new ejson::Number(_value)); +} + bool ejson::Number::iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc) { JSON_PARSE_ELEMENT("start parse : 'Number' "); std::string tmpVal; @@ -49,13 +53,13 @@ bool ejson::Number::iGenerate(std::string& _data, size_t _indent) const { } -bool ejson::Number::transfertIn(ejson::Value* _obj) { - if (NULL == _obj) { - JSON_ERROR("Request transfer on an NULL pointer"); +bool ejson::Number::transfertIn(std::shared_ptr _obj) { + if (_obj == nullptr) { + JSON_ERROR("Request transfer on an nullptr pointer"); return false; } - ejson::Number* other = _obj->toNumber(); - if (NULL == other) { + std::shared_ptr other = _obj->toNumber(); + if (other == nullptr) { JSON_ERROR("Request transfer on an element that is not an Number"); return false; } @@ -65,11 +69,11 @@ bool ejson::Number::transfertIn(ejson::Value* _obj) { return true; } -ejson::Value* ejson::Number::duplicate() const { - ejson::Number* output = new ejson::Number(m_value); - if (NULL == output) { +std::shared_ptr ejson::Number::duplicate() const { + std::shared_ptr output = ejson::Number::create(m_value); + if (output == nullptr) { JSON_ERROR("Allocation error ..."); - return NULL; + return nullptr; } return output; } diff --git a/ejson/Number.h b/ejson/Number.h index a098a1d..613f44f 100644 --- a/ejson/Number.h +++ b/ejson/Number.h @@ -22,6 +22,8 @@ namespace ejson { m_value(_value) { }; + public: + static std::shared_ptr create(double _value=0.0); /** * @brief destructor */ @@ -60,8 +62,8 @@ 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 bool transfertIn(ejson::Value* _obj); - virtual ejson::Value* duplicate() const; + virtual bool transfertIn(std::shared_ptr _obj); + virtual std::shared_ptr duplicate() const; }; }; diff --git a/ejson/Object.cpp b/ejson/Object.cpp index 0c4c3f7..aebada9 100644 --- a/ejson/Object.cpp +++ b/ejson/Object.cpp @@ -19,14 +19,13 @@ #undef __class__ #define __class__ "Object" + + +std::shared_ptr ejson::Object::create() { + return std::shared_ptr(new ejson::Object()); +} + void ejson::Object::clear() { - for (int32_t iii=0; iii tmpElement = ejson::Object::create(); + if (tmpElement == nullptr) { EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in object"); _pos=iii; return false; @@ -135,8 +134,8 @@ bool ejson::Object::iParse(const std::string& _data, size_t& _pos, ejson::filePo || _data[iii] == '\'') { // find a string: JSON_PARSE_ELEMENT("find String quoted"); - ejson::String * tmpElement = new ejson::String(); - if (NULL == tmpElement) { + std::shared_ptr tmpElement = ejson::String::create(); + if (tmpElement == nullptr) { EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in String"); _pos=iii; return false; @@ -147,8 +146,8 @@ bool ejson::Object::iParse(const std::string& _data, size_t& _pos, ejson::filePo } else if (_data[iii] == '[') { // find a list: JSON_PARSE_ELEMENT("find List"); - ejson::Array * tmpElement = new ejson::Array(); - if (NULL == tmpElement) { + std::shared_ptr tmpElement = ejson::Array::create(); + if (tmpElement == nullptr) { EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Array"); _pos=iii; return false; @@ -160,8 +159,8 @@ bool ejson::Object::iParse(const std::string& _data, size_t& _pos, ejson::filePo || _data[iii] == 't' ) { // find boolean: JSON_PARSE_ELEMENT("find Boolean"); - ejson::Boolean * tmpElement = new ejson::Boolean(); - if (NULL == tmpElement) { + std::shared_ptr tmpElement = ejson::Boolean::create(); + if (tmpElement == nullptr) { EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Boolean"); _pos=iii; return false; @@ -172,8 +171,8 @@ bool ejson::Object::iParse(const std::string& _data, size_t& _pos, ejson::filePo } else if( _data[iii] == 'n') { // find null: JSON_PARSE_ELEMENT("find Null"); - ejson::Null * tmpElement = new ejson::Null(); - if (NULL == tmpElement) { + std::shared_ptr tmpElement = ejson::Null::create(); + if (tmpElement == nullptr) { EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Boolean"); _pos=iii; return false; @@ -184,8 +183,8 @@ bool ejson::Object::iParse(const std::string& _data, size_t& _pos, ejson::filePo } else if(true == checkNumber(_data[iii])) { // find number: JSON_PARSE_ELEMENT("find Number"); - ejson::Number * tmpElement = new ejson::Number(); - if (NULL == tmpElement) { + std::shared_ptr tmpElement = ejson::Number::create(); + if (tmpElement == nullptr) { EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Boolean"); _pos=iii; return false; @@ -221,20 +220,20 @@ bool ejson::Object::iGenerate(std::string& _data, size_t _indent) const { oneLine=false; } else { for (int32_t iii=0; iii tmp = m_value[iii]; + if (tmp == nullptr) { continue; } - if (true == tmp->isObject()) { + if (tmp->isObject() == true) { oneLine=false; break; } - if (true == tmp->isArray()) { + if (tmp->isArray() == true) { oneLine=false; break; } - if (true == tmp->isString()) { - ejson::String* tmp2 = tmp->toString(); + if (tmp->isString() == true) { + std::shared_ptr tmp2 = tmp->toString(); if (tmp2 != nullptr) { if( tmp2->get().size()>25 || m_value.getKey(iii).size()>25) { @@ -278,160 +277,158 @@ bool ejson::Object::exist(const std::string& _name) const { return m_value.exist(_name); } -ejson::Value* ejson::Object::get(const std::string& _name) { +std::shared_ptr ejson::Object::get(const std::string& _name) { if (false == m_value.exist(_name)) { - return NULL; + return nullptr; } return m_value[_name]; } -const ejson::Value* ejson::Object::get(const std::string& _name) const { +const std::shared_ptr ejson::Object::get(const std::string& _name) const { if (false == m_value.exist(_name)) { - return NULL; + return nullptr; } return m_value[_name]; } -ejson::Object* ejson::Object::getObject(const std::string& _name) { - ejson::Value* tmp = get(_name); - if (NULL == tmp) { - return NULL; +std::shared_ptr ejson::Object::getObject(const std::string& _name) { + std::shared_ptr tmp = get(_name); + if (tmp == nullptr) { + return nullptr; } - return dynamic_cast(tmp); + return std::dynamic_pointer_cast(tmp); } -const ejson::Object* ejson::Object::getObject(const std::string& _name) const { - const ejson::Value* tmp = get(_name); - if (NULL == tmp) { - return NULL; +const std::shared_ptr ejson::Object::getObject(const std::string& _name) const { + const std::shared_ptr tmp = get(_name); + if (tmp == nullptr) { + return nullptr; } - return dynamic_cast(tmp); + return std::dynamic_pointer_cast(tmp); } -ejson::Array* ejson::Object::getArray(const std::string& _name) { - ejson::Value* tmp = get(_name); - if (NULL == tmp) { - return NULL; +std::shared_ptr ejson::Object::getArray(const std::string& _name) { + std::shared_ptr tmp = get(_name); + if (tmp == nullptr) { + return nullptr; } - return dynamic_cast(tmp); + return std::dynamic_pointer_cast(tmp); } -const ejson::Array* ejson::Object::getArray(const std::string& _name) const { - const ejson::Value* tmp = get(_name); - if (NULL == tmp) { - return NULL; +const std::shared_ptr ejson::Object::getArray(const std::string& _name) const { + const std::shared_ptr tmp = get(_name); + if (tmp == nullptr) { + return nullptr; } - return dynamic_cast(tmp); + return std::dynamic_pointer_cast(tmp); } -ejson::Null* ejson::Object::getNull(const std::string& _name) { - ejson::Value* tmp = get(_name); - if (NULL == tmp) { - return NULL; +std::shared_ptr ejson::Object::getNull(const std::string& _name) { + std::shared_ptr tmp = get(_name); + if (tmp == nullptr) { + return nullptr; } - return dynamic_cast(tmp); + return std::dynamic_pointer_cast(tmp); } -const ejson::Null* ejson::Object::getNull(const std::string& _name) const { - const ejson::Value* tmp = get(_name); - if (NULL == tmp) { - return NULL; +const std::shared_ptr ejson::Object::getNull(const std::string& _name) const { + const std::shared_ptr tmp = get(_name); + if (tmp == nullptr) { + return nullptr; } - return dynamic_cast(tmp); + return std::dynamic_pointer_cast(tmp); } -ejson::String* ejson::Object::getString(const std::string& _name) { - ejson::Value* tmp = get(_name); - if (NULL == tmp) { - return NULL; +std::shared_ptr ejson::Object::getString(const std::string& _name) { + std::shared_ptr tmp = get(_name); + if (tmp == nullptr) { + return nullptr; } - return dynamic_cast(tmp); + return std::dynamic_pointer_cast(tmp); } -const ejson::String* ejson::Object::getString(const std::string& _name) const { - const ejson::Value* tmp = get(_name); - if (NULL == tmp) { - return NULL; +const std::shared_ptr ejson::Object::getString(const std::string& _name) const { + const std::shared_ptr tmp = get(_name); + if (tmp == nullptr) { + return nullptr; } - return dynamic_cast(tmp); + return std::dynamic_pointer_cast(tmp); } const std::string& ejson::Object::getStringValue(const std::string& _name) const { static const std::string errorString(""); - const ejson::String* tmpp = getString(_name); - if (NULL == tmpp) { + const std::shared_ptr tmpp = getString(_name); + if (tmpp == nullptr) { return errorString; } return tmpp->get(); } std::string ejson::Object::getStringValue(const std::string& _name, const std::string& _errorValue) const { - const ejson::String* tmpp = getString(_name); - if (NULL == tmpp) { + const std::shared_ptr tmpp = getString(_name); + if (tmpp == nullptr) { return _errorValue; } return tmpp->get(); } -ejson::Boolean* ejson::Object::getBoolean(const std::string& _name) { - ejson::Value* tmp = get(_name); - if (NULL == tmp) { - return NULL; +std::shared_ptr ejson::Object::getBoolean(const std::string& _name) { + std::shared_ptr tmp = get(_name); + if (tmp == nullptr) { + return nullptr; } return tmp->toBoolean(); } -const ejson::Boolean* ejson::Object::getBoolean(const std::string& _name) const { - const ejson::Value* tmp = get(_name); - if (NULL == tmp) { - return NULL; +const std::shared_ptr ejson::Object::getBoolean(const std::string& _name) const { + const std::shared_ptr tmp = get(_name); + if (tmp == nullptr) { + return nullptr; } return tmp->toBoolean(); } bool ejson::Object::getBooleanValue(const std::string& _name, bool _errorValue) const { - const ejson::Boolean* tmpp = getBoolean(_name); - if (NULL == tmpp) { + const std::shared_ptr tmpp = getBoolean(_name); + if (tmpp == nullptr) { return _errorValue; } return tmpp->get(); } -ejson::Number* ejson::Object::getNumber(const std::string& _name) { - ejson::Value* tmp = get(_name); - if (NULL == tmp) { - return NULL; +std::shared_ptr ejson::Object::getNumber(const std::string& _name) { + std::shared_ptr tmp = get(_name); + if (tmp == nullptr) { + return nullptr; } return tmp->toNumber(); } -const ejson::Number* ejson::Object::getNumber(const std::string& _name) const { - const ejson::Value* tmp = get(_name); - if (NULL == tmp) { - return NULL; +const std::shared_ptr ejson::Object::getNumber(const std::string& _name) const { + const std::shared_ptr tmp = get(_name); + if (tmp == nullptr) { + return nullptr; } return tmp->toNumber(); } double ejson::Object::getNumberValue(const std::string& _name, double _errorValue) const { - const ejson::Number* tmpp = getNumber(_name); - if (NULL == tmpp) { + const std::shared_ptr tmpp = getNumber(_name); + if (tmpp == nullptr) { return _errorValue; } return tmpp->get(); } -bool ejson::Object::add(const std::string& _name, ejson::Value* _value) { - if (NULL == _value) { +bool ejson::Object::add(const std::string& _name, std::shared_ptr _value) { + if (_value == nullptr) { return false; } if (_name.size() == 0) { return false; } if (m_value.exist(_name)) { - ejson::Value* tmp = m_value[_name]; - delete(tmp); m_value[_name] = _value; return true; } @@ -440,28 +437,28 @@ bool ejson::Object::add(const std::string& _name, ejson::Value* _value) { } bool ejson::Object::addString(const std::string& _name, const std::string& _value) { - return add(_name, new ejson::String(_value)); + return add(_name, ejson::String::create(_value)); } bool ejson::Object::addNull(const std::string& _name) { - return add(_name, new ejson::Null()); + return add(_name, ejson::Null::create()); } bool ejson::Object::addBoolean(const std::string& _name, bool _value) { - return add(_name, new ejson::Boolean(_value)); + return add(_name, ejson::Boolean::create(_value)); } bool ejson::Object::addNumber(const std::string& _name, double _value) { - return add(_name, new ejson::Number(_value)); + return add(_name, ejson::Number::create(_value)); } -bool ejson::Object::transfertIn(ejson::Value* _obj) { - if (NULL == _obj) { - JSON_ERROR("Request transfer on an NULL pointer"); +bool ejson::Object::transfertIn(std::shared_ptr _obj) { + if (_obj == nullptr) { + JSON_ERROR("Request transfer on an nullptr pointer"); return false; } - ejson::Object* other = _obj->toObject(); - if (NULL == other) { + std::shared_ptr other = _obj->toObject(); + if (other == nullptr) { JSON_ERROR("Request transfer on an element that is not an object"); return false; } @@ -475,16 +472,16 @@ bool ejson::Object::transfertIn(ejson::Value* _obj) { } // TODO : Manage error ... -ejson::Value* ejson::Object::duplicate() const { - ejson::Object* output = new ejson::Object(); - if (NULL == output) { +std::shared_ptr ejson::Object::duplicate() const { + std::shared_ptr output = ejson::Object::create(); + if (output == nullptr) { JSON_ERROR("Allocation error ..."); - return NULL; + return nullptr; } for (int32_t iii=0; iii val = m_value.getValue(iii); std::string key = m_value.getKey(iii); - if (NULL == val) { + if (val == nullptr) { continue; } output->add(key, val->duplicate()); diff --git a/ejson/Object.h b/ejson/Object.h index 148a124..7b49177 100644 --- a/ejson/Object.h +++ b/ejson/Object.h @@ -21,14 +21,15 @@ namespace ejson { * @brief basic element of a xml structure */ Object() { }; + public: + static std::shared_ptr create(); /** * @brief destructor */ virtual ~Object() { }; protected: - etk::Hash 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.... /** * @brief check if an element exist. * @param[in] _name name of the object. @@ -38,17 +39,17 @@ namespace ejson { /** * @brief get the sub element with his name (no cast check) * @param[in] _name name of the object - * @return pointer on the element requested or NULL if it not the corect type or does not existed + * @return pointer on the element requested or nullptr if it not the corect type or does not existed */ - ejson::Value* get(const std::string& _name); + std::shared_ptr get(const std::string& _name); //! @previous - const ejson::Value* get(const std::string& _name) const; + const std::shared_ptr get(const std::string& _name) const; //! @previous - ejson::Value* operator[] (const std::string& _name) { + std::shared_ptr operator[] (const std::string& _name) { return get(_name); } //! @previous - const ejson::Value* operator[] (const std::string& _name) const { + const std::shared_ptr operator[] (const std::string& _name) const { return get(_name); } public: @@ -69,21 +70,21 @@ namespace ejson { /** * @brief get the pointer on an element reference with his ID. * @param[in] _id Id of the element. - * @return NULL if the element does not exist. + * @return nullptr if the element does not exist. */ - ejson::Value* get(size_t _id) { + std::shared_ptr get(size_t _id) { return m_value[_id]; }; //! @previous - const ejson::Value* get(size_t _id) const{ + const std::shared_ptr get(size_t _id) const{ return m_value[_id]; }; //! @previous - ejson::Value* operator[] (size_t _id) { + std::shared_ptr operator[] (size_t _id) { return m_value[_id]; } //! @previous - const ejson::Value* operator[] (size_t _id) const { + const std::shared_ptr operator[] (size_t _id) const { return m_value[_id]; } /** @@ -97,35 +98,35 @@ namespace ejson { /** * @brief get the sub element with his name (Casted as Object if it is possible) * @param[in] _name name of the object - * @return pointer on the element requested or NULL if it not the corect type or does not existed + * @return pointer on the element requested or nullptr if it not the corect type or does not existed */ - ejson::Object* getObject(const std::string& _name); + std::shared_ptr getObject(const std::string& _name); //! @previous - const ejson::Object* getObject(const std::string& _name) const; + const std::shared_ptr getObject(const std::string& _name) const; /** * @brief get the sub element with his name (Casted as Array if it is possible) * @param[in] _name name of the object - * @return pointer on the element requested or NULL if it not the corect type or does not existed + * @return pointer on the element requested or nullptr if it not the corect type or does not existed */ - ejson::Array* getArray(const std::string& _name); + std::shared_ptr getArray(const std::string& _name); //! @previous - const ejson::Array* getArray(const std::string& _name) const; + const std::shared_ptr getArray(const std::string& _name) const; /** * @brief get the sub element with his name (Casted as Null if it is possible) * @param[in] _name name of the object - * @return pointer on the element requested or NULL if it not the corect type or does not existed + * @return pointer on the element requested or nullptr if it not the corect type or does not existed */ - ejson::Null* getNull(const std::string& _name); + std::shared_ptr getNull(const std::string& _name); //! @previous - const ejson::Null* getNull(const std::string& _name) const; + const std::shared_ptr getNull(const std::string& _name) const; /** * @brief get the sub element with his name (Casted as String if it is possible) * @param[in] _name name of the object - * @return pointer on the element requested or NULL if it not the corect type or does not existed + * @return pointer on the element requested or nullptr if it not the corect type or does not existed */ - ejson::String* getString(const std::string& _name); + std::shared_ptr getString(const std::string& _name); //! @previous - const ejson::String* getString(const std::string& _name) const; + const std::shared_ptr getString(const std::string& _name) const; /** * @brief get the sub string value of the requested element * @param[in] _name name of the object @@ -142,11 +143,11 @@ namespace ejson { /** * @brief get the sub element with his name (Casted as Boolean if it is possible) * @param[in] _name name of the object - * @return pointer on the element requested or NULL if it not the corect type or does not existed + * @return pointer on the element requested or nullptr if it not the corect type or does not existed */ - ejson::Boolean* getBoolean(const std::string& _name); + std::shared_ptr getBoolean(const std::string& _name); //! @previous - const ejson::Boolean* getBoolean(const std::string& _name) const; + const std::shared_ptr getBoolean(const std::string& _name) const; /** * @brief get the sub boolean value of the requested element. * @param[in] _name name of the object. @@ -157,11 +158,11 @@ namespace ejson { /** * @brief get the sub element with his name (Casted as Number if it is possible) * @param[in] _name name of the object - * @return pointer on the element requested or NULL if it not the corect type or does not existed + * @return pointer on the element requested or nullptr if it not the corect type or does not existed */ - ejson::Number* getNumber(const std::string& _name); + std::shared_ptr getNumber(const std::string& _name); //! @previous - const ejson::Number* getNumber(const std::string& _name) const; + const std::shared_ptr getNumber(const std::string& _name) const; /** * @brief get the sub Number value of the requested element. * @param[in] _name name of the object. @@ -176,7 +177,7 @@ namespace ejson { * @param[in] _value Element to add * @return false if an error occured */ - bool add(const std::string& _name, ejson::Value* _value); + bool add(const std::string& _name, std::shared_ptr _value); /** * @brief add a string element in the Object (automatic creation) * @param[in] _name name of the object @@ -208,8 +209,8 @@ namespace ejson { 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 void clear(); - virtual bool transfertIn(ejson::Value* _obj); - virtual ejson::Value* duplicate() const; + virtual bool transfertIn(std::shared_ptr _obj); + virtual std::shared_ptr duplicate() const; }; }; diff --git a/ejson/String.cpp b/ejson/String.cpp index 8afb108..2a137b6 100644 --- a/ejson/String.cpp +++ b/ejson/String.cpp @@ -17,6 +17,9 @@ #define __class__ "String" +std::shared_ptr ejson::String::create(const std::string& _value) { + return std::shared_ptr(new ejson::String(_value)); +} bool ejson::String::iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc) { JSON_PARSE_ELEMENT("start parse : 'String' "); @@ -49,13 +52,13 @@ bool ejson::String::iGenerate(std::string& _data, size_t _indent) const { } -bool ejson::String::transfertIn(ejson::Value* _obj) { - if (NULL == _obj) { - JSON_ERROR("Request transfer on an NULL pointer"); +bool ejson::String::transfertIn(std::shared_ptr _obj) { + if (_obj == nullptr) { + JSON_ERROR("Request transfer on an nullptr pointer"); return false; } - ejson::String* other = _obj->toString(); - if (NULL == other) { + std::shared_ptr other = _obj->toString(); + if (other == nullptr) { JSON_ERROR("Request transfer on an element that is not an String"); return false; } @@ -64,11 +67,11 @@ bool ejson::String::transfertIn(ejson::Value* _obj) { return true; } -ejson::Value* ejson::String::duplicate() const { - ejson::String* output = new ejson::String(m_value); - if (NULL == output) { +std::shared_ptr ejson::String::duplicate() const { + std::shared_ptr output = ejson::String::create(m_value); + if (output == nullptr) { JSON_ERROR("Allocation error ..."); - return NULL; + return nullptr; } return output; } diff --git a/ejson/String.h b/ejson/String.h index c771b39..68d9f8c 100644 --- a/ejson/String.h +++ b/ejson/String.h @@ -14,7 +14,7 @@ namespace ejson { class String : public ejson::Value { - public: + protected: /** * @brief basic element of a xml structure */ @@ -22,6 +22,8 @@ namespace ejson { m_value(_value) { }; + public: + static std::shared_ptr create(const std::string& _value=""); /** * @brief destructor */ @@ -46,8 +48,8 @@ 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 bool transfertIn(ejson::Value* _obj); - virtual ejson::Value* duplicate() const; + virtual bool transfertIn(std::shared_ptr _obj); + virtual std::shared_ptr duplicate() const; }; }; diff --git a/ejson/Value.cpp b/ejson/Value.cpp index e30ea73..bd1b1e4 100644 --- a/ejson/Value.cpp +++ b/ejson/Value.cpp @@ -13,8 +13,6 @@ #undef __class__ #define __class__ "Value" - - ejson::Value::~Value() { clear(); } @@ -124,45 +122,45 @@ bool ejson::Value::checkNumber(char32_t _val) const { } -ejson::Document* ejson::Value::toDocument() { - return dynamic_cast(this); +std::shared_ptr ejson::Value::toDocument() { + return std::dynamic_pointer_cast(shared_from_this()); }; -const ejson::Document* ejson::Value::toDocument() const { - return dynamic_cast(this); +const std::shared_ptr ejson::Value::toDocument() const { + return std::dynamic_pointer_cast(shared_from_this()); }; -ejson::Array* ejson::Value::toArray() { - return dynamic_cast(this); +std::shared_ptr ejson::Value::toArray() { + return std::dynamic_pointer_cast(shared_from_this()); }; -const ejson::Array* ejson::Value::toArray() const{ - return dynamic_cast(this); +const std::shared_ptr ejson::Value::toArray() const{ + return std::dynamic_pointer_cast(shared_from_this()); }; -ejson::Object* ejson::Value::toObject() { - return dynamic_cast(this); +std::shared_ptr ejson::Value::toObject() { + return std::dynamic_pointer_cast(shared_from_this()); }; -const ejson::Object* ejson::Value::toObject() const{ - return dynamic_cast(this); +const std::shared_ptr ejson::Value::toObject() const{ + return std::dynamic_pointer_cast(shared_from_this()); }; -ejson::String* ejson::Value::toString() { - return dynamic_cast(this); +std::shared_ptr ejson::Value::toString() { + return std::dynamic_pointer_cast(shared_from_this()); }; -const ejson::String* ejson::Value::toString() const{ - return dynamic_cast(this); +const std::shared_ptr ejson::Value::toString() const{ + return std::dynamic_pointer_cast(shared_from_this()); }; -ejson::Number* ejson::Value::toNumber() { - return dynamic_cast(this); +std::shared_ptr ejson::Value::toNumber() { + return std::dynamic_pointer_cast(shared_from_this()); }; -const ejson::Number* ejson::Value::toNumber() const{ - return dynamic_cast(this); +const std::shared_ptr ejson::Value::toNumber() const{ + return std::dynamic_pointer_cast(shared_from_this()); }; -ejson::Boolean* ejson::Value::toBoolean() { - return dynamic_cast(this); +std::shared_ptr ejson::Value::toBoolean() { + return std::dynamic_pointer_cast(shared_from_this()); }; -const ejson::Boolean* ejson::Value::toBoolean() const{ - return dynamic_cast(this); +const std::shared_ptr ejson::Value::toBoolean() const{ + return std::dynamic_pointer_cast(shared_from_this()); }; -ejson::Null* ejson::Value::toNull() { - return dynamic_cast(this); +std::shared_ptr ejson::Value::toNull() { + return std::dynamic_pointer_cast(shared_from_this()); }; -const ejson::Null* ejson::Value::toNull() const{ - return dynamic_cast(this); +const std::shared_ptr ejson::Value::toNull() const{ + return std::dynamic_pointer_cast(shared_from_this()); }; \ No newline at end of file diff --git a/ejson/Value.h b/ejson/Value.h index a162349..e936239 100644 --- a/ejson/Value.h +++ b/ejson/Value.h @@ -10,6 +10,7 @@ #define __ETK_JSON_VALUE_H__ #include +#include namespace ejson { //#define ENABLE_DISPLAY_PARSED_ELEMENT @@ -104,8 +105,8 @@ namespace ejson { }; std::ostream& operator <<(std::ostream& _os, const filePos& _obj); - class Value { - public: + class Value : public std::enable_shared_from_this { + protected: /** * @brief basic element of a xml structure */ @@ -165,113 +166,113 @@ namespace ejson { public: /** * @brief Cast the element in a Value if it is possible. - * @return pointer on the class or NULL. + * @return pointer on the class or nullptr. */ - ejson::Value* toValue() { - return this; + std::shared_ptr toValue() { + return shared_from_this(); }; //! @previous - const ejson::Value* toValue() const { - return this; + const std::shared_ptr toValue() const { + return shared_from_this(); }; /** * @brief Cast the element in a Document if it is possible. - * @return pointer on the class or NULL. + * @return pointer on the class or nullptr. */ - ejson::Document* toDocument(); + std::shared_ptr toDocument(); //! @previous - const ejson::Document* toDocument() const; + const std::shared_ptr toDocument() const; /** * @brief Cast the element in a Array if it is possible. - * @return pointer on the class or NULL. + * @return pointer on the class or nullptr. */ - ejson::Array* toArray(); + std::shared_ptr toArray(); //! @previous - const ejson::Array* toArray() const; + const std::shared_ptr toArray() const; /** * @brief Cast the element in a Object if it is possible. - * @return pointer on the class or NULL. + * @return pointer on the class or nullptr. */ - ejson::Object* toObject(); + std::shared_ptr toObject(); //! @previous - const ejson::Object* toObject() const; + const std::shared_ptr toObject() const; /** * @brief Cast the element in a String if it is possible. - * @return pointer on the class or NULL. + * @return pointer on the class or nullptr. */ - ejson::String* toString(); + std::shared_ptr toString(); //! @previous - const ejson::String* toString() const; + const std::shared_ptr toString() const; /** * @brief Cast the element in a Number if it is possible. - * @return pointer on the class or NULL. + * @return pointer on the class or nullptr. */ - ejson::Number* toNumber(); + std::shared_ptr toNumber(); //! @previous - const ejson::Number* toNumber() const; + const std::shared_ptr toNumber() const; /** * @brief Cast the element in a Boolean if it is possible. - * @return pointer on the class or NULL. + * @return pointer on the class or nullptr. */ - ejson::Boolean* toBoolean(); + std::shared_ptr toBoolean(); //! @previous - const ejson::Boolean* toBoolean() const; + const std::shared_ptr toBoolean() const; /** * @brief Cast the element in a Null if it is possible. - * @return pointer on the class or NULL. + * @return pointer on the class or nullptr. */ - ejson::Null* toNull(); + std::shared_ptr toNull(); //! @previous - const ejson::Null* toNull() const; + const std::shared_ptr toNull() const; /** * @brief check if the node is a ejson::Document * @return true if the node is a ejson::Document */ bool isDocument() const { - return toDocument() != NULL; + return toDocument() != nullptr; }; /** * @brief check if the node is a ejson::Array * @return true if the node is a ejson::Array */ bool isArray() const { - return toArray() != NULL; + return toArray() != nullptr; }; /** * @brief check if the node is a ejson::Object * @return true if the node is a ejson::Object */ bool isObject() const { - return toObject() != NULL; + return toObject() != nullptr; }; /** * @brief check if the node is a ejson::String * @return true if the node is a ejson::String */ bool isString() const { - return toString() != NULL; + return toString() != nullptr; }; /** * @brief check if the node is a ejson::Number * @return true if the node is a ejson::Number */ bool isNumber() const { - return toNumber() != NULL; + return toNumber() != nullptr; }; /** * @brief check if the node is a ejson::Boolean * @return true if the node is a ejson::Boolean */ bool isBoolean() const { - return toBoolean() != NULL; + return toBoolean() != nullptr; }; /** * @brief check if the node is a ejson::Null * @return true if the node is a ejson::Null */ bool isNull() const { - return toNull() != NULL; + return toNull() != nullptr; }; /** @@ -284,15 +285,15 @@ namespace ejson { * @return true if transfer is done corectly * @note all element is remove from the curent element. */ - virtual bool transfertIn(ejson::Value* _obj) { + virtual bool transfertIn(std::shared_ptr _obj) { return false; }; /** * @brief Copy the curent node and all the child in the curent one. - * @return NULL in an error occured, the pointer on the element otherwise + * @return nullptr in an error occured, the pointer on the element otherwise */ - virtual ejson::Value* duplicate() const { - return NULL; + virtual std::shared_ptr duplicate() const { + return nullptr; }; protected: /** diff --git a/ejson/ejson.cpp b/ejson/ejson.cpp index c9db86e..82f4a80 100644 --- a/ejson/ejson.cpp +++ b/ejson/ejson.cpp @@ -20,6 +20,11 @@ #undef __class__ #define __class__ "Document" + +std::shared_ptr ejson::Document::create() { + return std::shared_ptr(new ejson::Document()); +} + ejson::Document::Document() : m_writeErrorWhenDetexted(true), m_comment(""), @@ -70,21 +75,14 @@ bool ejson::Document::load(const std::string& _file) { return false; } // allocate data - char * fileBuffer = new char[fileSize+5]; - if (NULL == fileBuffer) { - JSON_ERROR("Error Memory allocation size=" << fileSize); - return false; - } - // TODO : change this ... get the charset from the Declaration element ... - memset(fileBuffer, 0, (fileSize+5)*sizeof(char)); + std::vector fileBuffer; + fileBuffer.resize(fileSize+5, 0); // load data from the file : - tmpFile.fileRead(fileBuffer, 1, fileSize); + tmpFile.fileRead(&fileBuffer[0], 1, fileSize); // close the file: tmpFile.fileClose(); - std::string tmpDataUnicode(fileBuffer); - // remove temporary buffer: - delete[] fileBuffer; + std::string tmpDataUnicode(&fileBuffer[0]); // parse the data : bool ret = parse(tmpDataUnicode); //Display(); diff --git a/ejson/ejson.h b/ejson/ejson.h index 05f88d9..52de0b1 100644 --- a/ejson/ejson.h +++ b/ejson/ejson.h @@ -23,6 +23,7 @@ namespace ejson { * @brief Constructor */ Document(); + static std::shared_ptr create(); /** * @brief Destructor */ diff --git a/ejson/test.cpp b/ejson/test.cpp index fbac6f3..3060d45 100644 --- a/ejson/test.cpp +++ b/ejson/test.cpp @@ -462,7 +462,9 @@ void init() { } int main(int argc, const char *argv[]) { - etk::log::setLevel(etk::log::logLevelVerbose); + etk::log::setLevel(etk::log::logLevelInfo); + + init(); int32_t countError = 0; int32_t countSeparator = 0;