All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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"
15 // ============================================================================
27 template <class TYPE>
29 {
30 public:
31  // ==========================================================================
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  }
90  // ==========================================================================
91 public:
92  // ==========================================================================
94  const CLID& clID() const override
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  std::copy_if( begin(), end(), output, std::cref(cut) );
207  return output ;
208  }
210  void erase ( iterator i ) { m_data.erase ( i ) ; }
228  template <class PREDICATE>
229  void erase ( const PREDICATE& cut )
230  { m_data.erase( std::remove_if ( begin() , end() , cut ) , end() ) ; }
249  bool erase ( const TYPE* object )
250  {
251  auto i = std::find ( begin() , end() , object ) ;
252  if ( end() == i ) { return false ; }
253  m_data.erase ( i ) ;
254  return true ;
255  }
256  // ==========================================================================
257 public:
258  // ==========================================================================
260  reference operator[] ( size_type index ) { return m_data [index] ; }
262  const_reference operator[] ( size_type index ) const { return m_data [index] ; }
264  reference operator() ( size_type index ) { return m_data [index] ; }
266  const_reference operator() ( size_type index ) const { return m_data [index] ; }
268  reference at ( size_type index ) { return m_data.at(index) ; }
270  const_reference at ( size_type index ) const { return m_data.at(index) ; }
271  // ==========================================================================
272 public:
273  // ==========================================================================
274  iterator begin () { return m_data . begin () ; }
275  iterator end () { return m_data . end () ; }
276  const_iterator begin () const { return m_data . begin () ; }
277  const_iterator end () const { return m_data . end () ; }
278  reverse_iterator rbegin () { return m_data . rbegin () ; }
279  reverse_iterator rend () { return m_data . rend () ; }
280  const_reverse_iterator rbegin () const { return m_data . rbegin () ; }
281  const_reverse_iterator rend () const { return m_data . rend () ; }
282  // ==========================================================================
283 public:
284  // ==========================================================================
286  reference front () { return m_data . front () ; }
288  const_reference front () const { return m_data . front () ; }
290  reference back () { return m_data . back () ; }
292  const_reference back () const { return m_data . back () ; }
293  // ==========================================================================
294 public:
295  // ==========================================================================
297  bool operator== ( const SharedObjectsContainer& right ) const
298  { return &right == this || right.m_data == m_data ; }
300  bool operator== ( const ConstVector& right ) const
301  { return m_data == right ; }
303  bool operator < ( const SharedObjectsContainer& right ) const
304  { return m_data < right.m_data ; }
306  bool operator < ( const ConstVector& right ) const
307  { return m_data < right ; }
308  // ==========================================================================
309 public: // ObjectContainerBase methods:
310  // ==========================================================================
314  long index( const ContainedObject* object ) const override
315  {
316  auto _i = std::find ( begin() , end() , object ) ;
317  return end() != _i ? ( _i - begin() ) : -1 ; // RETURN
318  }
323  ContainedObject* containedObject ( long index ) const override
324  {
325  if ( 0 > index || !(index < (long) size () ) ) { return 0 ; } // RETURN
326  const ContainedObject* co = m_data[index] ;
327  return const_cast<ContainedObject*>( co ) ;
328  }
330  size_type numberOfObjects() const override { return m_data.size() ; }
335  long add ( ContainedObject* object) override
336  {
337  if ( !object ) { return -1 ; } // RETURN
338  TYPE* _obj = dynamic_cast<TYPE*> ( object ) ;
339  if ( !_obj ) { return -1 ; } // RETURN
340  const size_type pos = size() ;
341  push_back ( _obj ) ;
342  return pos ;
343  }
348  long remove ( ContainedObject* value ) override
349  {
350  auto _i = std::find ( begin() , end() , value ) ;
351  if ( end() == _i ) { return -1 ; } // RETURN
352  const size_type pos = _i - begin() ;
353  m_data.erase ( _i ) ;
354  return pos ; // RETURN
355  }
356  // ==========================================================================
357 private:
358  // ==========================================================================
359  // the actual data
360  ConstVector m_data ; // the actual data
361  // ==========================================================================
362 };
363 // ============================================================================
364 // The END
365 // ============================================================================
366 #endif // GAUDIKERNEL_SHAREDOBJECTSCONTAINER_H
367 // ============================================================================
bool erase(const TYPE *object)
erase the first occurance of the certain element
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...
ContainedObject * containedObject(long index) const override
Pointer to an object of a given distance.
T empty(T...args)
const_reference at(size_type index) const
checked access (const-version)
T copy_if(T...args)
T distance(T...args)
reference operator()(size_type index)
&#39;functional&#39;-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
size_type numberOfObjects() const override
Number of objects in the container.
T remove_if(T...args)
SharedObjectsContainer(DATA first, DATA last, const PREDICATE &cut)
the templated constructor from the pair of iterators and the predicate.
ConstVector::iterator iterator
ConstVector::const_reference const_reference
T at(T...args)
void insert(const TYPE *object)
insert one object into the container
T push_back(T...args)
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)
T erase(T...args)
reference at(size_type index)
checked access
long index(const ContainedObject *object) const override
Distance of a given object from the beginning of its container.
long add(ContainedObject *object) override
Virtual functions (forwards to the concrete container definitions) Add an object to the container...
~SharedObjectsContainer() override
destructor
unsigned int CLID
Class ID definition.
Definition: ClassID.h:8
T clear(T...args)
bool empty() const
empty container?
reference operator[](size_type index)
index access
std::vector< const TYPE * > ConstVector
the actual container type
T insert(T...args)
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
T find(T...args)
const_reverse_iterator rend() const
T size(T...args)
T cref(T...args)
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.
const CLID & clID() const override
Retrieve the unique class ID (virtual)
SharedObjectsContainer(DATA first, DATA last)
the templated constructor from the pair of iterators
TO * reference(FROM *from)
Definition: KeyedObject.cpp:20
T reserve(T...args)
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)