1 #ifndef LCRTRelations_H
2 #define LCRTRelations_H 1
9 namespace lcrtrel_helper{
19 typedef void (*DeleteFPtr)(
void*) ;
25 struct NoDelete{
static void clean(
void *) { } } ;
33 struct DeletePtr{
static void clean(
void *v) {
delete (T*) v ; } } ;
40 static void clean(
void *v) {
41 T* vec =
static_cast<T*
>(v) ;
42 for(
typename T::iterator it = vec->begin();it != vec->end(); ++it){
51 typedef std::map< unsigned , void * > PtrMap ;
54 typedef std::vector< DeleteFPtr > DPtrVec ;
61 template <
class U,
class T ,
class I,
class D,
bool b>
67 static const int allowed_to_call_ext = b ;
69 static void clean(
void *v) {
73 return (
ptr) I::init() ;
75 static DeleteFPtr deletePtr() {
return &clean ; } ;
80 template <
class U,
class T ,
class I=SimplePtrInit,
class D=NoDelete ,
bool b=1>
85 typedef T*& ext_type ;
89 static const bool is_container=false ;
93 template <
class U,
class T ,
class I=CreationPtrInit<T>,
class D=DeletePtr<T> ,
bool b=1>
99 typedef const T* rel_type ;
100 typedef typename T::value_type obj_ptr ;
102 typedef typename T::iterator iterator ;
103 typedef typename T::const_iterator const_iterator ;
105 static const bool is_container=true ;
110 template <
class U,
class T>
116 template <
class U,
class T>
119 CreationPtrInit< std::list<T*> > ,
120 DeletePtr<std::list<T*> > ,false > {};
130 template <
class From,
class To>
138 template <
bool is_container>
141 template <
class T,
class S>
142 inline static void add( T t, S s) { t->push_back( s ) ; }
144 template <
class T,
class S>
145 inline static void remove( T t, S s) { t->remove( s ) ; }
153 template <
class T,
class S>
154 inline static void add( T& t, S s) { t = s ; }
156 template <
class T,
class S>
157 inline static void remove( T& t, S ) { t = 0 ; }
167 using namespace lcrtrel_helper ;
173 template <
class U,
typename T>
181 template <
class U,
typename T>
186 template <
class U,
class T>
189 CreationPtrInit< std::vector<T*> > ,
190 DeletePtr<std::vector<T*> > > {};
196 template <
class U,
class T>
199 CreationPtrInit< std::vector<T*> > ,
200 DeleteElements< std::vector<T*> > > {};
204 template <
class U,
class T>
207 CreationPtrInit< std::list<T*> > ,
208 DeletePtr<std::list<T*> > > {};
213 template <
class U,
class T>
216 CreationPtrInit< std::list<T*> > ,
217 DeleteElements< std::list<T*> > > {};
221 template <
class U,
class From,
class To>
224 RelationOneSide<ToRelation<U>,To> > {
229 template <
class U,
class From,
class To>
232 RelationManySide<ToRelation<U>,To> > {
236 template <
class U,
class From,
class To>
239 RelationManySide<ToRelation<U>,To> > {
248 typedef long& ext_type ;
252 static const int allowed_to_call_ext = 1 ;
254 static void clean(
void *) { }
259 static DeleteFPtr deletePtr() {
return &clean ; } ;
271 typedef float& ext_type ;
273 #else // use double on 64bit systems
275 typedef double& ext_type ;
280 static const int allowed_to_call_ext = 1 ;
281 static void clean(
void *) { }
282 static ptr init() {
return 0 ; }
283 static DeleteFPtr deletePtr() {
return &clean ; } ;
300 typename R::to::obj_ptr t) ;
310 typename R::to::obj_ptr t) ;
316 typename R::to::obj_ptr t) ;
329 typename R::from::obj_ptr f2) ;
416 typename R::to::obj_ptr t);
422 typename R::to::obj_ptr t) ;
425 typename R::to::obj_ptr t) ;
431 typename R::from::obj_ptr f2) ;
440 inline typename V::ext_type
ext() {
448 inline const typename V::ext_type ext()
const {
460 inline typename V::rel_type
rel() {
480 for( PtrMap::iterator it = _map.begin() ; it != _map.end() ; ++it ){
481 cleaners()[ it->first ] ( it->second ) ;
502 typename V::ptr &
ptr()
const {
505 typedef std::map< unsigned , typename V::ptr > MyPtrMap ;
508 char* char_map =
reinterpret_cast<char *
> ( &_map ) ;
509 MyPtrMap* map =
reinterpret_cast<MyPtrMap*
>( char_map ) ;
511 typename MyPtrMap::iterator it = map->find( typeID<V>() ) ;
513 if( it == map->end() )
514 it = map->insert( map->begin(),
515 std::make_pair( typeID<V>() , V::init() )) ;
522 static DPtrVec& cleaners() {
527 inline unsigned nextID(DeleteFPtr cp)
const {
528 static unsigned id(0) ;
533 cleaners().push_back( cp ) ;
539 inline unsigned typeID()
const {
540 static const unsigned uid = nextID( T::deletePtr() ) ;
547 mutable PtrMap _map ;
560 LCRTRelations* t = f->LCRTRelations::template rel<typename R::to>() ;
563 t->LCRTRelations::ptr<
typename R::from>() = 0 ;
565 f->LCRTRelations::template ptr<typename R::to>() = 0 ;
570 void set_relation(
typename R::from::obj_ptr f,
typename R::to::obj_ptr t){
573 unset_relation<R>( f ) ;
574 unset_relation<R>(t->LCRTRelations::template rel<typename R::from>() ) ;
576 f->LCRTRelations::template ptr<typename R::to>() = t ;
577 t->LCRTRelations::template ptr<typename R::from>() = f ;
585 typename R::to::obj_ptr t){
587 f->LCRTRelations::template ptr<typename R::to>()->push_back( t ) ;
598 typename R::to::obj_ptr t ) {
600 f->LCRTRelations::template ptr<typename R::to>()->
remove( t ) ;
609 typename R::to::ptr cl = f->LCRTRelations::template ptr<typename R::to>() ;
611 for(
typename R::to::iterator it = cl->begin(); it!=cl->end(); ++it){
621 typename R::from::obj_ptr f2 ) {
623 typename R::to::ptr lt2 = f2->LCRTRelations::template ptr<typename R::to>() ;
625 for(
typename R::to::iterator it = lt2->begin() ;it != lt2->end() ; it++ ){
631 f1->LCRTRelations::template ptr<typename R::to>()->merge( *lt2 ) ;
Factory for objects of type T.
Definition: LCRTRelations.h:29
void unset_relation(typename R::from::obj_ptr f)
Unset the 1-to-1 relation from f.
Definition: LCRTRelations.h:556
void set_relation(typename R::from::obj_ptr f, typename R::to::obj_ptr t)
Set the 1-to-1 relation between two objects - prexisting inconsistent relations involving the two obj...
Definition: LCRTRelations.h:570
V::rel_type rel()
Provides read access to relations - the object types and their connectivity are defined by the class ...
Definition: LCRTRelations.h:460
Delete function for pointers w/ ownership.
Definition: LCRTRelations.h:33
Helper class for biderectional relations provides the to and from type.
Definition: LCRTRelations.h:131
Delete function for containers of owned objects.
Definition: LCRTRelations.h:38
V::ptr & ptr() const
Returns the reference to the pointer to the extension/relation object.
Definition: LCRTRelations.h:502
void merge_relations(typename R::from::obj_ptr f1, typename R::from::obj_ptr f2)
Merge the relations from f2 to f1 - after this call f1 will hold all the relations and f2 will be emp...
Definition: LCRTRelations.h:620
Base class that provides run time (user) extensions and relation between objects. ...
Definition: LCRTRelations.h:411
Helper class for relations.
Definition: LCRTRelations.h:126
Helper class for relations.
Definition: LCRTRelations.h:111
Simple init function for simple pointers.
Definition: LCRTRelations.h:22
Definition: LCRTRelations.h:267
void remove_relation(typename R::from::obj_ptr f, typename R::to::obj_ptr t)
Remove the link from from f to t from the N-to-N relation ship.
Definition: LCRTRelations.h:597
void remove_relations(typename R::from::obj_ptr f)
Removes all relations from the given object.
Definition: LCRTRelations.h:607
T * ptr
base pointer type
Definition: LCRTRelations.h:64
Base class for all extensions and relations of single objects.
Definition: LCRTRelations.h:81
Helper class for relations.
Definition: LCRTRelations.h:123
Vector of pointers to extension obbjects.
Definition: LCRTRelations.h:62
Simple Extension - pointer to an object of type T where the ownership is taken over by the object hol...
Definition: LCRTRelations.h:182
V::ext_type ext()
Provides access to an extension object - the type and ownership is defined by the class V which shoul...
Definition: LCRTRelations.h:440
Special Extension that allows to write int extensions directly (not through a pointer !)...
Definition: LCRTRelations.h:245
Empty delete function for pointers w/o ownership.
Definition: LCRTRelations.h:25
Base class for all containers of extensions and relations, vectors, lists,...
Definition: LCRTRelations.h:94
void add_relation(typename R::from::obj_ptr f, typename R::to::obj_ptr t)
Add a link from f to t to an N-to-N relation ship.
Definition: LCRTRelations.h:584
Extension list holding pointers to objects of type T - ownership of the objects is taken...
Definition: LCRTRelations.h:214
Extension vector holding pointers to objects of type T - ownership of the objects is taken...
Definition: LCRTRelations.h:197
One to one relation between two objects of type From and To.
Definition: LCRTRelations.h:222
Extension vector holding pointers to objects of type T - no ownership of the objects is taken...
Definition: LCRTRelations.h:187
One to many relation between one object of type From to many objects of type To.
Definition: LCRTRelations.h:230
Helper functions that treat single objects and containers.
Definition: LCRTRelations.h:139
Many to many relation between objects of type From to objects of type To.
Definition: LCRTRelations.h:237
Definition: LCRTRelations.h:14
Extension list holding pointers to objects of type T - no ownership of the objects is taken...
Definition: LCRTRelations.h:205
Simple Extension - pointer to an object of type T.
Definition: LCRTRelations.h:174
Helper class for relations.
Definition: LCRTRelations.h:117