Hurricane VLSI Database


JsonObject.h
1 // -*- C++ -*-
2 //
3 // Copyright (c) BULL S.A. 2015-2018, All Rights Reserved
4 //
5 // This file is part of Hurricane.
6 //
7 // Hurricane is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
11 //
12 // Hurricane is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-
14 // TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the Lesser GNU General Public
18 // License along with Hurricane. If not, see
19 // <http://www.gnu.org/licenses/>.
20 //
21 // +-----------------------------------------------------------------+
22 // | H U R R I C A N E |
23 // | V L S I B a c k e n d D a t a - B a s e |
24 // | |
25 // | Author : Jean-Paul Chaput |
26 // | E-mail : Jean-Paul.Chaput@lip6.fr |
27 // | =============================================================== |
28 // | C++ Header : "./hurricane/JsonObject.h" |
29 // +-----------------------------------------------------------------+
30 
31 
32 #ifndef HURRICANE_JSON_OBJECT_H
33 #define HURRICANE_JSON_OBJECT_H
34 
35 #ifndef HURRICANE_SLOT_H
36 #error "JsonObject.h must be included through Commons.h"
37 #endif
38 
39 #include <iostream>
40 #include <typeinfo>
41 #include <typeindex>
42 #include <type_traits>
43 #include <set>
44 #include <map>
45 #include <string>
46 #include <functional>
47 #include <boost/any.hpp>
48 
49 
50 namespace Hurricane {
51 
52  class DBo;
53  class Hook;
54  class Entity;
55  class Cell;
56  class Component;
57  class JsonStack;
58 
59  class JsonArray { };
60 
61 
62 // -------------------------------------------------------------------
63 // Class : "JsonAttribute".
64 
65  class JsonAttribute {
66  public:
67  inline JsonAttribute ( const std::string& key, std::type_index tid );
68  inline std::string key () const;
69  inline std::type_index tid () const;
70  private:
71  std::string _key;
72  std::type_index _tid;
73  };
74 
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; }
78 
79 
80 // -------------------------------------------------------------------
81 // Class : "JsonObject".
82 
83  class JsonObject {
84  public:
85  JsonObject ( unsigned long flags );
86  virtual ~JsonObject ();
87  virtual bool isDummy () const;
88  virtual std::string getTypeName () const = 0;
89  inline std::string getStackName () const;
90  bool check ( JsonStack&, std::string fname ) 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 );
98  inline void clear ();
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;
104  virtual JsonObject* clone ( unsigned long flags ) const = 0;
105  virtual void toData ( JsonStack& );
106  unsigned int presetId ( JsonStack& );
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;
111  protected:
112  unsigned long _flags;
113  std::string _name;
114  std::vector<JsonAttribute> _stackeds;
115  std::vector<JsonAttribute> _attributes;
116  std::vector<JsonAttribute> _collections;
117  boost::any _object;
118  };
119 
120 
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; }
127 
128  inline std::string JsonObject::getStackName () const
129  { return (_name.empty()) ? std::string(".")+getTypeName(): _name; }
130 
131  template<typename T> inline T JsonObject::getObject () const
132  { return boost::any_cast<T>(_object); }
133 
134  template<typename T> inline void JsonObject::setObject ( T t )
135  { _object = t; }
136 
137  inline void JsonObject::copyAttrs ( const JsonObject* other, bool reset )
138  {
139  if (reset) _attributes.clear();
140  for ( size_t i=0 ; i<other->_attributes.size() ; ++i )
141  _attributes.push_back( other->_attributes[i] );
142  }
143 
144  void JsonObject::clear ()
145  {
146  _stackeds.clear();
147  _attributes.clear();
148  _collections.clear();
149  boost::any emptyAny;
150  _object.swap( emptyAny );
151  }
152 
153 
154 // -------------------------------------------------------------------
155 // Class : "JsonKey".
156 
157  class JsonKey : public JsonObject {
158  public:
159  inline JsonKey ( const std::string& );
160  virtual std::string getTypeName () const;
161  virtual JsonKey* clone ( unsigned long ) const;
162  private:
163  std::string _key;
164  };
165 
166 
167 // -------------------------------------------------------------------
168 // Class : "JsonDummy".
169 
170  class JsonDummy : public JsonObject {
171  public:
172  JsonDummy ();
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;
177  private:
178  std::string _typename;
179  };
180 
181 
182 // -------------------------------------------------------------------
183 // Class : "JsonBaseArray".
184 
185  template<typename T>
186  class JsonBaseArray : public JsonObject {
187  public:
188  inline JsonBaseArray ( unsigned long flags );
189  inline const std::vector<T>& array () const;
190  inline void push_back ( T );
191  private:
192  std::vector<T> _array;
193  };
194 
195 
196  template<typename T>
197  inline JsonBaseArray<T>::JsonBaseArray ( unsigned long flags )
198  : JsonObject(flags)
199  , _array()
200  { }
201 
202  template<typename T>
203  inline const std::vector<T>& JsonBaseArray<T>::array () const { return _array; }
204 
205  template<typename T>
206  inline void JsonBaseArray<T>::push_back ( T element )
207  { _array.push_back(element); }
208 
209 
210 } // Hurricane namespace.
211 
212 
213 namespace std {
214 
215  template<>
216  struct less<Hurricane::JsonObject*> {
217  inline bool operator() ( const Hurricane::JsonObject* lhs, const Hurricane::JsonObject* rhs ) const
218  { return lhs->getTypeName() < rhs->getTypeName(); }
219  };
220 
221 } // std namespace.
222 
223 
224 namespace Hurricane {
225 
226 // -------------------------------------------------------------------
227 // Class : "JsonTypes".
228 
229  class JsonTypes {
230  public:
231  static void initialize ();
232  static void registerType ( JsonObject* );
233  static JsonObject* find ( const std::string& tname );
234  private:
235  JsonTypes ();
236  ~JsonTypes ();
237  JsonTypes ( const JsonTypes& );
238  void _registerType ( JsonObject* );
239  JsonObject* _find ( const std::string& tname );
240  private:
241  static JsonTypes* _jsonTypes;
242  std::set<JsonObject*> _jsonObjects;
243  };
244 
245 
246 // -------------------------------------------------------------------
247 // Class : "JsonStack".
248 
249  class JsonStack {
250  public:
251  typedef std::pair<std::string, boost::any> Element;
252  public:
253  inline JsonStack ();
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;
260  inline void push_back_dbo ( DBo* );
261  inline void pop_back_dbo ();
262  inline DBo* back_dbo () const;
263  inline vector<JsonObject*>& jobjects ();
264  template<typename T> inline T getEntity ( unsigned int ) const;
265  void addEntity ( unsigned int jsonId, Entity* );
266  void print ( std::ostream& ) 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;
272  private:
273  unsigned long _flags;
274  vector<Element> _stack;
275  vector<JsonObject*> _jstack;
276  vector<DBo*> _dbos;
277  std::map<unsigned int,Entity*> _entities;
278  };
279 
280 
281  inline JsonStack::JsonStack ()
282  : _flags(0), _stack(), _jstack(), _dbos(), _entities()
283  { }
284 
285  template<typename T> inline void JsonStack::push_back ( const std::string& key, T t )
286  {
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)));
290  }
291 
292  inline void JsonStack::pop_back ( size_t count )
293  {
294  while (count--) {
295  if (_stack.empty()) {
296  std::cerr << "[ERROR] JsonStack::pop_back(): Stack is empty, but "
297  << (count+1) << " elements remains to pop." << std::endl;
298  break;
299  }
300  cdebug_log(19,0) << "| _stack.pop_back() \"" << _stack.back().first
301  << "\", size:" << _stack.size() << ", dbos:" << _dbos.size() << endl;
302  _stack.pop_back();
303  }
304  }
305 
306  inline const JsonStack::Element& JsonStack::operator[] ( int index ) const
307  {
308  if (index < 0) return _stack[_stack.size()+index];
309  return _stack[index];
310  }
311 
312  inline int JsonStack::rhas ( const std::string& key ) const
313  {
314  if (_stack.empty()) return 0;
315 
316  int i = _stack.size()-1;
317  do {
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()) << ")."
322  << endl;
323  return i-(int)_stack.size();
324  }
325  if (i == 0) break;
326  --i;
327  } while ( true );
328 
329  cdebug_log(19,0) << "JsonStack::rhas(): key \"" << key << "\" not found (returning index: 0)." << endl;
330  return 0;
331  }
332 
333  template<typename T> inline T JsonStack::as ( const std::string& key ) const
334  {
335  if (not _stack.empty()) {
336  int i = _stack.size()-1;
337  do {
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 );
342  }
343  if (i == 0) break;
344  --i;
345  } while ( true );
346 
347  std::cerr << "[ERROR] JsonStack::as<T>(key): No element with key \""
348  << key << "\" in stack." << std::endl;
349  } else {
350  std::cerr << "[ERROR] JsonStack::as<T>(key): Stack is empty while searching for key \""
351  << key << "\"." << std::endl;
352  }
353 
354  return T();
355  }
356 
357  template<typename T> inline T JsonStack::as ( int index ) const
358  {
359  size_t i = (index >= 0) ? index : (_stack.size()+index);
360  return boost::any_cast<T>( _stack[i].second );
361  }
362 
363 
364  template<typename T> inline T JsonStack::getEntity ( unsigned int id ) const
365  {
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);
369  }
370 
371  inline void JsonStack::push_back_dbo ( DBo* dbo ) { _dbos.push_back(dbo); }
372  inline void JsonStack::pop_back_dbo () { _dbos.pop_back(); }
373  inline DBo* JsonStack::back_dbo () const { return (_dbos.empty()) ? NULL : _dbos.back(); }
374  inline vector<JsonObject*>& JsonStack::jobjects () { return _jstack; }
375  inline size_t JsonStack::size () const { return _stack.size(); }
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; }
380 
381  template<typename T>
382  T JsonObject::get ( JsonStack& stack, const std::string& key ) const
383  {
384  int index = stack.rhas(key);
385  if (index == 0) return T();;
386  if (stack[index].second.type() == typeid(void*)) return T();
387 
388  return stack.as<T>( index );
389  }
390 
391  template<typename T>
392  T* JsonObject::jget ( JsonStack& stack ) const
393  {
394  vector<JsonObject*>& jobjects = stack.jobjects();
395  vector<JsonObject*>::reverse_iterator rit = jobjects.rbegin();
396 
397  for ( ; rit != jobjects.rend() ; ++rit ) {
398  T* jobject = dynamic_cast<T*>( *rit );
399  if (jobject) return jobject;
400  }
401 
402  return NULL;
403  }
404 
405  template<typename T>
406  inline void JsonObject::update ( JsonStack& stack, T hobject )
407  {
408  cdebug_log(19,0) << "JsonObject::update<T>()" << endl;
409  stack.pop_back( _attributes.size() );
410  stack.push_back<T>( getStackName(), hobject );
411  setObject<T>( hobject );
412  }
413 
414 
415 // -------------------------------------------------------------------
416 // Function : Json Parsers.
417 
418  Cell* jsonCellParse ( std::string filename );
419  Cell* jsonBlobParse ( std::string filename );
420 
421 
422 } // Hurricane namespace.
423 
424 #endif // HURRICANE_JSON_OBJECT_H
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)


Generated by doxygen 1.9.1 on Thu Aug 11 2022 Return to top of page
Hurricane VLSI Database Copyright © 2000-2020 Bull S.A. All rights reserved