The Gaudi Framework  master (37c0b60a)
SharedObjectsContainer.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDIKERNEL_SHAREDOBJECTSCONTAINER_H
12 #define GAUDIKERNEL_SHAREDOBJECTSCONTAINER_H 1
13 // ============================================================================
14 // Include files
15 // ============================================================================
16 // STD & STL
17 // ============================================================================
18 #include <algorithm>
19 #include <vector>
20 // ============================================================================
21 // GaudiKernel
22 // ============================================================================
23 #include <GaudiKernel/ClassID.h>
24 #include <GaudiKernel/Kernel.h>
26 // ============================================================================
38 template <class TYPE>
40 public:
41  // ==========================================================================
45  typedef typename ConstVector::value_type value_type;
46  typedef typename ConstVector::size_type size_type;
48  typedef typename ConstVector::const_reference const_reference;
49  typedef typename ConstVector::iterator iterator;
50  typedef typename ConstVector::const_iterator const_iterator;
51  typedef typename ConstVector::reverse_iterator reverse_iterator;
52  typedef typename ConstVector::const_reverse_iterator const_reverse_iterator;
53  // ==========================================================================
54 public:
55  // ==========================================================================
56  // the default constructor (creates the empty vector)
58  // move constructor and move assignement
61  // the constructor from the data
64 
69  template <class DATA>
70  SharedObjectsContainer( DATA first, DATA last ) : m_data( first, last ) {}
94  template <class DATA, class PREDICATE>
95  SharedObjectsContainer( DATA first, DATA last, const PREDICATE& cut ) {
96  insert( first, last, cut );
97  }
98  // ==========================================================================
99 public:
100  // ==========================================================================
102  const CLID& clID() const override { return SharedObjectsContainer<TYPE>::classID(); }
104  static const CLID& classID() {
105  static const CLID s_clid = ( ( static_cast<CLID>( -1 ) << 16 ) // 16 used and 16 empty bits
106  & !CLID_ObjectVector // not an ObjectVector
107  & !CLID_ObjectList // not an ObjectList
108  & !static_cast<CLID>( 0x00030000 ) // not a KeyedContainer/map
109  & !static_cast<CLID>( 0x00040000 ) ) // not a KeyedContainer/hashmap
110  + TYPE::classID(); // the specific CLID from the contents
111  //
112  return s_clid;
113  }
114  // ==========================================================================
115 public:
116  // ==========================================================================
118  inline const ConstVector& data() const { return m_data; }
120  operator const ConstVector&() const { return data(); }
121  // ==========================================================================
122 public:
123  // ==========================================================================
125  size_type size() const { return m_data.size(); }
127  bool empty() const { return m_data.empty(); }
131  void push_back( const TYPE* object ) { m_data.push_back( object ); }
135  void insert( const TYPE* object ) { m_data.push_back( object ); }
140  template <class DATA>
141  void insert( DATA first, DATA last ) {
142  m_data.insert( end(), first, last );
143  }
170  template <class DATA, class PREDICATE>
171  void insert( DATA first, DATA last, const PREDICATE& cut ) {
172  m_data.reserve( m_data.size() + std::distance( first, last ) );
173  std::copy_if( first, last, std::back_inserter( m_data ), std::cref( cut ) );
174  }
199  template <class OUTPUT, class PREDICATE>
200  OUTPUT get( const PREDICATE& cut, OUTPUT output ) const {
201  return std::copy_if( begin(), end(), output, std::cref( cut ) );
202  }
204  void erase( iterator i ) { m_data.erase( i ); }
222  template <class PREDICATE>
223  void erase( const PREDICATE& cut ) {
224  m_data.erase( std::remove_if( begin(), end(), cut ), end() );
225  }
244  bool erase( const TYPE* object ) {
245  auto i = std::find( begin(), end(), object );
246  if ( end() == i ) { return false; }
247  m_data.erase( i );
248  return true;
249  }
250  // ==========================================================================
251 public:
252  // ==========================================================================
264  const_reference at( size_type index ) const { return m_data.at( index ); }
265  // ==========================================================================
266 public:
267  // ==========================================================================
268  iterator begin() { return m_data.begin(); }
269  iterator end() { return m_data.end(); }
270  const_iterator begin() const { return m_data.begin(); }
271  const_iterator end() const { return m_data.end(); }
275  const_reverse_iterator rend() const { return m_data.rend(); }
276  // ==========================================================================
277 public:
278  // ==========================================================================
280  reference front() { return m_data.front(); }
282  const_reference front() const { return m_data.front(); }
284  reference back() { return m_data.back(); }
286  const_reference back() const { return m_data.back(); }
287  // ==========================================================================
288 public:
289  // ==========================================================================
291  bool operator==( const SharedObjectsContainer& right ) const { return &right == this || right.m_data == m_data; }
293  bool operator==( const ConstVector& right ) const { return m_data == right; }
295  bool operator<( const SharedObjectsContainer& right ) const { return m_data < right.m_data; }
297  bool operator<( const ConstVector& right ) const { return m_data < right; }
298  // ==========================================================================
299  // ObjectContainerBase methods:
300  // ==========================================================================
301 public:
305  long index( const ContainedObject* object ) const override {
306  auto _i = std::find( begin(), end(), object );
307  return end() != _i ? ( _i - begin() ) : -1; // RETURN
308  }
313  const ContainedObject* containedObject( long index ) const override {
314  if ( 0 > index || !( index < (long)size() ) ) { return nullptr; } // RETURN
315  return m_data[index];
316  }
318  if ( 0 > index || !( index < (long)size() ) ) { return nullptr; } // RETURN
319  return &const_cast<TYPE&>( *m_data[index] );
320  }
322  size_type numberOfObjects() const override { return m_data.size(); }
327  long add( ContainedObject* object ) override {
328  if ( !object ) { return -1; } // RETURN
329  TYPE* _obj = dynamic_cast<TYPE*>( object );
330  if ( !_obj ) { return -1; } // RETURN
331  const size_type pos = size();
332  push_back( _obj );
333  return pos;
334  }
339  long remove( ContainedObject* value ) override {
340  auto _i = std::find( begin(), end(), value );
341  if ( end() == _i ) { return -1; } // RETURN
342  const size_type pos = _i - begin();
343  m_data.erase( _i );
344  return pos; // RETURN
345  }
346  // ==========================================================================
347 private:
348  // ==========================================================================
349  // the actual data
350  ConstVector m_data; // the actual data
351  // ==========================================================================
352 };
353 // ============================================================================
354 // The END
355 // ============================================================================
356 #endif // GAUDIKERNEL_SHAREDOBJECTSCONTAINER_H
SharedObjectsContainer::operator[]
const_reference operator[](size_type index) const
index access (const-version)
Definition: SharedObjectsContainer.h:256
SharedObjectsContainer::get
OUTPUT get(const PREDICATE &cut, OUTPUT output) const
get from the container all objects which satisfy the certain criteria
Definition: SharedObjectsContainer.h:200
SharedObjectsContainer::SharedObjectsContainer
SharedObjectsContainer(DATA first, DATA last, const PREDICATE &cut)
the templated constructor from the pair of iterators and the predicate.
Definition: SharedObjectsContainer.h:95
SharedObjectsContainer::operator==
bool operator==(const SharedObjectsContainer &right) const
equal content with other container ?
Definition: SharedObjectsContainer.h:291
SharedObjectsContainer::SharedObjectsContainer
SharedObjectsContainer(DATA first, DATA last)
the templated constructor from the pair of iterators
Definition: SharedObjectsContainer.h:70
SharedObjectsContainer::push_back
void push_back(const TYPE *object)
insert one object into the container
Definition: SharedObjectsContainer.h:131
SharedObjectsContainer::operator()
const_reference operator()(size_type index) const
'functional'-access (const version)
Definition: SharedObjectsContainer.h:260
SharedObjectsContainer::rbegin
const_reverse_iterator rbegin() const
Definition: SharedObjectsContainer.h:274
SharedObjectsContainer::rbegin
reverse_iterator rbegin()
Definition: SharedObjectsContainer.h:272
SharedObjectsContainer::operator==
bool operator==(const ConstVector &right) const
equal content with corresponding vector ?
Definition: SharedObjectsContainer.h:293
SharedObjectsContainer::add
long add(ContainedObject *object) override
Virtual functions (forwards to the concrete container definitions) Add an object to the container.
Definition: SharedObjectsContainer.h:327
SharedObjectsContainer::reference
ConstVector::reference reference
Definition: SharedObjectsContainer.h:47
SharedObjectsContainer::SharedObjectsContainer
SharedObjectsContainer()=default
SharedObjectsContainer::const_iterator
ConstVector::const_iterator const_iterator
Definition: SharedObjectsContainer.h:50
SharedObjectsContainer::operator<
bool operator<(const SharedObjectsContainer &right) const
comparisons with other container
Definition: SharedObjectsContainer.h:295
std::vector::reserve
T reserve(T... args)
SharedObjectsContainer::reverse_iterator
ConstVector::reverse_iterator reverse_iterator
Definition: SharedObjectsContainer.h:51
SharedObjectsContainer::ConstVector
std::vector< const TYPE * > ConstVector
the actual container type
Definition: SharedObjectsContainer.h:43
std::vector< const TYPE * >
std::find
T find(T... args)
std::vector::size
T size(T... args)
ClassID.h
std::back_inserter
T back_inserter(T... args)
SharedObjectsContainer::insert
void insert(DATA first, DATA last)
add the sequence of objects into the container
Definition: SharedObjectsContainer.h:141
SharedObjectsContainer::SharedObjectsContainer
SharedObjectsContainer(SharedObjectsContainer &&)=default
std::distance
T distance(T... args)
SharedObjectsContainer::at
reference at(size_type index)
checked access
Definition: SharedObjectsContainer.h:262
SharedObjectsContainer::classID
static const CLID & classID()
Retrieve the unuqie class ID (static)
Definition: SharedObjectsContainer.h:104
std::vector::back
T back(T... args)
SharedObjectsContainer::remove
long remove(ContainedObject *value) override
Release object from the container (the pointer will be removed from the container,...
Definition: SharedObjectsContainer.h:339
SharedObjectsContainer::const_reverse_iterator
ConstVector::const_reverse_iterator const_reverse_iterator
Definition: SharedObjectsContainer.h:52
gaudirun.output
output
Definition: gaudirun.py:521
SharedObjectsContainer::erase
bool erase(const TYPE *object)
erase the first occurance of the certain element
Definition: SharedObjectsContainer.h:244
std::vector::front
T front(T... args)
SharedObjectsContainer::containedObject
const ContainedObject * containedObject(long index) const override
Pointer to an object of a given distance.
Definition: SharedObjectsContainer.h:313
SharedObjectsContainer::operator=
SharedObjectsContainer & operator=(SharedObjectsContainer &&)=default
std::vector::push_back
T push_back(T... args)
SharedObjectsContainer::erase
void erase(const PREDICATE &cut)
erase the objects which satisfy the criteria
Definition: SharedObjectsContainer.h:223
SharedObjectsContainer::data
const ConstVector & data() const
get the access to the underlying container (const)
Definition: SharedObjectsContainer.h:118
std::vector::at
T at(T... args)
SharedObjectsContainer::m_data
ConstVector m_data
Definition: SharedObjectsContainer.h:350
SharedObjectsContainer::front
const_reference front() const
the first element (only for non-empty vectors) (const-version)
Definition: SharedObjectsContainer.h:282
SharedObjectsContainer::const_reference
ConstVector::const_reference const_reference
Definition: SharedObjectsContainer.h:48
SharedObjectsContainer::back
reference back()
the last element (only for non-empty vectors)
Definition: SharedObjectsContainer.h:284
SharedObjectsContainer::operator[]
reference operator[](size_type index)
index access
Definition: SharedObjectsContainer.h:254
SharedObjectsContainer::at
const_reference at(size_type index) const
checked access (const-version)
Definition: SharedObjectsContainer.h:264
SharedObjectsContainer::front
reference front()
the first element (only for non-empty vectors)
Definition: SharedObjectsContainer.h:280
SharedObjectsContainer::size_type
ConstVector::size_type size_type
Definition: SharedObjectsContainer.h:46
SharedObjectsContainer
Definition: SharedObjectsContainer.h:39
std::vector::erase
T erase(T... args)
std::copy_if
T copy_if(T... args)
ObjectContainerBase::size_type
size_t size_type
size_type, to conform the STL container interface
Definition: ObjectContainerBase.h:40
CLID
unsigned int CLID
Class ID definition.
Definition: ClassID.h:18
SharedObjectsContainer::operator()
reference operator()(size_type index)
'functional'-access
Definition: SharedObjectsContainer.h:258
std::remove_if
T remove_if(T... args)
SharedObjectsContainer::empty
bool empty() const
empty container?
Definition: SharedObjectsContainer.h:127
SharedObjectsContainer::rend
const_reverse_iterator rend() const
Definition: SharedObjectsContainer.h:275
std::vector::rend
T rend(T... args)
SharedObjectsContainer::erase
void erase(iterator i)
erase the object by iterator
Definition: SharedObjectsContainer.h:204
SharedObjectsContainer::containedObject
ContainedObject * containedObject(long index) override
Definition: SharedObjectsContainer.h:317
SharedObjectsContainer::value_type
ConstVector::value_type value_type
various types (to make STL happy)
Definition: SharedObjectsContainer.h:45
SharedObjectsContainer::insert
void insert(const TYPE *object)
insert one object into the container
Definition: SharedObjectsContainer.h:135
ObjectContainerBase
Definition: ObjectContainerBase.h:29
std::vector::begin
T begin(T... args)
SharedObjectsContainer::rend
reverse_iterator rend()
Definition: SharedObjectsContainer.h:273
std
STL namespace.
std::vector::insert
T insert(T... args)
Kernel.h
SharedObjectsContainer::operator<
bool operator<(const ConstVector &right) const
comparisons with corresponding vector
Definition: SharedObjectsContainer.h:297
SharedObjectsContainer::begin
iterator begin()
Definition: SharedObjectsContainer.h:268
fixtures.reference
Generator[dict, None, None] reference(request, Optional[Path] reference_path)
Definition: fixtures.py:211
SharedObjectsContainer::end
const_iterator end() const
Definition: SharedObjectsContainer.h:271
ObjectContainerBase.h
SharedObjectsContainer::clID
const CLID & clID() const override
Retrieve the unique class ID (virtual)
Definition: SharedObjectsContainer.h:102
std::vector::empty
T empty(T... args)
SharedObjectsContainer::begin
const_iterator begin() const
Definition: SharedObjectsContainer.h:270
SharedObjectsContainer::size
size_type size() const
get the actual size of the container
Definition: SharedObjectsContainer.h:125
std::vector::end
T end(T... args)
SharedObjectsContainer::end
iterator end()
Definition: SharedObjectsContainer.h:269
SharedObjectsContainer::back
const_reference back() const
the last element (only for non-empty vectors) (const-version)
Definition: SharedObjectsContainer.h:286
SharedObjectsContainer::insert
void insert(DATA first, DATA last, const PREDICATE &cut)
add the sequence of 'good'objects into the container
Definition: SharedObjectsContainer.h:171
SharedObjectsContainer::iterator
ConstVector::iterator iterator
Definition: SharedObjectsContainer.h:49
SharedObjectsContainer::index
long index(const ContainedObject *object) const override
Distance of a given object from the beginning of its container.
Definition: SharedObjectsContainer.h:305
std::cref
T cref(T... args)
ContainedObject
Definition: ContainedObject.h:41
std::vector::rbegin
T rbegin(T... args)
SharedObjectsContainer::numberOfObjects
size_type numberOfObjects() const override
Number of objects in the container.
Definition: SharedObjectsContainer.h:322
SharedObjectsContainer::SharedObjectsContainer
SharedObjectsContainer(const ConstVector &data)
Definition: SharedObjectsContainer.h:62
SharedObjectsContainer::SharedObjectsContainer
SharedObjectsContainer(ConstVector &&data)
Definition: SharedObjectsContainer.h:63