The Gaudi Framework  v28r3 (cc1cf868)
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 #include <vector>
10 // ============================================================================
11 // GaudiKernel
12 // ============================================================================
13 #include "GaudiKernel/Kernel.h"
14 #include "GaudiKernel/ClassID.h"
16 // ============================================================================
28 template <class TYPE>
30 {
31 public:
32  // ==========================================================================
36  typedef typename ConstVector::value_type value_type ;
37  typedef typename ConstVector::size_type size_type ;
38  typedef typename ConstVector::reference reference ;
39  typedef typename ConstVector::const_reference const_reference ;
40  typedef typename ConstVector::iterator iterator ;
41  typedef typename ConstVector::const_iterator const_iterator ;
42  typedef typename ConstVector::reverse_iterator reverse_iterator ;
43  typedef typename ConstVector::const_reverse_iterator const_reverse_iterator ;
44  // ==========================================================================
45 public:
46  // ==========================================================================
47  // the default constructor (creates the empty vector)
48  SharedObjectsContainer () = default;
49  // move constructor and move assignement
52  // the constructor from the data
53  SharedObjectsContainer ( const ConstVector& data )
54  : m_data(data) {}
55  SharedObjectsContainer ( ConstVector&& data )
56  : m_data(std::move(data)) {}
57 
62  template <class DATA>
63  SharedObjectsContainer(DATA first, DATA last)
64  : m_data(first, last) {}
88  template <class DATA, class PREDICATE>
89  SharedObjectsContainer(DATA first, DATA last, const PREDICATE& cut)
90  {
91  insert ( first , last , cut ) ;
92  }
93  // ==========================================================================
94 public:
95  // ==========================================================================
97  const CLID& clID() const override
100  static const CLID& classID()
101  {
102  static const CLID s_clid =
103  ( ( static_cast<CLID> ( -1 ) << 16 ) // 16 used and 16 empty bits
104  & !CLID_ObjectVector // not an ObjectVector
105  & !CLID_ObjectList // not an ObjectList
106  & !static_cast<CLID> ( 0x00030000 ) // not a KeyedContainer/map
107  & !static_cast<CLID> ( 0x00040000 ) )// not a KeyedContainer/hashmap
108  + TYPE::classID() ; // the specific CLID from the contents
109  //
110  return s_clid;
111  }
112  // ==========================================================================
113 public:
114  // ==========================================================================
116  inline const ConstVector& data () const { return m_data ; }
118  operator const ConstVector& () const { return data () ; }
119  // ==========================================================================
120 public:
121  // ==========================================================================
123  size_type size () const { return m_data.size () ; }
125  bool empty () const { return m_data.empty () ; }
129  void push_back ( const TYPE* object ) { m_data.push_back ( object ) ; }
133  void insert ( const TYPE* object ) { m_data.push_back ( object ) ; }
138  template <class DATA>
139  void insert
140  ( DATA first ,
141  DATA last ) { m_data.insert ( end() , first ,last ) ; }
168  template <class DATA, class PREDICATE>
169  void insert
170  ( DATA first ,
171  DATA last ,
172  const PREDICATE& cut )
173  {
174  m_data.reserve ( m_data.size() + std::distance ( first , last ) ) ;
175  std::copy_if( first, last, std::back_inserter(m_data), std::cref(cut) );
176  }
201  template <class OUTPUT, class PREDICATE>
202  OUTPUT get ( const PREDICATE& cut ,
203  OUTPUT output ) const
204  {
205  return std::copy_if( begin(), end(), output, std::cref(cut) );
206  }
208  void erase ( iterator i ) { m_data.erase ( i ) ; }
226  template <class PREDICATE>
227  void erase ( const PREDICATE& cut )
228  { m_data.erase( std::remove_if ( begin() , end() , cut ) , end() ) ; }
247  bool erase ( const TYPE* object )
248  {
249  auto i = std::find ( begin() , end() , object ) ;
250  if ( end() == i ) { return false ; }
251  m_data.erase ( i ) ;
252  return true ;
253  }
254  // ==========================================================================
255 public:
256  // ==========================================================================
258  reference operator[] ( size_type index ) { return m_data [index] ; }
260  const_reference operator[] ( size_type index ) const { return m_data [index] ; }
262  reference operator() ( size_type index ) { return m_data [index] ; }
264  const_reference operator() ( size_type index ) const { return m_data [index] ; }
266  reference at ( size_type index ) { return m_data.at(index) ; }
268  const_reference at ( size_type index ) const { return m_data.at(index) ; }
269  // ==========================================================================
270 public:
271  // ==========================================================================
272  iterator begin () { return m_data . begin () ; }
273  iterator end () { return m_data . end () ; }
274  const_iterator begin () const { return m_data . begin () ; }
275  const_iterator end () const { return m_data . end () ; }
276  reverse_iterator rbegin () { return m_data . rbegin () ; }
277  reverse_iterator rend () { return m_data . rend () ; }
278  const_reverse_iterator rbegin () const { return m_data . rbegin () ; }
279  const_reverse_iterator rend () const { return m_data . rend () ; }
280  // ==========================================================================
281 public:
282  // ==========================================================================
284  reference front () { return m_data . front () ; }
286  const_reference front () const { return m_data . front () ; }
288  reference back () { return m_data . back () ; }
290  const_reference back () const { return m_data . back () ; }
291  // ==========================================================================
292 public:
293  // ==========================================================================
295  bool operator== ( const SharedObjectsContainer& right ) const
296  { return &right == this || right.m_data == m_data ; }
298  bool operator== ( const ConstVector& right ) const
299  { return m_data == right ; }
301  bool operator < ( const SharedObjectsContainer& right ) const
302  { return m_data < right.m_data ; }
304  bool operator < ( const ConstVector& right ) const
305  { return m_data < right ; }
306  // ==========================================================================
307 public: // ObjectContainerBase methods:
308  // ==========================================================================
312  long index( const ContainedObject* object ) const override
313  {
314  auto _i = std::find ( begin() , end() , object ) ;
315  return end() != _i ? ( _i - begin() ) : -1 ; // RETURN
316  }
321  ContainedObject* containedObject ( long index ) const override
322  {
323  if ( 0 > index || !(index < (long) size () ) ) { return nullptr; } // RETURN
324  const ContainedObject* co = m_data[index] ;
325  return const_cast<ContainedObject*>( co ) ;
326  }
328  size_type numberOfObjects() const override { return m_data.size() ; }
333  long add ( ContainedObject* object) override
334  {
335  if ( !object ) { return -1 ; } // RETURN
336  TYPE* _obj = dynamic_cast<TYPE*> ( object ) ;
337  if ( !_obj ) { return -1 ; } // RETURN
338  const size_type pos = size() ;
339  push_back ( _obj ) ;
340  return pos ;
341  }
346  long remove ( ContainedObject* value ) override
347  {
348  auto _i = std::find ( begin() , end() , value ) ;
349  if ( end() == _i ) { return -1 ; } // RETURN
350  const size_type pos = _i - begin() ;
351  m_data.erase ( _i ) ;
352  return pos ; // RETURN
353  }
354  // ==========================================================================
355 private:
356  // ==========================================================================
357  // the actual data
358  ConstVector m_data ; // the actual data
359  // ==========================================================================
360 };
361 // ============================================================================
362 // The END
363 // ============================================================================
364 #endif // GAUDIKERNEL_SHAREDOBJECTSCONTAINER_H
365 // ============================================================================
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 owned by this 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
STL namespace.
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...
unsigned int CLID
Class ID definition.
Definition: ClassID.h:8
SharedObjectsContainer()=default
bool empty() const
empty container?
reference operator[](size_type index)
index access
SharedObjectsContainer & operator=(SharedObjectsContainer &&)=default
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)
SharedObjectsContainer(ConstVector &&data)
T cref(T...args)
T back_inserter(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)