# Hurricane VLSI Database

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

Generated by doxygen 1.8.14 on Sun Mar 8 2020 | Return to top of page

Hurricane VLSI Database | Copyright © 2000-2016 Bull S.A. All rights reserved