The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
ObjectList.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>
17#include <iomanip>
18#include <list>
19
20// Definition of the CLID for this class defined in ClassID.h
21// static const CLID CLID_ObjectList = (1<<18); // ObjectList (bit 18 set)
22
41template <class TYPE>
43
44public:
45 typedef TYPE contained_type;
46 typedef typename std::list<TYPE*>::value_type value_type;
47
48 typedef typename std::list<TYPE*>::reference reference;
49 typedef typename std::list<TYPE*>::const_reference const_reference;
50
51 typedef typename std::list<TYPE*>::iterator iterator;
52 typedef typename std::list<TYPE*>::const_iterator const_iterator;
53
54 typedef typename std::list<TYPE*>::reverse_iterator reverse_iterator;
55 typedef typename std::list<TYPE*>::const_reverse_iterator const_reverse_iterator;
56
57 typedef typename std::vector<TYPE*>::pointer pointer;
58 typedef typename std::vector<TYPE*>::const_pointer const_pointer;
59
60public:
61 ObjectList() = default;
62 ObjectList( const ObjectList<TYPE>& ) = delete;
64
65 ~ObjectList() override { clear(); }
66
68 const CLID& clID() const override { return ObjectList<TYPE>::classID(); }
69 static const CLID& classID() {
70 static CLID clid = TYPE::classID() + CLID_ObjectList;
71 return clid;
72 }
73
75 typename ObjectList<TYPE>::iterator begin() { return m_list.begin(); }
76
78 typename ObjectList<TYPE>::const_iterator begin() const { return m_list.begin(); }
79
81 typename ObjectList<TYPE>::iterator end() { return m_list.end(); }
82
84 typename ObjectList<TYPE>::const_iterator end() const { return m_list.end(); }
85
88 typename ObjectList<TYPE>::reverse_iterator rbegin() { return m_list.rbegin(); }
89
91 typename ObjectList<TYPE>::const_reverse_iterator rbegin() const { return m_list.rbegin(); }
92
94 typename ObjectList<TYPE>::reverse_iterator rend() { return m_list.rend(); }
95
97 typename ObjectList<TYPE>::const_reverse_iterator rend() const { return m_list.rend(); }
98
103 // C++11: std::list::size is constant (pre C++11 it could be linear!)
104 return m_list.size();
105 }
106
107 typename ObjectList<TYPE>::size_type numberOfObjects() const override { return m_list.size(); }
108
110 typename ObjectList<TYPE>::size_type max_size() const { return m_list.max_size(); }
111
113 bool empty() const { return m_list.empty(); }
114
116 typename ObjectList<TYPE>::reference front() { return m_list.front(); }
117
119 typename ObjectList<TYPE>::const_reference front() const { return m_list.front(); }
120
122 typename ObjectList<TYPE>::reference back() { return m_list.back(); }
123
125 typename ObjectList<TYPE>::const_reference back() const { return m_list.back(); }
126
129 if ( value->parent() ) { const_cast<ObjectContainerBase*>( value->parent() )->remove( value ); }
130 value->setParent( this );
131 m_list.push_back( value );
132 }
133
135 long add( ContainedObject* pObject ) override {
136 try {
137 auto ptr = dynamic_cast<typename ObjectList<TYPE>::value_type>( pObject );
138 if ( ptr ) {
139 push_back( ptr );
140 return m_list.size() - 1;
141 }
142 } catch ( ... ) {}
143 return -1;
144 }
145
148 void pop_back() {
149 auto position = m_list.back();
150 // Set the back pointer to 0 to avoid repetitional searching
151 // for the object in the container, and deleting the object
152 position->setParent( nullptr );
153 delete position;
154 // Removing from the container itself
155 m_list.pop_back();
156 }
157
160 long remove( ContainedObject* value ) override {
161 // Find the object of value value
162 long idx = 0;
163 auto iter = std::find_if( begin(), end(), [&]( const ContainedObject* i ) { return i == value; } );
164 if ( iter == end() ) {
165 // Object cannot be released from the container,
166 // as it is not contained in it
167 return -1;
168 }
169
170 // Set the back pointer to 0 to avoid repetitional searching
171 // for the object in the container and deleting the object
172 ( *iter )->setParent( nullptr );
173 erase( iter );
174 return idx;
175 }
176
179 typename ObjectList<TYPE>::const_reference value ) {
180 value->setParent( this );
181 return m_list.insert( position, value );
182 }
183
185 void erase( typename ObjectList<TYPE>::iterator position ) {
186 if ( ( *position )->parent() ) {
187 // Set the back pointer to 0 to avoid repetitional searching
188 // for the object in the container, and deleting the object
189 ( *position )->setParent( nullptr );
190 delete *position;
191 }
192 // Removing from the container itself
193 m_list.erase( position );
194 }
195
197 void erase( typename ObjectList<TYPE>::iterator first, typename ObjectList<TYPE>::iterator last ) {
198 for ( auto iter = first; iter != last; ++iter ) {
199 // Set the back pointer to 0 to avoid repetitional searching
200 // for the object in the container, and deleting the object
201 ( *iter )->setParent( nullptr );
202 delete *iter;
203 }
204 // Removing from the container itself
205 m_list.erase( first, last );
206 }
207
209 void clear() { erase( begin(), end() ); }
210
213 long index( const ContainedObject* obj ) const override {
214 auto i = std::find_if( begin(), end(), [&]( const ContainedObject* o ) { return o == obj; } );
215 return i != end() ? std::distance( begin(), i ) : -1;
216 }
217
219 const ContainedObject* containedObject( long dist ) const override {
220 return dist < size() ? *std::next( begin(), dist ) : nullptr;
221 }
222
223 ContainedObject* containedObject( long dist ) override {
224 return dist < size() ? *std::next( begin(), dist ) : nullptr;
225 }
226
228 std::ostream& fillStream( std::ostream& s ) const override {
229 s << "class ObjectList : size = " << std::setw( 12 ) << size() << "\n";
230 // Output the base class
231 // ObjectContainerBase::fillStream(s);
232 if ( !empty() ) {
233 s << "\nContents of the STL list :";
234 long count = 0;
235 for ( const auto& iter : m_list ) {
236 s << "\nIndex " << std::setw( 12 ) << count++ << " of object of type " << *iter;
237 }
238 }
239 return s;
240 }
241
242private:
243 std::list<TYPE*> m_list;
244};
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.
size_t size_type
size_type, to conform the STL container interface
ObjectList< TYPE >::const_iterator end() const
Return a const_iterator pointing to the end of the container.
Definition ObjectList.h:84
ObjectList< TYPE >::size_type size() const
Return the size of the container Size means the number of objects stored in the container,...
Definition ObjectList.h:102
ObjectList(const ObjectList< TYPE > &)=delete
const ContainedObject * containedObject(long dist) const override
Return const pointer to an object of a given distance.
Definition ObjectList.h:219
std::list< ContainedObject * >::reverse_iterator reverse_iterator
Definition ObjectList.h:54
ObjectList< TYPE >::reverse_iterator rend()
Return a reverse_iterator pointing to the end of the reversed container.
Definition ObjectList.h:94
ObjectList< TYPE >::reference front()
Return reference to the first element.
Definition ObjectList.h:116
void erase(typename ObjectList< TYPE >::iterator position)
Erase the object at "position" from the container. The removed object will be deleted.
Definition ObjectList.h:185
std::list< ContainedObject * >::reference reference
Definition ObjectList.h:48
const CLID & clID() const override
Retrieve pointer to class definition structure.
Definition ObjectList.h:68
~ObjectList() override
Definition ObjectList.h:65
ObjectList & operator=(const ObjectList< TYPE > &)=delete
ContainedObject * containedObject(long dist) override
Return const pointer to an object of a given distance.
Definition ObjectList.h:223
std::list< ContainedObject * > m_list
Definition ObjectList.h:243
std::vector< ContainedObject * >::const_pointer const_pointer
Definition ObjectList.h:58
ObjectList< TYPE >::const_reverse_iterator rend() const
Return a const_reverse_iterator pointing to the end of the reversed container.
Definition ObjectList.h:97
bool empty() const
Return true if the size of the container is 0.
Definition ObjectList.h:113
std::ostream & fillStream(std::ostream &s) const override
Fill the output stream (ASCII)
Definition ObjectList.h:228
ObjectList< TYPE >::iterator insert(typename ObjectList< TYPE >::iterator position, typename ObjectList< TYPE >::const_reference value)
Insert "value" before "position".
Definition ObjectList.h:178
std::vector< ContainedObject * >::pointer pointer
Definition ObjectList.h:57
ObjectList< TYPE >::reverse_iterator rbegin()
Return a reverse_iterator pointing to the beginning of the reversed container.
Definition ObjectList.h:88
void push_back(typename ObjectList< TYPE >::const_reference value)
push_back = append = insert a new element at the end of the container
Definition ObjectList.h:128
ContainedObject contained_type
Definition ObjectList.h:45
ObjectList< TYPE >::iterator begin()
Return an iterator pointing to the beginning of the container.
Definition ObjectList.h:75
void clear()
Clear the entire content of the container and delete all contained objects.
Definition ObjectList.h:209
ObjectList< TYPE >::size_type numberOfObjects() const override
The same as size(), return number of objects in the container.
Definition ObjectList.h:107
std::list< ContainedObject * >::iterator iterator
Definition ObjectList.h:51
void erase(typename ObjectList< TYPE >::iterator first, typename ObjectList< TYPE >::iterator last)
Erase the range [first, last) from the container. The removed object will be deleted.
Definition ObjectList.h:197
std::list< ContainedObject * >::const_iterator const_iterator
Definition ObjectList.h:52
ObjectList< TYPE >::reference back()
Return reference to the last element.
Definition ObjectList.h:122
ObjectList< TYPE >::const_reference front() const
Return const_reference to the first element.
Definition ObjectList.h:119
long index(const ContainedObject *obj) const override
Return distance of a given object from the beginning of its container It corresponds to the "index" (...
Definition ObjectList.h:213
ObjectList< TYPE >::size_type max_size() const
Return the largest possible size of the container.
Definition ObjectList.h:110
ObjectList< TYPE >::iterator end()
Return an iterator pointing to the end of the container.
Definition ObjectList.h:81
std::list< ContainedObject * >::value_type value_type
Definition ObjectList.h:46
ObjectList< TYPE >::const_iterator begin() const
Return a const_iterator pointing to the beginning of the container.
Definition ObjectList.h:78
std::list< ContainedObject * >::const_reverse_iterator const_reverse_iterator
Definition ObjectList.h:55
long remove(ContainedObject *value) override
Release object from the container (the pointer will be removed from the container,...
Definition ObjectList.h:160
ObjectList< TYPE >::const_reverse_iterator rbegin() const
Return a const_reverse_iterator pointing to the beginning of the reversed container.
Definition ObjectList.h:91
void pop_back()
pop_back = remove the last element from the container The removed object will be deleted (see the met...
Definition ObjectList.h:148
long add(ContainedObject *pObject) override
Add an object to the container.
Definition ObjectList.h:135
std::list< ContainedObject * >::const_reference const_reference
Definition ObjectList.h:49
ObjectList< TYPE >::const_reference back() const
Return const_reference to the last element.
Definition ObjectList.h:125
static const CLID & classID()
Definition ObjectList.h:69
ObjectList()=default