SharedObjectsContainer.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_SHAREDOBJECTSCONTAINER_H
2 #define GAUDIKERNEL_SHAREDOBJECTSCONTAINER_H 1
3 // ============================================================================
4 // Include files
5 // ============================================================================
6 // STD & STL
7 // ============================================================================
8 #include <algorithm>
9 // ============================================================================
10 // GaudiKernel
11 // ============================================================================
12 #include "GaudiKernel/Kernel.h"
13 #include "GaudiKernel/ClassID.h"
14 #include "GaudiKernel/ObjectContainerBase.h"
15 // ============================================================================
27 template <class TYPE>
29 {
30 public:
31  // ==========================================================================
33  typedef std::vector<const TYPE*> ConstVector ;
35  typedef typename ConstVector::value_type value_type ;
36  typedef typename ConstVector::size_type size_type ;
37  typedef typename ConstVector::reference reference ;
38  typedef typename ConstVector::const_reference const_reference ;
39  typedef typename ConstVector::iterator iterator ;
40  typedef typename ConstVector::const_iterator const_iterator ;
41  typedef typename ConstVector::reverse_iterator reverse_iterator ;
42  typedef typename ConstVector::const_reverse_iterator const_reverse_iterator ;
43  // ==========================================================================
44 public:
45  // ==========================================================================
46  // the default constructor (creates the empty vector)
48  : ObjectContainerBase(), m_data() {} ;
49  // the constructor from the data
50  SharedObjectsContainer ( const ConstVector& data )
51  : ObjectContainerBase(), m_data(data) {}
56  template <class DATA>
57  SharedObjectsContainer(DATA first, DATA last)
58  : ObjectContainerBase(), m_data(first, last) {}
82  template <class DATA, class PREDICATE>
83  SharedObjectsContainer(DATA first, DATA last, const PREDICATE& cut)
85  {
86  insert ( first , last , cut ) ;
87  }
89  virtual ~SharedObjectsContainer() { m_data.clear() ; }
90  // ==========================================================================
91 public:
92  // ==========================================================================
94  virtual const CLID& clID() const
97  static const CLID& classID()
98  {
99  static const CLID s_clid =
100  ( ( static_cast<CLID> ( -1 ) << 16 ) // 16 used and 16 empty bits
101  & !CLID_ObjectVector // not an ObjectVector
102  & !CLID_ObjectList // not an ObjectList
103  & !static_cast<CLID> ( 0x00030000 ) // not a KeyedContainer/map
104  & !static_cast<CLID> ( 0x00040000 ) )// not a KeyedContainer/hashmap
105  + TYPE::classID() ; // the specific CLID from the contents
106  //
107  return s_clid;
108  }
109  // ==========================================================================
110 public:
111  // ==========================================================================
113  inline const ConstVector& data () const { return m_data ; }
115  operator const ConstVector& () const { return data () ; }
116  // ==========================================================================
117 public:
118  // ==========================================================================
120  size_type size () const { return m_data.size () ; }
122  bool empty () const { return m_data.empty () ; }
126  void push_back ( const TYPE* object ) { m_data.push_back ( object ) ; }
130  void insert ( const TYPE* object ) { m_data.push_back ( object ) ; }
135  template <class DATA>
136  void insert
137  ( DATA first ,
138  DATA last ) { m_data.insert ( end() , first ,last ) ; }
165  template <class DATA, class PREDICATE>
166  void insert
167  ( DATA first ,
168  DATA last ,
169  const PREDICATE& cut )
170  {
171  m_data.reserve ( m_data.size() + std::distance ( first , last ) ) ;
172  for ( ; first != last ; ++first )
173  { if ( cut ( *first ) ) { m_data.push_back ( *first ) ; } }
174  }
202  template <class OUTPUT, class PREDICATE>
203  OUTPUT get ( const PREDICATE& cut ,
204  OUTPUT output ) const
205  {
206  for ( const_iterator iobj = begin() ; end() != iobj ; ++iobj )
207  { if ( cut ( *iobj ) ) { *output = *iobj ; ++output ; } }
208  return output ;
209  }
211  void erase ( iterator i ) { m_data.erase ( i ) ; }
229  template <class PREDICATE>
230  void erase ( const PREDICATE& cut )
231  { m_data.erase( std::remove_if ( begin() , end() , cut ) , end() ) ; }
250  bool erase ( const TYPE* object )
251  {
252  iterator i = std::find ( begin() , end() , object ) ;
253  if ( end() == i ) { return false ; }
254  m_data.erase ( i ) ;
255  return true ;
256  }
257  // ==========================================================================
258 public:
259  // ==========================================================================
261  reference operator[] ( size_type index ) { return m_data [index] ; }
263  const_reference operator[] ( size_type index ) const { return m_data [index] ; }
265  reference operator() ( size_type index ) { return m_data [index] ; }
267  const_reference operator() ( size_type index ) const { return m_data [index] ; }
269  reference at ( size_type index ) { return m_data.at(index) ; }
271  const_reference at ( size_type index ) const { return m_data.at(index) ; }
272  // ==========================================================================
273 public:
274  // ==========================================================================
275  iterator begin () { return m_data . begin () ; }
276  iterator end () { return m_data . end () ; }
277  const_iterator begin () const { return m_data . begin () ; }
278  const_iterator end () const { return m_data . end () ; }
279  reverse_iterator rbegin () { return m_data . rbegin () ; }
280  reverse_iterator rend () { return m_data . rend () ; }
281  const_reverse_iterator rbegin () const { return m_data . rbegin () ; }
282  const_reverse_iterator rend () const { return m_data . rend () ; }
283  // ==========================================================================
284 public:
285  // ==========================================================================
287  reference front () { return m_data . front () ; }
289  const_reference front () const { return m_data . front () ; }
291  reference back () { return m_data . back () ; }
293  const_reference back () const { return m_data . back () ; }
294  // ==========================================================================
295 public:
296  // ==========================================================================
298  bool operator== ( const SharedObjectsContainer& right ) const
299  { return &right == this || right.m_data == m_data ; }
301  bool operator== ( const ConstVector& right ) const
302  { return m_data == right ; }
304  bool operator < ( const SharedObjectsContainer& right ) const
305  { return m_data < right.m_data ; }
307  bool operator < ( const ConstVector& right ) const
308  { return m_data < right ; }
309  // ==========================================================================
310 public: // ObjectContainerBase methods:
311  // ==========================================================================
315  virtual long index( const ContainedObject* object ) const
316  {
317  const_iterator _i = std::find ( begin() , end() , object ) ;
318  return end() != _i ? ( _i - begin() ) : -1 ; // RETURN
319  }
324  virtual ContainedObject* containedObject ( long index ) const
325  {
326  if ( 0 > index || !(index < (long) size () ) ) { return 0 ; } // RETURN
327  const ContainedObject* co = m_data[index] ;
328  return const_cast<ContainedObject*>( co ) ;
329  }
331  virtual size_type numberOfObjects() const { return m_data.size() ; }
336  virtual long add ( ContainedObject* object)
337  {
338  if ( 0 == object ) { return -1 ; } // RETURN
339  TYPE* _obj = dynamic_cast<TYPE*> ( object ) ;
340  if ( 0 == _obj ) { return -1 ; } // RETURN
341  const size_type pos = size() ;
342  push_back ( _obj ) ;
343  return pos ;
344  }
349  virtual long remove ( ContainedObject* value )
350  {
351  iterator _i = std::find ( begin() , end() , value ) ;
352  if ( end() == _i ) { return -1 ; } // RETURN
353  const size_type pos = _i - begin() ;
354  m_data.erase ( _i ) ;
355  return pos ; // RETURN
356  }
357  // ==========================================================================
358 private:
359  // ==========================================================================
360  // the actual data
361  ConstVector m_data ; // the actual data
362  // ==========================================================================
363 };
364 // ============================================================================
365 // The END
366 // ============================================================================
367 #endif // GAUDIKERNEL_SHAREDOBJECTSCONTAINER_H
368 // ============================================================================
bool erase(const TYPE *object)
erase the first occurance of the certain element
virtual const CLID & clID() const
Retrieve the unique class ID (virtual)
reference front()
the first element (only for non-empty vectors)
Very simple class to represent the container of objects which are not ownered by the container...
const_reference at(size_type index) const
checked access (const-version)
virtual ContainedObject * containedObject(long index) const
Pointer to an object of a given distance.
reference operator()(size_type index)
'functional'-access
const_iterator begin() const
void erase(const PREDICATE &cut)
erase the objects which satisfy the criteria
bool operator==(const SharedObjectsContainer &right) const
equal content with other container ?
void push_back(const TYPE *object)
insert one object into the container
bool operator<(const SharedObjectsContainer &right) const
comparisons with other container
ConstVector::size_type size_type
ConstVector::reverse_iterator reverse_iterator
size_type size() const
get the actual size of the container
SharedObjectsContainer(DATA first, DATA last, const PREDICATE &cut)
the templated constructor from the pair of iterators and the predicate.
virtual long add(ContainedObject *object)
Virtual functions (forwards to the concrete container definitions) Add an object to the container...
ConstVector::iterator iterator
ConstVector::const_reference const_reference
void insert(const TYPE *object)
insert one object into the container
virtual long index(const ContainedObject *object) const
Distance of a given object from the beginning of its container.
const_reference front() const
the first element (only for non-empty vectors) (const-version)
const_iterator end() const
const_reverse_iterator rbegin() const
ConstVector::const_iterator const_iterator
SharedObjectsContainer(const ConstVector &data)
const_reference back() const
the last element (only for non-empty vectors) (const-version)
const ConstVector & data() const
get the access to the underlying container (const)
virtual ~SharedObjectsContainer()
destructor
reference at(size_type index)
checked access
unsigned int CLID
Class ID definition.
Definition: ClassID.h:8
virtual size_type numberOfObjects() const
Number of objects in the container.
bool empty() const
empty container?
reference operator[](size_type index)
index access
std::vector< const TYPE * > ConstVector
the actual container type
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
const_reverse_iterator rend() const
static const CLID & classID()
Retrieve the unuqie class ID (static)
ConstVector::reference reference
reference back()
the last element (only for non-empty vectors)
ObjectContainerBase is the base class for Gaudi container classes.
list i
Definition: ana.py:128
SharedObjectsContainer(DATA first, DATA last)
the templated constructor from the pair of iterators
TO * reference(FROM *from)
Definition: KeyedObject.cpp:20
void erase(iterator i)
erase the object by iterator
ConstVector::const_reverse_iterator const_reverse_iterator
ConstVector::value_type value_type
various types (to make STL happy)