Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-21 10:00:34

0001 /***********************************************************************************\
0002 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
0003 *                                                                                   *
0004 * This software is distributed under the terms of the Apache version 2 licence,     *
0005 * copied verbatim in the file "LICENSE".                                            *
0006 *                                                                                   *
0007 * In applying this licence, CERN does not waive the privileges and immunities       *
0008 * granted to it by virtue of its status as an Intergovernmental Organization        *
0009 * or submit itself to any jurisdiction.                                             *
0010 \***********************************************************************************/
0011 #ifndef GAUDIKERNEL_OBJECTLIST_H
0012 #define GAUDIKERNEL_OBJECTLIST_H
0013 
0014 // Include files
0015 #include "GaudiKernel/ClassID.h"
0016 #include "GaudiKernel/Kernel.h"
0017 #include "GaudiKernel/ObjectContainerBase.h"
0018 #include "GaudiKernel/StreamBuffer.h"
0019 
0020 #include <iomanip>
0021 #include <list>
0022 
0023 // Definition of the CLID for this class  defined in ClassID.h
0024 // static const CLID CLID_ObjectList = (1<<18);  // ObjectList   (bit 18 set)
0025 
0026 /** @class ObjectList ObjectList.h GaudiKernel/ObjectList.h
0027 
0028     ObjectList is one of the basic Gaudi container classes capable of being
0029     registered in Data Stores.
0030     It is based on Standard Library (STL) std::list
0031     (see <A HREF="http://www.sgi.com/Technology/STL/">STL Programmer's Guide</A>)
0032     ObjectList has all functions of the std::list interface,
0033 
0034     Each object is allowed to belong into a single container only.
0035     After inserting the object into the container, it takes over
0036     all responsibilities for the object.  E.g. erasing the object
0037     from its container causes removing the object's pointer from
0038     the container and deleting the object itself.
0039 
0040     @author  Pavel Binko
0041     @author  Pere Mato
0042     @date    19/10/1999, 30/11/2000
0043 */
0044 template <class TYPE>
0045 class ObjectList : public ObjectContainerBase {
0046 
0047 public:
0048   typedef TYPE                                  contained_type;
0049   typedef typename std::list<TYPE*>::value_type value_type;
0050 
0051   typedef typename std::list<TYPE*>::reference       reference;
0052   typedef typename std::list<TYPE*>::const_reference const_reference;
0053 
0054   typedef typename std::list<TYPE*>::iterator       iterator;
0055   typedef typename std::list<TYPE*>::const_iterator const_iterator;
0056 
0057   typedef typename std::list<TYPE*>::reverse_iterator       reverse_iterator;
0058   typedef typename std::list<TYPE*>::const_reverse_iterator const_reverse_iterator;
0059 
0060 #ifdef _WIN32
0061   typedef typename std::vector<TYPE*>::_Tptr  pointer;
0062   typedef typename std::vector<TYPE*>::_Ctptr const_pointer;
0063 #else
0064   typedef typename std::vector<TYPE*>::pointer       pointer;
0065   typedef typename std::vector<TYPE*>::const_pointer const_pointer;
0066 #endif
0067 
0068 public:
0069   /// Constructors
0070   ObjectList() = default;
0071 
0072   ObjectList( const ObjectList<TYPE>& )            = delete;
0073   ObjectList& operator=( const ObjectList<TYPE>& ) = delete;
0074 
0075   /// Destructor
0076   ~ObjectList() override { clear(); }
0077 
0078   /// Retrieve pointer to class definition structure
0079   const CLID&        clID() const override { return ObjectList<TYPE>::classID(); }
0080   static const CLID& classID() {
0081     static CLID clid = TYPE::classID() + CLID_ObjectList;
0082     return clid;
0083   }
0084 
0085   /// Return an iterator pointing to the beginning of the container
0086   typename ObjectList<TYPE>::iterator begin() { return m_list.begin(); }
0087 
0088   /// Return a const_iterator pointing to the beginning of the container
0089   typename ObjectList<TYPE>::const_iterator begin() const { return m_list.begin(); }
0090 
0091   /// Return an iterator pointing to the end of the container
0092   typename ObjectList<TYPE>::iterator end() { return m_list.end(); }
0093 
0094   /// Return a const_iterator pointing to the end of the container
0095   typename ObjectList<TYPE>::const_iterator end() const { return m_list.end(); }
0096 
0097   /// Return a reverse_iterator pointing to the beginning
0098   ///   of the reversed container
0099   typename ObjectList<TYPE>::reverse_iterator rbegin() { return m_list.rbegin(); }
0100 
0101   /// Return a const_reverse_iterator pointing to the beginning of the reversed container
0102   typename ObjectList<TYPE>::const_reverse_iterator rbegin() const { return m_list.rbegin(); }
0103 
0104   /// Return a reverse_iterator pointing to the end of the reversed container
0105   typename ObjectList<TYPE>::reverse_iterator rend() { return m_list.rend(); }
0106 
0107   /// Return a const_reverse_iterator pointing to the end of the reversed container
0108   typename ObjectList<TYPE>::const_reverse_iterator rend() const { return m_list.rend(); }
0109 
0110   /// Return the size of the container
0111   /// Size means the number of objects stored in the container, independently on the amount of information stored in
0112   /// each object
0113   typename ObjectList<TYPE>::size_type size() const {
0114     // C++11: std::list::size is constant (pre C++11 it could be linear!)
0115     return m_list.size();
0116   }
0117   /// The same as size(), return number of objects in the container
0118   typename ObjectList<TYPE>::size_type numberOfObjects() const override { return m_list.size(); }
0119 
0120   /// Return the largest possible size of the container
0121   typename ObjectList<TYPE>::size_type max_size() const { return m_list.max_size(); }
0122 
0123   /// Return true if the size of the container is 0
0124   bool empty() const { return m_list.empty(); }
0125 
0126   /// Return reference to the first element
0127   typename ObjectList<TYPE>::reference front() { return m_list.front(); }
0128 
0129   /// Return const_reference to the first element
0130   typename ObjectList<TYPE>::const_reference front() const { return m_list.front(); }
0131 
0132   /// Return reference to the last element
0133   typename ObjectList<TYPE>::reference back() { return m_list.back(); }
0134 
0135   /// Return const_reference to the last element
0136   typename ObjectList<TYPE>::const_reference back() const { return m_list.back(); }
0137 
0138   /// push_back = append = insert a new element at the end of the container
0139   void push_back( typename ObjectList<TYPE>::const_reference value ) {
0140     if ( value->parent() ) { const_cast<ObjectContainerBase*>( value->parent() )->remove( value ); }
0141     value->setParent( this );
0142     m_list.push_back( value );
0143   }
0144 
0145   /// Add an object to the container
0146   long add( ContainedObject* pObject ) override {
0147     try {
0148       auto ptr = dynamic_cast<typename ObjectList<TYPE>::value_type>( pObject );
0149       if ( ptr ) {
0150         push_back( ptr );
0151         return m_list.size() - 1;
0152       }
0153     } catch ( ... ) {}
0154     return -1;
0155   }
0156 
0157   /// pop_back = remove the last element from the container
0158   /// The removed object will be deleted (see the method release)
0159   void pop_back() {
0160     auto position = m_list.back();
0161     // Set the back pointer to 0 to avoid repetitional searching
0162     // for the object in the container, and deleting the object
0163     position->setParent( nullptr );
0164     delete position;
0165     // Removing from the container itself
0166     m_list.pop_back();
0167   }
0168 
0169   /// Release object from the container (the pointer will be removed
0170   /// from the container, but the object itself will remain alive) (see the method pop_back)
0171   long remove( ContainedObject* value ) override {
0172     // Find the object of value value
0173     long idx  = 0;
0174     auto iter = std::find_if( begin(), end(), [&]( const ContainedObject* i ) { return i == value; } );
0175     if ( iter == end() ) {
0176       // Object cannot be released from the container,
0177       // as it is not contained in it
0178       return -1;
0179     }
0180 
0181     // Set the back pointer to 0 to avoid repetitional searching
0182     // for the object in the container and deleting the object
0183     ( *iter )->setParent( nullptr );
0184     erase( iter );
0185     return idx;
0186   }
0187 
0188   /// Insert "value" before "position"
0189   typename ObjectList<TYPE>::iterator insert( typename ObjectList<TYPE>::iterator        position,
0190                                               typename ObjectList<TYPE>::const_reference value ) {
0191     value->setParent( this );
0192     return m_list.insert( position, value );
0193   }
0194 
0195   /// Erase the object at "position" from the container. The removed object will be deleted.
0196   void erase( typename ObjectList<TYPE>::iterator position ) {
0197     if ( ( *position )->parent() ) {
0198       // Set the back pointer to 0 to avoid repetitional searching
0199       // for the object in the container, and deleting the object
0200       ( *position )->setParent( nullptr );
0201       delete *position;
0202     }
0203     // Removing from the container itself
0204     m_list.erase( position );
0205   }
0206 
0207   /// Erase the range [first, last) from the container. The removed object will be deleted
0208   void erase( typename ObjectList<TYPE>::iterator first, typename ObjectList<TYPE>::iterator last ) {
0209     for ( auto iter = first; iter != last; ++iter ) {
0210       // Set the back pointer to 0 to avoid repetitional searching
0211       // for the object in the container, and deleting the object
0212       ( *iter )->setParent( nullptr );
0213       delete *iter;
0214     }
0215     // Removing from the container itself
0216     m_list.erase( first, last );
0217   }
0218 
0219   /// Clear the entire content of the container and delete all contained objects
0220   void clear() { erase( begin(), end() ); }
0221 
0222   /// Return distance of a given object from the beginning of its container
0223   /// It corresponds to the "index" ( from 0 to size()-1 ) If "obj" not fount, return -1
0224   long index( const ContainedObject* obj ) const override {
0225     auto i = std::find_if( begin(), end(), [&]( const ContainedObject* o ) { return o == obj; } );
0226     return i != end() ? std::distance( begin(), i ) : -1;
0227   }
0228 
0229   /// Return const pointer to an object of a given distance
0230   const ContainedObject* containedObject( long dist ) const override {
0231     return dist < size() ? *std::next( begin(), dist ) : nullptr;
0232   }
0233   /// Return const pointer to an object of a given distance
0234   ContainedObject* containedObject( long dist ) override {
0235     return dist < size() ? *std::next( begin(), dist ) : nullptr;
0236   }
0237 
0238   /// Fill the output stream (ASCII)
0239   std::ostream& fillStream( std::ostream& s ) const override {
0240     s << "class ObjectList :    size = " << std::setw( 12 ) << size() << "\n";
0241     // Output the base class
0242     // ObjectContainerBase::fillStream(s);
0243     if ( !empty() ) {
0244       s << "\nContents of the STL list :";
0245       long count = 0;
0246       for ( const auto& iter : m_list ) {
0247         s << "\nIndex " << std::setw( 12 ) << count++ << " of object of type " << *iter;
0248       }
0249     }
0250     return s;
0251   }
0252 
0253 private:
0254   /// The STL list
0255   std::list<TYPE*> m_list;
0256 };
0257 
0258 #endif // GAUDI_OBJECTLIST_H