The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
SharedObjectsContainer.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 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#pragma once
12
13#include <GaudiKernel/ClassID.h>
14#include <GaudiKernel/Kernel.h>
16#include <algorithm>
17#include <vector>
18
30template <class TYPE>
32public:
34 typedef std::vector<const TYPE*> ConstVector;
35 // various types (to make STL happy)
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
50
55 template <class DATA>
56 SharedObjectsContainer( DATA first, DATA last ) : m_data( first, last ) {}
80 template <class DATA, class PREDICATE>
81 SharedObjectsContainer( DATA first, DATA last, const PREDICATE& cut ) {
82 insert( first, last, cut );
83 }
84
86 const CLID& clID() const override { return SharedObjectsContainer<TYPE>::classID(); }
88 static const CLID& classID() {
89 static const CLID s_clid = ( ( static_cast<CLID>( -1 ) << 16 ) // 16 used and 16 empty bits
90 & !CLID_ObjectVector // not an ObjectVector
91 & !CLID_ObjectList // not an ObjectList
92 & !static_cast<CLID>( 0x00030000 ) // not a KeyedContainer/map
93 & !static_cast<CLID>( 0x00040000 ) ) // not a KeyedContainer/hashmap
94 + TYPE::classID(); // the specific CLID from the contents
95 //
96 return s_clid;
97 }
98
99 inline const ConstVector& data() const { return m_data; }
101 operator const ConstVector&() const { return data(); }
103 size_type size() const { return m_data.size(); }
105 bool empty() const { return m_data.empty(); }
109 void push_back( const TYPE* object ) { m_data.push_back( object ); }
113 void insert( const TYPE* object ) { m_data.push_back( object ); }
118 template <class DATA>
119 void insert( DATA first, DATA last ) {
120 m_data.insert( end(), first, last );
121 }
122
148 template <class DATA, class PREDICATE>
149 void insert( DATA first, DATA last, const PREDICATE& cut ) {
150 m_data.reserve( m_data.size() + std::distance( first, last ) );
151 std::copy_if( first, last, std::back_inserter( m_data ), std::cref( cut ) );
152 }
153
177 template <class OUTPUT, class PREDICATE>
178 OUTPUT get( const PREDICATE& cut, OUTPUT output ) const {
179 return std::copy_if( begin(), end(), output, std::cref( cut ) );
180 }
181
182 void erase( iterator i ) { m_data.erase( i ); }
200 template <class PREDICATE>
201 void erase( const PREDICATE& cut ) {
202 m_data.erase( std::remove_if( begin(), end(), cut ), end() );
203 }
204
222 bool erase( const TYPE* object ) {
223 auto i = std::find( begin(), end(), object );
224 if ( end() == i ) { return false; }
225 m_data.erase( i );
226 return true;
227 }
228
237 reference at( size_type index ) { return m_data.at( index ); }
239 const_reference at( size_type index ) const { return m_data.at( index ); }
240 iterator begin() { return m_data.begin(); }
241 iterator end() { return m_data.end(); }
242 const_iterator begin() const { return m_data.begin(); }
243 const_iterator end() const { return m_data.end(); }
244 reverse_iterator rbegin() { return m_data.rbegin(); }
245 reverse_iterator rend() { return m_data.rend(); }
246 const_reverse_iterator rbegin() const { return m_data.rbegin(); }
247 const_reverse_iterator rend() const { return m_data.rend(); }
249 reference front() { return m_data.front(); }
251 const_reference front() const { return m_data.front(); }
253 reference back() { return m_data.back(); }
255 const_reference back() const { return m_data.back(); }
257 bool operator==( const SharedObjectsContainer& right ) const { return &right == this || right.m_data == m_data; }
259 bool operator==( const ConstVector& right ) const { return m_data == right; }
261 bool operator<( const SharedObjectsContainer& right ) const { return m_data < right.m_data; }
263 bool operator<( const ConstVector& right ) const { return m_data < right; }
264 // ==========================================================================
265 // ObjectContainerBase methods:
266 // ==========================================================================
270 long index( const ContainedObject* object ) const override {
271 auto _i = std::find( begin(), end(), object );
272 return end() != _i ? ( _i - begin() ) : -1;
273 }
274
278 const ContainedObject* containedObject( long index ) const override {
279 if ( 0 > index || !( index < (long)size() ) ) { return nullptr; }
280 return m_data[index];
281 }
283 if ( 0 > index || !( index < (long)size() ) ) { return nullptr; }
284 return &const_cast<TYPE&>( *m_data[index] );
285 }
286
287 size_type numberOfObjects() const override { return m_data.size(); }
292 long add( ContainedObject* object ) override {
293 if ( !object ) { return -1; }
294 TYPE* _obj = dynamic_cast<TYPE*>( object );
295 if ( !_obj ) { return -1; }
296 const size_type pos = size();
297 push_back( _obj );
298 return pos;
299 }
300
304 long remove( ContainedObject* value ) override {
305 auto _i = std::find( begin(), end(), value );
306 if ( end() == _i ) { return -1; }
307 const size_type pos = _i - begin();
308 m_data.erase( _i );
309 return pos;
310 }
311
312private:
313 ConstVector m_data; // the actual data
314};
unsigned int CLID
Class ID definition.
Definition ClassID.h:16
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
ObjectContainerBase()=default
Constructor.
const_iterator end() const
long remove(ContainedObject *value) override
Release object from the container (the pointer will be removed from the container,...
const_reference front() const
the first element (only for non-empty vectors) (const-version)
ConstVector::reverse_iterator reverse_iterator
const_reverse_iterator rend() const
bool empty() const
empty container?
size_type size() const
get the actual size of the container
SharedObjectsContainer & operator=(SharedObjectsContainer &&)=default
reference operator()(size_type index)
'functional'-access
const_reference at(size_type index) const
checked access (const-version)
std::vector< const TYPE * > ConstVector
the actual container type
OUTPUT get(const PREDICATE &cut, OUTPUT output) const
get from the container all objects which satisfy the certain criteria
const_reverse_iterator rbegin() const
ConstVector::size_type size_type
bool operator==(const ConstVector &right) const
equal content with corresponding vector ?
ConstVector::const_iterator const_iterator
reference back()
the last element (only for non-empty vectors)
const ContainedObject * containedObject(long index) const override
Pointer to an object of a given distance.
ConstVector::value_type value_type
void insert(const TYPE *object)
insert one object into the container
void insert(DATA first, DATA last, const PREDICATE &cut)
add the sequence of 'good'objects into the container
SharedObjectsContainer(ConstVector &&data)
void insert(DATA first, DATA last)
add the sequence of objects into the container
SharedObjectsContainer(DATA first, DATA last, const PREDICATE &cut)
the templated constructor from the pair of iterators and the predicate.
ConstVector::const_reference const_reference
SharedObjectsContainer(DATA first, DATA last)
the templated constructor from the pair of iterators
void erase(iterator i)
erase the object by iterator
void erase(const PREDICATE &cut)
erase the objects which satisfy the criteria
SharedObjectsContainer(const ConstVector &data)
const_reference operator[](size_type index) const
index access (const-version)
long index(const ContainedObject *object) const override
const_reference back() const
the last element (only for non-empty vectors) (const-version)
bool operator==(const SharedObjectsContainer &right) const
equal content with other container ?
SharedObjectsContainer(SharedObjectsContainer &&)=default
ContainedObject * containedObject(long index) override
SharedObjectsContainer()=default
const_iterator begin() const
const CLID & clID() const override
Retrieve the unique class ID (virtual)
ConstVector::const_reverse_iterator const_reverse_iterator
long add(ContainedObject *object) override
Virtual functions (forwards to the concrete container definitions) Add an object to the container.
reference front()
the first element (only for non-empty vectors)
reference at(size_type index)
checked access
void push_back(const TYPE *object)
insert one object into the container
bool operator<(const SharedObjectsContainer &right) const
comparisons with other container
reference operator[](size_type index)
index access
ConstVector::reference reference
size_type numberOfObjects() const override
Number of objects in the container.
const_reference operator()(size_type index) const
'functional'-access (const version)
ConstVector::iterator iterator
bool operator<(const ConstVector &right) const
comparisons with corresponding vector
static const CLID & classID()
Retrieve the unuqie class ID (static)
bool erase(const TYPE *object)
erase the first occurance of the certain element
STL namespace.