LCIO  "2.7.4"
 All Classes Namespaces Functions Variables Typedefs Friends Pages
LCRTRelations.h
1 #ifndef LCRTRelations_H
2 #define LCRTRelations_H 1
3 
4 #include <iostream>
5 #include <vector>
6 #include <list>
7 #include <map>
8 
9 namespace lcrtrel_helper{
10 
11  //------------------ internal helper typdefs, function and classes ----------
12 
13  template <bool B>
14  struct can_call_ext{};
15  template <>
16  struct can_call_ext<true>{ static void check(){/* no_op */ } ; };
17 
19  typedef void (*DeleteFPtr)(void*) ;
20 
22  struct SimplePtrInit{ static void* init() { return 0 ; } } ;
23 
25  struct NoDelete{ static void clean(void *) { /* no_op */ } } ;
26 
28  template <class T>
29  struct CreationPtrInit{ static void* init() { return new T ; } } ;
30 
32  template <class T>
33  struct DeletePtr{ static void clean(void *v) { delete (T*) v ; } } ;
34 
35 
37  template <class T>
39 
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){
43  delete *it ;
44  }
45  delete vec ;
46  }
47  };
48 
49 
51  typedef std::map< unsigned , void * > PtrMap ;
52 
54  typedef std::vector< DeleteFPtr > DPtrVec ;
55 
57  //typedef std::vector< void * > PtrVec ;
58 
59 
61  template <class U, class T , class I, class D, bool b>
62  struct LCBaseTraits{
63 
64  typedef T* ptr ;
65  typedef U tag ; // this ensures that a new class instance is created for every user extension
66 
67  static const int allowed_to_call_ext = b ;
68 
69  static void clean(void *v) {
70  D::clean( v ) ;
71  }
72  static ptr init() {
73  return (ptr) I::init() ;
74  }
75  static DeleteFPtr deletePtr() { return &clean ; } ;
76  };
77 
78 
80  template <class U, class T , class I=SimplePtrInit, class D=NoDelete , bool b=1>
81  struct LCBaseLinkTraits : public LCBaseTraits<U,T,I,D,b>{
82 
84 
85  typedef T*& ext_type ; // return value of ext<>()
86  typedef T* rel_type ; // return value of rel<>()
87  typedef typename base::ptr obj_ptr ; // pointer to object
88 
89  static const bool is_container=false ;
90  };
91 
93  template <class U, class T , class I=CreationPtrInit<T>, class D=DeletePtr<T> , bool b=1>
94  struct LCBaseLinkContainerTraits : public LCBaseTraits<U,T,I,D,b>{
95 
97 
98  typedef T* ext_type ; // return value of ext<>()
99  typedef const T* rel_type ; // return value of rel<>()
100  typedef typename T::value_type obj_ptr ; // pointer to object
101 
102  typedef typename T::iterator iterator ;
103  typedef typename T::const_iterator const_iterator ;
104 
105  static const bool is_container=true ;
106  };
107 
108 
110  template <class U, class T>
111  struct RelationOneSide :
112  public LCBaseLinkTraits<U,T,SimplePtrInit,NoDelete,false> {};
113 
114 
116  template <class U, class T>
118  public LCBaseLinkContainerTraits< U, std::list<T*>,
119  CreationPtrInit< std::list<T*> > ,
120  DeletePtr<std::list<T*> > ,false > {};
121 
123  template <class U> struct FromRelation{} ;
124 
126  template <class U> struct ToRelation{} ;
127 
128 
130  template <class From, class To>
132  typedef From from ;
133  typedef To to ;
134  } ;
135 
136 
138  template <bool is_container>
139  struct objorcont{
140 
141  template <class T, class S>
142  inline static void add( T t, S s) { t->push_back( s ) ; }
143 
144  template <class T, class S>
145  inline static void remove( T t, S s) { t->remove( s ) ; }
146  };
147 
148 
150  template <>
151  struct objorcont<false>{
152 
153  template <class T, class S>
154  inline static void add( T& t, S s) { t = s ; }
155 
156  template <class T, class S>
157  inline static void remove( T& t, S ) { t = 0 ; }
158  };
159 
160  //-----------end of internal helper typdefs, function and classes ----------
161 
162 
163 } // end namespace lcrtrel_helper
164 
165 namespace lcrtrel{
166 
167  using namespace lcrtrel_helper ;
168 
173  template <class U, typename T>
174  struct LCExtension : public LCBaseLinkTraits< U, T > {};
175 
181  template <class U, typename T>
182  class LCOwnedExtension : public LCBaseLinkTraits< U, T , SimplePtrInit , DeletePtr<T> > {};
183 
184 
186  template <class U, class T>
188  public LCBaseLinkContainerTraits< U, std::vector<T*>,
189  CreationPtrInit< std::vector<T*> > ,
190  DeletePtr<std::vector<T*> > > {};
191 
192 
196  template <class U, class T>
198  public LCBaseLinkContainerTraits< U, std::vector<T*>,
199  CreationPtrInit< std::vector<T*> > ,
200  DeleteElements< std::vector<T*> > > {};
201 
202 
204  template <class U, class T>
206  public LCBaseLinkContainerTraits< U, std::list<T*>,
207  CreationPtrInit< std::list<T*> > ,
208  DeletePtr<std::list<T*> > > {};
209 
213  template <class U, class T>
215  public LCBaseLinkContainerTraits< U, std::list<T*>,
216  CreationPtrInit< std::list<T*> > ,
217  DeleteElements< std::list<T*> > > {};
218 
219 
221  template <class U, class From, class To>
222  struct LC1To1Relation :
223  public BiDirectional<RelationOneSide<FromRelation<U>,From>,
224  RelationOneSide<ToRelation<U>,To> > {
225 
226  } ;
227 
229  template <class U, class From, class To>
230  struct LC1ToNRelation :
231  public BiDirectional<RelationOneSide<FromRelation<U>,From>,
232  RelationManySide<ToRelation<U>,To> > {
233  } ;
234 
236  template <class U, class From, class To>
237  struct LCNToNRelation :
238  public BiDirectional<RelationManySide<FromRelation<U>,From>,
239  RelationManySide<ToRelation<U>,To> > {
240  } ;
241 
242 
244  template <class U >
245  struct LCIntExtension{
246 
247  typedef long ptr ; // base pointer type - use long to work for 64 bit (long is 32 on 32-bit systems/64 on 64bit systems)
248  typedef long& ext_type ;
249 
250  typedef U tag ; // this ensures that a new class instance is created for every user extension
251 
252  static const int allowed_to_call_ext = 1 ;
253 
254  static void clean(void *) { }
255 
256  static ptr init() {
257  return 0 ;
258  }
259  static DeleteFPtr deletePtr() { return &clean ; } ;
260 
261  };
262 
263 
264 
265  template <class U >
266 
267  struct LCFloatExtension{// FIXME: need to check on 64 bit architecture...
268 
269 #ifdef __i386__
270  typedef float ptr ; // base pointer type
271  typedef float& ext_type ; // return value of ext<>()
272 
273 #else // use double on 64bit systems
274  typedef double ptr ; // base pointer type
275  typedef double& ext_type ; // return value of ext<>()
276 #endif
277 
278  typedef U tag ; // this ensures that a new class instance is created for every user extension
279 
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 ; } ;
284  };
285 
286 
287  //--------------------------------------------------------------------
288 
289 
290 
291  class LCRTRelations ;
292 
293  // exclude from dictionary (template lookup does not work)
294 #ifndef __CINT__
295 
298  template <class R>
299  void set_relation( typename R::from::obj_ptr f,
300  typename R::to::obj_ptr t) ;
301 
303  template <class R>
304  void unset_relation(typename R::from::obj_ptr f );
305 
306 
308  template <class R>
309  void add_relation( typename R::from::obj_ptr f,
310  typename R::to::obj_ptr t) ;
311 
312 
314  template <class R>
315  void remove_relation( typename R::from::obj_ptr f,
316  typename R::to::obj_ptr t) ;
317 
318 
320  template <class R>
321  void remove_relations(typename R::from::obj_ptr f );
322 
323 
327  template <class R>
328  void merge_relations( typename R::from::obj_ptr f1,
329  typename R::from::obj_ptr f2) ;
330 
331 
332 
412 
413  // declare functions for relation handling as friends
414  template <class R>
415  friend void set_relation( typename R::from::obj_ptr f,
416  typename R::to::obj_ptr t);
417  template <class R>
418  friend void unset_relation(typename R::from::obj_ptr f );
419 
420  template <class R>
421  friend void add_relation( typename R::from::obj_ptr f,
422  typename R::to::obj_ptr t) ;
423  template <class R>
424  friend void remove_relation( typename R::from::obj_ptr f,
425  typename R::to::obj_ptr t) ;
426  template <class R>
427  friend void remove_relations(typename R::from::obj_ptr f );
428 
429  template <class R>
430  friend void merge_relations( typename R::from::obj_ptr f1,
431  typename R::from::obj_ptr f2) ;
432 
433  public:
434 
439  template <class V>
440  inline typename V::ext_type ext() {
441 
443 
444  return ptr<V>() ;
445  }
446 
447  template <class V>
448  inline const typename V::ext_type ext() const {
449 
451 
452  return ptr<V>() ;
453  }
454 
459  template <class V>
460  inline typename V::rel_type rel() {
461 
462  return ptr<V>() ;
463  }
464 
465 
466 
467  // LCRTRelations() {
468  // _vec = new PtrVec( 32 ) ; // initialize to prevent from to many resizes
469  // }
470  // ~LCRTRelations() {
471  // for( unsigned i=0 ; i< cleaners().size() ; ++i){
472  // cleaners()[i]( _vec->operator[](i) ) ; // call the delete function
473  // }
474  // delete _vec ;
475  // }
476 
477 
478 
479  ~LCRTRelations() {
480  for( PtrMap::iterator it = _map.begin() ; it != _map.end() ; ++it ){
481  cleaners()[ it->first ] ( it->second ) ; // call the delete function
482  }
483  }
484 
485  // void print(){
486  // std::cout << " ---- LCRTRelations -- : " << std::endl ;
487  // typedef std::map< void * , void* > MyPtrMap ;
488  // MyPtrMap& map = *(MyPtrMap*) &_map ;
489  // for( MyPtrMap::iterator it = map.begin() ;
490  // it != map.end() ; ++it ){
491  // std::cout << " ---- key : " << &(it->first) << " value "
492  // << (void*)it->second << std::endl ;
493  // }
494  // }
495 
496 
497  protected:
498 
499 
501  template <class V>
502  typename V::ptr & ptr() const {
503 
504  // convert the pointer to the generic PtrMap to a Map that holds V::ptr objects
505  typedef std::map< unsigned , typename V::ptr > MyPtrMap ;
506 
507  // going through a pointer to char* avoids the strict-aliasing warning of gcc
508  char* char_map = reinterpret_cast<char *> ( &_map ) ;
509  MyPtrMap* map = reinterpret_cast<MyPtrMap*>( char_map ) ;
510 
511  typename MyPtrMap::iterator it = map->find( typeID<V>() ) ;
512 
513  if( it == map->end() )
514  it = map->insert( map->begin(),
515  std::make_pair( typeID<V>() , V::init() )) ;
516 
517  return it->second ;
518  }
519 
520  private:
521 
522  static DPtrVec& cleaners() {
523  static DPtrVec v ;
524  return v ;
525  }
526 
527  inline unsigned nextID(DeleteFPtr cp) const {
528  static unsigned id(0) ;
529 
530  // std::cout << " ---- nextID " << id+1 << " - delete Ptr "
531  // << cp << std::endl ;
532 
533  cleaners().push_back( cp ) ;
534 
535  return id++ ;
536  }
537 
538  template <class T>
539  inline unsigned typeID() const {
540  static const unsigned uid = nextID( T::deletePtr() ) ;
541 
542  return uid ;
543  }
544 
545 
546  // PtrVec* _vec ;
547  mutable PtrMap _map ;
548 
549  } ;
550 
551 
552  //----------------------- relation function definitions -----------------------------------
553 
554 
555  template <class R>
556  void unset_relation(typename R::from::obj_ptr f){
557 
558  if( f != 0 ){
559 
560  LCRTRelations* t = f->LCRTRelations::template rel<typename R::to>() ;
561 
562  if( t != 0 )
563  t->LCRTRelations::ptr<typename R::from>() = 0 ;
564 
565  f->LCRTRelations::template ptr<typename R::to>() = 0 ;
566  }
567  }
568 
569  template <class R>
570  void set_relation(typename R::from::obj_ptr f, typename R::to::obj_ptr t){
571 
572  // clear old relations first
573  unset_relation<R>( f ) ;
574  unset_relation<R>(t->LCRTRelations::template rel<typename R::from>() ) ;
575 
576  f->LCRTRelations::template ptr<typename R::to>() = t ;
577  t->LCRTRelations::template ptr<typename R::from>() = f ;
578  }
579 
580 
581 
582 
583  template <class R>
584  void add_relation( typename R::from::obj_ptr f,
585  typename R::to::obj_ptr t){
586 
587  f->LCRTRelations::template ptr<typename R::to>()->push_back( t ) ;
588 
589  // std::cout << " ask to assign " << f << " to " << t << std::endl ;
590  objorcont<R::from::is_container>::add( t->LCRTRelations::template ptr<typename R::from>() , f ) ;
591  }
592 
593 
594 
595 
596  template <class R>
597  void remove_relation( typename R::from::obj_ptr f,
598  typename R::to::obj_ptr t ) {
599 
600  f->LCRTRelations::template ptr<typename R::to>()->remove( t ) ;
601 
602  objorcont<R::from::is_container>::remove( t->LCRTRelations::template ptr<typename R::from>() , f ) ;
603  }
604 
605 
606  template <class R>
607  void remove_relations( typename R::from::obj_ptr f ) {
608 
609  typename R::to::ptr cl = f->LCRTRelations::template ptr<typename R::to>() ;
610 
611  for( typename R::to::iterator it = cl->begin(); it!=cl->end(); ++it){
612 
613  objorcont<R::from::is_container>::remove((*it)->LCRTRelations::template ptr<typename R::from>(), f ) ;
614 
615  }
616  cl->clear() ;
617  }
618 
619  template <class R>
620  void merge_relations(typename R::from::obj_ptr f1,
621  typename R::from::obj_ptr f2 ) {
622 
623  typename R::to::ptr lt2 = f2->LCRTRelations::template ptr<typename R::to>() ;
624 
625  for( typename R::to::iterator it = lt2->begin() ;it != lt2->end() ; it++ ){
626 
627  objorcont<R::from::is_container>::remove( (*it)->LCRTRelations::template ptr<typename R::from>(), f2 ) ;
628  objorcont<R::from::is_container>::add( (*it)->LCRTRelations::template ptr<typename R::from>(), f1 ) ;
629  }
630 
631  f1->LCRTRelations::template ptr<typename R::to>()->merge( *lt2 ) ;
632  }
633 #endif // __CINT__
634 
635 } // end namespace
636 
637 #endif
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