From 2ece8a2477109e9efd8c2c743cce1231778681e1 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Wed, 27 Apr 2016 21:29:44 +0200 Subject: [PATCH] [DEV] clean API and ionterface (add documentation adn add remove elements --- doc/write.md | 37 ++++--- ejson/Array.cpp | 82 ++++++--------- ejson/Array.h | 25 +++-- ejson/Boolean.cpp | 6 +- ejson/Boolean.h | 3 +- ejson/Document.cpp | 2 +- ejson/Null.cpp | 2 +- ejson/Number.cpp | 29 +----- ejson/Number.h | 17 +--- ejson/Object.cpp | 74 ++++++-------- ejson/Object.h | 27 +++-- ejson/String.cpp | 7 +- ejson/String.h | 3 +- ejson/Value.cpp | 15 +-- ejson/Value.h | 2 + ejson/details/iterator.hxx | 5 + ejson/internal/Array.cpp | 179 +++------------------------------ ejson/internal/Array.h | 132 +++---------------------- ejson/internal/Boolean.cpp | 16 ++- ejson/internal/Boolean.h | 29 ++---- ejson/internal/Document.cpp | 4 - ejson/internal/Document.h | 10 +- ejson/internal/Null.cpp | 6 +- ejson/internal/Null.h | 18 ++-- ejson/internal/Number.cpp | 15 ++- ejson/internal/Number.h | 41 ++------ ejson/internal/Object.cpp | 192 +++++++----------------------------- ejson/internal/Object.h | 176 +++++---------------------------- ejson/internal/String.cpp | 17 +++- ejson/internal/String.h | 27 ++--- ejson/internal/Value.cpp | 79 --------------- ejson/internal/Value.h | 138 +++----------------------- ejson/iterator.h | 21 +++- sample/write.cpp | 4 +- 34 files changed, 356 insertions(+), 1084 deletions(-) diff --git a/doc/write.md b/doc/write.md index ec0a288..99741cb 100644 --- a/doc/write.md +++ b/doc/write.md @@ -28,31 +28,42 @@ Writing a stream is done like this: Operation on Tree {#ejson_tutorial_write_operation} ================= -Add Node/Declaration: -@snippet write.cpp ejson_sample_write_add_declaration +Add String: +@snippet write.cpp ejson_sample_write_add_string_1 +@snippet write.cpp ejson_sample_write_add_string_2 -Add an Node/Element: -@snippet write.cpp ejson_sample_write_add_element +Add Null: +@snippet write.cpp ejson_sample_write_add_null_1 +@snippet write.cpp ejson_sample_write_add_null_2 -Remove a Node/Element: -@snippet write.cpp ejson_sample_write_rm_node +Add Number: +@snippet write.cpp ejson_sample_write_add_number_1 +@snippet write.cpp ejson_sample_write_add_number_2 +Add Boolean: +@snippet write.cpp ejson_sample_write_add_boolean_1 +@snippet write.cpp ejson_sample_write_add_boolean_1 -Add an attribute (simple version): -@snippet write.cpp ejson_sample_write_add_attribute_simple +Add Array with values: +@snippet write.cpp ejson_sample_write_add_array -Add an attribute (complex version): -@snippet write.cpp ejson_sample_write_add_attribute_complex +Add Object with values: +@snippet write.cpp ejson_sample_write_add_object + +Remove a Value in an Object: +@snippet write.cpp ejson_sample_write_rm_object + +Remove a Value in an Object: +@snippet write.cpp ejson_sample_write_rm_array -Remove an attribute: -@snippet write.cpp ejson_sample_write_rm_attribute Object concept {#ejson_tutorial_concept} ============== -the ejson concept is to abstract the implementation of the internal system. All the element are maped on shared memory. +The ejson concept is to abstract the implementation of the internal system. All the element are mapped on shared memory. Then if you asign an element to an other, it is the same. You need to clone it if you want to have new standalone element. +@snippet write.cpp ejson_sample_read_clone All example file {#ejson_tutorial_write_all} ================ diff --git a/ejson/Array.cpp b/ejson/Array.cpp index 7938b26..6391422 100644 --- a/ejson/Array.cpp +++ b/ejson/Array.cpp @@ -8,13 +8,17 @@ #include #include #include +#include +#include +#include +#include ejson::Array::Array(ememory::SharedPtr _internalValue) : ejson::Value(_internalValue) { if (m_data == nullptr) { return; } - if (m_data->isArray() == false) { + if (m_data->getType() != ejson::valueType::array) { // try to set wrong type inside ... ==> remove it ... m_data = nullptr; } @@ -59,45 +63,16 @@ const ejson::Value ejson::Array::operator[] (size_t _id) const { return ejson::Value(static_cast(m_data.get())->get(_id)); } -std::string ejson::Array::getStringValue(size_t _id) { - if (m_data == nullptr) { - EJSON_ERROR("Can not parse (nullptr) ..."); - return ""; - } - return static_cast(m_data.get())->getStringValue(_id); -} - -const std::string& ejson::Array::getStringValue(size_t _id) const { - if (m_data == nullptr) { - static const std::string errorValue = ""; - EJSON_ERROR("Can not getStringValue (nullptr) ..."); - return errorValue; - } - return static_cast(m_data.get())->getStringValue(_id); -} - std::string ejson::Array::getStringValue(size_t _id, const std::string& _errorValue) const { - if (m_data == nullptr) { - EJSON_ERROR("Can not getStringValue (nullptr) ..."); - return _errorValue; - } - return static_cast(m_data.get())->getStringValue(_id, _errorValue); + return (*this)[_id].toString().get(_errorValue); } double ejson::Array::getNumberValue(size_t _id, double _errorValue) const { - if (m_data == nullptr) { - EJSON_ERROR("Can not getNumberValue (nullptr) ..."); - return _errorValue; - } - return static_cast(m_data.get())->getNumberValue(_id, _errorValue); + return (*this)[_id].toNumber().get(_errorValue); } bool ejson::Array::getBooleanValue(size_t _id, bool _errorValue) const { - if (m_data == nullptr) { - EJSON_ERROR("Can not getBooleanValue (nullptr) ..."); - return _errorValue; - } - return static_cast(m_data.get())->getBooleanValue(_id, _errorValue); + return (*this)[_id].toBoolean().get(_errorValue); } bool ejson::Array::add(const ejson::Value& _element) { @@ -108,36 +83,39 @@ bool ejson::Array::add(const ejson::Value& _element) { return static_cast(m_data.get())->add(_element.m_data); } -bool ejson::Array::addString(const std::string& _value) { +void ejson::Array::remove(size_t _id) { if (m_data == nullptr) { - EJSON_ERROR("Can not addString (nullptr) ..."); - return false; + EJSON_ERROR("Can not remove (nullptr) ..."); + return; } - return static_cast(m_data.get())->addString(_value); + static_cast(m_data.get())->remove(_id); +} + +ejson::Array::iterator ejson::Array::remove(const ejson::Array::iterator& _it) { + if (m_data == nullptr) { + EJSON_ERROR("Can not remove (nullptr) ..."); + return _it; + } + static_cast(m_data.get())->remove(_it.getId()); + return ejson::Array::iterator(*this, _it.getId()); +} + + + +bool ejson::Array::addString(const std::string& _value) { + return add(ejson::String(_value)); } bool ejson::Array::addNull() { - if (m_data == nullptr) { - EJSON_ERROR("Can not addNull (nullptr) ..."); - return false; - } - return static_cast(m_data.get())->addNull(); + return add(ejson::Null()); } bool ejson::Array::addBoolean(bool _value) { - if (m_data == nullptr) { - EJSON_ERROR("Can not addBoolean (nullptr) ..."); - return false; - } - return static_cast(m_data.get())->addBoolean(_value); + return add(ejson::Boolean(_value)); } bool ejson::Array::addNumber(double _value) { - if (m_data == nullptr) { - EJSON_ERROR("Can not addNumber (nullptr) ..."); - return false; - } - return static_cast(m_data.get())->addNumber(_value); + return add(ejson::Number(_value)); } ejson::Array::iterator ejson::Array::begin() { diff --git a/ejson/Array.h b/ejson/Array.h index c5de53e..5d96e7f 100644 --- a/ejson/Array.h +++ b/ejson/Array.h @@ -45,36 +45,33 @@ namespace ejson { * @return nullptr if the element does not exist. */ ejson::Value operator[] (size_t _id); - const ejson::Value operator[] (size_t _id) const; /** - * @brief get the value of the string element (if not a string return "") + * @brief get the pointer on an element reference with his ID. * @param[in] _id Id of the element. - * @return value of the element. + * @return nullptr if the element does not exist. */ - std::string getStringValue(size_t _id); - //! @previous - const std::string& getStringValue(size_t _id) const; + const ejson::Value operator[] (size_t _id) const; /** * @brief get the value of the string element * @param[in] _id Id of the element. * @param[in] _errorValue The return value if an error occured. * @return value of the element, or the _errorValue. */ - std::string getStringValue(size_t _id, const std::string& _errorValue) const; + std::string getStringValue(size_t _id, const std::string& _errorValue="") const; /** * @brief get the value of the Number element * @param[in] _id Id of the element. * @param[in] _errorValue The return value if an error occured. * @return value of the element, or the _errorValue. */ - double getNumberValue(size_t _id, double _errorValue) const; + double getNumberValue(size_t _id, double _errorValue=0.0) const; /** * @brief get the value of the Boolean element * @param[in] _id Id of the element. * @param[in] _errorValue The return value if an error occured. * @return value of the element, or the _errorValue. */ - bool getBooleanValue(size_t _id, bool _errorValue) const; + bool getBooleanValue(size_t _id, bool _errorValue=false) const; /** * @brief add an element on the array. * @param[in] _element element to add. @@ -104,6 +101,11 @@ namespace ejson { * @return false if an error occured */ bool addNumber(double _value); + /** + * @brief Remove Value with his Id + * @param[in] _id Id of the element. + */ + void remove(size_t _id); public: using iterator = ejson::iterator; //!< Specify iterator of the Array methode /** @@ -126,6 +128,11 @@ namespace ejson { * @return const iterator on the next of the last position of the Value */ const iterator end() const; + /** + * @brief Remove Value with his iterator + * @param[in] _it Iterator on the Value. + */ + iterator remove(const iterator& _it); }; } diff --git a/ejson/Boolean.cpp b/ejson/Boolean.cpp index 5a76d87..fb55780 100644 --- a/ejson/Boolean.cpp +++ b/ejson/Boolean.cpp @@ -16,7 +16,7 @@ ejson::Boolean::Boolean(ememory::SharedPtr _internalValu if (m_data == nullptr) { return; } - if (m_data->isBoolean() == false) { + if (m_data->getType() != ejson::valueType::boolean) { // try to set wrong type inside ... ==> remove it ... m_data = nullptr; } @@ -45,10 +45,10 @@ void ejson::Boolean::set(bool _value) { static_cast(m_data.get())->set(_value); } -bool ejson::Boolean::get() const { +bool ejson::Boolean::get(bool _errorValue) const { if (m_data == nullptr) { EJSON_ERROR("Can not get (nullptr) ..."); - return false; + return _errorValue; } return static_cast(m_data.get())->get(); } diff --git a/ejson/Boolean.h b/ejson/Boolean.h index 9feda8f..0fc04a7 100644 --- a/ejson/Boolean.h +++ b/ejson/Boolean.h @@ -41,9 +41,10 @@ namespace ejson { void set(bool _value); /** * @brief get the current element Value. + * @param[in] _errorValue Value return if no value Exist * @return the reference of the string value. */ - bool get() const; + bool get(bool _errorValue=false) const; }; } diff --git a/ejson/Document.cpp b/ejson/Document.cpp index 766f034..c740536 100644 --- a/ejson/Document.cpp +++ b/ejson/Document.cpp @@ -14,7 +14,7 @@ ejson::Document::Document(ememory::SharedPtr _internalVa if (m_data == nullptr) { return; } - if (m_data->isDocument() == false) { + if (m_data->getType() != ejson::valueType::document) { // try to set wrong type inside ... ==> remove it ... m_data = nullptr; } diff --git a/ejson/Null.cpp b/ejson/Null.cpp index 5252717..b4384e2 100644 --- a/ejson/Null.cpp +++ b/ejson/Null.cpp @@ -14,7 +14,7 @@ ejson::Null::Null(ememory::SharedPtr _internalValue) : if (m_data == nullptr) { return; } - if (m_data->isNull() == false) { + if (m_data->getType() != ejson::valueType::null) { // try to set wrong type inside ... ==> remove it ... m_data = nullptr; } diff --git a/ejson/Number.cpp b/ejson/Number.cpp index 8643e1f..0adcdfa 100644 --- a/ejson/Number.cpp +++ b/ejson/Number.cpp @@ -15,7 +15,7 @@ ejson::Number::Number(ememory::SharedPtr _internalValue) if (m_data == nullptr) { return; } - if (m_data->isNumber() == false) { + if (m_data->getType() != ejson::valueType::number) { // try to set wrong type inside ... ==> remove it ... m_data = nullptr; } @@ -44,35 +44,10 @@ void ejson::Number::set(double _value) { static_cast(m_data.get())->set(_value); } -double ejson::Number::get() const { - if (m_data == nullptr) { - EJSON_ERROR("Can not parse (nullptr) ..."); - return 0.0; - } - return static_cast(m_data.get())->get(); -} - double ejson::Number::get(double _errorValue) const { if (m_data == nullptr) { - EJSON_ERROR("Can not parse (nullptr) ..."); + EJSON_ERROR("Can not get (nullptr) ..."); return _errorValue; } return static_cast(m_data.get())->get(); } - -int32_t ejson::Number::getInt32() const { - if (m_data == nullptr) { - EJSON_ERROR("Can not getInt32 (nullptr) ..."); - return 0; - } - return static_cast(m_data.get())->getInt32(); -} - -int64_t ejson::Number::getInt64() const { - if (m_data == nullptr) { - EJSON_ERROR("Can not getInt64 (nullptr) ..."); - return 0; - } - return static_cast(m_data.get())->getInt64(); -} - diff --git a/ejson/Number.h b/ejson/Number.h index e5d4c7e..83ba9f1 100644 --- a/ejson/Number.h +++ b/ejson/Number.h @@ -40,27 +40,12 @@ namespace ejson { * @param[in] _value New value of the node. */ void set(double _value); - /** - * @brief Get the current element Value. - * @return The double number registered - */ - double get() const; /** * @brief Get the current element Value. * @param[in] _errorValue Value return if no value Exist * @return The double number registered */ - double get(double _errorValue) const; - /** - * @brief Get the current element Value. - * @return The 32 bit integer number registered - */ - int32_t getInt32() const; - /** - * @brief Get the current element Value. - * @return The 64 bit integer number registered - */ - int64_t getInt64() const; + double get(double _errorValue=0.0) const; }; } diff --git a/ejson/Object.cpp b/ejson/Object.cpp index fb63eab..2965bbf 100644 --- a/ejson/Object.cpp +++ b/ejson/Object.cpp @@ -15,7 +15,7 @@ ejson::Object::Object(ememory::SharedPtr _internalValue) if (m_data == nullptr) { return; } - if (m_data->isObject() == false) { + if (m_data->getType() != ejson::valueType::null) { // try to set wrong type inside ... ==> remove it ... m_data = nullptr; } @@ -106,37 +106,16 @@ std::string ejson::Object::getKey(size_t _id) const { return static_cast(m_data.get())->getKey(_id); } -const std::string& ejson::Object::getStringValue(const std::string& _name) const { - if (m_data == nullptr) { - static const std::string errorString = ""; - EJSON_ERROR("Can not getStringValue (nullptr) ..."); - return errorString; - } - return static_cast(m_data.get())->getStringValue(_name); -} - std::string ejson::Object::getStringValue(const std::string& _name, const std::string& _errorValue) const { - if (m_data == nullptr) { - EJSON_ERROR("Can not getStringValue (nullptr) ..."); - return ""; - } - return static_cast(m_data.get())->getStringValue(_name, _errorValue); + return (*this)[_name].toString().get(_errorValue); } bool ejson::Object::getBooleanValue(const std::string& _name, bool _errorValue) const { - if (m_data == nullptr) { - EJSON_ERROR("Can not getBooleanValue (nullptr) ..."); - return false; - } - return static_cast(m_data.get())->getBooleanValue(_name, _errorValue); + return (*this)[_name].toBoolean().get(_errorValue); } double ejson::Object::getNumberValue(const std::string& _name, double _errorValue) const { - if (m_data == nullptr) { - EJSON_ERROR("Can not getNumberValue (nullptr) ..."); - return 0.0; - } - return static_cast(m_data.get())->getNumberValue(_name, _errorValue); + return (*this)[_name].toNumber().get(_errorValue); } bool ejson::Object::add(const std::string& _name, const ejson::Value& _value) { @@ -148,35 +127,44 @@ bool ejson::Object::add(const std::string& _name, const ejson::Value& _value) { } bool ejson::Object::addString(const std::string& _name, const std::string& _value) { - if (m_data == nullptr) { - EJSON_ERROR("Can not addString (nullptr) ..."); - return false; - } - return static_cast(m_data.get())->addString(_name, _value); + return add(_name, ejson::String(_value)); } bool ejson::Object::addNull(const std::string& _name) { - if (m_data == nullptr) { - EJSON_ERROR("Can not addNull (nullptr) ..."); - return false; - } - return static_cast(m_data.get())->addNull(_name); + return add(_name, ejson::Null()); } bool ejson::Object::addBoolean(const std::string& _name, bool _value) { - if (m_data == nullptr) { - EJSON_ERROR("Can not addBoolean (nullptr) ..."); - return false; - } - return static_cast(m_data.get())->addBoolean(_name, _value); + return add(_name, ejson::Boolean(_value)); } bool ejson::Object::addNumber(const std::string& _name, double _value) { + return add(_name, ejson::Number(_value)); +} + +void ejson::Object::remove(const std::string& _name) { if (m_data == nullptr) { - EJSON_ERROR("Can not addNumber (nullptr) ..."); - return false; + EJSON_ERROR("Can not remove (nullptr) ..."); + return; } - return static_cast(m_data.get())->addNumber(_name, _value); + static_cast(m_data.get())->remove(_name); +} + +void ejson::Object::remove(size_t _id) { + if (m_data == nullptr) { + EJSON_ERROR("Can not remove (nullptr) ..."); + return; + } + static_cast(m_data.get())->remove(_id); +} + +ejson::Object::iterator ejson::Object::remove(const ejson::Object::iterator& _it) { + if (m_data == nullptr) { + EJSON_ERROR("Can not remove (nullptr) ..."); + return _it; + } + static_cast(m_data.get())->remove(_it.getId()); + return ejson::Object::iterator(*this, _it.getId()); } diff --git a/ejson/Object.h b/ejson/Object.h index c9fdeaa..2aed29e 100644 --- a/ejson/Object.h +++ b/ejson/Object.h @@ -44,13 +44,13 @@ namespace ejson { public: /** * @brief check if an element exist. - * @param[in] _name name of the object. + * @param[in] _name Name of the object. * @return The existance of the element. */ bool valueExist(const std::string& _name) const; /** * @brief get the sub element with his name (no cast check) - * @param[in] _name name of the object + * @param[in] _name Name of the object * @return pointer on the element requested or nullptr if it not the corect type or does not existed */ ejson::Value operator[] (const std::string& _name); @@ -79,19 +79,13 @@ namespace ejson { * @return The name (key). */ std::string getKey(size_t _id) const; - /** - * @brief get the sub string value of the requested element - * @param[in] _name name of the object - * @return Value of the string or an error string (empty) - */ - const std::string& getStringValue(const std::string& _name) const; /** * @brief get the sub string value of the requested element (with error return value) * @param[in] _name name of the object * @param[in] _errorValue The return value if the element does not exist. * @return Value of the string or an error string (empty) */ - std::string getStringValue(const std::string& _name, const std::string& _errorValue) const; + std::string getStringValue(const std::string& _name, const std::string& _errorValue="") const; /** * @brief get the sub boolean value of the requested element. * @param[in] _name name of the object. @@ -141,6 +135,16 @@ namespace ejson { * @return false if an error occured */ bool addNumber(const std::string& _name, double _value); + /** + * @brief Remove Value with his name + * @param[in] _name Name of the object + */ + void remove(const std::string& _name); + /** + * @brief Remove Value with his id + * @param[in] _id Id of the element. + */ + void remove(size_t _id); public: using iterator = ejson::iterator; //!< Specify iterator of the Object methode /** @@ -163,6 +167,11 @@ namespace ejson { * @return const iterator on the next of the last position of the Value */ const iterator end() const; + /** + * @brief Remove Value with his iterator + * @param[in] _it Iterator on the Value. + */ + iterator remove(const iterator& _it); }; } diff --git a/ejson/String.cpp b/ejson/String.cpp index f93cfbe..5a6ec9c 100644 --- a/ejson/String.cpp +++ b/ejson/String.cpp @@ -14,7 +14,7 @@ ejson::String::String(ememory::SharedPtr _internalValue) if (m_data == nullptr) { return; } - if (m_data->isString() == false) { + if (m_data->getType() != ejson::valueType::string) { // try to set wrong type inside ... ==> remove it ... m_data = nullptr; } @@ -43,11 +43,10 @@ void ejson::String::set(const std::string& _value) { static_cast(m_data.get())->set(_value); } -const std::string& ejson::String::get() const { +std::string ejson::String::get(const std::string& _errorValue) const { if (m_data == nullptr) { - static const std::string errorValue = ""; EJSON_ERROR("Can not get (nullptr) ..."); - return errorValue; + return _errorValue; } return static_cast(m_data.get())->get(); } diff --git a/ejson/String.h b/ejson/String.h index 9dfc519..41940bf 100644 --- a/ejson/String.h +++ b/ejson/String.h @@ -41,9 +41,10 @@ namespace ejson { void set(const std::string& _value); /** * @brief get the current element Value. + * @param[in] _errorValue The return value if an error occured. * @return the reference of the string value. */ - const std::string& get() const; + std::string get(const std::string& _errorValue="") const; }; } diff --git a/ejson/Value.cpp b/ejson/Value.cpp index f49b5ac..613d304 100644 --- a/ejson/Value.cpp +++ b/ejson/Value.cpp @@ -125,49 +125,50 @@ bool ejson::Value::isDocument() const { if (m_data == nullptr) { return false; } - return m_data->isDocument(); + enum ejson::valueType type = m_data->getType(); + return type == ejson::valueType::document; } bool ejson::Value::isArray() const { if (m_data == nullptr) { return false; } - return m_data->isArray(); + return m_data->getType() == ejson::valueType::array; } bool ejson::Value::isObject() const { if (m_data == nullptr) { return false; } - return m_data->isObject(); + return m_data->getType() == ejson::valueType::object; } bool ejson::Value::isString() const { if (m_data == nullptr) { return false; } - return m_data->isString(); + return m_data->getType() == ejson::valueType::string; } bool ejson::Value::isNumber() const { if (m_data == nullptr) { return false; } - return m_data->isNumber(); + return m_data->getType() == ejson::valueType::number; } bool ejson::Value::isBoolean() const { if (m_data == nullptr) { return false; } - return m_data->isBoolean(); + return m_data->getType() == ejson::valueType::boolean; } bool ejson::Value::isNull() const { if (m_data == nullptr) { return false; } - return m_data->isNull(); + return m_data->getType() == ejson::valueType::null; } diff --git a/ejson/Value.h b/ejson/Value.h index 7412e39..d5ab112 100644 --- a/ejson/Value.h +++ b/ejson/Value.h @@ -63,6 +63,8 @@ namespace ejson { void display() const; /** * @brief Check if the element exit + * @return true The element exist + * @return False The element does NOT exist */ bool exist() const; /** diff --git a/ejson/details/iterator.hxx b/ejson/details/iterator.hxx index bc0194f..8788115 100644 --- a/ejson/details/iterator.hxx +++ b/ejson/details/iterator.hxx @@ -120,3 +120,8 @@ ejson::Value ejson::iterator::operator *() noexcept { return m_data[m_id]; } +template +size_t ejson::iterator::getId() const noexcept { + return m_id; +} + diff --git a/ejson/internal/Array.cpp b/ejson/internal/Array.cpp index 2184b59..8028fae 100644 --- a/ejson/internal/Array.cpp +++ b/ejson/internal/Array.cpp @@ -162,21 +162,20 @@ bool ejson::internal::Array::iGenerate(std::string& _data, size_t _indent) const if (tmp == nullptr) { continue; } - if (true == tmp->isObject()) { + if ( tmp->getType() == ejson::valueType::object + || tmp->getType() == ejson::valueType::document) { oneLine=false; break; } - if (true == tmp->isArray()) { + if (tmp->getType() == ejson::valueType::array) { oneLine=false; break; } - if (true == tmp->isString()) { - ememory::SharedPtr tmp2 = tmp->toString(); - if (tmp2 != nullptr) { - if(tmp2->get().size()>40) { - oneLine=false; - break; - } + if (tmp->getType() == ejson::valueType::string) { + ememory::SharedPtr tmp2 = std::static_pointer_cast(tmp); + if(tmp2->get().size()>40) { + oneLine=false; + break; } } } @@ -218,20 +217,11 @@ bool ejson::internal::Array::add(ememory::SharedPtr _ele return true; } -bool ejson::internal::Array::addString(const std::string& _value) { - return add(ejson::internal::String::create(_value)); -} - -bool ejson::internal::Array::addNull() { - return add(ejson::internal::Null::create()); -} - -bool ejson::internal::Array::addBoolean(bool _value) { - return add(ejson::internal::Boolean::create(_value)); -} - -bool ejson::internal::Array::addNumber(double _value) { - return add(ejson::internal::Number::create(_value)); +void ejson::internal::Array::remove(size_t _id) { + if (_id > m_value.size()) { + EJSON_ERROR("try remove out of bound element"); + } + m_value.erase(m_value.begin() + _id); } @@ -240,11 +230,11 @@ bool ejson::internal::Array::transfertIn(ememory::SharedPtr other = _obj->toArray(); - if (other == nullptr) { - EJSON_ERROR("Request transfer on an element that is not an array"); + if (_obj->getType() != ejson::valueType::array) { + EJSON_ERROR("Request transfer on an element that is not an Array"); return false; } + ememory::SharedPtr other = std::static_pointer_cast(_obj); // remove destination elements other->clear(); // Copy to the destination @@ -271,140 +261,3 @@ ememory::SharedPtr ejson::internal::Array::clone() const return output; } -ememory::SharedPtr ejson::internal::Array::getObject(size_t _id) { - ememory::SharedPtr tmpElement = m_value[_id]; - if (tmpElement == nullptr) { - return ememory::SharedPtr(); - } - return tmpElement->toObject(); -} -const ememory::SharedPtr ejson::internal::Array::getObject(size_t _id) const { - const ememory::SharedPtr tmpElement = m_value[_id]; - if (tmpElement == nullptr) { - return ememory::SharedPtr(); - } - return tmpElement->toObject(); -} - -ememory::SharedPtr ejson::internal::Array::getString(size_t _id) { - ememory::SharedPtr tmpElement = m_value[_id]; - if (tmpElement == nullptr) { - return ememory::SharedPtr(); - } - return tmpElement->toString(); -} - -const ememory::SharedPtr ejson::internal::Array::getString(size_t _id) const { - const ememory::SharedPtr tmpElement = m_value[_id]; - if (tmpElement == nullptr) { - return ememory::SharedPtr(); - } - return tmpElement->toString(); -} - -ememory::SharedPtr ejson::internal::Array::getArray(size_t _id) { - ememory::SharedPtr tmpElement = m_value[_id]; - if (tmpElement == nullptr) { - return ememory::SharedPtr(); - } - return tmpElement->toArray(); -} - -const ememory::SharedPtr ejson::internal::Array::getArray(size_t _id) const { - const ememory::SharedPtr tmpElement = m_value[_id]; - if (tmpElement == nullptr) { - return ememory::SharedPtr(); - } - return tmpElement->toArray(); -} - -ememory::SharedPtr ejson::internal::Array::getNull(size_t _id) { - ememory::SharedPtr tmpElement = m_value[_id]; - if (tmpElement == nullptr) { - return ememory::SharedPtr(); - } - return tmpElement->toNull(); -} - -const ememory::SharedPtr ejson::internal::Array::getNull(size_t _id) const { - const ememory::SharedPtr tmpElement = m_value[_id]; - if (tmpElement == nullptr) { - return ememory::SharedPtr(); - } - return tmpElement->toNull(); -} - -ememory::SharedPtr ejson::internal::Array::getNumber(size_t _id) { - ememory::SharedPtr tmpElement = m_value[_id]; - if (tmpElement == nullptr) { - return ememory::SharedPtr(); - } - return tmpElement->toNumber(); -} - -const ememory::SharedPtr ejson::internal::Array::getNumber(size_t _id) const { - const ememory::SharedPtr tmpElement = m_value[_id]; - if (tmpElement == nullptr) { - return ememory::SharedPtr(); - } - return tmpElement->toNumber(); -} - -ememory::SharedPtr ejson::internal::Array::getBoolean(size_t _id) { - ememory::SharedPtr tmpElement = m_value[_id]; - if (tmpElement == nullptr) { - return ememory::SharedPtr(); - } - return tmpElement->toBoolean(); -} - -const ememory::SharedPtr ejson::internal::Array::getBoolean(size_t _id) const { - const ememory::SharedPtr tmpElement = m_value[_id]; - if (tmpElement == nullptr) { - return ememory::SharedPtr(); - } - return tmpElement->toBoolean(); -} - -std::string ejson::internal::Array::getStringValue(size_t _id) { - ememory::SharedPtr tmpElement = getString(_id); - if (tmpElement == nullptr) { - return ""; - } - return tmpElement->get(); -} - -const std::string& ejson::internal::Array::getStringValue(size_t _id) const { - static const std::string errorValue(""); - const ememory::SharedPtr tmpElement = getString(_id); - if (tmpElement == nullptr) { - return errorValue; - } - return tmpElement->get(); -} - -std::string ejson::internal::Array::getStringValue(size_t _id, const std::string& _errorValue) const { - const ememory::SharedPtr tmpElement = getString(_id); - if (tmpElement == nullptr) { - return _errorValue; - } - return tmpElement->get(); -} - -double ejson::internal::Array::getNumberValue(size_t _id, double _errorValue) const { - const ememory::SharedPtr tmpElement = getNumber(_id); - if (tmpElement == nullptr) { - return _errorValue; - } - return tmpElement->get(); -} - -bool ejson::internal::Array::getBooleanValue(size_t _id, bool _errorValue) const { - const ememory::SharedPtr tmpElement = getBoolean(_id); - if (tmpElement == nullptr) { - return _errorValue; - } - return tmpElement->get(); -} - - diff --git a/ejson/internal/Array.h b/ejson/internal/Array.h index 321e1cc..8fac474 100644 --- a/ejson/internal/Array.h +++ b/ejson/internal/Array.h @@ -19,13 +19,9 @@ namespace ejson { */ Array() { m_type = ejson::valueType::array; - }; + } public: static ememory::SharedPtr create(); - /** - * @brief destructor - */ - virtual ~Array() { }; private: std::vector > m_value; //!< vector of sub elements public: @@ -35,7 +31,7 @@ namespace ejson { */ size_t size() const { return m_value.size(); - }; + } /** * @brief get the pointer on an element reference with his ID. * @param[in] _id Id of the element. @@ -43,96 +39,11 @@ namespace ejson { */ ememory::SharedPtr get(size_t _id) { return m_value[_id]; - }; + } //! @previous const ememory::SharedPtr get(size_t _id) const{ return m_value[_id]; - }; - //! @previous - ememory::SharedPtr operator[] (size_t _id) { - return m_value[_id]; } - //! @previous - const ememory::SharedPtr 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 nullptr if the element does not exist. - */ - ememory::SharedPtr getObject(size_t _id); - //! @previous - const ememory::SharedPtr 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 nullptr if the element does not exist. - */ - ememory::SharedPtr getString(size_t _id); - //! @previous - const ememory::SharedPtr 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. - * @return value of the element. - */ - std::string getStringValue(size_t _id); - //! @previous - const std::string& getStringValue(size_t _id) const; - /** - * @brief get the value of the string element - * @param[in] _id Id of the element. - * @param[in] _errorValue The return value if an error occured. - * @return value of the element, or the _errorValue. - */ - std::string getStringValue(size_t _id, const std::string& _errorValue) const; - /** - * @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 nullptr if the element does not exist. - */ - ememory::SharedPtr getArray(size_t _id); - //! @previous - const ememory::SharedPtr 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 nullptr if the element does not exist. - */ - ememory::SharedPtr getNull(size_t _id); - //! @previous - const ememory::SharedPtr 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 nullptr if the element does not exist. - */ - ememory::SharedPtr getNumber(size_t _id); - //! @previous - const ememory::SharedPtr getNumber(size_t _id) const; - /** - * @brief get the value of the Number element - * @param[in] _id Id of the element. - * @param[in] _errorValue The return value if an error occured. - * @return value of the element, or the _errorValue. - */ - double getNumberValue(size_t _id, double _errorValue) const; - /** - * @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 nullptr if the element does not exist. - */ - ememory::SharedPtr getBoolean(size_t _id); - //! @previous - const ememory::SharedPtr getBoolean(size_t _id) const; - /** - * @brief get the value of the Boolean element - * @param[in] _id Id of the element. - * @param[in] _errorValue The return value if an error occured. - * @return value of the element, or the _errorValue. - */ - bool getBooleanValue(size_t _id, bool _errorValue) const; /** * @brief add an element on the array. * @param[in] _element element to add. @@ -140,35 +51,16 @@ namespace ejson { */ bool add(ememory::SharedPtr _element); /** - * @brief add a string element in the Object (automatic creation) - * @param[in] _value string value to add - * @return false if an error occured + * @brief Remove Value with his Id + * @param[in] _id Id of the element. */ - bool addString(const std::string& _value); - /** - * @brief add a "null" element in the Object (automatic creation) - * @return false if an error occured - */ - bool addNull(); - /** - * @brief add a boolean element in the Object (automatic creation) - * @param[in] _value boolean value to add - * @return false if an error occured - */ - bool addBoolean(bool _value); - /** - * @brief add a double element in the Object (automatic creation) - * @param[in] _value double value to add - * @return false if an error occured - */ - bool addNumber(double _value); - - public: // herited function : - virtual bool iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc); - virtual bool iGenerate(std::string& _data, size_t _indent) const; - virtual void clear(); - virtual bool transfertIn(ememory::SharedPtr _obj); - virtual ememory::SharedPtr clone() const; + void remove(size_t _id); + public: + bool iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc) override; + bool iGenerate(std::string& _data, size_t _indent) const override; + void clear() override; + bool transfertIn(ememory::SharedPtr _obj) override; + ememory::SharedPtr clone() const override; }; } } diff --git a/ejson/internal/Boolean.cpp b/ejson/internal/Boolean.cpp index b1fa4b8..32cc491 100644 --- a/ejson/internal/Boolean.cpp +++ b/ejson/internal/Boolean.cpp @@ -14,6 +14,18 @@ ememory::SharedPtr ejson::internal::Boolean::create(bo return ememory::SharedPtr(new ejson::internal::Boolean(_value)); } +ejson::internal::Boolean::Boolean(bool _value) : + m_value(_value) { + m_type = ejson::valueType::boolean; +} + +void ejson::internal::Boolean::set(bool _value) { + m_value = _value; +} + +bool ejson::internal::Boolean::get() const { + return m_value; +} bool ejson::internal::Boolean::iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc) { EJSON_PARSE_ELEMENT("start parse : 'Boolean' "); @@ -59,11 +71,11 @@ bool ejson::internal::Boolean::transfertIn(ememory::SharedPtr other = _obj->toBoolean(); - if (other == nullptr) { + if (_obj->getType() != ejson::valueType::boolean) { EJSON_ERROR("Request transfer on an element that is not an Boolean"); return false; } + ememory::SharedPtr other = std::static_pointer_cast(_obj); // remove destination elements other->m_value = m_value; m_value = false; diff --git a/ejson/internal/Boolean.h b/ejson/internal/Boolean.h index fc41122..c0a33e0 100644 --- a/ejson/internal/Boolean.h +++ b/ejson/internal/Boolean.h @@ -17,18 +17,9 @@ namespace ejson { /** * @brief basic element of a xml structure */ - Boolean(bool _value=false) : - m_value(_value) { - m_type = ejson::valueType::boolean; - }; + Boolean(bool _value=false); public: static ememory::SharedPtr create(bool _value=false); - /** - * @brief destructor - */ - virtual ~Boolean() { - - }; protected: bool m_value; //!< value of the node public: @@ -36,21 +27,17 @@ namespace ejson { * @brief set the value of the node. * @param[in] _value New value of the node. */ - void set(bool _value) { - m_value = _value; - }; + void set(bool _value); /** * @brief get the current element Value. * @return the reference of the string value. */ - bool get() const { - return m_value; - }; - public: // herited function : - virtual bool iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc); - virtual bool iGenerate(std::string& _data, size_t _indent) const; - virtual bool transfertIn(ememory::SharedPtr _obj); - virtual ememory::SharedPtr clone() const; + bool get() const; + public: + bool iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc) override; + bool iGenerate(std::string& _data, size_t _indent) const override; + bool transfertIn(ememory::SharedPtr _obj) override; + ememory::SharedPtr clone() const override; }; } } diff --git a/ejson/internal/Document.cpp b/ejson/internal/Document.cpp index 3685b37..9289303 100644 --- a/ejson/internal/Document.cpp +++ b/ejson/internal/Document.cpp @@ -29,10 +29,6 @@ ejson::internal::Document::Document() : m_type = ejson::valueType::document; } -ejson::internal::Document::~Document() { - -} - bool ejson::internal::Document::iGenerate(std::string& _data, size_t _indent) const { ejson::internal::Object::iGenerate(_data, _indent+1); _data += "\n"; diff --git a/ejson/internal/Document.h b/ejson/internal/Document.h index 9673d59..178342c 100644 --- a/ejson/internal/Document.h +++ b/ejson/internal/Document.h @@ -23,10 +23,6 @@ namespace ejson { */ Document(); static ememory::SharedPtr create(); - /** - * @brief Destructor - */ - virtual ~Document(); public: /** * @brief parse a string that contain an XML @@ -71,9 +67,9 @@ namespace ejson { void createError(const std::string& _data, size_t _pos, const ejson::FilePos& _filePos, const std::string& _comment); void displayError(); - public: // herited function: - virtual bool iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc); - virtual bool iGenerate(std::string& _data, size_t _indent) const; + public: + bool iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc) override; + bool iGenerate(std::string& _data, size_t _indent) const override; }; } } diff --git a/ejson/internal/Null.cpp b/ejson/internal/Null.cpp index d0c7b24..b341afb 100644 --- a/ejson/internal/Null.cpp +++ b/ejson/internal/Null.cpp @@ -14,6 +14,9 @@ ememory::SharedPtr ejson::internal::Null::create() { return ememory::SharedPtr(new ejson::internal::Null()); } +ejson::internal::Null::Null() { + m_type = ejson::valueType::null; +} bool ejson::internal::Null::iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc) { EJSON_PARSE_ELEMENT("start parse : 'Null' "); @@ -45,8 +48,7 @@ bool ejson::internal::Null::transfertIn(ememory::SharedPtr other = _obj->toNull(); - if (other == nullptr) { + if (_obj->getType() == ejson::valueType::null) { EJSON_ERROR("Request transfer on an element that is not an Null"); return false; } diff --git a/ejson/internal/Null.h b/ejson/internal/Null.h index aefadf5..1931e83 100644 --- a/ejson/internal/Null.h +++ b/ejson/internal/Null.h @@ -17,20 +17,14 @@ namespace ejson { /** * @brief basic element of a xml structure */ - Null() { - m_type = ejson::valueType::null; - }; + Null(); public: static ememory::SharedPtr create(); - /** - * @brief destructor - */ - virtual ~Null() { }; - public: // herited function : - virtual bool iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc); - virtual bool iGenerate(std::string& _data, size_t _indent) const; - virtual bool transfertIn(ememory::SharedPtr _obj); - virtual ememory::SharedPtr clone() const; + public: + bool iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc) override; + bool iGenerate(std::string& _data, size_t _indent) const override; + bool transfertIn(ememory::SharedPtr _obj) override; + ememory::SharedPtr clone() const override; }; } } diff --git a/ejson/internal/Number.cpp b/ejson/internal/Number.cpp index 2f06fe8..bbf4b1a 100644 --- a/ejson/internal/Number.cpp +++ b/ejson/internal/Number.cpp @@ -15,6 +15,11 @@ ememory::SharedPtr ejson::internal::Number::create(doub return ememory::SharedPtr(new ejson::internal::Number(_value)); } +ejson::internal::Number::Number(double _value) : + m_value(_value) { + m_type = ejson::valueType::number; +} + bool ejson::internal::Number::iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc) { EJSON_PARSE_ELEMENT("start parse : 'Number' "); std::string tmpVal; @@ -55,11 +60,11 @@ bool ejson::internal::Number::transfertIn(ememory::SharedPtr other = _obj->toNumber(); - if (other == nullptr) { + if (_obj->getType() != ejson::valueType::number) { EJSON_ERROR("Request transfer on an element that is not an Number"); return false; } + ememory::SharedPtr other = std::static_pointer_cast(_obj); // remove destination elements other->m_value = m_value; m_value = 0; @@ -75,4 +80,10 @@ ememory::SharedPtr ejson::internal::Number::clone() cons return output; } +void ejson::internal::Number::set(double _value) { + m_value = _value; +} +double ejson::internal::Number::get() const { + return m_value; +} diff --git a/ejson/internal/Number.h b/ejson/internal/Number.h index 2a26f53..993d585 100644 --- a/ejson/internal/Number.h +++ b/ejson/internal/Number.h @@ -17,16 +17,9 @@ namespace ejson { /** * @brief basic element of a xml structure */ - Number(double _value=0.0) : - m_value(_value) { - m_type = ejson::valueType::number; - }; + Number(double _value=0.0); public: static ememory::SharedPtr create(double _value=0.0); - /** - * @brief destructor - */ - virtual ~Number() { }; protected: double m_value; //!< value of the node public: @@ -34,35 +27,17 @@ namespace ejson { * @brief set the value of the node. * @param[in] _value New value of the node. */ - void set(double _value) { - m_value = _value; - }; + void set(double _value); /** * @brief Get the current element Value. * @return The double number registered */ - double get() const { - return m_value; - }; - /** - * @brief Get the current element Value. - * @return The 32 bit integer number registered - */ - int32_t getInt32() const { - return (int32_t)m_value; - }; - /** - * @brief Get the current element Value. - * @return The 64 bit integer number registered - */ - int64_t getInt64() const { - return (int64_t)m_value; - }; - public: // herited function : - virtual bool iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc); - virtual bool iGenerate(std::string& _data, size_t _indent) const; - virtual bool transfertIn(ememory::SharedPtr _obj); - virtual ememory::SharedPtr clone() const; + double get() const; + public: + bool iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc) override; + bool iGenerate(std::string& _data, size_t _indent) const override; + bool transfertIn(ememory::SharedPtr _obj) override; + ememory::SharedPtr clone() const override; }; } } diff --git a/ejson/internal/Object.cpp b/ejson/internal/Object.cpp index 61d5532..7dac4d6 100644 --- a/ejson/internal/Object.cpp +++ b/ejson/internal/Object.cpp @@ -222,26 +222,25 @@ bool ejson::internal::Object::iGenerate(std::string& _data, size_t _indent) cons oneLine=false; } else { for (int32_t iii=0; iii tmp = m_value[iii]; + ememory::SharedPtr tmp = m_value[iii]; if (tmp == nullptr) { continue; } - if (tmp->isObject() == true) { + if ( tmp->getType() == ejson::valueType::object + || tmp->getType() == ejson::valueType::document) { oneLine=false; break; } - if (tmp->isArray() == true) { + if (tmp->getType() == ejson::valueType::array) { oneLine=false; break; } - if (tmp->isString() == true) { - ememory::SharedPtr tmp2 = tmp->toString(); - if (tmp2 != nullptr) { - if( tmp2->get().size()>25 - || m_value.getKey(iii).size()>25) { - oneLine=false; - break; - } + if (tmp->getType() == ejson::valueType::string) { + ememory::SharedPtr tmp2 = std::static_pointer_cast(tmp); + if( tmp2->get().size()>25 + || m_value.getKey(iii).size()>25) { + oneLine=false; + break; } } } @@ -279,150 +278,40 @@ bool ejson::internal::Object::exist(const std::string& _name) const { return m_value.exist(_name); } +std::vector ejson::internal::Object::getKeys() const { + return m_value.getKeys(); +} + +size_t ejson::internal::Object::size() const { + return m_value.size(); +} + +ememory::SharedPtr ejson::internal::Object::get(size_t _id) { + return m_value[_id]; +} + +const ememory::SharedPtr ejson::internal::Object::get(size_t _id) const{ + return m_value[_id]; +} + ememory::SharedPtr ejson::internal::Object::get(const std::string& _name) { - if (false == m_value.exist(_name)) { + if (m_value.exist(_name) == false) { return ememory::SharedPtr(); } return m_value[_name]; } const ememory::SharedPtr ejson::internal::Object::get(const std::string& _name) const { - if (false == m_value.exist(_name)) { + if (m_value.exist(_name) == false) { return ememory::SharedPtr(); } return m_value[_name]; } -ememory::SharedPtr ejson::internal::Object::getObject(const std::string& _name) { - ememory::SharedPtr tmp = get(_name); - if (tmp == nullptr) { - return ememory::SharedPtr(); - } - return std::dynamic_pointer_cast(tmp); +std::string ejson::internal::Object::getKey(size_t _id) const { + return m_value.getKey(_id); } -const ememory::SharedPtr ejson::internal::Object::getObject(const std::string& _name) const { - const ememory::SharedPtr tmp = get(_name); - if (tmp == nullptr) { - return ememory::SharedPtr(); - } - return std::dynamic_pointer_cast(tmp); -} - -ememory::SharedPtr ejson::internal::Object::getArray(const std::string& _name) { - ememory::SharedPtr tmp = get(_name); - if (tmp == nullptr) { - return ememory::SharedPtr(); - } - return std::dynamic_pointer_cast(tmp); -} - -const ememory::SharedPtr ejson::internal::Object::getArray(const std::string& _name) const { - const ememory::SharedPtr tmp = get(_name); - if (tmp == nullptr) { - return ememory::SharedPtr(); - } - return std::dynamic_pointer_cast(tmp); -} - -ememory::SharedPtr ejson::internal::Object::getNull(const std::string& _name) { - ememory::SharedPtr tmp = get(_name); - if (tmp == nullptr) { - return ememory::SharedPtr(); - } - return std::dynamic_pointer_cast(tmp); -} - -const ememory::SharedPtr ejson::internal::Object::getNull(const std::string& _name) const { - const ememory::SharedPtr tmp = get(_name); - if (tmp == nullptr) { - return ememory::SharedPtr(); - } - return std::dynamic_pointer_cast(tmp); -} - -ememory::SharedPtr ejson::internal::Object::getString(const std::string& _name) { - ememory::SharedPtr tmp = get(_name); - if (tmp == nullptr) { - return ememory::SharedPtr(); - } - return std::dynamic_pointer_cast(tmp); -} - -const ememory::SharedPtr ejson::internal::Object::getString(const std::string& _name) const { - const ememory::SharedPtr tmp = get(_name); - if (tmp == nullptr) { - return ememory::SharedPtr(); - } - return std::dynamic_pointer_cast(tmp); -} - -const std::string& ejson::internal::Object::getStringValue(const std::string& _name) const { - static const std::string errorString(""); - const ememory::SharedPtr tmpp = getString(_name); - if (tmpp == nullptr) { - return errorString; - } - return tmpp->get(); -} - -std::string ejson::internal::Object::getStringValue(const std::string& _name, const std::string& _errorValue) const { - const ememory::SharedPtr tmpp = getString(_name); - if (tmpp == nullptr) { - return _errorValue; - } - return tmpp->get(); -} - -ememory::SharedPtr ejson::internal::Object::getBoolean(const std::string& _name) { - ememory::SharedPtr tmp = get(_name); - if (tmp == nullptr) { - return ememory::SharedPtr(); - } - return tmp->toBoolean(); -} - -const ememory::SharedPtr ejson::internal::Object::getBoolean(const std::string& _name) const { - const ememory::SharedPtr tmp = get(_name); - if (tmp == nullptr) { - return ememory::SharedPtr(); - } - return tmp->toBoolean(); -} - -bool ejson::internal::Object::getBooleanValue(const std::string& _name, bool _errorValue) const { - const ememory::SharedPtr tmpp = getBoolean(_name); - if (tmpp == nullptr) { - return _errorValue; - } - return tmpp->get(); -} - -ememory::SharedPtr ejson::internal::Object::getNumber(const std::string& _name) { - ememory::SharedPtr tmp = get(_name); - if (tmp == nullptr) { - return ememory::SharedPtr(); - } - return tmp->toNumber(); -} - -const ememory::SharedPtr ejson::internal::Object::getNumber(const std::string& _name) const { - const ememory::SharedPtr tmp = get(_name); - if (tmp == nullptr) { - return ememory::SharedPtr(); - } - return tmp->toNumber(); -} - -double ejson::internal::Object::getNumberValue(const std::string& _name, double _errorValue) const { - const ememory::SharedPtr tmpp = getNumber(_name); - if (tmpp == nullptr) { - return _errorValue; - } - return tmpp->get(); -} - - bool ejson::internal::Object::add(const std::string& _name, ememory::SharedPtr _value) { if (_value == nullptr) { return false; @@ -438,20 +327,12 @@ bool ejson::internal::Object::add(const std::string& _name, ememory::SharedPtr _obj) { @@ -459,11 +340,12 @@ bool ejson::internal::Object::transfertIn(ememory::SharedPtr other = _obj->toObject(); - if (other == nullptr) { + if ( _obj->getType() != ejson::valueType::object + && _obj->getType() != ejson::valueType::document) { EJSON_ERROR("Request transfer on an element that is not an object"); return false; } + ememory::SharedPtr other = std::static_pointer_cast(_obj); // remove destination elements other->clear(); // Copy to the destination diff --git a/ejson/internal/Object.h b/ejson/internal/Object.h index 55d9d9b..f040a43 100644 --- a/ejson/internal/Object.h +++ b/ejson/internal/Object.h @@ -25,10 +25,6 @@ namespace ejson { public: static ememory::SharedPtr create(); static ememory::SharedPtr create(const std::string& _data); - /** - * @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 ...) public: @@ -46,140 +42,35 @@ namespace ejson { ememory::SharedPtr get(const std::string& _name); //! @previous const ememory::SharedPtr get(const std::string& _name) const; - //! @previous - ememory::SharedPtr operator[] (const std::string& _name) { - return get(_name); - } - //! @previous - const ememory::SharedPtr 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() const { - return m_value.getKeys(); - } + std::vector getKeys() const; /** * @brief get the number of sub element in the current one * @return the Number of stored element */ - size_t size() const { - return m_value.size(); - }; + size_t size() const; /** * @brief get the pointer on an element reference with his ID. * @param[in] _id Id of the element. * @return nullptr if the element does not exist. */ - ememory::SharedPtr get(size_t _id) { - return m_value[_id]; - }; - //! @previous - const ememory::SharedPtr get(size_t _id) const{ - return m_value[_id]; - }; - //! @previous - ememory::SharedPtr operator[] (size_t _id) { - return m_value[_id]; - } - //! @previous - const ememory::SharedPtr operator[] (size_t _id) const { - return m_value[_id]; - } + ememory::SharedPtr get(size_t _id); + /** + * @brief get the pointer on an element reference with his ID. + * @param[in] _id Id of the element. + * @return nullptr if the element does not exist. + */ + const ememory::SharedPtr get(size_t _id) const; /** * @brief Get the element name (key). * @param[in] _id Id of the element. * @return The name (key). */ - std::string getKey(size_t _id) const { - return m_value.getKey(_id); - } - /** - * @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 nullptr if it not the corect type or does not existed - */ - ememory::SharedPtr getObject(const std::string& _name); - /** - * @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 nullptr if it not the corect type or does not existed - */ - const ememory::SharedPtr 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 nullptr if it not the corect type or does not existed - */ - ememory::SharedPtr getArray(const std::string& _name); - /** - * @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 nullptr if it not the corect type or does not existed - */ - const ememory::SharedPtr 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 nullptr if it not the corect type or does not existed - */ - ememory::SharedPtr getNull(const std::string& _name); - //! @previous - const ememory::SharedPtr 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 nullptr if it not the corect type or does not existed - */ - ememory::SharedPtr getString(const std::string& _name); - //! @previous - const ememory::SharedPtr getString(const std::string& _name) const; - /** - * @brief get the sub string value of the requested element - * @param[in] _name name of the object - * @return Value of the string or an error string (empty) - */ - const std::string& getStringValue(const std::string& _name) const; - /** - * @brief get the sub string value of the requested element (with error return value) - * @param[in] _name name of the object - * @param[in] _errorValue The return value if the element does not exist. - * @return Value of the string or an error string (empty) - */ - std::string getStringValue(const std::string& _name, const std::string& _errorValue) const; - /** - * @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 nullptr if it not the corect type or does not existed - */ - ememory::SharedPtr getBoolean(const std::string& _name); - //! @previous - const ememory::SharedPtr getBoolean(const std::string& _name) const; - /** - * @brief get the sub boolean value of the requested element. - * @param[in] _name name of the object. - * @param[in] _errorValue The return value if the element does not exist. - * @return Value of the Boolean or the _errorValue; - */ - bool getBooleanValue(const std::string& _name, bool _errorValue=false) const; - /** - * @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 nullptr if it not the corect type or does not existed - */ - ememory::SharedPtr getNumber(const std::string& _name); - //! @previous - const ememory::SharedPtr getNumber(const std::string& _name) const; - /** - * @brief get the sub Number value of the requested element. - * @param[in] _name name of the object. - * @param[in] _errorValue The return value if the element does not exist. - * @return Value of the Number or the _errorValue; - */ - double getNumberValue(const std::string& _name, double _errorValue=0.0) const; + std::string getKey(size_t _id) const; public: /** * @brief add an element in the Object @@ -189,40 +80,25 @@ namespace ejson { */ bool add(const std::string& _name, ememory::SharedPtr _value); /** - * @brief add a string element in the Object (automatic creation) - * @param[in] _name name of the object - * @param[in] _value string value to add - * @return false if an error occured + * @brief Remove Value with his name + * @param[in] _name Name of the object */ - bool addString(const std::string& _name, const std::string& _value); + void remove(const std::string& _name); /** - * @brief add a "null" element in the Object (automatic creation) - * @param[in] _name name of the object - * @return false if an error occured + * @brief Remove Value with his id + * @param[in] _id Id of the element. */ - bool addNull(const std::string& _name); - /** - * @brief add a boolean element in the Object (automatic creation) - * @param[in] _name name of the object - * @param[in] _value boolean value to add - * @return false if an error occured - */ - bool addBoolean(const std::string& _name, bool _value); - /** - * @brief add a double element in the Object (automatic creation) - * @param[in] _name name of the object - * @param[in] _value double value to add - * @return false if an error occured - */ - bool addNumber(const std::string& _name, double _value); - public: // herited function : - virtual bool iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc); - virtual bool iGenerate(std::string& _data, size_t _indent) const; - virtual void clear(); - virtual bool transfertIn(ememory::SharedPtr _obj); - virtual bool cloneIn(const ememory::SharedPtr& _obj) const; - virtual ememory::SharedPtr clone() const; - virtual ememory::SharedPtr cloneObj() const; + void remove(size_t _id); + public: + bool iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc) override; + bool iGenerate(std::string& _data, size_t _indent) const override; + void clear() override; + bool transfertIn(ememory::SharedPtr _obj) override; + ememory::SharedPtr clone() const override; + + + bool cloneIn(const ememory::SharedPtr& _obj) const; + ememory::SharedPtr cloneObj() const; }; } } diff --git a/ejson/internal/String.cpp b/ejson/internal/String.cpp index 82bb9bf..d7063bb 100644 --- a/ejson/internal/String.cpp +++ b/ejson/internal/String.cpp @@ -17,6 +17,19 @@ ememory::SharedPtr ejson::internal::String::create(cons return ememory::SharedPtr(new ejson::internal::String(_value)); } +ejson::internal::String::String(const std::string& _value) : + m_value(_value) { + m_type = ejson::valueType::string; +} + +void ejson::internal::String::set(const std::string& _value) { + m_value = _value; +} + +const std::string& ejson::internal::String::get() const { + return m_value; +} + bool ejson::internal::String::iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc) { EJSON_PARSE_ELEMENT("start parse : 'String' "); char end = _data[_pos]; @@ -53,11 +66,11 @@ bool ejson::internal::String::transfertIn(ememory::SharedPtr other = _obj->toString(); - if (other == nullptr) { + if (_obj->getType() != ejson::valueType::string) { EJSON_ERROR("Request transfer on an element that is not an String"); return false; } + ememory::SharedPtr other = std::static_pointer_cast(_obj); other->m_value = m_value; m_value = ""; return true; diff --git a/ejson/internal/String.h b/ejson/internal/String.h index bbf5274..089d932 100644 --- a/ejson/internal/String.h +++ b/ejson/internal/String.h @@ -17,16 +17,9 @@ namespace ejson { /** * @brief basic element of a xml structure */ - String(const std::string& _value="") : - m_value(_value) { - m_type = ejson::valueType::string; - }; + String(const std::string& _value=""); public: static ememory::SharedPtr create(const std::string& _value=""); - /** - * @brief destructor - */ - virtual ~String() { }; protected: std::string m_value; //!< value of the node (for element this is the name, for text it is the inside text ...) public: @@ -34,21 +27,17 @@ namespace ejson { * @brief set the value of the node. * @param[in] _value New value of the node. */ - void set(const std::string& _value) { - m_value = _value; - }; + void set(const std::string& _value); /** * @brief get the current element Value. * @return the reference of the string value. */ - const std::string& get() const { - return m_value; - }; - public: // herited function : - virtual bool iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc); - virtual bool iGenerate(std::string& _data, size_t _indent) const; - virtual bool transfertIn(ememory::SharedPtr _obj); - virtual ememory::SharedPtr clone() const; + const std::string& get() const; + public: + virtual bool iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc) override; + virtual bool iGenerate(std::string& _data, size_t _indent) const override; + virtual bool transfertIn(ememory::SharedPtr _obj) override; + virtual ememory::SharedPtr clone() const override; }; } } diff --git a/ejson/internal/Value.cpp b/ejson/internal/Value.cpp index 719b302..78a84af 100644 --- a/ejson/internal/Value.cpp +++ b/ejson/internal/Value.cpp @@ -111,91 +111,12 @@ bool ejson::internal::Value::checkNumber(char32_t _val) const { return false; } -ememory::SharedPtr ejson::internal::Value::toValue() { - return shared_from_this(); -}; -const ememory::SharedPtr ejson::internal::Value::toValue() const { - return shared_from_this(); -}; -ememory::SharedPtr ejson::internal::Value::toDocument() { - return std::dynamic_pointer_cast(shared_from_this()); -}; -const ememory::SharedPtr ejson::internal::Value::toDocument() const { - return std::dynamic_pointer_cast(shared_from_this()); -}; -ememory::SharedPtr ejson::internal::Value::toArray() { - return std::dynamic_pointer_cast(shared_from_this()); -}; -const ememory::SharedPtr ejson::internal::Value::toArray() const{ - return std::dynamic_pointer_cast(shared_from_this()); -}; -ememory::SharedPtr ejson::internal::Value::toObject() { - return std::dynamic_pointer_cast(shared_from_this()); -}; -const ememory::SharedPtr ejson::internal::Value::toObject() const{ - return std::dynamic_pointer_cast(shared_from_this()); -}; -ememory::SharedPtr ejson::internal::Value::toString() { - return std::dynamic_pointer_cast(shared_from_this()); -}; -const ememory::SharedPtr ejson::internal::Value::toString() const{ - return std::dynamic_pointer_cast(shared_from_this()); -}; -ememory::SharedPtr ejson::internal::Value::toNumber() { - return std::dynamic_pointer_cast(shared_from_this()); -}; -const ememory::SharedPtr ejson::internal::Value::toNumber() const{ - return std::dynamic_pointer_cast(shared_from_this()); -}; -ememory::SharedPtr ejson::internal::Value::toBoolean() { - return std::dynamic_pointer_cast(shared_from_this()); -}; -const ememory::SharedPtr ejson::internal::Value::toBoolean() const{ - return std::dynamic_pointer_cast(shared_from_this()); -}; -ememory::SharedPtr ejson::internal::Value::toNull() { - return std::dynamic_pointer_cast(shared_from_this()); -}; -const ememory::SharedPtr ejson::internal::Value::toNull() const{ - return std::dynamic_pointer_cast(shared_from_this()); -}; - void ejson::internal::Value::display() const { std::string tmpp; iGenerate(tmpp, 0); EJSON_INFO("Generated JSON : \n" << tmpp); } - -bool ejson::internal::Value::isDocument() const { - return toDocument() != nullptr; -} - -bool ejson::internal::Value::isArray() const { - return toArray() != nullptr; -} - -bool ejson::internal::Value::isObject() const { - return toObject() != nullptr; -} - -bool ejson::internal::Value::isString() const { - return toString() != nullptr; -} - -bool ejson::internal::Value::isNumber() const { - return toNumber() != nullptr; -} - -bool ejson::internal::Value::isBoolean() const { - return toBoolean() != nullptr; -} - -bool ejson::internal::Value::isNull() const { - return toNull() != nullptr; -} - - void ejson::internal::Value::clear() { } diff --git a/ejson/internal/Value.h b/ejson/internal/Value.h index 09da44d..be0c560 100644 --- a/ejson/internal/Value.h +++ b/ejson/internal/Value.h @@ -64,18 +64,22 @@ namespace ejson { * @brief parse the Current node [pure VIRUAL] * @param[in] _data data string to parse. * @param[in,out] _pos position in the string to start parse, return the position end of parsing. - * @param[in] _caseSensitive Request a parsion of element that is not case sensitive (all element is in low case) - * @param[in,out] file parsing position (line x col x) + * @param[in,out] _filePos Position in the file (in X/Y) + * @param[in,out] _doc Reference on the main document * @return false if an error occured. */ - virtual bool iParse(const std::string& _data, size_t& _pos, ejson::FilePos& _filePos, ejson::internal::Document& _doc) = 0; + virtual bool iParse(const std::string& _data, + size_t& _pos, + ejson::FilePos& _filePos, + ejson::internal::Document& _doc) = 0; /** * @brief generate a string with the tree of the xml * @param[in,out] _data string where to add the elements - * @param[in] current indentation of the file + * @param[in] _indent current indentation of the file * @return false if an error occured. */ - virtual bool iGenerate(std::string& _data, size_t _indent) const = 0; + virtual bool iGenerate(std::string& _data, + size_t _indent) const = 0; /** * @brief Display the Document on console */ @@ -94,13 +98,17 @@ namespace ejson { */ void drawElementParsed(char32_t _val, const ejson::FilePos& _filePos) const; /** - * @brief check if an name (for object named) (not : !"#$%&'()*+,/;<=>?@[\]^`{|}~ \n\t\r). + * @brief check if an name (for object named) (not : !"#$%&'()*+,/;<=>?@[\]^`{|}~ \\n\\t\\r). * @param[in] _val Value to check the conformity. + * @return true The element char is considerable as a string + * @return false The element char is NOT considerable as a string */ bool checkString(char32_t _val) const; /** * @brief check if an number -+.0123456789e). * @param[in] _val Value to check the conformity. + * @return true The element char is considerable as a number + * @return false The element char is NOT considerable as a number */ bool checkNumber(char32_t _val) const; /** @@ -112,123 +120,6 @@ namespace ejson { */ int32_t countWhiteChar(const std::string& _data, size_t _pos, ejson::FilePos& _filePos) const; public: - /** - * @brief Cast the element in a Value if it is possible. - * @return pointer on the class or nullptr. - */ - ememory::SharedPtr toValue(); - /** - * @brief Cast the element in a Value if it is possible. - * @return CONST pointer on the class or nullptr. - */ - const ememory::SharedPtr toValue() const; - /** - * @brief Cast the element in a Document if it is possible. - * @return pointer on the class or nullptr. - */ - ememory::SharedPtr toDocument(); - /** - * @brief Cast the element in a Document if it is possible. - * @return CONST pointer on the class or nullptr. - */ - const ememory::SharedPtr toDocument() const; - /** - * @brief Cast the element in a Array if it is possible. - * @return pointer on the class or nullptr. - */ - ememory::SharedPtr toArray(); - /** - * @brief Cast the element in a Array if it is possible. - * @return CONST pointer on the class or nullptr. - */ - const ememory::SharedPtr toArray() const; - /** - * @brief Cast the element in a Object if it is possible. - * @return pointer on the class or nullptr. - */ - ememory::SharedPtr toObject(); - /** - * @brief Cast the element in a Object if it is possible. - * @return CONST pointer on the class or nullptr. - */ - const ememory::SharedPtr toObject() const; - /** - * @brief Cast the element in a String if it is possible. - * @return pointer on the class or nullptr. - */ - ememory::SharedPtr toString(); - /** - * @brief Cast the element in a String if it is possible. - * @return CONST pointer on the class or nullptr. - */ - const ememory::SharedPtr toString() const; - /** - * @brief Cast the element in a Number if it is possible. - * @return pointer on the class or nullptr. - */ - ememory::SharedPtr toNumber(); - /** - * @brief Cast the element in a Number if it is possible. - * @return CONST pointer on the class or nullptr. - */ - const ememory::SharedPtr toNumber() const; - /** - * @brief Cast the element in a Boolean if it is possible. - * @return pointer on the class or nullptr. - */ - ememory::SharedPtr toBoolean(); - /** - * @brief Cast the element in a Boolean if it is possible. - * @return CONST pointer on the class or nullptr. - */ - const ememory::SharedPtr toBoolean() const; - /** - * @brief Cast the element in a Null if it is possible. - * @return pointer on the class or nullptr. - */ - ememory::SharedPtr toNull(); - /** - * @brief Cast the element in a Null if it is possible. - * @return CONST pointer on the class or nullptr. - */ - const ememory::SharedPtr toNull() const; - - /** - * @brief check if the node is a ejson::internal::Document - * @return true if the node is a ejson::internal::Document - */ - bool isDocument() const; - /** - * @brief check if the node is a ejson::internal::Array - * @return true if the node is a ejson::internal::Array - */ - bool isArray() const; - /** - * @brief check if the node is a ejson::internal::Object - * @return true if the node is a ejson::internal::Object - */ - bool isObject() const; - /** - * @brief check if the node is a ejson::internal::String - * @return true if the node is a ejson::internal::String - */ - bool isString() const; - /** - * @brief check if the node is a ejson::internal::Number - * @return true if the node is a ejson::internal::Number - */ - bool isNumber() const; - /** - * @brief check if the node is a ejson::internal::Boolean - * @return true if the node is a ejson::internal::Boolean - */ - bool isBoolean() const; - /** - * @brief check if the node is a ejson::internal::Null - * @return true if the node is a ejson::internal::Null - */ - bool isNull() const; - /** * @brief clear the Node */ @@ -248,6 +139,7 @@ namespace ejson { protected: /** * @brief check if the current element is white or not : '\\t' '\\n' '\\r' ' ' + * @param[in] _val Char value to check * @return tue if it is white char */ static bool isWhiteChar(char32_t _val); diff --git a/ejson/iterator.h b/ejson/iterator.h index a19b4d0..b2d6a05 100644 --- a/ejson/iterator.h +++ b/ejson/iterator.h @@ -20,12 +20,26 @@ namespace ejson { EJSON_BASE_T& m_data; //!< Reference on the ejson::Element size_t m_id; //!< Id of the element that we are parsing public: + /** + * @brief Constructor of the generic object class + * @param[in] _obj Reference on the object to go threw + * @param[in] _pos Position in the object + */ iterator(EJSON_BASE_T& _obj, size_t _pos); + /** + * @brief Const constructor of the generic const object class + * @param[in] _obj Reference on the object to go threw + * @param[in] _pos Position in the object + */ iterator(const EJSON_BASE_T& _obj, size_t _pos); + /** + * @brief Copy iterator + * @param[in] _obj Iterator to copy + */ iterator(const iterator& _obj); /** * @brief Operator+= Addition value - * @param[in] _val Value to addition + * @param[in] _obj Value to addition * @return Local reference of the iterator additionned */ iterator& operator= (const iterator& _obj); @@ -97,6 +111,11 @@ namespace ejson { * @return Const reference on the value. */ ejson::Value operator *() noexcept; + /** + * @brief Get ID of an element + * @return Position in the Element + */ + size_t getId() const noexcept; /** * @brief Get Key of an element * @return Key of the Element diff --git a/sample/write.cpp b/sample/write.cpp index 341c224..9e72854 100644 --- a/sample/write.cpp +++ b/sample/write.cpp @@ -89,12 +89,12 @@ static void writeAll() { // remove the object named "F" //! [ejson_sample_write_rm_object] - //doc.remove("F"); + doc.remove("F"); //! [ejson_sample_write_rm_object] // Remove element 2 in the array //! [ejson_sample_write_rm_array] - //array.remove(2); + array.remove(2); //! [ejson_sample_write_rm_array] doc.display(); }