33#include "hurricane/Name.h"
34#include "hurricane/Properties.h"
35#include "hurricane/DBo.h"
36#include "hurricane/Error.h"
42 extern const char* propertyTypeNameError;
48 struct JsonEnabled {
enum State { enabled=1 }; };
49 struct JsonDisabled {
enum State { enabled=0 }; };
60 template<
typename DerivedProperty>
61 static DerivedProperty* get (
const DBo* );
62 static Name staticGetName ();
64 template<
typename DerivedProperty>
65 static DerivedProperty* create ();
66 template<
typename DerivedProperty,
typename Value>
67 static DerivedProperty* create (
const Value& );
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;
82 static Name _baseName;
87 virtual void _postCreate () {};
88 virtual void _preDestroy () {};
90 Property (
const Property& );
91 Property& operator= (
const Property& );
95 template<
typename DerivedProperty>
96 DerivedProperty* Property::create ()
98 DerivedProperty*
property =
new DerivedProperty();
99 property->_postCreate();
104 template<
typename DerivedProperty,
typename Value>
105 DerivedProperty* Property::create (
const Value& value )
107 DerivedProperty*
property =
new DerivedProperty(value);
108 property->_postCreate();
113 template<
typename DerivedProperty>
114 DerivedProperty* Property::get (
const DBo*
object )
116 Property* property1 =
object->getProperty ( DerivedProperty::staticGetName() );
117 DerivedProperty* property2 =
dynamic_cast<DerivedProperty*
> ( property1 );
119 if ( property1 && !property2 )
120 throw Error ( propertyTypeNameError
121 , getString(DerivedProperty::staticGetName()).c_str()
122 , getString(
object).c_str() );
132 class PrivateProperty :
public Property {
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;
149 virtual void _preDestroy ();
161 template<
typename Value,
typename JsonState=JsonDisabled>
162 class StandardPrivateProperty :
public PrivateProperty {
164 static Name staticGetName ();
165 static Value* staticGetValue (
const DBo* );
166 static StandardPrivateProperty* get (
const DBo*,
bool create=
false );
168 static StandardPrivateProperty* create ();
169 static StandardPrivateProperty* create (
const Value& );
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;
183 static StandardPrivateProperty* _cache;
184 mutable Value _value;
187 StandardPrivateProperty ();
188 StandardPrivateProperty (
const Value& );
192 static void initialize ();
193 JsonProperty (
unsigned long flags );
194 virtual string getTypeName ()
const;
195 virtual JsonProperty* clone (
unsigned long )
const;
201 template<
typename Value,
typename JsonState>
202 StandardPrivateProperty<Value,JsonState>::JsonProperty::JsonProperty (
unsigned long flags )
205 if (flags & JsonWriter::RegisterMode)
206 cerr <<
"Registering JsonProperty" << endl;
207 add(
"_value",
typeid(Value) );
211 template<
typename Value,
typename JsonState>
212 string StandardPrivateProperty<Value,JsonState>::JsonProperty::getTypeName ()
const
213 {
return getString(StandardPrivateProperty<Value,JsonState>::staticGetName()); }
216 template<
typename Value,
typename JsonState>
217 void StandardPrivateProperty<Value,JsonState>::JsonProperty::initialize ()
218 { JsonTypes::registerType(
new JsonProperty (JsonWriter::RegisterMode) ); }
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 ); }
227 template<
typename Value,
typename JsonState>
228 void StandardPrivateProperty<Value,JsonState>::JsonProperty::toData ( JsonStack& stack )
230 check( stack,
"JsonProperty::toData" );
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 );
238 update( stack, property );
242 template<
typename Value,
typename JsonState>
243 DBo* StandardPrivateProperty<Value,JsonState>::_owner = NULL;
246 template<
typename Value,
typename JsonState>
247 StandardPrivateProperty<Value,JsonState>* StandardPrivateProperty<Value,JsonState>::_cache = NULL;
250 template<
typename Value,
typename JsonState>
251 Name StandardPrivateProperty<Value,JsonState>::staticGetName ()
257 template<
typename Value,
typename JsonState>
258 Value* StandardPrivateProperty<Value,JsonState>::staticGetValue (
const DBo*
object )
260 if ( (
object == _owner ) || get(
object) )
return _cache->getValue();
265 template<
typename Value,
typename JsonState>
266 StandardPrivateProperty<Value,JsonState>* StandardPrivateProperty<Value,JsonState>::create ()
268 _cache =
new StandardPrivateProperty<Value>();
269 _cache->_postCreate();
274 template<
typename Value,
typename JsonState>
275 StandardPrivateProperty<Value,JsonState>* StandardPrivateProperty<Value,JsonState>::create (
const Value& value )
277 _cache =
new StandardPrivateProperty<Value>(value);
278 _cache->_postCreate();
283 template<
typename Value,
typename JsonState>
284 StandardPrivateProperty<Value,JsonState>* StandardPrivateProperty<Value,JsonState>::get (
const DBo*
object,
bool create )
286 if (
object == _owner )
return _cache;
288 Property*
property =
object->getProperty ( StandardPrivateProperty<Value>::staticGetName() );
289 _cache =
dynamic_cast<StandardPrivateProperty<Value>*
> ( property );
293 throw Error ( propertyTypeNameError
294 , getString(StandardPrivateProperty<Value>::staticGetName()).c_str()
295 , getString(
object).c_str() );
297 const_cast<DBo*
>(object)->put ( StandardPrivateProperty<Value>::create() );
304 template<
typename Value,
typename JsonState>
305 StandardPrivateProperty<Value,JsonState>::StandardPrivateProperty ()
306 : PrivateProperty(), _value()
310 template<
typename Value,
typename JsonState>
311 StandardPrivateProperty<Value,JsonState>::StandardPrivateProperty (
const Value& value )
316 template<
typename Value,
typename JsonState>
317 Name StandardPrivateProperty<Value,JsonState>::getName()
const
319 return staticGetName();
323 template<
typename Value,
typename JsonState>
324 Value& StandardPrivateProperty<Value,JsonState>::getValue ()
const
330 template<
typename Value,
typename JsonState>
331 void StandardPrivateProperty<Value,JsonState>::setValue (
const Value& value )
337 template<
typename Value,
typename JsonState>
338 bool StandardPrivateProperty<Value,JsonState>::hasJson ()
const
340 return JsonState::enabled;
344 template<
typename Value,
typename JsonState>
345 void StandardPrivateProperty<Value,JsonState>::toJson ( JsonWriter* w,
const DBo* )
const
348 std::string tname = getString(staticGetName());
349 jsonWrite( w,
"@typename", tname );
350 jsonWrite( w,
"_value", _value );
355 template<
typename Value,
typename JsonState>
356 string StandardPrivateProperty<Value,JsonState>::_getTypeName ()
const
358 return _TName(
"StandardPrivateProperty");
361 template<
typename Value,
typename JsonState>
362 string StandardPrivateProperty<Value,JsonState>::_getString ()
const
364 string s = PrivateProperty::_getString();
365 s.insert(s.length() - 1,
" " + getString<Value&>(_value));
369 template<
typename Value,
typename JsonState>
370 Record* StandardPrivateProperty<Value,JsonState>::_getRecord ()
const
372 Record* record = PrivateProperty::_getRecord();
374 record->add ( getSlot(
"_name" , staticGetName()) );
375 record->add ( getSlot(
"_value" ,&_value) );
376 record->add ( getSlot(
"JSON support", JsonState::enabled) );
386 class SharedProperty :
public Property {
390 inline Orphaned ( SharedProperty* );
392 SharedProperty* _property;
393 unsigned int _refcount;
397 typedef set<DBo*,DBo::CompareById> DBoSet;
398 typedef map<string,Orphaned> OrphanedMap;
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 ();
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;
417 static OrphanedMap _orphaneds;
422 virtual void _preDestroy ();
427 inline SharedProperty::Orphaned::Orphaned (
SharedProperty* property )
428 : _property(property), _refcount(0), _count(0)
431 inline DBos SharedProperty::getOwners ()
const {
return getCollection(_ownerSet); }
432 inline SharedProperty::DBoSet& SharedProperty::_getOwnerSet () {
return _ownerSet; }
439 template<
typename Value>
class StandardSharedProperty :
public SharedProperty {
442 static Name staticGetName ();
443 static Value* staticGetValue (
const DBo* );
444 static StandardSharedProperty* get (
const DBo*,
bool create=
false );
446 static StandardSharedProperty* create ();
447 static StandardSharedProperty* create (
const Value& );
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;
460 static StandardSharedProperty* _cache;
461 mutable Value _value;
465 StandardSharedProperty ();
466 StandardSharedProperty (
const Value& );
471 template<
typename Value>
472 DBo* StandardSharedProperty<Value>::_owner = NULL;
475 template<
typename Value>
479 template<
typename Value>
480 Name StandardSharedProperty<Value>::staticGetName ()
486 template<
typename Value>
487 Value* StandardSharedProperty<Value>::staticGetValue (
const DBo*
object )
489 if ( (
object == _owner ) || get(
object) )
return _cache->getValue();
494 template<
typename Value>
498 _cache->_postCreate();
503 template<
typename Value>
507 _cache->_postCreate();
512 template<
typename Value>
515 if ( _owner ==
object )
return _cache;
517 Property*
property =
object->getProperty ( StandardSharedProperty<Value>::staticGetName() );
522 throw Error ( propertyTypeNameError
523 , getString(StandardSharedProperty<Value>::staticGetName()).c_str()
524 , getString(
object).c_str() );
526 const_cast<DBo*
>(object)->put ( StandardSharedProperty<Value>::create() );
533 template<
typename Value>
534 StandardSharedProperty<Value>::StandardSharedProperty ()
539 template<
typename Value>
540 StandardSharedProperty<Value>::StandardSharedProperty (
const Value& value )
545 template<
typename Value>
546 Name StandardSharedProperty<Value>::getName()
const
548 return staticGetName();
552 template<
typename Value>
553 Value& StandardSharedProperty<Value>::getValue()
const
559 template<
typename Value>
560 void StandardSharedProperty<Value>::setValue(
const Value& value)
566 template<
typename Value>
567 string StandardSharedProperty<Value>::_getTypeName()
const
569 return _TName(
"StandardSharedProperty");
573 template<
typename Value>
574 string StandardSharedProperty<Value>::_getString()
const
576 string s = SharedProperty::_getString();
577 s.insert(s.length() - 1,
" " + getString(_value));
582 template<
typename Value>
583 Record* StandardSharedProperty<Value>::_getRecord()
const
585 Record* record = SharedProperty::_getRecord();
587 record->add ( getSlot(
"Name" , staticGetName()) );
588 record->add ( getSlot(
"Value", &_value) );
DataBase object root class (API).
Definition DBo.h:45
Error description (API)
Definition Error.h:43
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 Name getName() const =0
virtual void onCapturedBy(DBo *owner)=0
virtual void onReleasedBy(DBo *owner)=0
SharedProperty description (API)
Definition Property.h:386
StandardPrivateProperty description (API)
Definition Property.h:162
StandardSharedProperty description (API)
Definition Property.h:439
Contains Almost Everything.
Definition BasicLayer.h:39
GenericCollection< DBo * > DBos
Definition DBos.h:35