Hurricane VLSI Database


Property.h
1 // -*- C++ -*-
2 //
3 // Copyright (c) BULL S.A. 2000-2021, 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 : Remy Escassut |
26 // | E-mail : Jean-Paul.Chaput@lip6.fr |
27 // | =============================================================== |
28 // | C++ Header : "./hurricane/Property.h" |
29 // +-----------------------------------------------------------------+
30 
31 
32 #pragma once
33 #include "hurricane/Name.h"
34 #include "hurricane/Properties.h"
35 #include "hurricane/DBo.h"
36 #include "hurricane/Error.h"
37 
38 
39 namespace Hurricane {
40 
41 
42  extern const char* propertyTypeNameError;
43 
44 
45 // -------------------------------------------------------------------
46 // Classes : template enable/disable Json support.
47 
48  struct JsonEnabled { enum State { enabled=1 }; };
49  struct JsonDisabled { enum State { enabled=0 }; };
50 
51 
52 // -------------------------------------------------------------------
53 // Class : "Hurricane::Property".
54 
55 
56  class Property {
57 
58  public:
59  // Static Method.
60  template<typename DerivedProperty>
61  static DerivedProperty* get ( const DBo* );
62  static Name staticGetName ();
63  // Constructor.
64  template<typename DerivedProperty>
65  static DerivedProperty* create ();
66  template<typename DerivedProperty, typename Value>
67  static DerivedProperty* create ( const Value& );
68  // Destructor.
69  virtual void destroy ();
70  // Methods.
71  virtual Name getName () const = 0;
72  virtual void onCapturedBy ( DBo* owner ) = 0;
73  virtual void onReleasedBy ( DBo* owner ) = 0;
74  // Hurricane Managment.
75  virtual bool hasJson () const;
76  virtual void toJson ( JsonWriter*, const DBo* ) const;
77  virtual string _getTypeName () const = 0;
78  virtual string _getString () const;
79  virtual Record* _getRecord () const;
80 
81  private:
82  static Name _baseName;
83  protected:
84  // Internal: Constructors & Destructors.
85  Property ();
86  virtual ~Property ();
87  virtual void _postCreate () {};
88  virtual void _preDestroy () {};
89  private:
90  Property ( const Property& );
91  Property& operator= ( const Property& );
92  };
93 
94 
95  template<typename DerivedProperty>
96  DerivedProperty* Property::create ()
97  {
98  DerivedProperty* property = new DerivedProperty();
99  property->_postCreate();
100  return property;
101  }
102 
103 
104  template<typename DerivedProperty, typename Value>
105  DerivedProperty* Property::create ( const Value& value )
106  {
107  DerivedProperty* property = new DerivedProperty(value);
108  property->_postCreate();
109  return property;
110  }
111 
112 
113  template<typename DerivedProperty>
114  DerivedProperty* Property::get ( const DBo* object )
115  {
116  Property* property1 = object->getProperty ( DerivedProperty::staticGetName() );
117  DerivedProperty* property2 = dynamic_cast<DerivedProperty*> ( property1 );
118 
119  if ( property1 && !property2 )
120  throw Error ( propertyTypeNameError
121  , getString(DerivedProperty::staticGetName()).c_str()
122  , getString(object).c_str() );
123 
124  return property2;
125  }
126 
127 
128 // -------------------------------------------------------------------
129 // Class : "Hurricane::PrivateProperty".
130 
131 
132  class PrivateProperty : public Property {
133 
134  public:
135  // Methods.
136  inline DBo* getOwner () const;
137  virtual void onCapturedBy ( DBo* owner );
138  virtual void onReleasedBy ( DBo* owner );
139  virtual void onNotOwned ();
140  virtual string _getString () const;
141  virtual Record* _getRecord () const;
142 
143  private:
144  // Internal: Attributes.
145  DBo* _owner;
146  protected:
147  // Internal: Constructor & Destructors.
148  PrivateProperty ();
149  virtual void _preDestroy ();
150  };
151 
152 
153 // Inline Functions.
154  inline DBo* PrivateProperty::getOwner () const { return _owner; };
155 
156 
157 // -------------------------------------------------------------------
158 // Template Class : "Hurricane::StandardPrivateProperty".
159 
160 
161  template<typename Value, typename JsonState=JsonDisabled>
163  public:
164  static Name staticGetName ();
165  static Value* staticGetValue ( const DBo* );
166  static StandardPrivateProperty* get ( const DBo*, bool create=false );
167  // Constructors.
168  static StandardPrivateProperty* create ();
169  static StandardPrivateProperty* create ( const Value& );
170  // Methods.
171  virtual Name getName () const;
172  Value& getValue () const;
173  void setValue ( const Value& );
174  virtual bool hasJson () const;
175  virtual void toJson ( JsonWriter*, const DBo* ) const;
176  virtual string _getTypeName () const;
177  virtual string _getString () const;
178  virtual Record* _getRecord () const;
179  private:
180  // Internal: Attributes.
181  static Name _name;
182  static DBo* _owner;
183  static StandardPrivateProperty* _cache;
184  mutable Value _value;
185  protected:
186  // Internal: Constructor.
188  StandardPrivateProperty ( const Value& );
189  public:
190  class JsonProperty : public JsonObject {
191  public:
192  static void initialize ();
193  JsonProperty ( unsigned long flags );
194  virtual string getTypeName () const;
195  virtual JsonProperty* clone ( unsigned long ) const;
196  virtual void toData ( JsonStack& );
197  };
198  };
199 
200 
201  template<typename Value, typename JsonState>
203  : JsonObject(flags)
204  {
205  if (flags & JsonWriter::RegisterMode)
206  cerr << "Registering JsonProperty" << endl;
207  add( "_value", typeid(Value) );
208  }
209 
210 
211  template<typename Value, typename JsonState>
212  string StandardPrivateProperty<Value,JsonState>::JsonProperty::getTypeName () const
213  { return getString(StandardPrivateProperty<Value,JsonState>::staticGetName()); }
214 
215 
216  template<typename Value, typename JsonState>
217  void StandardPrivateProperty<Value,JsonState>::JsonProperty::initialize ()
218  { JsonTypes::registerType( new JsonProperty (JsonWriter::RegisterMode) ); }
219 
220 
221  template<typename Value, typename JsonState>
222  typename StandardPrivateProperty<Value,JsonState>::JsonProperty*
223  StandardPrivateProperty<Value,JsonState>::JsonProperty::clone ( unsigned long flags ) const
224  { return new JsonProperty ( flags ); }
225 
226 
227  template<typename Value, typename JsonState>
228  void StandardPrivateProperty<Value,JsonState>::JsonProperty::toData ( JsonStack& stack )
229  {
230  check( stack, "JsonProperty::toData" );
231 
232  DBo* dbo = stack.back_dbo();
233  Value value = get<string>(stack,"_value");
234  StandardPrivateProperty<Value,JsonState>* property
235  = StandardPrivateProperty<Value,JsonState>::create(value);
236  if (dbo) dbo->put( property );
237 
238  update( stack, property );
239  }
240 
241 
242  template<typename Value, typename JsonState>
243  DBo* StandardPrivateProperty<Value,JsonState>::_owner = NULL;
244 
245 
246  template<typename Value, typename JsonState>
247  StandardPrivateProperty<Value,JsonState>* StandardPrivateProperty<Value,JsonState>::_cache = NULL;
248 
249 
250  template<typename Value, typename JsonState>
251  Name StandardPrivateProperty<Value,JsonState>::staticGetName ()
252  {
253  return _name;
254  }
255 
256 
257  template<typename Value, typename JsonState>
258  Value* StandardPrivateProperty<Value,JsonState>::staticGetValue ( const DBo* object )
259  {
260  if ( ( object == _owner ) || get(object) ) return _cache->getValue();
261  return NULL;
262  }
263 
264 
265  template<typename Value, typename JsonState>
266  StandardPrivateProperty<Value,JsonState>* StandardPrivateProperty<Value,JsonState>::create ()
267  {
268  _cache = new StandardPrivateProperty<Value>();
269  _cache->_postCreate();
270  return _cache;
271  }
272 
273 
274  template<typename Value, typename JsonState>
275  StandardPrivateProperty<Value,JsonState>* StandardPrivateProperty<Value,JsonState>::create ( const Value& value )
276  {
277  _cache = new StandardPrivateProperty<Value>(value);
278  _cache->_postCreate();
279  return _cache;
280  }
281 
282 
283  template<typename Value, typename JsonState>
284  StandardPrivateProperty<Value,JsonState>* StandardPrivateProperty<Value,JsonState>::get ( const DBo* object, bool create )
285  {
286  if ( object == _owner ) return _cache;
287 
288  Property* property = object->getProperty ( StandardPrivateProperty<Value>::staticGetName() );
289  _cache = dynamic_cast<StandardPrivateProperty<Value>*> ( property );
290 
291  if ( !_cache ) {
292  if ( property )
293  throw Error ( propertyTypeNameError
294  , getString(StandardPrivateProperty<Value>::staticGetName()).c_str()
295  , getString(object).c_str() );
296  else if ( create )
297  const_cast<DBo*>(object)->put ( StandardPrivateProperty<Value>::create() );
298  }
299 
300  return _cache;
301  }
302 
303 
304  template<typename Value, typename JsonState>
305  StandardPrivateProperty<Value,JsonState>::StandardPrivateProperty ()
306  : PrivateProperty(), _value()
307  { }
308 
309 
310  template<typename Value, typename JsonState>
311  StandardPrivateProperty<Value,JsonState>::StandardPrivateProperty ( const Value& value )
312  : PrivateProperty(), _value(value)
313  { }
314 
315 
316  template<typename Value, typename JsonState>
317  Name StandardPrivateProperty<Value,JsonState>::getName() const
318  {
319  return staticGetName();
320  }
321 
322 
323  template<typename Value, typename JsonState>
324  Value& StandardPrivateProperty<Value,JsonState>::getValue () const
325  {
326  return _value;
327  }
328 
329 
330  template<typename Value, typename JsonState>
331  void StandardPrivateProperty<Value,JsonState>::setValue ( const Value& value )
332  {
333  _value = value;
334  }
335 
336 
337  template<typename Value, typename JsonState>
338  bool StandardPrivateProperty<Value,JsonState>::hasJson () const
339  {
340  return JsonState::enabled;
341  }
342 
343 
344  template<typename Value, typename JsonState>
345  void StandardPrivateProperty<Value,JsonState>::toJson ( JsonWriter* w, const DBo* ) const
346  {
347  w->startObject();
348  std::string tname = getString(staticGetName());
349  jsonWrite( w, "@typename", tname );
350  jsonWrite( w, "_value", _value );
351  w->endObject();
352  }
353 
354 
355  template<typename Value, typename JsonState>
356  string StandardPrivateProperty<Value,JsonState>::_getTypeName () const
357  {
358  return _TName("StandardPrivateProperty");
359  }
360 
361  template<typename Value, typename JsonState>
362  string StandardPrivateProperty<Value,JsonState>::_getString () const
363  {
364  string s = PrivateProperty::_getString();
365  s.insert(s.length() - 1, " " + getString<Value&>(_value));
366  return s;
367  }
368 
369  template<typename Value, typename JsonState>
370  Record* StandardPrivateProperty<Value,JsonState>::_getRecord () const
371  {
372  Record* record = PrivateProperty::_getRecord();
373  if (record) {
374  record->add ( getSlot("_name" , staticGetName()) );
375  record->add ( getSlot("_value" ,&_value) );
376  record->add ( getSlot("JSON support", JsonState::enabled) );
377  }
378  return record;
379  }
380 
381 
382 // -------------------------------------------------------------------
383 // Class : "Hurricane::SharedProperty".
384 
385 
386  class SharedProperty : public Property {
387  private:
388  class Orphaned {
389  public:
390  inline Orphaned ( SharedProperty* );
391  public:
392  SharedProperty* _property;
393  unsigned int _refcount;
394  unsigned int _count;
395  };
396  public:
397  typedef set<DBo*,DBo::CompareById> DBoSet;
398  typedef map<string,Orphaned> OrphanedMap;
399  public:
400  static const OrphanedMap& getOrphaneds ();
401  static SharedProperty* getOrphaned ( const string& );
402  static void addOrphaned ( const string&, SharedProperty* );
403  static void refOrphaned ( const string& );
404  static void countOrphaned ( const string&, unsigned int );
405  static void removeOrphaned ( const string& );
406  static void clearOrphaneds ();
407  public:
408  inline DBos getOwners () const;
409  virtual void onCapturedBy ( DBo* owner );
410  virtual void onReleasedBy ( DBo* owner );
411  virtual void onNotOwned ();
412  void _erase ( DBo* owner );
413  inline DBoSet& _getOwnerSet ();
414  virtual string _getString () const;
415  virtual Record* _getRecord () const;
416  private:
417  static OrphanedMap _orphaneds;
418  private:
419  DBoSet _ownerSet;
420  protected:
421  SharedProperty ();
422  virtual void _preDestroy ();
423  };
424 
425 
426 // Inline Functions.
427  inline SharedProperty::Orphaned::Orphaned ( SharedProperty* property )
428  : _property(property), _refcount(0), _count(0)
429  { }
430 
431  inline DBos SharedProperty::getOwners () const { return getCollection(_ownerSet); }
432  inline SharedProperty::DBoSet& SharedProperty::_getOwnerSet () { return _ownerSet; }
433 
434 
435 // -------------------------------------------------------------------
436 // Template Class : "Hurricane::StandardSharedProperty".
437 
438 
439  template<typename Value> class StandardSharedProperty : public SharedProperty {
440 
441  public:
442  static Name staticGetName ();
443  static Value* staticGetValue ( const DBo* );
444  static StandardSharedProperty* get ( const DBo*, bool create=false );
445  // Constructors.
446  static StandardSharedProperty* create ();
447  static StandardSharedProperty* create ( const Value& );
448  // Methods.
449  virtual Name getName () const;
450  Value& getValue () const;
451  void setValue ( const Value& );
452  virtual string _getTypeName () const;
453  virtual string _getString () const;
454  virtual Record* _getRecord () const;
455 
456  private:
457  // Internal: Attributes.
458  static Name _name;
459  static DBo* _owner;
460  static StandardSharedProperty* _cache;
461  mutable Value _value;
462 
463  protected:
464  // Internal: Constructor.
466  StandardSharedProperty ( const Value& );
467  };
468 
469 
470 // Template function members.
471  template<typename Value>
473 
474 
475  template<typename Value>
477 
478 
479  template<typename Value>
481  {
482  return _name;
483  }
484 
485 
486  template<typename Value>
487  Value* StandardSharedProperty<Value>::staticGetValue ( const DBo* object )
488  {
489  if ( ( object == _owner ) || get(object) ) return _cache->getValue();
490  return NULL;
491  }
492 
493 
494  template<typename Value>
495  StandardSharedProperty<Value>* StandardSharedProperty<Value>::create ()
496  {
497  _cache = new StandardSharedProperty<Value>();
498  _cache->_postCreate();
499  return _cache;
500  }
501 
502 
503  template<typename Value>
504  StandardSharedProperty<Value>* StandardSharedProperty<Value>::create ( const Value& value )
505  {
506  _cache = new StandardPrivateProperty<Value>(value);
507  _cache->_postCreate();
508  return _cache;
509  }
510 
511 
512  template<typename Value>
513  StandardSharedProperty<Value>* StandardSharedProperty<Value>::get ( const DBo* object, bool create )
514  {
515  if ( _owner == object ) return _cache;
516 
517  Property* property = object->getProperty ( StandardSharedProperty<Value>::staticGetName() );
518  _cache = dynamic_cast<StandardSharedProperty<Value>*> ( property );
519 
520  if ( !_cache ) {
521  if ( property )
522  throw Error ( propertyTypeNameError
523  , getString(StandardSharedProperty<Value>::staticGetName()).c_str()
524  , getString(object).c_str() );
525  else if ( create )
526  const_cast<DBo*>(object)->put ( StandardSharedProperty<Value>::create() );
527  }
528 
529  return _cache;
530  }
531 
532 
533  template<typename Value>
534  StandardSharedProperty<Value>::StandardSharedProperty ()
535  : SharedProperty(), _value()
536  { }
537 
538 
539  template<typename Value>
540  StandardSharedProperty<Value>::StandardSharedProperty ( const Value& value )
541  : SharedProperty(), _value(value)
542  { }
543 
544 
545  template<typename Value>
546  Name StandardSharedProperty<Value>::getName() const
547  {
548  return staticGetName();
549  }
550 
551 
552  template<typename Value>
553  Value& StandardSharedProperty<Value>::getValue() const
554  {
555  return _value;
556  }
557 
558 
559  template<typename Value>
560  void StandardSharedProperty<Value>::setValue(const Value& value)
561  {
562  _value = value;
563  }
564 
565 
566  template<typename Value>
567  string StandardSharedProperty<Value>::_getTypeName() const
568  {
569  return _TName("StandardSharedProperty");
570  }
571 
572 
573  template<typename Value>
574  string StandardSharedProperty<Value>::_getString() const
575  {
576  string s = SharedProperty::_getString();
577  s.insert(s.length() - 1, " " + getString(_value));
578  return s;
579  }
580 
581 
582  template<typename Value>
583  Record* StandardSharedProperty<Value>::_getRecord() const
584  {
585  Record* record = SharedProperty::_getRecord();
586  if (record) {
587  record->add ( getSlot("Name" , staticGetName()) );
588  record->add ( getSlot("Value", &_value) );
589  }
590  return record;
591  }
592 
593 
594 } // Hurricane namespace.
595 
596 
597 INSPECTOR_P_SUPPORT(Hurricane::Property);
DataBase object root class (API).
Definition: DBo.h:45
Generic Collection auto-pointer.
Definition: Collection.h:235
Support for JSON export.
Definition: JsonObject.h:83
JSON Parser Stack.
Definition: JsonObject.h:249
Name description (API)
Definition: Name.h:35
PrivateProperty description (API)
Definition: Property.h:132
DBo * getOwner() const
Definition: Property.h:154
Property description (API)
Definition: Property.h:56
virtual void onReleasedBy(DBo *owner)=0
virtual Name getName() const =0
virtual void destroy()
virtual void onCapturedBy(DBo *owner)=0
SharedProperty description (API)
Definition: Property.h:386
StandardPrivateProperty description (API)
Definition: Property.h:162
StandardSharedProperty description (API)
Definition: Property.h:439
The namespace dedicated to Hurricane.
Definition: Generalities.dox:5
GenericCollection< DBo * > DBos
Definition: DBos.h:27


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