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

View File

@ -14,17 +14,19 @@
namespace ejson {
class Array : public ejson::Value {
public:
protected:
/**
* @brief basic element of a xml structure
*/
Array() { };
public:
static std::shared_ptr<Array> create();
/**
* @brief destructor
*/
virtual ~Array() { };
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:
/**
* @brief get the number of sub element in the current one
@ -36,39 +38,39 @@ namespace ejson {
/**
* @brief get the pointer on an element reference with his ID.
* @param[in] _id Id of the element.
* @return NULL if the element does not exist.
* @return nullptr if the element does not exist.
*/
ejson::Value* get(size_t _id) {
std::shared_ptr<ejson::Value> get(size_t _id) {
return m_value[_id];
};
//! @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];
};
//! @previous
ejson::Value* operator[] (size_t _id) {
std::shared_ptr<ejson::Value> operator[] (size_t _id) {
return m_value[_id];
}
//! @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];
}
/**
* @brief get the pointer on an element reference with his ID (casted in Object if it is an object).
* @param[in] _id Id of the element.
* @return NULL if the element does not exist.
* @return nullptr if the element does not exist.
*/
ejson::Object* getObject(size_t _id);
std::shared_ptr<ejson::Object> getObject(size_t _id);
//! @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).
* @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
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 "")
* @param[in] _id Id of the element.
@ -87,27 +89,27 @@ namespace ejson {
/**
* @brief get the pointer on an element reference with his ID (casted in Array if it is an Array).
* @param[in] _id Id of the element.
* @return NULL if the element does not exist.
* @return nullptr if the element does not exist.
*/
ejson::Array* getArray(size_t _id);
std::shared_ptr<ejson::Array> getArray(size_t _id);
//! @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).
* @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
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).
* @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
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
* @param[in] _id Id of the element.
@ -118,11 +120,11 @@ namespace ejson {
/**
* @brief get the pointer on an element reference with his ID (casted in Boolean if it is an Boolean).
* @param[in] _id Id of the element.
* @return NULL if the element does not exist.
* @return nullptr if the element does not exist.
*/
ejson::Boolean* getBoolean(size_t _id);
std::shared_ptr<ejson::Boolean> getBoolean(size_t _id);
//! @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
* @param[in] _id Id of the element.
@ -135,7 +137,7 @@ namespace ejson {
* @param[in] _element element to add.
* @return false if an error occured.
*/
bool add(ejson::Value* _element);
bool add(std::shared_ptr<ejson::Value> _element);
/**
* @brief add a string element in the Object (automatic creation)
* @param[in] _value string value to add
@ -164,8 +166,8 @@ namespace ejson {
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc);
virtual bool iGenerate(std::string& _data, size_t _indent) const;
virtual void clear();
virtual bool transfertIn(ejson::Value* _obj);
virtual ejson::Value* duplicate() const;
virtual bool transfertIn(std::shared_ptr<ejson::Value> _obj);
virtual std::shared_ptr<ejson::Value> duplicate() const;
};
};

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -21,14 +21,15 @@ namespace ejson {
* @brief basic element of a xml structure
*/
Object() { };
public:
static std::shared_ptr<Object> create();
/**
* @brief destructor
*/
virtual ~Object() { };
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:
// TODO : add direct id access....
/**
* @brief check if an element exist.
* @param[in] _name name of the object.
@ -38,17 +39,17 @@ namespace ejson {
/**
* @brief get the sub element with his name (no cast check)
* @param[in] _name name of the object
* @return pointer on the element requested or NULL if it not the corect type or does not existed
* @return pointer on the element requested or nullptr if it not the corect type or does not existed
*/
ejson::Value* get(const std::string& _name);
std::shared_ptr<ejson::Value> get(const std::string& _name);
//! @previous
const ejson::Value* get(const std::string& _name) const;
const std::shared_ptr<const ejson::Value> get(const std::string& _name) const;
//! @previous
ejson::Value* operator[] (const std::string& _name) {
std::shared_ptr<ejson::Value> operator[] (const std::string& _name) {
return get(_name);
}
//! @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);
}
public:
@ -69,21 +70,21 @@ namespace ejson {
/**
* @brief get the pointer on an element reference with his ID.
* @param[in] _id Id of the element.
* @return NULL if the element does not exist.
* @return nullptr if the element does not exist.
*/
ejson::Value* get(size_t _id) {
std::shared_ptr<ejson::Value> get(size_t _id) {
return m_value[_id];
};
//! @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];
};
//! @previous
ejson::Value* operator[] (size_t _id) {
std::shared_ptr<ejson::Value> operator[] (size_t _id) {
return m_value[_id];
}
//! @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];
}
/**
@ -97,35 +98,35 @@ namespace ejson {
/**
* @brief get the sub element with his name (Casted as Object if it is possible)
* @param[in] _name name of the object
* @return pointer on the element requested or NULL if it not the corect type or does not existed
* @return pointer on the element requested or nullptr if it not the corect type or does not existed
*/
ejson::Object* getObject(const std::string& _name);
std::shared_ptr<ejson::Object> getObject(const std::string& _name);
//! @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)
* @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
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)
* @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
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)
* @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
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
* @param[in] _name name of the object
@ -142,11 +143,11 @@ namespace ejson {
/**
* @brief get the sub element with his name (Casted as Boolean if it is possible)
* @param[in] _name name of the object
* @return pointer on the element requested or NULL if it not the corect type or does not existed
* @return pointer on the element requested or nullptr if it not the corect type or does not existed
*/
ejson::Boolean* getBoolean(const std::string& _name);
std::shared_ptr<ejson::Boolean> getBoolean(const std::string& _name);
//! @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.
* @param[in] _name name of the object.
@ -157,11 +158,11 @@ namespace ejson {
/**
* @brief get the sub element with his name (Casted as Number if it is possible)
* @param[in] _name name of the object
* @return pointer on the element requested or NULL if it not the corect type or does not existed
* @return pointer on the element requested or nullptr if it not the corect type or does not existed
*/
ejson::Number* getNumber(const std::string& _name);
std::shared_ptr<ejson::Number> getNumber(const std::string& _name);
//! @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.
* @param[in] _name name of the object.
@ -176,7 +177,7 @@ namespace ejson {
* @param[in] _value Element to add
* @return false if an error occured
*/
bool add(const std::string& _name, ejson::Value* _value);
bool add(const std::string& _name, std::shared_ptr<ejson::Value> _value);
/**
* @brief add a string element in the Object (automatic creation)
* @param[in] _name name of the object
@ -208,8 +209,8 @@ namespace ejson {
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc);
virtual bool iGenerate(std::string& _data, size_t _indent) const;
virtual void clear();
virtual bool transfertIn(ejson::Value* _obj);
virtual ejson::Value* duplicate() const;
virtual bool transfertIn(std::shared_ptr<ejson::Value> _obj);
virtual std::shared_ptr<ejson::Value> duplicate() const;
};
};

View File

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

View File

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

View File

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

View File

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

View File

@ -20,6 +20,11 @@
#undef __class__
#define __class__ "Document"
std::shared_ptr<ejson::Document> ejson::Document::create() {
return std::shared_ptr<ejson::Document>(new ejson::Document());
}
ejson::Document::Document() :
m_writeErrorWhenDetexted(true),
m_comment(""),
@ -70,21 +75,14 @@ bool ejson::Document::load(const std::string& _file) {
return false;
}
// allocate data
char * fileBuffer = new char[fileSize+5];
if (NULL == fileBuffer) {
JSON_ERROR("Error Memory allocation size=" << fileSize);
return false;
}
// TODO : change this ... get the charset from the Declaration element ...
memset(fileBuffer, 0, (fileSize+5)*sizeof(char));
std::vector<char> fileBuffer;
fileBuffer.resize(fileSize+5, 0);
// load data from the file :
tmpFile.fileRead(fileBuffer, 1, fileSize);
tmpFile.fileRead(&fileBuffer[0], 1, fileSize);
// close the file:
tmpFile.fileClose();
std::string tmpDataUnicode(fileBuffer);
// remove temporary buffer:
delete[] fileBuffer;
std::string tmpDataUnicode(&fileBuffer[0]);
// parse the data :
bool ret = parse(tmpDataUnicode);
//Display();

View File

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

View File

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