[DEV] use std::map insted of etk::Hash

This commit is contained in:
Edouard DUPIN 2014-03-04 21:18:41 +01:00
parent e1c8319249
commit 74f13fd919
11 changed files with 199 additions and 213 deletions

View File

@ -28,18 +28,35 @@ include_directories(.)
#Create src file list
set(src_files
ejson/debug.cpp
ejson/debug.h
ejson/ejson.cpp
ejson/ejson.h
ejson/Array.cpp
ejson/Array.h
ejson/Boolean.cpp
ejson/Boolean.h
ejson/Null.cpp
ejson/Null.h
ejson/Number.cpp
ejson/Number.h
ejson/String.cpp
ejson/String.h
ejson/Object.cpp
ejson/Object.h
ejson/Value.cpp
ejson/Value.h
)
add_definitions( -DDEBUG_LEVEL=3 )
add_definitions( -DDEBUG=1 )
if (APPLE)
add_definitions( -D__TARGET_OS__MacOs )
elseif (UNIX)
add_definitions( -D__TARGET_OS__Linux )
elseif (WIN32)
add_definitions( -D__TARGET_OS__Windows )
endif ()
include_directories(${etk_SOURCE_DIR})
include_directories(${linearmath_SOURCE_DIR}/bullet/src/)

12
ejson/Array.h Normal file → Executable file
View File

@ -10,8 +10,6 @@
#define __ETK_JSON_ARRAY_H__
#include <etk/types.h>
#include <etk/types.h>
#include <etk/math/Vector2D.h>
#include <ejson/Value.h>
namespace ejson {
@ -165,16 +163,6 @@ namespace ejson {
public: // herited function :
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc);
virtual bool iGenerate(std::string& _data, size_t _indent) const;
virtual enum nodeType getType(void) const {
return typeArray;
};
virtual ejson::Array* toArray(void) {
return this;
};
//! @previous
virtual const ejson::Array* toArray(void) const {
return this;
};
virtual void clear(void);
virtual bool transfertIn(ejson::Value* _obj);
virtual ejson::Value* duplicate(void) const;

12
ejson/Boolean.h Normal file → Executable file
View File

@ -10,8 +10,6 @@
#define __ETK_JSON_BOOLEAN_H__
#include <etk/types.h>
#include <etk/types.h>
#include <etk/math/Vector2D.h>
#include <ejson/Value.h>
namespace ejson {
@ -50,16 +48,6 @@ namespace ejson {
public: // herited function :
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc);
virtual bool iGenerate(std::string& _data, size_t _indent) const;
virtual enum nodeType getType(void) const {
return typeString;
};
virtual ejson::Boolean* toBoolean(void) {
return this;
};
//! @previous
virtual const ejson::Boolean* toBoolean(void) const{
return this;
};
virtual bool transfertIn(ejson::Value* _obj);
virtual ejson::Value* duplicate(void) const;
};

12
ejson/Null.h Normal file → Executable file
View File

@ -10,8 +10,6 @@
#define __ETK_JSON_NULL_H__
#include <etk/types.h>
#include <etk/types.h>
#include <etk/math/Vector2D.h>
#include <ejson/Value.h>
namespace ejson {
@ -28,16 +26,6 @@ namespace ejson {
public: // herited function :
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc);
virtual bool iGenerate(std::string& _data, size_t _indent) const;
virtual enum nodeType getType(void) const {
return typeString;
};
virtual ejson::Null* toNull(void) {
return this;
};
//! @previous
virtual const ejson::Null* toNull(void) const{
return this;
};
virtual bool transfertIn(ejson::Value* _obj);
virtual ejson::Value* duplicate(void) const;
};

12
ejson/Number.h Normal file → Executable file
View File

@ -10,8 +10,6 @@
#define __ETK_JSON_NUMBER_H__
#include <etk/types.h>
#include <etk/types.h>
#include <etk/math/Vector2D.h>
#include <ejson/Value.h>
namespace ejson {
@ -62,16 +60,6 @@ namespace ejson {
public: // herited function :
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc);
virtual bool iGenerate(std::string& _data, size_t _indent) const;
virtual enum nodeType getType(void) const {
return typeString;
};
virtual ejson::Number* toNumber(void) {
return this;
};
//! @previous
virtual const ejson::Number* toNumber(void) const{
return this;
};
virtual bool transfertIn(ejson::Value* _obj);
virtual ejson::Value* duplicate(void) const;
};

View File

@ -20,12 +20,12 @@
#define __class__ "Object"
void ejson::Object::clear(void) {
for (int32_t iii=0; iii<m_value.size(); ++iii) {
if (NULL == m_value[iii]) {
for(auto &it : m_value) {
if (it.second == NULL) {
continue;
}
delete(m_value[iii]);
m_value[iii] = NULL;
delete(it.second);
it.second = NULL;
}
m_value.clear();
}
@ -209,8 +209,8 @@ bool ejson::Object::iGenerate(std::string& _data, size_t _indent) const {
} else if (_indent<=1) {
oneLine=false;
} else {
for (int32_t iii=0; iii<m_value.size() ; iii++) {
ejson::Value* tmp = m_value[iii];
for(auto it = m_value.begin(); it != m_value.end(); ++it) {
ejson::Value* tmp = it->second;
if (tmp == NULL) {
continue;
}
@ -224,9 +224,9 @@ bool ejson::Object::iGenerate(std::string& _data, size_t _indent) const {
}
if (true == tmp->isString()) {
ejson::String* tmp2 = tmp->toString();
if (NULL!=tmp2) {
if (tmp2 != NULL) {
if( tmp2->get().size()>25
|| m_value.getKey(iii).size()>25) {
|| it->first.size()>25) {
oneLine=false;
break;
}
@ -239,15 +239,17 @@ bool ejson::Object::iGenerate(std::string& _data, size_t _indent) const {
} else {
_data += "{\n";
}
for (int32_t iii=0; iii<m_value.size() ; iii++) {
for(auto it = m_value.begin();
it != m_value.end();
++it) {
if (false == oneLine) {
addIndent(_data, _indent);
}
_data += "\"";
_data += m_value.getKey(iii);
_data += it->first;
_data += "\": ";
m_value.getValue(iii)->iGenerate(_data, _indent+1);
if (iii<m_value.size()-1) {
it->second->iGenerate(_data, _indent+1);
if (it != m_value.end()) {
_data += ",";
}
if (true == oneLine) {
@ -264,85 +266,111 @@ bool ejson::Object::iGenerate(std::string& _data, size_t _indent) const {
}
bool ejson::Object::exist(const std::string& _name) const {
return m_value.exist(_name);
return m_value.find(_name) != m_value.end();
}
ejson::Value* ejson::Object::get(const std::string& _name) {
if (false == m_value.exist(_name)) {
auto it = m_value.find(_name);
if (it == m_value.end()) {
return NULL;
}
return m_value[_name];
return it->second;
}
const ejson::Value* ejson::Object::get(const std::string& _name) const {
if (false == m_value.exist(_name)) {
auto it = m_value.find(_name);
if (it == m_value.end()) {
return NULL;
}
return m_value[_name];
return it->second;
}
ejson::Object* ejson::Object::getObject(const std::string& _name) {
ejson::Value* tmp = get(_name);
if (NULL == tmp) {
auto it = m_value.find(_name);
if (it == m_value.end()) {
return NULL;
}
return tmp->toObject();
if (NULL == it->second) {
return NULL;
}
return dynamic_cast<ejson::Object*>(it->second);
}
const ejson::Object* ejson::Object::getObject(const std::string& _name) const {
const ejson::Value* tmp = get(_name);
if (NULL == tmp) {
auto it = m_value.find(_name);
if (it == m_value.end()) {
return NULL;
}
return tmp->toObject();
if (NULL == it->second) {
return NULL;
}
return dynamic_cast<const ejson::Object*>(it->second);
}
ejson::Array* ejson::Object::getArray(const std::string& _name) {
ejson::Value* tmp = get(_name);
if (NULL == tmp) {
auto it = m_value.find(_name);
if (it == m_value.end()) {
return NULL;
}
return tmp->toArray();
if (NULL == it->second) {
return NULL;
}
return dynamic_cast<ejson::Array*>(it->second);
}
const ejson::Array* ejson::Object::getArray(const std::string& _name) const {
const ejson::Value* tmp = get(_name);
if (NULL == tmp) {
auto it = m_value.find(_name);
if (it == m_value.end()) {
return NULL;
}
return tmp->toArray();
if (NULL == it->second) {
return NULL;
}
return dynamic_cast<const ejson::Array*>(it->second);
}
ejson::Null* ejson::Object::getNull(const std::string& _name) {
ejson::Value* tmp = get(_name);
if (NULL == tmp) {
auto it = m_value.find(_name);
if (it == m_value.end()) {
return NULL;
}
return tmp->toNull();
if (NULL == it->second) {
return NULL;
}
return dynamic_cast<ejson::Null*>(it->second);
}
const ejson::Null* ejson::Object::getNull(const std::string& _name) const {
const ejson::Value* tmp = get(_name);
if (NULL == tmp) {
auto it = m_value.find(_name);
if (it == m_value.end()) {
return NULL;
}
return tmp->toNull();
if (NULL == it->second) {
return NULL;
}
return dynamic_cast<const ejson::Null*>(it->second);
}
ejson::String* ejson::Object::getString(const std::string& _name) {
ejson::Value* tmp = get(_name);
if (NULL == tmp) {
auto it = m_value.find(_name);
if (it == m_value.end()) {
return NULL;
}
return tmp->toString();
if (NULL == it->second) {
return NULL;
}
return dynamic_cast<ejson::String*>(it->second);
}
const ejson::String* ejson::Object::getString(const std::string& _name) const {
const ejson::Value* tmp = get(_name);
if (NULL == tmp) {
auto it = m_value.find(_name);
if (it == m_value.end()) {
return NULL;
}
return tmp->toString();
if (NULL == it->second) {
return NULL;
}
return dynamic_cast<const ejson::String*>(it->second);
}
const std::string& ejson::Object::getStringValue(const std::string& _name) const {
@ -418,13 +446,13 @@ bool ejson::Object::add(const std::string& _name, ejson::Value* _value) {
if (_name.size() == 0) {
return false;
}
if (m_value.exist(_name)) {
ejson::Value* tmp = m_value[_name];
delete(tmp);
m_value[_name] = _value;
auto it = m_value.find(_name);
if (it != m_value.end()) {
delete(it->second);
it->second = _value;
return true;
}
m_value.add(_name, _value);
m_value.insert(std::pair<std::string, ejson::Value*>(_name, _value));
return true;
}
@ -470,13 +498,11 @@ ejson::Value* ejson::Object::duplicate(void) const {
JSON_ERROR("Allocation error ...");
return NULL;
}
for (int32_t iii=0; iii<m_value.size(); ++iii) {
ejson::Value* val = m_value.getValue(iii);
std::string key = m_value.getKey(iii);
if (NULL == val) {
for(auto it = m_value.begin(); it != m_value.end(); ++it) {
if (it->second == NULL) {
continue;
}
output->add(key, val->duplicate());
output->add(it->first, it->second->duplicate());
}
return output;
}

52
ejson/Object.h Normal file → Executable file
View File

@ -10,9 +10,8 @@
#define __ETK_JSON_OBJECT_H__
#include <etk/types.h>
#include <etk/types.h>
#include <etk/Hash.h>
#include <etk/math/Vector2D.h>
#include <map>
#include <algorithm>
#include <ejson/Value.h>
namespace ejson {
@ -27,7 +26,7 @@ namespace ejson {
*/
virtual ~Object(void) { };
protected:
etk::Hash<ejson::Value*> m_value; //!< value of the node (for element this is the name, for text it is the inside text ...)
std::map<std::string, 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....
/**
@ -52,12 +51,17 @@ namespace ejson {
const ejson::Value* operator[] (const std::string& _name) const {
return get(_name);
}
public:
/**
* @brief Get all the element name (keys).
* @return a vector of all name (key).
*/
std::vector<std::string> getKeys(void) const {
return m_value.getKeys();
std::vector<std::string> keys;
for (auto &it : m_value) {
keys.push_back(it.first);
}
return keys;
}
/**
* @brief get the number of sub element in the current one
@ -72,19 +76,31 @@ namespace ejson {
* @return NULL if the element does not exist.
*/
ejson::Value* get(size_t _id) {
return m_value[_id];
size_t id = 0;
for(auto &it : m_value) {
if (id == _id) {
return it.second;
}
}
return NULL;
};
//! @previous
const ejson::Value* get(size_t _id) const{
return m_value[_id];
size_t id = 0;
for(auto &it : m_value) {
if (id == _id) {
return it.second;
}
}
return NULL;
};
//! @previous
ejson::Value* operator[] (size_t _id) {
return m_value[_id];
return get(_id);
}
//! @previous
const ejson::Value* operator[] (size_t _id) const {
return m_value[_id];
return get(_id);
}
/**
* @brief Get the element name (key).
@ -92,7 +108,13 @@ namespace ejson {
* @return The name (key).
*/
std::string getKey(size_t _id) const {
return m_value.getKey(_id);
size_t id = 0;
for(auto it = m_value.begin(); it != m_value.end(); ++it, ++id) {
if (id == _id) {
return it->first;
}
}
return NULL;
}
/**
* @brief get the sub element with his name (Casted as Object if it is possible)
@ -207,16 +229,6 @@ namespace ejson {
public: // herited function :
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc);
virtual bool iGenerate(std::string& _data, size_t _indent) const;
virtual enum nodeType getType(void) const {
return typeObject;
};
virtual ejson::Object* toObject(void) {
return this;
};
//! @previous
virtual const ejson::Object* toObject(void) const{
return this;
};
virtual void clear(void);
virtual bool transfertIn(ejson::Value* _obj);
virtual ejson::Value* duplicate(void) const;

12
ejson/String.h Normal file → Executable file
View File

@ -10,8 +10,6 @@
#define __ETK_JSON_STRING_H__
#include <etk/types.h>
#include <etk/types.h>
#include <etk/math/Vector2D.h>
#include <ejson/Value.h>
namespace ejson {
@ -48,16 +46,6 @@ namespace ejson {
public: // herited function :
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc);
virtual bool iGenerate(std::string& _data, size_t _indent) const;
virtual enum nodeType getType(void) const {
return typeString;
};
virtual ejson::String* toString(void) {
return this;
};
//! @previous
virtual const ejson::String* toString(void) const {
return this;
};
virtual bool transfertIn(ejson::Value* _obj);
virtual ejson::Value* duplicate(void) const;
};

View File

@ -123,3 +123,46 @@ bool ejson::Value::checkNumber(char32_t _val) const {
return false;
}
ejson::Document* ejson::Value::toDocument(void) {
return dynamic_cast<ejson::Document*>(this);
};
const ejson::Document* ejson::Value::toDocument(void) const {
return dynamic_cast<const ejson::Document*>(this);
};
ejson::Array* ejson::Value::toArray(void) {
return dynamic_cast<ejson::Array*>(this);
};
const ejson::Array* ejson::Value::toArray(void) const{
return dynamic_cast<const ejson::Array*>(this);
};
ejson::Object* ejson::Value::toObject(void) {
return dynamic_cast<ejson::Object*>(this);
};
const ejson::Object* ejson::Value::toObject(void) const{
return dynamic_cast<const ejson::Object*>(this);
};
ejson::String* ejson::Value::toString(void) {
return dynamic_cast<ejson::String*>(this);
};
const ejson::String* ejson::Value::toString(void) const{
return dynamic_cast<const ejson::String*>(this);
};
ejson::Number* ejson::Value::toNumber(void) {
return dynamic_cast<ejson::Number*>(this);
};
const ejson::Number* ejson::Value::toNumber(void) const{
return dynamic_cast<const ejson::Number*>(this);
};
ejson::Boolean* ejson::Value::toBoolean(void) {
return dynamic_cast<ejson::Boolean*>(this);
};
const ejson::Boolean* ejson::Value::toBoolean(void) const{
return dynamic_cast<const ejson::Boolean*>(this);
};
ejson::Null* ejson::Value::toNull(void) {
return dynamic_cast<ejson::Null*>(this);
};
const ejson::Null* ejson::Value::toNull(void) const{
return dynamic_cast<const ejson::Null*>(this);
};

103
ejson/Value.h Normal file → Executable file
View File

@ -10,8 +10,6 @@
#define __ETK_JSON_VALUE_H__
#include <etk/types.h>
#include <etk/types.h>
#include <etk/math/Vector2D.h>
namespace ejson {
//#define ENABLE_DISPLAY_PARSED_ELEMENT
@ -32,18 +30,6 @@ namespace ejson {
class Null;
class Number;
class String;
enum nodeType{
typeUnknow, //!< might be an error ...
typeValue, //!< XXXXXX:*
typeDocument, //!< all the file main access
typeArray, //!< [...]
typeString, //!< the ""
typeNumber, //!< the -1565.21515
typeBoolean, //!< the true and false
typeNull, //!< the null element
typeObject, //!< the { ... }
};
//! @not-in-doc
class filePos {
private:
@ -145,14 +131,6 @@ namespace ejson {
* @return false if an error occured.
*/
virtual bool iGenerate(std::string& _data, size_t _indent) const = 0;
public:
/**
* @brief get the node type.
* @return the type of the Node.
*/
virtual enum nodeType getType(void) const {
return typeValue;
};
protected:
/**
* @brief add indentation of the string input.
@ -189,139 +167,111 @@ namespace ejson {
* @brief Cast the element in a Value if it is possible.
* @return pointer on the class or NULL.
*/
virtual ejson::Value* toValue(void) {
ejson::Value* toValue(void) {
return this;
};
//! @previous
virtual const ejson::Value* toValue(void) const {
const ejson::Value* toValue(void) const {
return this;
};
/**
* @brief Cast the element in a Document if it is possible.
* @return pointer on the class or NULL.
*/
virtual ejson::Document* toDocument(void) {
return NULL;
};
ejson::Document* toDocument(void);
//! @previous
virtual const ejson::Document* toDocument(void) const {
return NULL;
};
const ejson::Document* toDocument(void) const;
/**
* @brief Cast the element in a Array if it is possible.
* @return pointer on the class or NULL.
*/
virtual ejson::Array* toArray(void) {
return NULL;
};
ejson::Array* toArray(void);
//! @previous
virtual const ejson::Array* toArray(void) const{
return NULL;
};
const ejson::Array* toArray(void) const;
/**
* @brief Cast the element in a Object if it is possible.
* @return pointer on the class or NULL.
*/
virtual ejson::Object* toObject(void) {
return NULL;
};
ejson::Object* toObject(void);
//! @previous
virtual const ejson::Object* toObject(void) const{
return NULL;
};
const ejson::Object* toObject(void) const;
/**
* @brief Cast the element in a String if it is possible.
* @return pointer on the class or NULL.
*/
virtual ejson::String* toString(void) {
return NULL;
};
ejson::String* toString(void);
//! @previous
virtual const ejson::String* toString(void) const{
return NULL;
};
const ejson::String* toString(void) const;
/**
* @brief Cast the element in a Number if it is possible.
* @return pointer on the class or NULL.
*/
virtual ejson::Number* toNumber(void) {
return NULL;
};
ejson::Number* toNumber(void);
//! @previous
virtual const ejson::Number* toNumber(void) const{
return NULL;
};
const ejson::Number* toNumber(void) const;
/**
* @brief Cast the element in a Boolean if it is possible.
* @return pointer on the class or NULL.
*/
virtual ejson::Boolean* toBoolean(void) {
return NULL;
};
ejson::Boolean* toBoolean(void);
//! @previous
virtual const ejson::Boolean* toBoolean(void) const{
return NULL;
};
const ejson::Boolean* toBoolean(void) const;
/**
* @brief Cast the element in a Null if it is possible.
* @return pointer on the class or NULL.
*/
virtual ejson::Null* toNull(void) {
return NULL;
};
ejson::Null* toNull(void);
//! @previous
virtual const ejson::Null* toNull(void) const{
return NULL;
};
const ejson::Null* toNull(void) const;
/**
* @brief check if the node is a ejson::Document
* @return true if the node is a ejson::Document
*/
bool isDocument(void) const {
return getType() == ejson::typeDocument;
return toDocument() != NULL;
};
/**
* @brief check if the node is a ejson::Array
* @return true if the node is a ejson::Array
*/
bool isArray(void) const {
return getType() == ejson::typeArray;
return toArray() != NULL;
};
/**
* @brief check if the node is a ejson::Object
* @return true if the node is a ejson::Object
*/
bool isObject(void) const {
return getType() == ejson::typeObject;
return toObject() != NULL;
};
/**
* @brief check if the node is a ejson::String
* @return true if the node is a ejson::String
*/
bool isString(void) const {
return getType() == ejson::typeString;
return toString() != NULL;
};
/**
* @brief check if the node is a ejson::Number
* @return true if the node is a ejson::Number
*/
bool isNumber(void) const {
return getType() == ejson::typeNumber;
return toNumber() != NULL;
};
/**
* @brief check if the node is a ejson::Boolean
* @return true if the node is a ejson::Boolean
*/
bool isBoolean(void) const {
return getType() == ejson::typeBoolean;
return toBoolean() != NULL;
};
/**
* @brief check if the node is a ejson::Null
* @return true if the node is a ejson::Null
*/
bool isNull(void) const {
return getType() == ejson::typeNull;
return toNull() != NULL;
};
/**
@ -353,5 +303,12 @@ namespace ejson {
};
};
#include <ejson/Array.h>
#include <ejson/Boolean.h>
#include <ejson/Null.h>
#include <ejson/Number.h>
#include <ejson/Object.h>
#include <ejson/String.h>
#endif

9
ejson/ejson.h Normal file → Executable file
View File

@ -76,17 +76,8 @@ namespace ejson {
void createError(const std::string& _data, size_t _pos, const ejson::filePos& _filePos, const std::string& _comment);
void displayError(void);
public: // herited function:
virtual enum nodeType getType(void) const {
return typeDocument;
};
virtual bool iParse(const std::string& _data, size_t& _pos, ejson::filePos& _filePos, ejson::Document& _doc);
virtual bool iGenerate(std::string& _data, size_t _indent) const;
virtual ejson::Document* toDocument(void) {
return this;
};
virtual const ejson::Document* toDocument(void) const {
return this;
};
};
};