The Gaudi Framework  v32r2 (46d42edc)
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/ClassID.h"
14 #include "GaudiKernel/Kernel.h"
16 // ============================================================================
28 template <class TYPE>
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)
47  SharedObjectsContainer() = default;
48  // move constructor and move assignement
51  // the constructor from the data
54 
59  template <class DATA>
60  SharedObjectsContainer( DATA first, DATA last ) : m_data( first, last ) {}
84  template <class DATA, class PREDICATE>
85  SharedObjectsContainer( DATA first, DATA last, const PREDICATE& cut ) {
86  insert( first, last, cut );
87  }
88  // ==========================================================================
89 public:
90  // ==========================================================================
92  const CLID& clID() const override { return SharedObjectsContainer<TYPE>::classID(); }
94  static const CLID& classID() {
95  static const CLID s_clid = ( ( static_cast<CLID>( -1 ) << 16 ) // 16 used and 16 empty bits
96  & !CLID_ObjectVector // not an ObjectVector
97  & !CLID_ObjectList // not an ObjectList
98  & !static_cast<CLID>( 0x00030000 ) // not a KeyedContainer/map
99  & !static_cast<CLID>( 0x00040000 ) ) // not a KeyedContainer/hashmap
100  + TYPE::classID(); // the specific CLID from the contents
101  //
102  return s_clid;
103  }
104  // ==========================================================================
105 public:
106  // ==========================================================================
108  inline const ConstVector& data() const { return m_data; }
110  operator const ConstVector&() const { return data(); }
111  // ==========================================================================
112 public:
113  // ==========================================================================
115  size_type size() const { return m_data.size(); }
117  bool empty() const { return m_data.empty(); }
121  void push_back( const TYPE* object ) { m_data.push_back( object ); }
125  void insert( const TYPE* object ) { m_data.push_back( object ); }
130  template <class DATA>
131  void insert( DATA first, DATA last ) {
132  m_data.insert( end(), first, last );
133  }
160  template <class DATA, class PREDICATE>
161  void insert( DATA first, DATA last, const PREDICATE& cut ) {
162  m_data.reserve( m_data.size() + std::distance( first, last ) );
163  std::copy_if( first, last, std::back_inserter( m_data ), std::cref( cut ) );
164  }
189  template <class OUTPUT, class PREDICATE>
190  OUTPUT get( const PREDICATE& cut, OUTPUT output ) const {
191  return std::copy_if( begin(), end(), output, std::cref( cut ) );
192  }
194  void erase( iterator i ) { m_data.erase( i ); }
212  template <class PREDICATE>
213  void erase( const PREDICATE& cut ) {
214  m_data.erase( std::remove_if( begin(), end(), cut ), end() );
215  }
234  bool erase( const TYPE* object ) {
235  auto i = std::find( begin(), end(), object );
236  if ( end() == i ) { return false; }
237  m_data.erase( i );
238  return true;
239  }
240  // ==========================================================================
241 public:
242  // ==========================================================================
254  const_reference at( size_type index ) const { return m_data.at( index ); }
255  // ==========================================================================
256 public:
257  // ==========================================================================
258  iterator begin() { return m_data.begin(); }
259  iterator end() { return m_data.end(); }
260  const_iterator begin() const { return m_data.begin(); }
261  const_iterator end() const { return m_data.end(); }
265  const_reverse_iterator rend() const { return m_data.rend(); }
266  // ==========================================================================
267 public:
268  // ==========================================================================
270  reference front() { return m_data.front(); }
272  const_reference front() const { return m_data.front(); }
274  reference back() { return m_data.back(); }
276  const_reference back() const { return m_data.back(); }
277  // ==========================================================================
278 public:
279  // ==========================================================================
281  bool operator==( const SharedObjectsContainer& right ) const { return &right == this || right.m_data == m_data; }
283  bool operator==( const ConstVector& right ) const { return m_data == right; }
285  bool operator<( const SharedObjectsContainer& right ) const { return m_data < right.m_data; }
287  bool operator<( const ConstVector& right ) const { return m_data < right; }
288  // ==========================================================================
289  // ObjectContainerBase methods:
290  // ==========================================================================
291 public:
295  long index( const ContainedObject* object ) const override {
296  auto _i = std::find( begin(), end(), object );
297  return end() != _i ? ( _i - begin() ) : -1; // RETURN
298  }
303  const ContainedObject* containedObject( long index ) const override {
304  if ( 0 > index || !( index < (long)size() ) ) { return nullptr; } // RETURN
305  return m_data[index];
306  }
308  if ( 0 > index || !( index < (long)size() ) ) { return nullptr; } // RETURN
309  return &const_cast<TYPE&>( *m_data[index] );
310  }
312  size_type numberOfObjects() const override { return m_data.size(); }
317  long add( ContainedObject* object ) override {
318  if ( !object ) { return -1; } // RETURN
319  TYPE* _obj = dynamic_cast<TYPE*>( object );
320  if ( !_obj ) { return -1; } // RETURN
321  const size_type pos = size();
322  push_back( _obj );
323  return pos;
324  }
329  long remove( ContainedObject* value ) override {
330  auto _i = std::find( begin(), end(), value );
331  if ( end() == _i ) { return -1; } // RETURN
332  const size_type pos = _i - begin();
333  m_data.erase( _i );
334  return pos; // RETURN
335  }
336  // ==========================================================================
337 private:
338  // ==========================================================================
339  // the actual data
340  ConstVector m_data; // the actual data
341  // ==========================================================================
342 };
343 // ============================================================================
344 // The END
345 // ============================================================================
346 #endif // GAUDIKERNEL_SHAREDOBJECTSCONTAINER_H
bool erase(const TYPE *object)
erase the first occurance of the certain element
const ContainedObject * containedObject(long index) const override
Pointer to an object of a given distance.
const_reference at(size_type index) const
checked access (const-version)
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.
T empty(T... args)
T copy_if(T... args)
T distance(T... args)
reference operator()(size_type index)
'functional'-access
void erase(const PREDICATE &cut)
erase the objects which satisfy the criteria
const CLID & clID() const override
Retrieve the unique class ID (virtual)
size_t size_type
size_type, to conform the STL container interface
size_type size() const
get the actual size of the container
T rend(T... args)
void push_back(const TYPE *object)
insert one object into the container
T front(T... args)
const_reference operator()(size_type index) const
'functional'-access (const version)
ConstVector::size_type size_type
STL namespace.
ConstVector::reverse_iterator reverse_iterator
T end(T... args)
size_type numberOfObjects() const override
Number of objects in the container.
T remove_if(T... args)
OUTPUT get(const PREDICATE &cut, OUTPUT output) const
get from the container all objects which satisfy the certain criteria
const_reference operator[](size_type index) const
index access (const-version)
SharedObjectsContainer(DATA first, DATA last, const PREDICATE &cut)
the templated constructor from the pair of iterators and the predicate.
void insert(DATA first, DATA last, const PREDICATE &cut)
add the sequence of 'good'objects into the container
ConstVector::iterator iterator
const_reference front() const
the first element (only for non-empty vectors) (const-version)
ConstVector::const_reference const_reference
long index(const ContainedObject *object) const override
Distance of a given object from the beginning of its container.
T at(T... args)
void insert(const TYPE *object)
insert one object into the container
T push_back(T... args)
ConstVector::const_iterator const_iterator
bool operator<(const ConstVector &right) const
comparisons with corresponding vector
SharedObjectsContainer(const ConstVector &data)
const_iterator begin() const
T erase(T... args)
reference at(size_type index)
checked access
long add(ContainedObject *object) override
Virtual functions (forwards to the concrete container definitions) Add an object to the container.
long remove(ContainedObject *value) override
Release object from the container (the pointer will be removed from the container,...
unsigned int CLID
Class ID definition.
Definition: ClassID.h:8
SharedObjectsContainer()=default
bool operator<(const SharedObjectsContainer &right) const
comparisons with other 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)
T size(T... args)
SharedObjectsContainer(ConstVector &&data)
T cref(T... args)
const ConstVector & data() const
get the access to the underlying container (const)
T begin(T... args)
bool operator==(const SharedObjectsContainer &right) const
equal content with other container ?
T back_inserter(T... args)
static const CLID & classID()
Retrieve the unuqie class ID (static)
const_reverse_iterator rend() const
bool operator==(const ConstVector &right) const
equal content with corresponding vector ?
const_reverse_iterator rbegin() const
T back(T... args)
ConstVector::reference reference
reference back()
the last element (only for non-empty vectors)
ObjectContainerBase is the base class for Gaudi container classes.
void insert(DATA first, DATA last)
add the sequence of objects into the container
const_iterator end() const
bool empty() const
empty container?
SharedObjectsContainer(DATA first, DATA last)
the templated constructor from the pair of iterators
T reserve(T... args)
const_reference back() const
the last element (only for non-empty vectors) (const-version)
void erase(iterator i)
erase the object by iterator
ConstVector::const_reverse_iterator const_reverse_iterator
ContainedObject * containedObject(long index) override
T rbegin(T... args)
ConstVector::value_type value_type
various types (to make STL happy)