| The Gaudi Framework v32r1 (f65d50dc)

ObjectList.h
Go to the documentation of this file.
1 #ifndef GAUDIKERNEL_OBJECTLIST_H
2 #define GAUDIKERNEL_OBJECTLIST_H
3
4 // Include files
5 #include "GaudiKernel/ClassID.h"
6 #include "GaudiKernel/Kernel.h"
7 #include "GaudiKernel/ObjectContainerBase.h"
8 #include "GaudiKernel/StreamBuffer.h"
9
10 #include <iomanip>
11 #include <list>
12
13 // Definition of the CLID for this class defined in ClassID.h
14 // static const CLID CLID_ObjectList = (1<<18); // ObjectList (bit 18 set)
15
34 template <class TYPE>
35 class ObjectList : public ObjectContainerBase {
36
37 public:
38 typedef TYPE contained_type;
39 typedef typename std::list<TYPE*>::value_type value_type;
40
41 typedef typename std::list<TYPE*>::reference reference;
42 typedef typename std::list<TYPE*>::const_reference const_reference;
43
44 typedef typename std::list<TYPE*>::iterator iterator;
45 typedef typename std::list<TYPE*>::const_iterator const_iterator;
46
47 typedef typename std::list<TYPE*>::reverse_iterator reverse_iterator;
48 typedef typename std::list<TYPE*>::const_reverse_iterator const_reverse_iterator;
49
50 #ifdef _WIN32
51 typedef typename std::vector<TYPE*>::_Tptr pointer;
52 typedef typename std::vector<TYPE*>::_Ctptr const_pointer;
53 #else
54 typedef typename std::vector<TYPE*>::pointer pointer;
55 typedef typename std::vector<TYPE*>::const_pointer const_pointer;
56 #endif
57
58 public:
60 ObjectList() = default;
61
62 ObjectList( const ObjectList<TYPE>& ) = delete;
63 ObjectList& operator=( const ObjectList<TYPE>& ) = delete;
64
66 ~ObjectList() override { clear(); }
67
69 const CLID& clID() const override { return ObjectList<TYPE>::classID(); }
70 static const CLID& classID() {
71 static CLID clid = TYPE::classID() + CLID_ObjectList;
72 return clid;
73 }
74
76 typename ObjectList<TYPE>::iterator begin() { return m_list.begin(); }
77
79 typename ObjectList<TYPE>::const_iterator begin() const { return m_list.begin(); }
80
82 typename ObjectList<TYPE>::iterator end() { return m_list.end(); }
83
85 typename ObjectList<TYPE>::const_iterator end() const { return m_list.end(); }
86
89 typename ObjectList<TYPE>::reverse_iterator rbegin() { return m_list.rbegin(); }
90
92 typename ObjectList<TYPE>::const_reverse_iterator rbegin() const { return m_list.rbegin(); }
93
95 typename ObjectList<TYPE>::reverse_iterator rend() { return m_list.rend(); }
96
98 typename ObjectList<TYPE>::const_reverse_iterator rend() const { return m_list.rend(); }
99
103 typename ObjectList<TYPE>::size_type size() const {
104 // C++11: std::list::size is constant (pre C++11 it could be linear!)
105 return m_list.size();
106 }
108 typename ObjectList<TYPE>::size_type numberOfObjects() const override { return m_list.size(); }
109
111 typename ObjectList<TYPE>::size_type max_size() const { return m_list.max_size(); }
112
114 bool empty() const { return m_list.empty(); }
115
117 typename ObjectList<TYPE>::reference front() { return m_list.front(); }
118
120 typename ObjectList<TYPE>::const_reference front() const { return m_list.front(); }
121
123 typename ObjectList<TYPE>::reference back() { return m_list.back(); }
124
126 typename ObjectList<TYPE>::const_reference back() const { return m_list.back(); }
127
129 void push_back( typename ObjectList<TYPE>::const_reference value ) {
130 if ( value->parent() ) { const_cast<ObjectContainerBase*>( value->parent() )->remove( value ); }
131 value->setParent( this );
132 m_list.push_back( value );
133 }
134
136 long add( ContainedObject* pObject ) override {
137 try {
138 auto ptr = dynamic_cast<typename ObjectList<TYPE>::value_type>( pObject );
139 if ( ptr ) {
140 push_back( ptr );
141 return m_list.size() - 1;
142 }
143 } catch ( ... ) {}
144 return -1;
145 }
146
149 void pop_back() {
150 auto position = m_list.back();
151 // Set the back pointer to 0 to avoid repetitional searching
152 // for the object in the container, and deleting the object
153 position->setParent( nullptr );
154 delete position;
155 // Removing from the container itself
156 m_list.pop_back();
157 }
158
161 long remove( ContainedObject* value ) override {
162 // Find the object of value value
163 long idx = 0;
164 auto iter = std::find_if( begin(), end(), [&]( const ContainedObject* i ) { return i == value; } );
165 if ( iter == end() ) {
166 // Object cannot be released from the container,
167 // as it is not contained in it
168 return -1;
169 }
170
171 // Set the back pointer to 0 to avoid repetitional searching
172 // for the object in the container and deleting the object
173 ( *iter )->setParent( nullptr );
174 erase( iter );
175 return idx;
176 }
177
179 typename ObjectList<TYPE>::iterator insert( typename ObjectList<TYPE>::iterator position,
180 typename ObjectList<TYPE>::const_reference value ) {
181 value->setParent( this );
182 return m_list.insert( position, value );
183 }
184
186 void erase( typename ObjectList<TYPE>::iterator position ) {
187 if ( ( *position )->parent() ) {
188 // Set the back pointer to 0 to avoid repetitional searching
189 // for the object in the container, and deleting the object
190 ( *position )->setParent( nullptr );
191 delete *position;
192 }
193 // Removing from the container itself
194 m_list.erase( position );
195 }
196
198 void erase( typename ObjectList<TYPE>::iterator first, typename ObjectList<TYPE>::iterator last ) {
199 for ( auto iter = first; iter != last; ++iter ) {
200 // Set the back pointer to 0 to avoid repetitional searching
201 // for the object in the container, and deleting the object
202 ( *iter )->setParent( nullptr );
203 delete *iter;
204 }
205 // Removing from the container itself
206 m_list.erase( first, last );
207 }
208
210 void clear() { erase( begin(), end() ); }
211
214 long index( const ContainedObject* obj ) const override {
215 auto i = std::find_if( begin(), end(), [&]( const ContainedObject* o ) { return o == obj; } );
216 return i != end() ? std::distance( begin(), i ) : -1;
217 }
218
220 const ContainedObject* containedObject( long dist ) const override {
221 return dist < size() ? *std::next( begin(), dist ) : nullptr;
222 }
224 ContainedObject* containedObject( long dist ) override {
225 return dist < size() ? *std::next( begin(), dist ) : nullptr;
226 }
227
229 std::ostream& fillStream( std::ostream& s ) const override {
230 s << "class ObjectList : size = " << std::setw( 12 ) << size() << "\n";
231 // Output the base class
232 // ObjectContainerBase::fillStream(s);
233 if ( !empty() ) {
234 s << "\nContents of the STL list :";
235 long count = 0;
236 for ( const auto& iter : m_list ) {
237 s << "\nIndex " << std::setw( 12 ) << count++ << " of object of type " << *iter;
238 }
239 }
240 return s;
241 }
242
243 private:
245 std::list<TYPE*> m_list;
246 };
247
248 #endif // GAUDI_OBJECTLIST_H
ObjectList::pointer
std::vector< TYPE * >::pointer pointer
Definition: ObjectList.h:54
ClassID.h
Kernel.h
std::list::empty
T empty(T... args)
ObjectList::rbegin
ObjectList< TYPE >::const_reverse_iterator rbegin() const
Return a const_reverse_iterator pointing to the beginning of the reversed container.
Definition: ObjectList.h:92
std::distance
T distance(T... args)
ObjectList::front
ObjectList< TYPE >::const_reference front() const
Return const_reference to the first element.
Definition: ObjectList.h:120
ObjectList::fillStream
std::ostream & fillStream(std::ostream &s) const override
Fill the output stream (ASCII)
Definition: ObjectList.h:229
ObjectList::contained_type
TYPE contained_type
Definition: ObjectList.h:38
ObjectContainerBase::size_type
size_t size_type
size_type, to conform the STL container interface
Definition: ObjectContainerBase.h:29
ObjectList::pop_back
void pop_back()
pop_back = remove the last element from the container The removed object will be deleted (see the met...
Definition: ObjectList.h:149
ObjectList::insert
ObjectList< TYPE >::iterator insert(typename ObjectList< TYPE >::iterator position, typename ObjectList< TYPE >::const_reference value)
Insert "value" before "position".
Definition: ObjectList.h:179
std::list::rend
T rend(T... args)
std::list::front
T front(T... args)
ObjectList::operator=
ObjectList & operator=(const ObjectList< TYPE > &)=delete
ObjectList::numberOfObjects
ObjectList< TYPE >::size_type numberOfObjects() const override
The same as size(), return number of objects in the container.
Definition: ObjectList.h:108
ObjectContainerBase.h
ObjectList::empty
bool empty() const
Return true if the size of the container is 0.
Definition: ObjectList.h:114
std::list::end
T end(T... args)
ObjectList::const_reference
std::list< TYPE * >::const_reference const_reference
Definition: ObjectList.h:42
ObjectList::clID
const CLID & clID() const override
Retrieve pointer to class definition structure.
Definition: ObjectList.h:69
ObjectList::size
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:103
StreamBuffer.h
ObjectList::const_iterator
std::list< TYPE * >::const_iterator const_iterator
Definition: ObjectList.h:45
std::setw
T setw(T... args)
ObjectList
ObjectList is one of the basic Gaudi container classes capable of being registered in Data Stores.
Definition: ContainedObject.h:14
ObjectList::back
ObjectList< TYPE >::const_reference back() const
Return const_reference to the last element.
Definition: ObjectList.h:126
ObjectList::reverse_iterator
std::list< TYPE * >::reverse_iterator reverse_iterator
Definition: ObjectList.h:47
ObjectList::end
ObjectList< TYPE >::const_iterator end() const
Return a const_iterator pointing to the end of the container.
Definition: ObjectList.h:85
ObjectList::index
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:214
std::list::push_back
T push_back(T... args)
ObjectList::reference
std::list< TYPE * >::reference reference
Definition: ObjectList.h:41
ObjectList::containedObject
ContainedObject * containedObject(long dist) override
Return const pointer to an object of a given distance.
Definition: ObjectList.h:224
ObjectList::~ObjectList
~ObjectList() override
Destructor.
Definition: ObjectList.h:66
ObjectList::value_type
std::list< TYPE * >::value_type value_type
Definition: ObjectList.h:39
std::next
T next(T... args)
ObjectList::rbegin
ObjectList< TYPE >::reverse_iterator rbegin()
Return a reverse_iterator pointing to the beginning of the reversed container.
Definition: ObjectList.h:89
std::list::erase
T erase(T... args)
ObjectList::begin
ObjectList< TYPE >::iterator begin()
Return an iterator pointing to the beginning of the container.
Definition: ObjectList.h:76
std::list::pop_back
T pop_back(T... args)
ObjectList::rend
ObjectList< TYPE >::reverse_iterator rend()
Return a reverse_iterator pointing to the end of the reversed container.
Definition: ObjectList.h:95
CLID
unsigned int CLID
Class ID definition.
Definition: ClassID.h:8
std::list
STL class.
ObjectList::back
ObjectList< TYPE >::reference back()
Return reference to the last element.
Definition: ObjectList.h:123
ObjectList::iterator
std::list< TYPE * >::iterator iterator
Definition: ObjectList.h:44
ObjectList::m_list
std::list< TYPE * > m_list
The STL list.
Definition: ObjectList.h:245
std::list::insert
T insert(T... args)
ObjectList::classID
static const CLID & classID()
Definition: ObjectList.h:70
ContainedObject
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
Definition: ContainedObject.h:31
std::find_if
T find_if(T... args)
ObjectList::ObjectList
ObjectList()=default
Constructors.
std::list::size
T size(T... args)
std::vector
STL class.
ObjectList::front
ObjectList< TYPE >::reference front()
Return reference to the first element.
Definition: ObjectList.h:117
ObjectList::const_reverse_iterator
std::list< TYPE * >::const_reverse_iterator const_reverse_iterator
Definition: ObjectList.h:48
std::list::begin
T begin(T... args)
ObjectList::remove
long remove(ContainedObject *value) override
Release object from the container (the pointer will be removed from the container,...
Definition: ObjectList.h:161
ObjectList::rend
ObjectList< TYPE >::const_reverse_iterator rend() const
Return a const_reverse_iterator pointing to the end of the reversed container.
Definition: ObjectList.h:98
ObjectList::erase
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:198
ObjectList::containedObject
const ContainedObject * containedObject(long dist) const override
Return const pointer to an object of a given distance.
Definition: ObjectList.h:220
std::list::max_size
T max_size(T... args)
ObjectList::max_size
ObjectList< TYPE >::size_type max_size() const
Return the largest possible size of the container.
Definition: ObjectList.h:111
std::list::back
T back(T... args)
gaudirun.s
string s
Definition: gaudirun.py:316
ObjectList::clear
void clear()
Clear the entire content of the container and delete all contained objects.
Definition: ObjectList.h:210
ObjectList::push_back
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:129
ObjectList::add
long add(ContainedObject *pObject) override
Add an object to the container.
Definition: ObjectList.h:136
ObjectContainerBase
ObjectContainerBase is the base class for Gaudi container classes.
Definition: ObjectContainerBase.h:19
ObjectList::const_pointer
std::vector< TYPE * >::const_pointer const_pointer
Definition: ObjectList.h:55
ObjectList::erase
void erase(typename ObjectList< TYPE >::iterator position)
Erase the object at "position" from the container. The removed object will be deleted.
Definition: ObjectList.h:186
std::ostream
STL class.
ObjectList::begin
ObjectList< TYPE >::const_iterator begin() const
Return a const_iterator pointing to the beginning of the container.
Definition: ObjectList.h:79
ObjectList::end
ObjectList< TYPE >::iterator end()
Return an iterator pointing to the end of the container.
Definition: ObjectList.h:82
std::list::rbegin
T rbegin(T... args)