All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SharedObjectsContainer.h
Go to the documentation of this file.
1 // $Id: SharedObjectsContainer.h,v 1.2 2008/10/10 10:26:14 marcocle Exp $
2 // ============================================================================
3 #ifndef GAUDIKERNEL_SHAREDOBJECTSCONTAINER_H
4 #define GAUDIKERNEL_SHAREDOBJECTSCONTAINER_H 1
5 // ============================================================================
6 // Include files
7 // ============================================================================
8 // STD & STL
9 // ============================================================================
10 #include <algorithm>
11 // ============================================================================
12 // GaudiKernel
13 // ============================================================================
14 #include "GaudiKernel/Kernel.h"
15 #include "GaudiKernel/ClassID.h"
17 // ============================================================================
29 template <class TYPE>
31 {
32 public:
33  // ==========================================================================
35  typedef std::vector<const TYPE*> ConstVector ;
37  typedef typename ConstVector::value_type value_type ;
38  typedef typename ConstVector::size_type size_type ;
39  typedef typename ConstVector::reference reference ;
40  typedef typename ConstVector::const_reference const_reference ;
41  typedef typename ConstVector::iterator iterator ;
42  typedef typename ConstVector::const_iterator const_iterator ;
43  typedef typename ConstVector::reverse_iterator reverse_iterator ;
44  typedef typename ConstVector::const_reverse_iterator const_reverse_iterator ;
45  // ==========================================================================
46 public:
47  // ==========================================================================
48  // the default constructor (creates the empty vector)
50  : ObjectContainerBase(), m_data() {} ;
51  // the constructor from the data
53  : ObjectContainerBase(), m_data(data) {}
58  template <class DATA>
59  SharedObjectsContainer(DATA first, DATA last)
60  : ObjectContainerBase(), m_data(first, last) {}
84  template <class DATA, class PREDICATE>
85  SharedObjectsContainer(DATA first, DATA last, const PREDICATE& cut)
87  {
88  insert ( first , last , cut ) ;
89  }
91  virtual ~SharedObjectsContainer() { m_data.clear() ; }
92  // ==========================================================================
93 public:
94  // ==========================================================================
96  virtual const CLID& clID() const
99  static const CLID& classID()
100  {
101  static const CLID s_clid =
102  ( ( static_cast<CLID> ( -1 ) << 16 ) // 16 used and 16 empty bits
103  & !CLID_ObjectVector // not an ObjectVector
104  & !CLID_ObjectList // not an ObjectList
105  & !static_cast<CLID> ( 0x00030000 ) // not a KeyedContainer/map
106  & !static_cast<CLID> ( 0x00040000 ) )// not a KeyedContainer/hashmap
107  + TYPE::classID() ; // the specific CLID from the contents
108  //
109  return s_clid;
110  }
111  // ==========================================================================
112 public:
113  // ==========================================================================
115  inline const ConstVector& data () const { return m_data ; }
117  operator const ConstVector& () const { return data () ; }
118  // ==========================================================================
119 public:
120  // ==========================================================================
122  size_type size () const { return m_data.size () ; }
124  bool empty () const { return m_data.empty () ; }
128  void push_back ( const TYPE* object ) { m_data.push_back ( object ) ; }
132  void insert ( const TYPE* object ) { m_data.push_back ( object ) ; }
137  template <class DATA>
138  void insert
139  ( DATA first ,
140  DATA last ) { m_data.insert ( end() , first ,last ) ; }
167  template <class DATA, class PREDICATE>
168  void insert
169  ( DATA first ,
170  DATA last ,
171  const PREDICATE& cut )
172  {
173  m_data.reserve ( m_data.size() + std::distance ( first , last ) ) ;
174  for ( ; first != last ; ++first )
175  { if ( cut ( *first ) ) { m_data.push_back ( *first ) ; } }
176  }
204  template <class OUTPUT, class PREDICATE>
205  OUTPUT get ( const PREDICATE& cut ,
206  OUTPUT output ) const
207  {
208  for ( const_iterator iobj = begin() ; end() != iobj ; ++iobj )
209  { if ( cut ( *iobj ) ) { *output = *iobj ; ++output ; } }
210  return output ;
211  }
213  void erase ( iterator i ) { m_data.erase ( i ) ; }
231  template <class PREDICATE>
232  void erase ( const PREDICATE& cut )
233  { m_data.erase( std::remove_if ( begin() , end() , cut ) , end() ) ; }
252  bool erase ( const TYPE* object )
253  {
254  iterator i = std::find ( begin() , end() , object ) ;
255  if ( end() == i ) { return false ; }
256  m_data.erase ( i ) ;
257  return true ;
258  }
259  // ==========================================================================
260 public:
261  // ==========================================================================
271  reference at ( size_type index ) { return m_data.at(index) ; }
273  const_reference at ( size_type index ) const { return m_data.at(index) ; }
274  // ==========================================================================
275 public:
276  // ==========================================================================
277  iterator begin () { return m_data . begin () ; }
278  iterator end () { return m_data . end () ; }
279  const_iterator begin () const { return m_data . begin () ; }
280  const_iterator end () const { return m_data . end () ; }
281  reverse_iterator rbegin () { return m_data . rbegin () ; }
282  reverse_iterator rend () { return m_data . rend () ; }
283  const_reverse_iterator rbegin () const { return m_data . rbegin () ; }
284  const_reverse_iterator rend () const { return m_data . rend () ; }
285  // ==========================================================================
286 public:
287  // ==========================================================================
289  reference front () { return m_data . front () ; }
291  const_reference front () const { return m_data . front () ; }
293  reference back () { return m_data . back () ; }
295  const_reference back () const { return m_data . back () ; }
296  // ==========================================================================
297 public:
298  // ==========================================================================
300  bool operator== ( const SharedObjectsContainer& right ) const
301  { return &right == this || right.m_data == m_data ; }
303  bool operator== ( const ConstVector& right ) const
304  { return m_data == right ; }
306  bool operator < ( const SharedObjectsContainer& right ) const
307  { return m_data < right.m_data ; }
309  bool operator < ( const ConstVector& right ) const
310  { return m_data < right ; }
311  // ==========================================================================
312 public: // ObjectContainerBase methods:
313  // ==========================================================================
317  virtual long index( const ContainedObject* object ) const
318  {
319  const_iterator _i = std::find ( begin() , end() , object ) ;
320  return end() != _i ? ( _i - begin() ) : -1 ; // RETURN
321  }
326  virtual ContainedObject* containedObject ( long index ) const
327  {
328  if ( 0 > index || !(index < (long) size () ) ) { return 0 ; } // RETURN
329  const ContainedObject* co = m_data[index] ;
330  return const_cast<ContainedObject*>( co ) ;
331  }
333  virtual size_type numberOfObjects() const { return m_data.size() ; }
338  virtual long add ( ContainedObject* object)
339  {
340  if ( 0 == object ) { return -1 ; } // RETURN
341  TYPE* _obj = dynamic_cast<TYPE*> ( object ) ;
342  if ( 0 == _obj ) { return -1 ; } // RETURN
343  const size_type pos = size() ;
344  push_back ( _obj ) ;
345  return pos ;
346  }
351  virtual long remove ( ContainedObject* value )
352  {
353  iterator _i = std::find ( begin() , end() , value ) ;
354  if ( end() == _i ) { return -1 ; } // RETURN
355  const size_type pos = _i - begin() ;
356  m_data.erase ( _i ) ;
357  return pos ; // RETURN
358  }
359  // ==========================================================================
360 private:
361  // ==========================================================================
362  // the actual data
363  ConstVector m_data ; // the actual data
364  // ==========================================================================
365 };
366 // ============================================================================
367 // The END
368 // ============================================================================
369 #endif // GAUDIKERNEL_SHAREDOBJECTSCONTAINER_H
370 // ============================================================================
bool erase(const TYPE *object)
erase the first occurance of the certain element
virtual const CLID & clID() const
Retrieve the unique class ID (virtual)
reference front()
the first element (only for non-empty vectors)
Very simple class to represent the container of objects which are not ownered by the container...
const_reference at(size_type index) const
checked access (const-version)
virtual ContainedObject * containedObject(long index) const
Pointer to an object of a given distance.
reference operator()(size_type index)
'functional'-access
const_iterator begin() const
void erase(const PREDICATE &cut)
erase the objects which satisfy the criteria
size_t size_type
size_type, to conform the STL container interface
bool operator==(const SharedObjectsContainer &right) const
equal content with other container ?
void push_back(const TYPE *object)
insert one object into the container
bool operator<(const SharedObjectsContainer &right) const
comparisons with other container
ConstVector::size_type size_type
ConstVector::reverse_iterator reverse_iterator
size_type size() const
get the actual size of the container
SharedObjectsContainer(DATA first, DATA last, const PREDICATE &cut)
the templated constructor from the pair of iterators and the predicate.
virtual long add(ContainedObject *object)
Virtual functions (forwards to the concrete container definitions) Add an object to the container...
ConstVector::iterator iterator
ConstVector::const_reference const_reference
void insert(const TYPE *object)
insert one object into the container
virtual long index(const ContainedObject *object) const
Distance of a given object from the beginning of its container.
const_reference front() const
the first element (only for non-empty vectors) (const-version)
const_iterator end() const
const_reverse_iterator rbegin() const
ConstVector::const_iterator const_iterator
SharedObjectsContainer(const ConstVector &data)
const_reference back() const
the last element (only for non-empty vectors) (const-version)
const ConstVector & data() const
get the access to the underlying container (const)
virtual ~SharedObjectsContainer()
destructor
reference at(size_type index)
checked access
unsigned int CLID
Class ID definition.
Definition: ClassID.h:9
virtual size_type numberOfObjects() const
Number of objects in the container.
bool empty() const
empty container?
reference operator[](size_type index)
index access
std::vector< const TYPE * > ConstVector
the actual container type
All classes that their objects may be contained in an LHCb ObjectContainer (e.g.
const_reverse_iterator rend() const
static const CLID & classID()
Retrieve the unuqie class ID (static)
ConstVector::reference reference
reference back()
the last element (only for non-empty vectors)
ObjectContainerBase is the base class for Gaudi container classes.
list i
Definition: ana.py:128
SharedObjectsContainer(DATA first, DATA last)
the templated constructor from the pair of iterators
TO * reference(FROM *from)
Definition: KeyedObject.cpp:21
void erase(iterator i)
erase the object by iterator
ConstVector::const_reverse_iterator const_reverse_iterator
ConstVector::value_type value_type
various types (to make STL happy)