[DEV] update to the new c++x11 interface shared_ptr

This commit is contained in:
Edouard DUPIN 2015-01-12 21:05:19 +01:00
parent bcab8b0706
commit 63a83873f8
17 changed files with 422 additions and 400 deletions

View File

@ -20,14 +20,11 @@
#define __class__ "Array" #define __class__ "Array"
std::shared_ptr<ejson::Array> ejson::Array::create() {
return std::shared_ptr<ejson::Array>(new ejson::Array());
}
void ejson::Array::clear() { void ejson::Array::clear() {
for (size_t iii=0; iii<m_value.size(); ++iii) {
if (NULL == m_value[iii]) {
continue;
}
delete(m_value[iii]);
m_value[iii] = NULL;
}
m_value.clear(); m_value.clear();
} }
@ -59,8 +56,8 @@ bool ejson::Array::iParse(const std::string& _data, size_t& _pos, ejson::filePos
} else if (_data[iii] == '{') { } else if (_data[iii] == '{') {
// find an object: // find an object:
JSON_PARSE_ELEMENT("find Object"); JSON_PARSE_ELEMENT("find Object");
ejson::Object * tmpElement = new ejson::Object(); std::shared_ptr<ejson::Object> tmpElement = ejson::Object::create();
if (NULL == tmpElement) { if (tmpElement == nullptr) {
EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in object"); EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in object");
_pos=iii; _pos=iii;
return false; return false;
@ -71,8 +68,8 @@ bool ejson::Array::iParse(const std::string& _data, size_t& _pos, ejson::filePos
|| _data[iii] == '\'') { || _data[iii] == '\'') {
// find a string: // find a string:
JSON_PARSE_ELEMENT("find String quoted"); JSON_PARSE_ELEMENT("find String quoted");
ejson::String * tmpElement = new ejson::String(); std::shared_ptr<ejson::String> tmpElement = ejson::String::create();
if (NULL == tmpElement) { if (tmpElement == nullptr) {
EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in String"); EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in String");
_pos=iii; _pos=iii;
return false; return false;
@ -82,8 +79,8 @@ bool ejson::Array::iParse(const std::string& _data, size_t& _pos, ejson::filePos
} else if (_data[iii] == '[') { } else if (_data[iii] == '[') {
// find a list: // find a list:
JSON_PARSE_ELEMENT("find List"); JSON_PARSE_ELEMENT("find List");
ejson::Array * tmpElement = new ejson::Array(); std::shared_ptr<ejson::Array> tmpElement = ejson::Array::create();
if (NULL == tmpElement) { if (tmpElement == nullptr) {
EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Array"); EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Array");
_pos=iii; _pos=iii;
return false; return false;
@ -94,8 +91,8 @@ bool ejson::Array::iParse(const std::string& _data, size_t& _pos, ejson::filePos
|| _data[iii] == 't' ) { || _data[iii] == 't' ) {
// find boolean: // find boolean:
JSON_PARSE_ELEMENT("find Boolean"); JSON_PARSE_ELEMENT("find Boolean");
ejson::Boolean * tmpElement = new ejson::Boolean(); std::shared_ptr<ejson::Boolean> tmpElement = ejson::Boolean::create();
if (NULL == tmpElement) { if (tmpElement == nullptr) {
EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Boolean"); EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Boolean");
_pos=iii; _pos=iii;
return false; return false;
@ -105,8 +102,8 @@ bool ejson::Array::iParse(const std::string& _data, size_t& _pos, ejson::filePos
} else if( _data[iii] == 'n') { } else if( _data[iii] == 'n') {
// find null: // find null:
JSON_PARSE_ELEMENT("find Null"); JSON_PARSE_ELEMENT("find Null");
ejson::Null * tmpElement = new ejson::Null(); std::shared_ptr<ejson::Null> tmpElement = ejson::Null::create();
if (NULL == tmpElement) { if (tmpElement == nullptr) {
EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Boolean"); EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Boolean");
_pos=iii; _pos=iii;
return false; 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])) { } else if(true == checkNumber(_data[iii])) {
// find number: // find number:
JSON_PARSE_ELEMENT("find Number"); JSON_PARSE_ELEMENT("find Number");
ejson::Number * tmpElement = new ejson::Number(); std::shared_ptr<ejson::Number> tmpElement = ejson::Number::create();
if (NULL == tmpElement) { if (tmpElement == nullptr) {
EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Boolean"); EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Boolean");
_pos=iii; _pos=iii;
return false; return false;
@ -146,8 +143,8 @@ bool ejson::Array::iGenerate(std::string& _data, size_t _indent) const {
oneLine=false; oneLine=false;
} else { } else {
for (size_t iii=0; iii<m_value.size() ; iii++) { for (size_t iii=0; iii<m_value.size() ; iii++) {
ejson::Value* tmp = m_value[iii]; std::shared_ptr<const ejson::Value> tmp = m_value[iii];
if (tmp == NULL) { if (tmp == nullptr) {
continue; continue;
} }
if (true == tmp->isObject()) { if (true == tmp->isObject()) {
@ -159,8 +156,8 @@ bool ejson::Array::iGenerate(std::string& _data, size_t _indent) const {
break; break;
} }
if (true == tmp->isString()) { if (true == tmp->isString()) {
ejson::String* tmp2 = tmp->toString(); std::shared_ptr<const ejson::String> tmp2 = tmp->toString();
if (NULL!=tmp2) { if (tmp2 != nullptr) {
if(tmp2->get().size()>40) { if(tmp2->get().size()>40) {
oneLine=false; oneLine=false;
break; break;
@ -178,7 +175,7 @@ bool ejson::Array::iGenerate(std::string& _data, size_t _indent) const {
if (false == oneLine) { if (false == oneLine) {
addIndent(_data, _indent); addIndent(_data, _indent);
} }
if (NULL != m_value[iii]) { if (m_value[iii] != nullptr) {
m_value[iii]->iGenerate(_data, _indent+1); m_value[iii]->iGenerate(_data, _indent+1);
if (iii<m_value.size()-1) { if (iii<m_value.size()-1) {
_data += ","; _data += ",";
@ -197,9 +194,9 @@ bool ejson::Array::iGenerate(std::string& _data, size_t _indent) const {
return true; return true;
} }
bool ejson::Array::add(ejson::Value* _element) { bool ejson::Array::add(std::shared_ptr<ejson::Value> _element) {
if (NULL == _element) { if (_element == nullptr) {
JSON_ERROR("Request add on an NULL pointer"); JSON_ERROR("Request add on an nullptr pointer");
return false; return false;
} }
m_value.push_back(_element); m_value.push_back(_element);
@ -207,29 +204,29 @@ bool ejson::Array::add(ejson::Value* _element) {
} }
bool ejson::Array::addString(const std::string& _value) { bool ejson::Array::addString(const std::string& _value) {
return add(new ejson::String(_value)); return add(ejson::String::create(_value));
} }
bool ejson::Array::addNull() { bool ejson::Array::addNull() {
return add(new ejson::Null()); return add(ejson::Null::create());
} }
bool ejson::Array::addBoolean(bool _value) { bool ejson::Array::addBoolean(bool _value) {
return add(new ejson::Boolean(_value)); return add(ejson::Boolean::create(_value));
} }
bool ejson::Array::addNumber(double _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) { bool ejson::Array::transfertIn(std::shared_ptr<ejson::Value> _obj) {
if (NULL == _obj) { if (_obj == nullptr) {
JSON_ERROR("Request transfer on an NULL pointer"); JSON_ERROR("Request transfer on an nullptr pointer");
return false; return false;
} }
ejson::Array* other = _obj->toArray(); std::shared_ptr<ejson::Array> other = _obj->toArray();
if (NULL == other) { if (other == nullptr) {
JSON_ERROR("Request transfer on an element that is not an array"); JSON_ERROR("Request transfer on an element that is not an array");
return false; return false;
} }
@ -243,15 +240,15 @@ bool ejson::Array::transfertIn(ejson::Value* _obj) {
} }
// TODO : Manage error ... // TODO : Manage error ...
ejson::Value* ejson::Array::duplicate() const { std::shared_ptr<ejson::Value> ejson::Array::duplicate() const {
ejson::Array* output = new ejson::Array(); std::shared_ptr<ejson::Array> output = ejson::Array::create();
if (NULL == output) { if (output == nullptr) {
JSON_ERROR("Allocation error ..."); JSON_ERROR("Allocation error ...");
return NULL; return nullptr;
} }
for (size_t iii=0; iii<m_value.size(); ++iii) { for (size_t iii=0; iii<m_value.size(); ++iii) {
ejson::Value* val = m_value[iii]; std::shared_ptr<const ejson::Value> val = m_value[iii];
if (NULL == val) { if (val == nullptr) {
continue; continue;
} }
output->add(val->duplicate()); output->add(val->duplicate());
@ -259,104 +256,104 @@ ejson::Value* ejson::Array::duplicate() const {
return output; return output;
} }
ejson::Object* ejson::Array::getObject(size_t _id) { std::shared_ptr<ejson::Object> ejson::Array::getObject(size_t _id) {
ejson::Value* tmpElement = m_value[_id]; std::shared_ptr<ejson::Value> tmpElement = m_value[_id];
if (NULL == tmpElement) { if (tmpElement == nullptr) {
return NULL; return nullptr;
} }
return tmpElement->toObject(); return tmpElement->toObject();
} }
const ejson::Object* ejson::Array::getObject(size_t _id) const { const std::shared_ptr<const ejson::Object> ejson::Array::getObject(size_t _id) const {
const ejson::Value* tmpElement = m_value[_id]; const std::shared_ptr<const ejson::Value> tmpElement = m_value[_id];
if (NULL == tmpElement) { if (tmpElement == nullptr) {
return NULL; return nullptr;
} }
return tmpElement->toObject(); return tmpElement->toObject();
} }
ejson::String* ejson::Array::getString(size_t _id) { std::shared_ptr<ejson::String> ejson::Array::getString(size_t _id) {
ejson::Value* tmpElement = m_value[_id]; std::shared_ptr<ejson::Value> tmpElement = m_value[_id];
if (NULL == tmpElement) { if (tmpElement == nullptr) {
return NULL; return nullptr;
} }
return tmpElement->toString(); return tmpElement->toString();
} }
const ejson::String* ejson::Array::getString(size_t _id) const { const std::shared_ptr<const ejson::String> ejson::Array::getString(size_t _id) const {
const ejson::Value* tmpElement = m_value[_id]; const std::shared_ptr<const ejson::Value> tmpElement = m_value[_id];
if (NULL == tmpElement) { if (tmpElement == nullptr) {
return NULL; return nullptr;
} }
return tmpElement->toString(); return tmpElement->toString();
} }
ejson::Array* ejson::Array::getArray(size_t _id) { std::shared_ptr<ejson::Array> ejson::Array::getArray(size_t _id) {
ejson::Value* tmpElement = m_value[_id]; std::shared_ptr<ejson::Value> tmpElement = m_value[_id];
if (NULL == tmpElement) { if (tmpElement == nullptr) {
return NULL; return nullptr;
} }
return tmpElement->toArray(); return tmpElement->toArray();
} }
const ejson::Array* ejson::Array::getArray(size_t _id) const { const std::shared_ptr<const ejson::Array> ejson::Array::getArray(size_t _id) const {
const ejson::Value* tmpElement = m_value[_id]; const std::shared_ptr<const ejson::Value> tmpElement = m_value[_id];
if (NULL == tmpElement) { if (tmpElement == nullptr) {
return NULL; return nullptr;
} }
return tmpElement->toArray(); return tmpElement->toArray();
} }
ejson::Null* ejson::Array::getNull(size_t _id) { std::shared_ptr<ejson::Null> ejson::Array::getNull(size_t _id) {
ejson::Value* tmpElement = m_value[_id]; std::shared_ptr<ejson::Value> tmpElement = m_value[_id];
if (NULL == tmpElement) { if (tmpElement == nullptr) {
return NULL; return nullptr;
} }
return tmpElement->toNull(); return tmpElement->toNull();
} }
const ejson::Null* ejson::Array::getNull(size_t _id) const { const std::shared_ptr<const ejson::Null> ejson::Array::getNull(size_t _id) const {
const ejson::Value* tmpElement = m_value[_id]; const std::shared_ptr<const ejson::Value> tmpElement = m_value[_id];
if (NULL == tmpElement) { if (tmpElement == nullptr) {
return NULL; return nullptr;
} }
return tmpElement->toNull(); return tmpElement->toNull();
} }
ejson::Number* ejson::Array::getNumber(size_t _id) { std::shared_ptr<ejson::Number> ejson::Array::getNumber(size_t _id) {
ejson::Value* tmpElement = m_value[_id]; std::shared_ptr<ejson::Value> tmpElement = m_value[_id];
if (NULL == tmpElement) { if (tmpElement == nullptr) {
return NULL; return nullptr;
} }
return tmpElement->toNumber(); return tmpElement->toNumber();
} }
const ejson::Number* ejson::Array::getNumber(size_t _id) const { const std::shared_ptr<const ejson::Number> ejson::Array::getNumber(size_t _id) const {
const ejson::Value* tmpElement = m_value[_id]; const std::shared_ptr<const ejson::Value> tmpElement = m_value[_id];
if (NULL == tmpElement) { if (tmpElement == nullptr) {
return NULL; return nullptr;
} }
return tmpElement->toNumber(); return tmpElement->toNumber();
} }
ejson::Boolean* ejson::Array::getBoolean(size_t _id) { std::shared_ptr<ejson::Boolean> ejson::Array::getBoolean(size_t _id) {
ejson::Value* tmpElement = m_value[_id]; std::shared_ptr<ejson::Value> tmpElement = m_value[_id];
if (NULL == tmpElement) { if (tmpElement == nullptr) {
return NULL; return nullptr;
} }
return tmpElement->toBoolean(); return tmpElement->toBoolean();
} }
const ejson::Boolean* ejson::Array::getBoolean(size_t _id) const { const std::shared_ptr<const ejson::Boolean> ejson::Array::getBoolean(size_t _id) const {
const ejson::Value* tmpElement = m_value[_id]; const std::shared_ptr<const ejson::Value> tmpElement = m_value[_id];
if (NULL == tmpElement) { if (tmpElement == nullptr) {
return NULL; return nullptr;
} }
return tmpElement->toBoolean(); return tmpElement->toBoolean();
} }
std::string ejson::Array::getStringValue(size_t _id) { std::string ejson::Array::getStringValue(size_t _id) {
ejson::String* tmpElement = getString(_id); std::shared_ptr<ejson::String> tmpElement = getString(_id);
if (NULL == tmpElement) { if (tmpElement == nullptr) {
return ""; return "";
} }
return tmpElement->get(); 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 { const std::string& ejson::Array::getStringValue(size_t _id) const {
static const std::string errorValue(""); static const std::string errorValue("");
const ejson::String* tmpElement = getString(_id); const std::shared_ptr<const ejson::String> tmpElement = getString(_id);
if (NULL == tmpElement) { if (tmpElement == nullptr) {
return errorValue; return errorValue;
} }
return tmpElement->get(); return tmpElement->get();
} }
std::string ejson::Array::getStringValue(size_t _id, const std::string& _errorValue) const { std::string ejson::Array::getStringValue(size_t _id, const std::string& _errorValue) const {
const ejson::String* tmpElement = getString(_id); const std::shared_ptr<const ejson::String> tmpElement = getString(_id);
if (NULL == tmpElement) { if (tmpElement == nullptr) {
return _errorValue; return _errorValue;
} }
return tmpElement->get(); return tmpElement->get();
} }
double ejson::Array::getNumberValue(size_t _id, double _errorValue) const { double ejson::Array::getNumberValue(size_t _id, double _errorValue) const {
const ejson::Number* tmpElement = getNumber(_id); const std::shared_ptr<const ejson::Number> tmpElement = getNumber(_id);
if (NULL == tmpElement) { if (tmpElement == nullptr) {
return _errorValue; return _errorValue;
} }
return tmpElement->get(); return tmpElement->get();
} }
bool ejson::Array::getBooleanValue(size_t _id, bool _errorValue) const { bool ejson::Array::getBooleanValue(size_t _id, bool _errorValue) const {
const ejson::Boolean* tmpElement = getBoolean(_id); const std::shared_ptr<const ejson::Boolean> tmpElement = getBoolean(_id);
if (NULL == tmpElement) { if (tmpElement == nullptr) {
return _errorValue; return _errorValue;
} }
return tmpElement->get(); return tmpElement->get();

View File

@ -14,17 +14,19 @@
namespace ejson { namespace ejson {
class Array : public ejson::Value { class Array : public ejson::Value {
public: protected:
/** /**
* @brief basic element of a xml structure * @brief basic element of a xml structure
*/ */
Array() { }; Array() { };
public:
static std::shared_ptr<Array> create();
/** /**
* @brief destructor * @brief destructor
*/ */
virtual ~Array() { }; virtual ~Array() { };
private: private:
std::vector<ejson::Value*> m_value; //!< vector of sub elements std::vector<std::shared_ptr<ejson::Value>> m_value; //!< vector of sub elements
public: public:
/** /**
* @brief get the number of sub element in the current one * @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. * @brief get the pointer on an element reference with his ID.
* @param[in] _id Id of the element. * @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<ejson::Value> get(size_t _id) {
return m_value[_id]; return m_value[_id];
}; };
//! @previous //! @previous
const ejson::Value* get(size_t _id) const{ const std::shared_ptr<const ejson::Value> get(size_t _id) const{
return m_value[_id]; return m_value[_id];
}; };
//! @previous //! @previous
ejson::Value* operator[] (size_t _id) { std::shared_ptr<ejson::Value> operator[] (size_t _id) {
return m_value[_id]; return m_value[_id];
} }
//! @previous //! @previous
const ejson::Value* operator[] (size_t _id) const { const std::shared_ptr<const ejson::Value> operator[] (size_t _id) const {
return m_value[_id]; return m_value[_id];
} }
/** /**
* @brief get the pointer on an element reference with his ID (casted in Object if it is an object). * @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. * @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<ejson::Object> getObject(size_t _id);
//! @previous //! @previous
const ejson::Object* getObject(size_t _id) const; const std::shared_ptr<const ejson::Object> getObject(size_t _id) const;
/** /**
* @brief get the pointer on an element reference with his ID (casted in String if it is an String). * @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. * @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<ejson::String> getString(size_t _id);
//! @previous //! @previous
const ejson::String* getString(size_t _id) const; const std::shared_ptr<const ejson::String> getString(size_t _id) const;
/** /**
* @brief get the value of the string element (if not a string return "") * @brief get the value of the string element (if not a string return "")
* @param[in] _id Id of the element. * @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). * @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. * @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<ejson::Array> getArray(size_t _id);
//! @previous //! @previous
const ejson::Array* getArray(size_t _id) const; const std::shared_ptr<const ejson::Array> getArray(size_t _id) const;
/** /**
* @brief get the pointer on an element reference with his ID (casted in Null if it is an Null). * @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. * @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<ejson::Null> getNull(size_t _id);
//! @previous //! @previous
const ejson::Null* getNull(size_t _id) const; const std::shared_ptr<const ejson::Null> getNull(size_t _id) const;
/** /**
* @brief get the pointer on an element reference with his ID (casted in Number if it is an Number). * @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. * @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<ejson::Number> getNumber(size_t _id);
//! @previous //! @previous
const ejson::Number* getNumber(size_t _id) const; const std::shared_ptr<const ejson::Number> getNumber(size_t _id) const;
/** /**
* @brief get the value of the Number element * @brief get the value of the Number element
* @param[in] _id Id of the 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). * @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. * @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<ejson::Boolean> getBoolean(size_t _id);
//! @previous //! @previous
const ejson::Boolean* getBoolean(size_t _id) const; const std::shared_ptr<const ejson::Boolean> getBoolean(size_t _id) const;
/** /**
* @brief get the value of the Boolean element * @brief get the value of the Boolean element
* @param[in] _id Id of the element. * @param[in] _id Id of the element.
@ -135,7 +137,7 @@ namespace ejson {
* @param[in] _element element to add. * @param[in] _element element to add.
* @return false if an error occured. * @return false if an error occured.
*/ */
bool add(ejson::Value* _element); bool add(std::shared_ptr<ejson::Value> _element);
/** /**
* @brief add a string element in the Object (automatic creation) * @brief add a string element in the Object (automatic creation)
* @param[in] _value string value to add * @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 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 iGenerate(std::string& _data, size_t _indent) const;
virtual void clear(); virtual void clear();
virtual bool transfertIn(ejson::Value* _obj); virtual bool transfertIn(std::shared_ptr<ejson::Value> _obj);
virtual ejson::Value* duplicate() const; virtual std::shared_ptr<ejson::Value> duplicate() const;
}; };
}; };

View File

@ -13,6 +13,11 @@
#undef __class__ #undef __class__
#define __class__ "Boolean" #define __class__ "Boolean"
std::shared_ptr<ejson::Boolean> ejson::Boolean::create(bool _value) {
return std::shared_ptr<ejson::Boolean>(new ejson::Boolean(_value));
}
bool ejson::Boolean::iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc) { bool ejson::Boolean::iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc) {
JSON_PARSE_ELEMENT("start parse : 'Boolean' "); JSON_PARSE_ELEMENT("start parse : 'Boolean' ");
m_value=false; 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) { bool ejson::Boolean::transfertIn(std::shared_ptr<ejson::Value> _obj) {
if (NULL == _obj) { if (_obj == nullptr) {
JSON_ERROR("Request transfer on an NULL pointer"); JSON_ERROR("Request transfer on an NULL pointer");
return false; return false;
} }
ejson::Boolean* other = _obj->toBoolean(); std::shared_ptr<ejson::Boolean> other = _obj->toBoolean();
if (NULL == other) { if (other == nullptr) {
JSON_ERROR("Request transfer on an element that is not an Boolean"); JSON_ERROR("Request transfer on an element that is not an Boolean");
return false; return false;
} }
@ -68,11 +73,11 @@ bool ejson::Boolean::transfertIn(ejson::Value* _obj) {
return true; return true;
} }
ejson::Value* ejson::Boolean::duplicate() const { std::shared_ptr<ejson::Value> ejson::Boolean::duplicate() const {
ejson::Boolean* output = new ejson::Boolean(m_value); std::shared_ptr<ejson::Boolean> output = ejson::Boolean::create(m_value);
if (NULL == output) { if (output == nullptr) {
JSON_ERROR("Allocation error ..."); JSON_ERROR("Allocation error ...");
return NULL; return nullptr;
} }
return output; return output;
} }

View File

@ -14,7 +14,7 @@
namespace ejson { namespace ejson {
class Boolean : public ejson::Value { class Boolean : public ejson::Value {
public: protected:
/** /**
* @brief basic element of a xml structure * @brief basic element of a xml structure
*/ */
@ -22,6 +22,8 @@ namespace ejson {
m_value(_value) { m_value(_value) {
}; };
public:
static std::shared_ptr<Boolean> create(bool _value=false);
/** /**
* @brief destructor * @brief destructor
*/ */
@ -48,8 +50,8 @@ namespace ejson {
public: // herited function : public: // herited function :
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc); 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 iGenerate(std::string& _data, size_t _indent) const;
virtual bool transfertIn(ejson::Value* _obj); virtual bool transfertIn(std::shared_ptr<ejson::Value> _obj);
virtual ejson::Value* duplicate() const; virtual std::shared_ptr<ejson::Value> duplicate() const;
}; };
}; };

View File

@ -14,6 +14,11 @@
#undef __class__ #undef __class__
#define __class__ "Null" #define __class__ "Null"
std::shared_ptr<ejson::Null> ejson::Null::create() {
return std::shared_ptr<ejson::Null>(new ejson::Null());
}
bool ejson::Null::iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc) { bool ejson::Null::iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc) {
JSON_PARSE_ELEMENT("start parse : 'Null' "); JSON_PARSE_ELEMENT("start parse : 'Null' ");
if (_pos+3 >= _data.size()){ 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) { bool ejson::Null::transfertIn(std::shared_ptr<ejson::Value> _obj) {
if (NULL == _obj) { if (_obj == nullptr) {
JSON_ERROR("Request transfer on an NULL pointer"); JSON_ERROR("Request transfer on an nullptr pointer");
return false; return false;
} }
ejson::Null* other = _obj->toNull(); std::shared_ptr<ejson::Null> other = _obj->toNull();
if (NULL == other) { if (other == nullptr) {
JSON_ERROR("Request transfer on an element that is not an Null"); JSON_ERROR("Request transfer on an element that is not an Null");
return false; return false;
} }
return true; return true;
} }
ejson::Value* ejson::Null::duplicate() const { std::shared_ptr<ejson::Value> ejson::Null::duplicate() const {
ejson::Null* output = new ejson::Null(); std::shared_ptr<ejson::Null> output = ejson::Null::create();
if (NULL == output) { if (output == nullptr) {
JSON_ERROR("Allocation error ..."); JSON_ERROR("Allocation error ...");
return NULL; return nullptr;
} }
return output; return output;
} }

View File

@ -14,11 +14,13 @@
namespace ejson { namespace ejson {
class Null : public ejson::Value { class Null : public ejson::Value {
public: protected:
/** /**
* @brief basic element of a xml structure * @brief basic element of a xml structure
*/ */
Null() { }; Null() { };
public:
static std::shared_ptr<Null> create();
/** /**
* @brief destructor * @brief destructor
*/ */
@ -26,8 +28,8 @@ namespace ejson {
public: // herited function : public: // herited function :
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc); 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 iGenerate(std::string& _data, size_t _indent) const;
virtual bool transfertIn(ejson::Value* _obj); virtual bool transfertIn(std::shared_ptr<ejson::Value> _obj);
virtual ejson::Value* duplicate() const; virtual std::shared_ptr<ejson::Value> duplicate() const;
}; };
}; };

View File

@ -14,6 +14,10 @@
#undef __class__ #undef __class__
#define __class__ "Number" #define __class__ "Number"
std::shared_ptr<ejson::Number> ejson::Number::create(double _value) {
return std::shared_ptr<ejson::Number>(new ejson::Number(_value));
}
bool ejson::Number::iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc) { bool ejson::Number::iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc) {
JSON_PARSE_ELEMENT("start parse : 'Number' "); JSON_PARSE_ELEMENT("start parse : 'Number' ");
std::string tmpVal; 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) { bool ejson::Number::transfertIn(std::shared_ptr<ejson::Value> _obj) {
if (NULL == _obj) { if (_obj == nullptr) {
JSON_ERROR("Request transfer on an NULL pointer"); JSON_ERROR("Request transfer on an nullptr pointer");
return false; return false;
} }
ejson::Number* other = _obj->toNumber(); std::shared_ptr<ejson::Number> other = _obj->toNumber();
if (NULL == other) { if (other == nullptr) {
JSON_ERROR("Request transfer on an element that is not an Number"); JSON_ERROR("Request transfer on an element that is not an Number");
return false; return false;
} }
@ -65,11 +69,11 @@ bool ejson::Number::transfertIn(ejson::Value* _obj) {
return true; return true;
} }
ejson::Value* ejson::Number::duplicate() const { std::shared_ptr<ejson::Value> ejson::Number::duplicate() const {
ejson::Number* output = new ejson::Number(m_value); std::shared_ptr<ejson::Number> output = ejson::Number::create(m_value);
if (NULL == output) { if (output == nullptr) {
JSON_ERROR("Allocation error ..."); JSON_ERROR("Allocation error ...");
return NULL; return nullptr;
} }
return output; return output;
} }

View File

@ -22,6 +22,8 @@ namespace ejson {
m_value(_value) { m_value(_value) {
}; };
public:
static std::shared_ptr<Number> create(double _value=0.0);
/** /**
* @brief destructor * @brief destructor
*/ */
@ -60,8 +62,8 @@ namespace ejson {
public: // herited function : public: // herited function :
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc); 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 iGenerate(std::string& _data, size_t _indent) const;
virtual bool transfertIn(ejson::Value* _obj); virtual bool transfertIn(std::shared_ptr<ejson::Value> _obj);
virtual ejson::Value* duplicate() const; virtual std::shared_ptr<ejson::Value> duplicate() const;
}; };
}; };

View File

@ -19,14 +19,13 @@
#undef __class__ #undef __class__
#define __class__ "Object" #define __class__ "Object"
std::shared_ptr<ejson::Object> ejson::Object::create() {
return std::shared_ptr<ejson::Object>(new ejson::Object());
}
void ejson::Object::clear() { void ejson::Object::clear() {
for (int32_t iii=0; iii<m_value.size(); ++iii) {
if (NULL == m_value[iii]) {
continue;
}
delete(m_value[iii]);
m_value[iii] = NULL;
}
m_value.clear(); m_value.clear();
} }
@ -122,8 +121,8 @@ bool ejson::Object::iParse(const std::string& _data, size_t& _pos, ejson::filePo
if (_data[iii] == '{') { if (_data[iii] == '{') {
// find an object: // find an object:
JSON_PARSE_ELEMENT("find Object"); JSON_PARSE_ELEMENT("find Object");
ejson::Object * tmpElement = new ejson::Object(); std::shared_ptr<ejson::Object> tmpElement = ejson::Object::create();
if (NULL == tmpElement) { if (tmpElement == nullptr) {
EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in object"); EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in object");
_pos=iii; _pos=iii;
return false; return false;
@ -135,8 +134,8 @@ bool ejson::Object::iParse(const std::string& _data, size_t& _pos, ejson::filePo
|| _data[iii] == '\'') { || _data[iii] == '\'') {
// find a string: // find a string:
JSON_PARSE_ELEMENT("find String quoted"); JSON_PARSE_ELEMENT("find String quoted");
ejson::String * tmpElement = new ejson::String(); std::shared_ptr<ejson::String> tmpElement = ejson::String::create();
if (NULL == tmpElement) { if (tmpElement == nullptr) {
EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in String"); EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in String");
_pos=iii; _pos=iii;
return false; return false;
@ -147,8 +146,8 @@ bool ejson::Object::iParse(const std::string& _data, size_t& _pos, ejson::filePo
} else if (_data[iii] == '[') { } else if (_data[iii] == '[') {
// find a list: // find a list:
JSON_PARSE_ELEMENT("find List"); JSON_PARSE_ELEMENT("find List");
ejson::Array * tmpElement = new ejson::Array(); std::shared_ptr<ejson::Array> tmpElement = ejson::Array::create();
if (NULL == tmpElement) { if (tmpElement == nullptr) {
EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Array"); EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Array");
_pos=iii; _pos=iii;
return false; return false;
@ -160,8 +159,8 @@ bool ejson::Object::iParse(const std::string& _data, size_t& _pos, ejson::filePo
|| _data[iii] == 't' ) { || _data[iii] == 't' ) {
// find boolean: // find boolean:
JSON_PARSE_ELEMENT("find Boolean"); JSON_PARSE_ELEMENT("find Boolean");
ejson::Boolean * tmpElement = new ejson::Boolean(); std::shared_ptr<ejson::Boolean> tmpElement = ejson::Boolean::create();
if (NULL == tmpElement) { if (tmpElement == nullptr) {
EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Boolean"); EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Boolean");
_pos=iii; _pos=iii;
return false; return false;
@ -172,8 +171,8 @@ bool ejson::Object::iParse(const std::string& _data, size_t& _pos, ejson::filePo
} else if( _data[iii] == 'n') { } else if( _data[iii] == 'n') {
// find null: // find null:
JSON_PARSE_ELEMENT("find Null"); JSON_PARSE_ELEMENT("find Null");
ejson::Null * tmpElement = new ejson::Null(); std::shared_ptr<ejson::Null> tmpElement = ejson::Null::create();
if (NULL == tmpElement) { if (tmpElement == nullptr) {
EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Boolean"); EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Boolean");
_pos=iii; _pos=iii;
return false; 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])) { } else if(true == checkNumber(_data[iii])) {
// find number: // find number:
JSON_PARSE_ELEMENT("find Number"); JSON_PARSE_ELEMENT("find Number");
ejson::Number * tmpElement = new ejson::Number(); std::shared_ptr<ejson::Number> tmpElement = ejson::Number::create();
if (NULL == tmpElement) { if (tmpElement == nullptr) {
EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Boolean"); EJSON_CREATE_ERROR(_doc, _data, iii, _filePos, "Allocation error in Boolean");
_pos=iii; _pos=iii;
return false; return false;
@ -221,20 +220,20 @@ bool ejson::Object::iGenerate(std::string& _data, size_t _indent) const {
oneLine=false; oneLine=false;
} else { } else {
for (int32_t iii=0; iii<m_value.size() ; iii++) { for (int32_t iii=0; iii<m_value.size() ; iii++) {
ejson::Value* tmp = m_value[iii]; std::shared_ptr<ejson::Value> tmp = m_value[iii];
if (tmp == NULL) { if (tmp == nullptr) {
continue; continue;
} }
if (true == tmp->isObject()) { if (tmp->isObject() == true) {
oneLine=false; oneLine=false;
break; break;
} }
if (true == tmp->isArray()) { if (tmp->isArray() == true) {
oneLine=false; oneLine=false;
break; break;
} }
if (true == tmp->isString()) { if (tmp->isString() == true) {
ejson::String* tmp2 = tmp->toString(); std::shared_ptr<ejson::String> tmp2 = tmp->toString();
if (tmp2 != nullptr) { if (tmp2 != nullptr) {
if( tmp2->get().size()>25 if( tmp2->get().size()>25
|| m_value.getKey(iii).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); return m_value.exist(_name);
} }
ejson::Value* ejson::Object::get(const std::string& _name) { std::shared_ptr<ejson::Value> ejson::Object::get(const std::string& _name) {
if (false == m_value.exist(_name)) { if (false == m_value.exist(_name)) {
return NULL; return nullptr;
} }
return m_value[_name]; return m_value[_name];
} }
const ejson::Value* ejson::Object::get(const std::string& _name) const { const std::shared_ptr<const ejson::Value> ejson::Object::get(const std::string& _name) const {
if (false == m_value.exist(_name)) { if (false == m_value.exist(_name)) {
return NULL; return nullptr;
} }
return m_value[_name]; return m_value[_name];
} }
ejson::Object* ejson::Object::getObject(const std::string& _name) { std::shared_ptr<ejson::Object> ejson::Object::getObject(const std::string& _name) {
ejson::Value* tmp = get(_name); std::shared_ptr<ejson::Value> tmp = get(_name);
if (NULL == tmp) { if (tmp == nullptr) {
return NULL; return nullptr;
} }
return dynamic_cast<ejson::Object*>(tmp); return std::dynamic_pointer_cast<ejson::Object>(tmp);
} }
const ejson::Object* ejson::Object::getObject(const std::string& _name) const { const std::shared_ptr<const ejson::Object> ejson::Object::getObject(const std::string& _name) const {
const ejson::Value* tmp = get(_name); const std::shared_ptr<const ejson::Value> tmp = get(_name);
if (NULL == tmp) { if (tmp == nullptr) {
return NULL; return nullptr;
} }
return dynamic_cast<const ejson::Object*>(tmp); return std::dynamic_pointer_cast<const ejson::Object>(tmp);
} }
ejson::Array* ejson::Object::getArray(const std::string& _name) { std::shared_ptr<ejson::Array> ejson::Object::getArray(const std::string& _name) {
ejson::Value* tmp = get(_name); std::shared_ptr<ejson::Value> tmp = get(_name);
if (NULL == tmp) { if (tmp == nullptr) {
return NULL; return nullptr;
} }
return dynamic_cast<ejson::Array*>(tmp); return std::dynamic_pointer_cast<ejson::Array>(tmp);
} }
const ejson::Array* ejson::Object::getArray(const std::string& _name) const { const std::shared_ptr<const ejson::Array> ejson::Object::getArray(const std::string& _name) const {
const ejson::Value* tmp = get(_name); const std::shared_ptr<const ejson::Value> tmp = get(_name);
if (NULL == tmp) { if (tmp == nullptr) {
return NULL; return nullptr;
} }
return dynamic_cast<const ejson::Array*>(tmp); return std::dynamic_pointer_cast<const ejson::Array>(tmp);
} }
ejson::Null* ejson::Object::getNull(const std::string& _name) { std::shared_ptr<ejson::Null> ejson::Object::getNull(const std::string& _name) {
ejson::Value* tmp = get(_name); std::shared_ptr<ejson::Value> tmp = get(_name);
if (NULL == tmp) { if (tmp == nullptr) {
return NULL; return nullptr;
} }
return dynamic_cast<ejson::Null*>(tmp); return std::dynamic_pointer_cast<ejson::Null>(tmp);
} }
const ejson::Null* ejson::Object::getNull(const std::string& _name) const { const std::shared_ptr<const ejson::Null> ejson::Object::getNull(const std::string& _name) const {
const ejson::Value* tmp = get(_name); const std::shared_ptr<const ejson::Value> tmp = get(_name);
if (NULL == tmp) { if (tmp == nullptr) {
return NULL; return nullptr;
} }
return dynamic_cast<const ejson::Null*>(tmp); return std::dynamic_pointer_cast<const ejson::Null>(tmp);
} }
ejson::String* ejson::Object::getString(const std::string& _name) { std::shared_ptr<ejson::String> ejson::Object::getString(const std::string& _name) {
ejson::Value* tmp = get(_name); std::shared_ptr<ejson::Value> tmp = get(_name);
if (NULL == tmp) { if (tmp == nullptr) {
return NULL; return nullptr;
} }
return dynamic_cast<ejson::String*>(tmp); return std::dynamic_pointer_cast<ejson::String>(tmp);
} }
const ejson::String* ejson::Object::getString(const std::string& _name) const { const std::shared_ptr<const ejson::String> ejson::Object::getString(const std::string& _name) const {
const ejson::Value* tmp = get(_name); const std::shared_ptr<const ejson::Value> tmp = get(_name);
if (NULL == tmp) { if (tmp == nullptr) {
return NULL; return nullptr;
} }
return dynamic_cast<const ejson::String*>(tmp); return std::dynamic_pointer_cast<const ejson::String>(tmp);
} }
const std::string& ejson::Object::getStringValue(const std::string& _name) const { const std::string& ejson::Object::getStringValue(const std::string& _name) const {
static const std::string errorString(""); static const std::string errorString("");
const ejson::String* tmpp = getString(_name); const std::shared_ptr<const ejson::String> tmpp = getString(_name);
if (NULL == tmpp) { if (tmpp == nullptr) {
return errorString; return errorString;
} }
return tmpp->get(); return tmpp->get();
} }
std::string ejson::Object::getStringValue(const std::string& _name, const std::string& _errorValue) const { std::string ejson::Object::getStringValue(const std::string& _name, const std::string& _errorValue) const {
const ejson::String* tmpp = getString(_name); const std::shared_ptr<const ejson::String> tmpp = getString(_name);
if (NULL == tmpp) { if (tmpp == nullptr) {
return _errorValue; return _errorValue;
} }
return tmpp->get(); return tmpp->get();
} }
ejson::Boolean* ejson::Object::getBoolean(const std::string& _name) { std::shared_ptr<ejson::Boolean> ejson::Object::getBoolean(const std::string& _name) {
ejson::Value* tmp = get(_name); std::shared_ptr<ejson::Value> tmp = get(_name);
if (NULL == tmp) { if (tmp == nullptr) {
return NULL; return nullptr;
} }
return tmp->toBoolean(); return tmp->toBoolean();
} }
const ejson::Boolean* ejson::Object::getBoolean(const std::string& _name) const { const std::shared_ptr<const ejson::Boolean> ejson::Object::getBoolean(const std::string& _name) const {
const ejson::Value* tmp = get(_name); const std::shared_ptr<const ejson::Value> tmp = get(_name);
if (NULL == tmp) { if (tmp == nullptr) {
return NULL; return nullptr;
} }
return tmp->toBoolean(); return tmp->toBoolean();
} }
bool ejson::Object::getBooleanValue(const std::string& _name, bool _errorValue) const { bool ejson::Object::getBooleanValue(const std::string& _name, bool _errorValue) const {
const ejson::Boolean* tmpp = getBoolean(_name); const std::shared_ptr<const ejson::Boolean> tmpp = getBoolean(_name);
if (NULL == tmpp) { if (tmpp == nullptr) {
return _errorValue; return _errorValue;
} }
return tmpp->get(); return tmpp->get();
} }
ejson::Number* ejson::Object::getNumber(const std::string& _name) { std::shared_ptr<ejson::Number> ejson::Object::getNumber(const std::string& _name) {
ejson::Value* tmp = get(_name); std::shared_ptr<ejson::Value> tmp = get(_name);
if (NULL == tmp) { if (tmp == nullptr) {
return NULL; return nullptr;
} }
return tmp->toNumber(); return tmp->toNumber();
} }
const ejson::Number* ejson::Object::getNumber(const std::string& _name) const { const std::shared_ptr<const ejson::Number> ejson::Object::getNumber(const std::string& _name) const {
const ejson::Value* tmp = get(_name); const std::shared_ptr<const ejson::Value> tmp = get(_name);
if (NULL == tmp) { if (tmp == nullptr) {
return NULL; return nullptr;
} }
return tmp->toNumber(); return tmp->toNumber();
} }
double ejson::Object::getNumberValue(const std::string& _name, double _errorValue) const { double ejson::Object::getNumberValue(const std::string& _name, double _errorValue) const {
const ejson::Number* tmpp = getNumber(_name); const std::shared_ptr<const ejson::Number> tmpp = getNumber(_name);
if (NULL == tmpp) { if (tmpp == nullptr) {
return _errorValue; return _errorValue;
} }
return tmpp->get(); return tmpp->get();
} }
bool ejson::Object::add(const std::string& _name, ejson::Value* _value) { bool ejson::Object::add(const std::string& _name, std::shared_ptr<ejson::Value> _value) {
if (NULL == _value) { if (_value == nullptr) {
return false; return false;
} }
if (_name.size() == 0) { if (_name.size() == 0) {
return false; return false;
} }
if (m_value.exist(_name)) { if (m_value.exist(_name)) {
ejson::Value* tmp = m_value[_name];
delete(tmp);
m_value[_name] = _value; m_value[_name] = _value;
return true; 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) { 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) { 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) { 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) { 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) { bool ejson::Object::transfertIn(std::shared_ptr<ejson::Value> _obj) {
if (NULL == _obj) { if (_obj == nullptr) {
JSON_ERROR("Request transfer on an NULL pointer"); JSON_ERROR("Request transfer on an nullptr pointer");
return false; return false;
} }
ejson::Object* other = _obj->toObject(); std::shared_ptr<ejson::Object> other = _obj->toObject();
if (NULL == other) { if (other == nullptr) {
JSON_ERROR("Request transfer on an element that is not an object"); JSON_ERROR("Request transfer on an element that is not an object");
return false; return false;
} }
@ -475,16 +472,16 @@ bool ejson::Object::transfertIn(ejson::Value* _obj) {
} }
// TODO : Manage error ... // TODO : Manage error ...
ejson::Value* ejson::Object::duplicate() const { std::shared_ptr<ejson::Value> ejson::Object::duplicate() const {
ejson::Object* output = new ejson::Object(); std::shared_ptr<ejson::Object> output = ejson::Object::create();
if (NULL == output) { if (output == nullptr) {
JSON_ERROR("Allocation error ..."); JSON_ERROR("Allocation error ...");
return NULL; return nullptr;
} }
for (int32_t iii=0; iii<m_value.size(); ++iii) { for (int32_t iii=0; iii<m_value.size(); ++iii) {
ejson::Value* val = m_value.getValue(iii); std::shared_ptr<ejson::Value> val = m_value.getValue(iii);
std::string key = m_value.getKey(iii); std::string key = m_value.getKey(iii);
if (NULL == val) { if (val == nullptr) {
continue; continue;
} }
output->add(key, val->duplicate()); output->add(key, val->duplicate());

View File

@ -21,14 +21,15 @@ namespace ejson {
* @brief basic element of a xml structure * @brief basic element of a xml structure
*/ */
Object() { }; Object() { };
public:
static std::shared_ptr<Object> create();
/** /**
* @brief destructor * @brief destructor
*/ */
virtual ~Object() { }; virtual ~Object() { };
protected: protected:
etk::Hash<ejson::Value*> m_value; //!< value of the node (for element this is the name, for text it is the inside text ...) etk::Hash<std::shared_ptr<ejson::Value>> m_value; //!< value of the node (for element this is the name, for text it is the inside text ...)
public: public:
// TODO : add direct id access....
/** /**
* @brief check if an element exist. * @brief check if an element exist.
* @param[in] _name name of the object. * @param[in] _name name of the object.
@ -38,17 +39,17 @@ namespace ejson {
/** /**
* @brief get the sub element with his name (no cast check) * @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 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<ejson::Value> get(const std::string& _name);
//! @previous //! @previous
const ejson::Value* get(const std::string& _name) const; const std::shared_ptr<const ejson::Value> get(const std::string& _name) const;
//! @previous //! @previous
ejson::Value* operator[] (const std::string& _name) { std::shared_ptr<ejson::Value> operator[] (const std::string& _name) {
return get(_name); return get(_name);
} }
//! @previous //! @previous
const ejson::Value* operator[] (const std::string& _name) const { const std::shared_ptr<const ejson::Value> operator[] (const std::string& _name) const {
return get(_name); return get(_name);
} }
public: public:
@ -69,21 +70,21 @@ namespace ejson {
/** /**
* @brief get the pointer on an element reference with his ID. * @brief get the pointer on an element reference with his ID.
* @param[in] _id Id of the element. * @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<ejson::Value> get(size_t _id) {
return m_value[_id]; return m_value[_id];
}; };
//! @previous //! @previous
const ejson::Value* get(size_t _id) const{ const std::shared_ptr<const ejson::Value> get(size_t _id) const{
return m_value[_id]; return m_value[_id];
}; };
//! @previous //! @previous
ejson::Value* operator[] (size_t _id) { std::shared_ptr<ejson::Value> operator[] (size_t _id) {
return m_value[_id]; return m_value[_id];
} }
//! @previous //! @previous
const ejson::Value* operator[] (size_t _id) const { const std::shared_ptr<const ejson::Value> operator[] (size_t _id) const {
return m_value[_id]; 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) * @brief get the sub element with his name (Casted as Object if it is possible)
* @param[in] _name name of the object * @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<ejson::Object> getObject(const std::string& _name);
//! @previous //! @previous
const ejson::Object* getObject(const std::string& _name) const; const std::shared_ptr<const ejson::Object> getObject(const std::string& _name) const;
/** /**
* @brief get the sub element with his name (Casted as Array if it is possible) * @brief get the sub element with his name (Casted as Array if it is possible)
* @param[in] _name name of the object * @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<ejson::Array> getArray(const std::string& _name);
//! @previous //! @previous
const ejson::Array* getArray(const std::string& _name) const; const std::shared_ptr<const ejson::Array> getArray(const std::string& _name) const;
/** /**
* @brief get the sub element with his name (Casted as Null if it is possible) * @brief get the sub element with his name (Casted as Null if it is possible)
* @param[in] _name name of the object * @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<ejson::Null> getNull(const std::string& _name);
//! @previous //! @previous
const ejson::Null* getNull(const std::string& _name) const; const std::shared_ptr<const ejson::Null> getNull(const std::string& _name) const;
/** /**
* @brief get the sub element with his name (Casted as String if it is possible) * @brief get the sub element with his name (Casted as String if it is possible)
* @param[in] _name name of the object * @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<ejson::String> getString(const std::string& _name);
//! @previous //! @previous
const ejson::String* getString(const std::string& _name) const; const std::shared_ptr<const ejson::String> getString(const std::string& _name) const;
/** /**
* @brief get the sub string value of the requested element * @brief get the sub string value of the requested element
* @param[in] _name name of the object * @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) * @brief get the sub element with his name (Casted as Boolean if it is possible)
* @param[in] _name name of the object * @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<ejson::Boolean> getBoolean(const std::string& _name);
//! @previous //! @previous
const ejson::Boolean* getBoolean(const std::string& _name) const; const std::shared_ptr<const ejson::Boolean> getBoolean(const std::string& _name) const;
/** /**
* @brief get the sub boolean value of the requested element. * @brief get the sub boolean value of the requested element.
* @param[in] _name name of the object. * @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) * @brief get the sub element with his name (Casted as Number if it is possible)
* @param[in] _name name of the object * @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<ejson::Number> getNumber(const std::string& _name);
//! @previous //! @previous
const ejson::Number* getNumber(const std::string& _name) const; const std::shared_ptr<const ejson::Number> getNumber(const std::string& _name) const;
/** /**
* @brief get the sub Number value of the requested element. * @brief get the sub Number value of the requested element.
* @param[in] _name name of the object. * @param[in] _name name of the object.
@ -176,7 +177,7 @@ namespace ejson {
* @param[in] _value Element to add * @param[in] _value Element to add
* @return false if an error occured * @return false if an error occured
*/ */
bool add(const std::string& _name, ejson::Value* _value); bool add(const std::string& _name, std::shared_ptr<ejson::Value> _value);
/** /**
* @brief add a string element in the Object (automatic creation) * @brief add a string element in the Object (automatic creation)
* @param[in] _name name of the object * @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 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 iGenerate(std::string& _data, size_t _indent) const;
virtual void clear(); virtual void clear();
virtual bool transfertIn(ejson::Value* _obj); virtual bool transfertIn(std::shared_ptr<ejson::Value> _obj);
virtual ejson::Value* duplicate() const; virtual std::shared_ptr<ejson::Value> duplicate() const;
}; };
}; };

View File

@ -17,6 +17,9 @@
#define __class__ "String" #define __class__ "String"
std::shared_ptr<ejson::String> ejson::String::create(const std::string& _value) {
return std::shared_ptr<ejson::String>(new ejson::String(_value));
}
bool ejson::String::iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc) { bool ejson::String::iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc) {
JSON_PARSE_ELEMENT("start parse : 'String' "); 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) { bool ejson::String::transfertIn(std::shared_ptr<ejson::Value> _obj) {
if (NULL == _obj) { if (_obj == nullptr) {
JSON_ERROR("Request transfer on an NULL pointer"); JSON_ERROR("Request transfer on an nullptr pointer");
return false; return false;
} }
ejson::String* other = _obj->toString(); std::shared_ptr<ejson::String> other = _obj->toString();
if (NULL == other) { if (other == nullptr) {
JSON_ERROR("Request transfer on an element that is not an String"); JSON_ERROR("Request transfer on an element that is not an String");
return false; return false;
} }
@ -64,11 +67,11 @@ bool ejson::String::transfertIn(ejson::Value* _obj) {
return true; return true;
} }
ejson::Value* ejson::String::duplicate() const { std::shared_ptr<ejson::Value> ejson::String::duplicate() const {
ejson::String* output = new ejson::String(m_value); std::shared_ptr<ejson::String> output = ejson::String::create(m_value);
if (NULL == output) { if (output == nullptr) {
JSON_ERROR("Allocation error ..."); JSON_ERROR("Allocation error ...");
return NULL; return nullptr;
} }
return output; return output;
} }

View File

@ -14,7 +14,7 @@
namespace ejson { namespace ejson {
class String : public ejson::Value { class String : public ejson::Value {
public: protected:
/** /**
* @brief basic element of a xml structure * @brief basic element of a xml structure
*/ */
@ -22,6 +22,8 @@ namespace ejson {
m_value(_value) { m_value(_value) {
}; };
public:
static std::shared_ptr<String> create(const std::string& _value="");
/** /**
* @brief destructor * @brief destructor
*/ */
@ -46,8 +48,8 @@ namespace ejson {
public: // herited function : public: // herited function :
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc); 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 iGenerate(std::string& _data, size_t _indent) const;
virtual bool transfertIn(ejson::Value* _obj); virtual bool transfertIn(std::shared_ptr<ejson::Value> _obj);
virtual ejson::Value* duplicate() const; virtual std::shared_ptr<ejson::Value> duplicate() const;
}; };
}; };

View File

@ -13,8 +13,6 @@
#undef __class__ #undef __class__
#define __class__ "Value" #define __class__ "Value"
ejson::Value::~Value() { ejson::Value::~Value() {
clear(); clear();
} }
@ -124,45 +122,45 @@ bool ejson::Value::checkNumber(char32_t _val) const {
} }
ejson::Document* ejson::Value::toDocument() { std::shared_ptr<ejson::Document> ejson::Value::toDocument() {
return dynamic_cast<ejson::Document*>(this); return std::dynamic_pointer_cast<ejson::Document>(shared_from_this());
}; };
const ejson::Document* ejson::Value::toDocument() const { const std::shared_ptr<const ejson::Document> ejson::Value::toDocument() const {
return dynamic_cast<const ejson::Document*>(this); return std::dynamic_pointer_cast<const ejson::Document>(shared_from_this());
}; };
ejson::Array* ejson::Value::toArray() { std::shared_ptr<ejson::Array> ejson::Value::toArray() {
return dynamic_cast<ejson::Array*>(this); return std::dynamic_pointer_cast<ejson::Array>(shared_from_this());
}; };
const ejson::Array* ejson::Value::toArray() const{ const std::shared_ptr<const ejson::Array> ejson::Value::toArray() const{
return dynamic_cast<const ejson::Array*>(this); return std::dynamic_pointer_cast<const ejson::Array>(shared_from_this());
}; };
ejson::Object* ejson::Value::toObject() { std::shared_ptr<ejson::Object> ejson::Value::toObject() {
return dynamic_cast<ejson::Object*>(this); return std::dynamic_pointer_cast<ejson::Object>(shared_from_this());
}; };
const ejson::Object* ejson::Value::toObject() const{ const std::shared_ptr<const ejson::Object> ejson::Value::toObject() const{
return dynamic_cast<const ejson::Object*>(this); return std::dynamic_pointer_cast<const ejson::Object>(shared_from_this());
}; };
ejson::String* ejson::Value::toString() { std::shared_ptr<ejson::String> ejson::Value::toString() {
return dynamic_cast<ejson::String*>(this); return std::dynamic_pointer_cast<ejson::String>(shared_from_this());
}; };
const ejson::String* ejson::Value::toString() const{ const std::shared_ptr<const ejson::String> ejson::Value::toString() const{
return dynamic_cast<const ejson::String*>(this); return std::dynamic_pointer_cast<const ejson::String>(shared_from_this());
}; };
ejson::Number* ejson::Value::toNumber() { std::shared_ptr<ejson::Number> ejson::Value::toNumber() {
return dynamic_cast<ejson::Number*>(this); return std::dynamic_pointer_cast<ejson::Number>(shared_from_this());
}; };
const ejson::Number* ejson::Value::toNumber() const{ const std::shared_ptr<const ejson::Number> ejson::Value::toNumber() const{
return dynamic_cast<const ejson::Number*>(this); return std::dynamic_pointer_cast<const ejson::Number>(shared_from_this());
}; };
ejson::Boolean* ejson::Value::toBoolean() { std::shared_ptr<ejson::Boolean> ejson::Value::toBoolean() {
return dynamic_cast<ejson::Boolean*>(this); return std::dynamic_pointer_cast<ejson::Boolean>(shared_from_this());
}; };
const ejson::Boolean* ejson::Value::toBoolean() const{ const std::shared_ptr<const ejson::Boolean> ejson::Value::toBoolean() const{
return dynamic_cast<const ejson::Boolean*>(this); return std::dynamic_pointer_cast<const ejson::Boolean>(shared_from_this());
}; };
ejson::Null* ejson::Value::toNull() { std::shared_ptr<ejson::Null> ejson::Value::toNull() {
return dynamic_cast<ejson::Null*>(this); return std::dynamic_pointer_cast<ejson::Null>(shared_from_this());
}; };
const ejson::Null* ejson::Value::toNull() const{ const std::shared_ptr<const ejson::Null> ejson::Value::toNull() const{
return dynamic_cast<const ejson::Null*>(this); return std::dynamic_pointer_cast<const ejson::Null>(shared_from_this());
}; };

View File

@ -10,6 +10,7 @@
#define __ETK_JSON_VALUE_H__ #define __ETK_JSON_VALUE_H__
#include <etk/types.h> #include <etk/types.h>
#include <memory>
namespace ejson { namespace ejson {
//#define ENABLE_DISPLAY_PARSED_ELEMENT //#define ENABLE_DISPLAY_PARSED_ELEMENT
@ -104,8 +105,8 @@ namespace ejson {
}; };
std::ostream& operator <<(std::ostream& _os, const filePos& _obj); std::ostream& operator <<(std::ostream& _os, const filePos& _obj);
class Value { class Value : public std::enable_shared_from_this<Value> {
public: protected:
/** /**
* @brief basic element of a xml structure * @brief basic element of a xml structure
*/ */
@ -165,113 +166,113 @@ namespace ejson {
public: public:
/** /**
* @brief Cast the element in a Value if it is possible. * @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() { std::shared_ptr<ejson::Value> toValue() {
return this; return shared_from_this();
}; };
//! @previous //! @previous
const ejson::Value* toValue() const { const std::shared_ptr<const ejson::Value> toValue() const {
return this; return shared_from_this();
}; };
/** /**
* @brief Cast the element in a Document if it is possible. * @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<ejson::Document> toDocument();
//! @previous //! @previous
const ejson::Document* toDocument() const; const std::shared_ptr<const ejson::Document> toDocument() const;
/** /**
* @brief Cast the element in a Array if it is possible. * @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<ejson::Array> toArray();
//! @previous //! @previous
const ejson::Array* toArray() const; const std::shared_ptr<const ejson::Array> toArray() const;
/** /**
* @brief Cast the element in a Object if it is possible. * @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<ejson::Object> toObject();
//! @previous //! @previous
const ejson::Object* toObject() const; const std::shared_ptr<const ejson::Object> toObject() const;
/** /**
* @brief Cast the element in a String if it is possible. * @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<ejson::String> toString();
//! @previous //! @previous
const ejson::String* toString() const; const std::shared_ptr<const ejson::String> toString() const;
/** /**
* @brief Cast the element in a Number if it is possible. * @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<ejson::Number> toNumber();
//! @previous //! @previous
const ejson::Number* toNumber() const; const std::shared_ptr<const ejson::Number> toNumber() const;
/** /**
* @brief Cast the element in a Boolean if it is possible. * @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<ejson::Boolean> toBoolean();
//! @previous //! @previous
const ejson::Boolean* toBoolean() const; const std::shared_ptr<const ejson::Boolean> toBoolean() const;
/** /**
* @brief Cast the element in a Null if it is possible. * @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<ejson::Null> toNull();
//! @previous //! @previous
const ejson::Null* toNull() const; const std::shared_ptr<const ejson::Null> toNull() const;
/** /**
* @brief check if the node is a ejson::Document * @brief check if the node is a ejson::Document
* @return true if the node is a ejson::Document * @return true if the node is a ejson::Document
*/ */
bool isDocument() const { bool isDocument() const {
return toDocument() != NULL; return toDocument() != nullptr;
}; };
/** /**
* @brief check if the node is a ejson::Array * @brief check if the node is a ejson::Array
* @return true if the node is a ejson::Array * @return true if the node is a ejson::Array
*/ */
bool isArray() const { bool isArray() const {
return toArray() != NULL; return toArray() != nullptr;
}; };
/** /**
* @brief check if the node is a ejson::Object * @brief check if the node is a ejson::Object
* @return true if the node is a ejson::Object * @return true if the node is a ejson::Object
*/ */
bool isObject() const { bool isObject() const {
return toObject() != NULL; return toObject() != nullptr;
}; };
/** /**
* @brief check if the node is a ejson::String * @brief check if the node is a ejson::String
* @return true if the node is a ejson::String * @return true if the node is a ejson::String
*/ */
bool isString() const { bool isString() const {
return toString() != NULL; return toString() != nullptr;
}; };
/** /**
* @brief check if the node is a ejson::Number * @brief check if the node is a ejson::Number
* @return true if the node is a ejson::Number * @return true if the node is a ejson::Number
*/ */
bool isNumber() const { bool isNumber() const {
return toNumber() != NULL; return toNumber() != nullptr;
}; };
/** /**
* @brief check if the node is a ejson::Boolean * @brief check if the node is a ejson::Boolean
* @return true if the node is a ejson::Boolean * @return true if the node is a ejson::Boolean
*/ */
bool isBoolean() const { bool isBoolean() const {
return toBoolean() != NULL; return toBoolean() != nullptr;
}; };
/** /**
* @brief check if the node is a ejson::Null * @brief check if the node is a ejson::Null
* @return true if the node is a ejson::Null * @return true if the node is a ejson::Null
*/ */
bool isNull() const { bool isNull() const {
return toNull() != NULL; return toNull() != nullptr;
}; };
/** /**
@ -284,15 +285,15 @@ namespace ejson {
* @return true if transfer is done corectly * @return true if transfer is done corectly
* @note all element is remove from the curent element. * @note all element is remove from the curent element.
*/ */
virtual bool transfertIn(ejson::Value* _obj) { virtual bool transfertIn(std::shared_ptr<ejson::Value> _obj) {
return false; return false;
}; };
/** /**
* @brief Copy the curent node and all the child in the curent one. * @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 { virtual std::shared_ptr<ejson::Value> duplicate() const {
return NULL; return nullptr;
}; };
protected: protected:
/** /**

View File

@ -20,6 +20,11 @@
#undef __class__ #undef __class__
#define __class__ "Document" #define __class__ "Document"
std::shared_ptr<ejson::Document> ejson::Document::create() {
return std::shared_ptr<ejson::Document>(new ejson::Document());
}
ejson::Document::Document() : ejson::Document::Document() :
m_writeErrorWhenDetexted(true), m_writeErrorWhenDetexted(true),
m_comment(""), m_comment(""),
@ -70,21 +75,14 @@ bool ejson::Document::load(const std::string& _file) {
return false; return false;
} }
// allocate data // allocate data
char * fileBuffer = new char[fileSize+5]; std::vector<char> fileBuffer;
if (NULL == fileBuffer) { fileBuffer.resize(fileSize+5, 0);
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));
// load data from the file : // load data from the file :
tmpFile.fileRead(fileBuffer, 1, fileSize); tmpFile.fileRead(&fileBuffer[0], 1, fileSize);
// close the file: // close the file:
tmpFile.fileClose(); tmpFile.fileClose();
std::string tmpDataUnicode(fileBuffer); std::string tmpDataUnicode(&fileBuffer[0]);
// remove temporary buffer:
delete[] fileBuffer;
// parse the data : // parse the data :
bool ret = parse(tmpDataUnicode); bool ret = parse(tmpDataUnicode);
//Display(); //Display();

View File

@ -23,6 +23,7 @@ namespace ejson {
* @brief Constructor * @brief Constructor
*/ */
Document(); Document();
static std::shared_ptr<Document> create();
/** /**
* @brief Destructor * @brief Destructor
*/ */

View File

@ -462,7 +462,9 @@ void init() {
} }
int main(int argc, const char *argv[]) { int main(int argc, const char *argv[]) {
etk::log::setLevel(etk::log::logLevelVerbose); etk::log::setLevel(etk::log::logLevelInfo);
init(); init();
int32_t countError = 0; int32_t countError = 0;
int32_t countSeparator = 0; int32_t countSeparator = 0;