32 #ifndef HURRICANE_JSON_OBJECT_H
33 #define HURRICANE_JSON_OBJECT_H
35 #ifndef HURRICANE_SLOT_H
36 #error "JsonObject.h must be included through Commons.h"
42 #include <type_traits>
47 #include <boost/any.hpp>
67 inline JsonAttribute (
const std::string& key, std::type_index tid );
68 inline std::string key ()
const;
69 inline std::type_index tid ()
const;
75 inline JsonAttribute::JsonAttribute (
const std::string& key, std::type_index tid ) : _key(key), _tid(tid) { }
76 inline std::string JsonAttribute::key ()
const {
return _key; }
77 inline std::type_index JsonAttribute::tid ()
const {
return _tid; }
89 inline std::string getStackName ()
const;
91 void print ( std::ostream& )
const;
92 bool has (
const std::string& key )
const;
93 void add (
const std::string& key, std::type_index tid );
94 void remove (
const std::string& key );
95 template<
typename T>
inline T get (
JsonStack&,
const std::string& key )
const;
96 template<
typename T>
inline T* jget (
JsonStack& )
const;
97 inline void copyAttrs (
const JsonObject*,
bool reset=
false );
99 inline std::string getName ()
const;
100 inline void setName (
const std::string& );
101 template<
typename T>
inline T getObject ()
const;
102 template<
typename T>
inline void setObject ( T ) ;
103 inline bool isBound ()
const;
107 template<
typename T>
inline void update (
JsonStack&, T );
108 inline JsonObject* setFlags (
unsigned long mask );
109 inline JsonObject* resetFlags (
unsigned long mask );
110 inline bool issetFlags (
unsigned long mask )
const;
112 unsigned long _flags;
114 std::vector<JsonAttribute> _stackeds;
115 std::vector<JsonAttribute> _attributes;
116 std::vector<JsonAttribute> _collections;
121 inline bool JsonObject::isBound ()
const {
return not _object.empty(); }
122 inline std::string JsonObject::getName ()
const {
return _name; }
123 inline void JsonObject::setName (
const string& name ) { _name=name; }
124 inline JsonObject* JsonObject::setFlags (
unsigned long mask ) { _flags |= mask;
return this; }
125 inline JsonObject* JsonObject::resetFlags (
unsigned long mask ) { _flags &= ~mask;
return this; }
126 inline bool JsonObject::issetFlags (
unsigned long mask )
const {
return _flags & mask; }
128 inline std::string JsonObject::getStackName ()
const
129 {
return (_name.empty()) ? std::string(
".")+getTypeName(): _name; }
131 template<
typename T>
inline T JsonObject::getObject ()
const
132 {
return boost::any_cast<T>(_object); }
134 template<
typename T>
inline void JsonObject::setObject ( T t )
137 inline void JsonObject::copyAttrs (
const JsonObject* other,
bool reset )
139 if (reset) _attributes.clear();
140 for (
size_t i=0 ; i<other->_attributes.size() ; ++i )
141 _attributes.push_back( other->_attributes[i] );
144 void JsonObject::clear ()
148 _collections.clear();
150 _object.swap( emptyAny );
159 inline JsonKey (
const std::string& );
161 virtual JsonKey*
clone (
unsigned long )
const;
170 class JsonDummy :
public JsonObject {
173 virtual bool isDummy ()
const;
174 virtual std::string getTypeName ()
const;
175 void setTypeName (
const std::string& name );
176 virtual JsonDummy* clone (
unsigned long )
const;
178 std::string _typename;
186 class JsonBaseArray :
public JsonObject {
188 inline JsonBaseArray (
unsigned long flags );
189 inline const std::vector<T>& array ()
const;
190 inline void push_back ( T );
192 std::vector<T> _array;
197 inline JsonBaseArray<T>::JsonBaseArray (
unsigned long flags )
203 inline const std::vector<T>& JsonBaseArray<T>::array ()
const {
return _array; }
206 inline void JsonBaseArray<T>::push_back ( T element )
207 { _array.push_back(element); }
231 static void initialize ();
232 static void registerType ( JsonObject* );
233 static JsonObject* find (
const std::string& tname );
237 JsonTypes (
const JsonTypes& );
238 void _registerType ( JsonObject* );
239 JsonObject* _find (
const std::string& tname );
241 static JsonTypes* _jsonTypes;
242 std::set<JsonObject*> _jsonObjects;
251 typedef std::pair<std::string, boost::any> Element;
254 inline size_t size ()
const;
255 template<
typename T>
inline void push_back (
const std::string&, T );
256 inline void pop_back (
size_t count=1 );
257 inline int rhas (
const std::string& )
const;
258 template<
typename T>
inline T
as (
const std::string& )
const;
259 template<
typename T>
inline T
as (
int )
const;
263 inline vector<JsonObject*>&
jobjects ();
264 template<
typename T>
inline T
getEntity (
unsigned int )
const;
267 inline unsigned long getFlags ()
const;
268 inline JsonStack* setFlags (
unsigned long mask );
269 inline JsonStack* resetFlags (
unsigned long mask );
270 inline bool issetFlags (
unsigned long mask )
const;
271 inline const Element& operator[] (
int index )
const;
273 unsigned long _flags;
274 vector<Element> _stack;
275 vector<JsonObject*> _jstack;
277 std::map<unsigned int,Entity*> _entities;
281 inline JsonStack::JsonStack ()
282 : _flags(0), _stack(), _jstack(), _dbos(), _entities()
287 cdebug_log(19,0) <<
"JsonStack::push_back(T) key:" << key <<
" value:" << t
288 <<
" (" <<
demangle(
typeid(T)) <<
")." << endl;
289 _stack.push_back(std::make_pair(key,boost::any(t)));
295 if (_stack.empty()) {
296 std::cerr <<
"[ERROR] JsonStack::pop_back(): Stack is empty, but "
297 << (count+1) <<
" elements remains to pop." << std::endl;
300 cdebug_log(19,0) <<
"| _stack.pop_back() \"" << _stack.back().first
301 <<
"\", size:" << _stack.size() <<
", dbos:" << _dbos.size() << endl;
306 inline const JsonStack::Element& JsonStack::operator[] (
int index )
const
308 if (index < 0)
return _stack[_stack.size()+index];
309 return _stack[index];
314 if (_stack.empty())
return 0;
316 int i = _stack.size()-1;
318 if (_stack[i].first == key) {
319 cdebug_log(19,0) <<
"JsonStack::rhas(): key \"" << key <<
"\" found at index:"
320 << (i-(int)_stack.size()) <<
" (i:" << i <<
") "
321 <<
"(" <<
demangle(_stack[i].second.type().name()) <<
")."
323 return i-(int)_stack.size();
329 cdebug_log(19,0) <<
"JsonStack::rhas(): key \"" << key <<
"\" not found (returning index: 0)." << endl;
333 template<
typename T>
inline T
JsonStack::as (
const std::string& key )
const
335 if (not _stack.empty()) {
336 int i = _stack.size()-1;
338 if (_stack[i].first == key) {
339 cdebug_log(19,0) <<
"JsonStack::as<T>() k:" << key
340 <<
" value:" <<
demangle(_stack[i].second.type().name()) << std::endl;
341 return boost::any_cast<T>( _stack[i].second );
347 std::cerr <<
"[ERROR] JsonStack::as<T>(key): No element with key \""
348 << key <<
"\" in stack." << std::endl;
350 std::cerr <<
"[ERROR] JsonStack::as<T>(key): Stack is empty while searching for key \""
351 << key <<
"\"." << std::endl;
359 size_t i = (index >= 0) ? index : (_stack.size()+index);
360 return boost::any_cast<T>( _stack[i].second );
366 std::map<unsigned int,Entity*>::const_iterator it = _entities.find(
id);
367 if (it == _entities.end())
return NULL;
368 return dynamic_cast<T
>((*it).second);
376 inline unsigned long JsonStack::getFlags ()
const {
return _flags; }
377 inline JsonStack* JsonStack::setFlags (
unsigned long mask ) { _flags |= mask;
return this; }
378 inline JsonStack* JsonStack::resetFlags (
unsigned long mask ) { _flags &= ~mask;
return this; }
379 inline bool JsonStack::issetFlags (
unsigned long mask )
const {
return _flags & mask; }
384 int index = stack.
rhas(key);
385 if (index == 0)
return T();;
386 if (stack[index].second.type() ==
typeid(
void*))
return T();
388 return stack.
as<T>( index );
394 vector<JsonObject*>& jobjects = stack.
jobjects();
395 vector<JsonObject*>::reverse_iterator rit = jobjects.rbegin();
397 for ( ; rit != jobjects.rend() ; ++rit ) {
398 T* jobject =
dynamic_cast<T*
>( *rit );
399 if (jobject)
return jobject;
408 cdebug_log(19,0) <<
"JsonObject::update<T>()" << endl;
409 stack.
pop_back( _attributes.size() );
411 setObject<T>( hobject );
418 Cell* jsonCellParse ( std::string filename );
419 Cell* jsonBlobParse ( std::string filename );
The model (API).
Definition: Cell.h:64
DataBase object root class (API).
Definition: DBo.h:45
Occurrenceable objects root class (API).
Definition: Entity.h:37
Support for JSON export.
Definition: JsonObject.h:83
std::string getStackName() const
Definition: JsonObject.h:128
unsigned int presetId(JsonStack &)
void add(const std::string &key, std::type_index tid)
virtual void toData(JsonStack &)
JsonObject(unsigned long flags)
void remove(const std::string &key)
bool check(JsonStack &, std::string fname) const
T * jget(JsonStack &) const
Definition: JsonObject.h:392
virtual bool isDummy() const
virtual JsonObject * clone(unsigned long flags) const =0
virtual std::string getTypeName() const =0
bool has(const std::string &key) const
void update(JsonStack &, T)
Definition: JsonObject.h:406
T get(JsonStack &, const std::string &key) const
Definition: JsonObject.h:382
JSON Parser Stack.
Definition: JsonObject.h:249
void push_back(const std::string &, T)
Definition: JsonObject.h:285
void addEntity(unsigned int jsonId, Entity *)
int rhas(const std::string &) const
Definition: JsonObject.h:312
void pop_back(size_t count=1)
Definition: JsonObject.h:292
void print(std::ostream &) const
size_t size() const
Definition: JsonObject.h:375
DBo * back_dbo() const
Definition: JsonObject.h:373
vector< JsonObject * > & jobjects()
Definition: JsonObject.h:374
T getEntity(unsigned int) const
Definition: JsonObject.h:364
void push_back_dbo(DBo *)
Definition: JsonObject.h:371
void pop_back_dbo()
Definition: JsonObject.h:372
T as(const std::string &) const
Definition: JsonObject.h:333
The namespace dedicated to Hurricane.
Definition: Generalities.dox:5
string demangle(const char *symbol)