Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

CdbRooRoVectorCollectionR.rdl

Go to the documentation of this file.
00001 #ifndef CDBROOREADONLY_VECTOR_COLLECTION_R_RDL
00002 #define CDBROOREADONLY_VECTOR_COLLECTION_R_RDL
00003 
00004 // File and Version Information:
00005 //      $Id: CdbRooRoVectorCollectionR.rdl,v 1.5 2005/11/11 01:41:29 gapon Exp $
00006 
00007 #include "CdbBase/CdbCPtr.hh"
00008 
00009 #include "CdbRooReadonly/CdbRooRoPersistentCollectionR.hh"
00010 
00011 #include <iostream>
00012 #include <vector>
00013 #include <string>
00014 
00015 class TTree;
00016 
00017 // Policy classes to fine tune an internal implementation of the vector
00018 // collection class. The policies affect the way collection's elements
00019 // are stored in the transient cache of the collection.
00020 
00021 /// The policy class for plain C/C++ pointers
00022 
00023 template< class T >
00024 struct CdbRooRoVectorCollection_PlainPointerPolicy {
00025 
00026     typedef T* PointerType;
00027 
00028     static void delete_object( PointerType ptr ) { delete ptr; }
00029 
00030     static bool is_null( PointerType ptr ) { return (0 == ptr); }
00031 
00032     static PointerType null_object( ) { return 0; }
00033 
00034     static T* to_plain_pointer( PointerType ptr ) { return ptr; }
00035 
00036     static PointerType copy_object( const CdbCPtr<T>& theObjectPtr ) { return new T( *theObjectPtr ); }
00037 
00038     static PointerType copy_object( const T*          theObjectPtr ) { return new T( *theObjectPtr ); }
00039 };
00040 
00041 /// The policy class for CdbCPtr pointers
00042 
00043 template< class T >
00044 struct CdbRooRoVectorCollection_SmartPointerPolicy {
00045 
00046     typedef CdbCPtr<T> PointerType;
00047 
00048     static void delete_object( PointerType ptr ) { }
00049 
00050     static bool is_null( PointerType ptr ) { return ptr.isNull( ); }
00051 
00052     static PointerType null_object( ) { return PointerType( ); }
00053 
00054     static T* to_plain_pointer( PointerType ptr ) { return ptr.get( ); }
00055 
00056     static PointerType copy_object( const CdbCPtr<T>& theObjectPtr ) { return theObjectPtr; }
00057 
00058     static PointerType copy_object( const T*          theObjectPtr ) { return new T( *theObjectPtr ); }
00059 };
00060 
00061 /// A persistent vector-like collection of objects
00062 /**
00063   * Classes eligible for this template must support the "value semantics"
00064   * providing the following minimal
00065   * interface:
00066   *
00067   *   class T {
00068   *   public:
00069   *     T();
00070   *     T( const T&);
00071   *     T& operator=(const T&);
00072   *     ~T();
00073   *   };
00074   *
00075   * The total number of elements stored in a collection of this type
00076   * is limited by 2**32 (unsigned 32-bit integer used as an index).
00077   * Therefore valid indexes of the elements lay in the following range:
00078   *
00079   *   [0..0xFFFFFFFF]
00080   *
00081   * inclusive at both sides of the range.
00082   *
00083   * THE STORAGE MODEL:
00084   *
00085   *   1. Objects of the vector are stored using the mechanism of the TTree/TBranch.
00086   *
00087   *   2. The key name of that TTree is formed by concatenating the vector collection's
00088   *      name and ".Elements".
00089   *
00090   *   3. The branch name is always "0".
00091   *
00092   * @see class CdbRooRoPersistentCollectionR
00093   */
00094 template< class T,
00095           class STORAGE_POLICY = CdbRooRoVectorCollection_PlainPointerPolicy<T> >
00096 
00097 class CdbRooRoVectorCollectionR : public CdbRooRoPersistentCollectionR {
00098 
00099 public:
00100 
00101   /// The default constructor
00102   /**
00103     * It's also  needed for ROOT I/O.
00104     */
00105     CdbRooRoVectorCollectionR( );
00106 
00107   /// The normal constructor
00108   /**
00109     * Initialize the collection with the specified number of elements
00110     * constructed using the default constructor of the elements class.
00111     */
00112     CdbRooRoVectorCollectionR( unsigned int theInitialSize );
00113 
00114   /// The copy constructor
00115   /**
00116     * IMPORTANT NOTE:
00117     *
00118     *   Watch out for the implementation of this constructor because objects of
00119     *   the class can be in two states: "stored" and "transient". The same object
00120     *   can't be stored more than one time due to a unique naming scheme for
00121     *   persistent collections (and their sub-collections (and their elements)) in ROOT files.
00122     *   The same rule applies to copies of an object - after the master copy got stored
00123     *   then its copies can't be stored anymore.
00124     *
00125     *   However, for the transient cache of objects the rules are slightly different.
00126     *   We must duplicate the cache of objects only if the input objects ia not stored.
00127     *   And we don't bother about duplicating it if it's not because it can easily be
00128     *   populated from the persistent store.
00129     *
00130     *   And we don't care about supplementary transient context (other than the transient
00131     *   list of objects). That context is only used to populate the transient list
00132     *   from the persistent store. Copying this context accross objects can be
00133     *   dangerous too.
00134     */
00135     CdbRooRoVectorCollectionR( const CdbRooRoVectorCollectionR<T,STORAGE_POLICY>& theOther );
00136 
00137   /// Destructor
00138 
00139     virtual ~CdbRooRoVectorCollectionR( );
00140 
00141   /// The assignment operator
00142   /**
00143     * IMPORTANT NOTE:
00144     *
00145     *   See notes for the copy constructor. The same rules apply.
00146     */
00147     CdbRooRoVectorCollectionR<T,STORAGE_POLICY>& operator=( const CdbRooRoVectorCollectionR<T,STORAGE_POLICY>& theOther );
00148 
00149     UInt_t size( ) const { return _size; }
00150 
00151   /// Resize the collection
00152   /**
00153     * If the collection gets extended then added elements will be initialized using
00154     * the default constructor of the element's class.
00155     */
00156     virtual CdbStatus resize( UInt_t theNewSize );
00157 
00158     virtual CdbStatus addElement( const T& theObject );
00159 
00160     virtual CdbStatus addElement( const CdbCPtr<T>& theObjectPtr );
00161 
00162     virtual CdbStatus elementAt( UInt_t theIndex,
00163                                  T&     theObject ) const;
00164 
00165     virtual CdbStatus elementAt( UInt_t      theIndex,
00166                                  CdbCPtr<T>& theObjectPtr ) const;
00167 
00168     virtual void dump( std::ostream&      o,
00169                        const std::string& indent = "" ) const;
00170 
00171 protected:
00172 
00173   /// Store sub-collections
00174   /**
00175     * Implement the corresponding method defined in a base class.
00176     *
00177     * @see CdbRooRoPersistentCollectionR::storeSubCollectionsAt()
00178     */
00179     virtual CdbStatus storeSubCollectionsAt( const CdbRooRoCollectionAddressR& theCollectionAddress,
00180                                              const CdbCPtr<TFile>&             theFilePtr,
00181                                              Int_t&                            theNumBytesStored );
00182 private:
00183 
00184   /// Make sure that an element at specifid position is loaded into the transient cache
00185 
00186     CdbStatus loadElementAt( UInt_t theIndex ) const;
00187 
00188 private:
00189 
00190   // Transient context
00191   //
00192   // NOTES:
00193   //
00194   //    1. The order and indexes of the vector's elements must match the ones
00195   //       in the TBranch. The check is done when the collection is being created.
00196   //
00197   //    2. All transient data members are used both when populating a collection
00198   //       and when reading from it.
00199   //
00200   //    3. In case when the collection is being populated the transient list of objects
00201   //       must be consistent with the persistent TBranch. Otherwise a error status is
00202   //       returned by the operations. The below defined "_stored" flag is used for this.
00203   //
00204   //    4. In case when the collection was fetched from a database storing is not
00205   //       possible. Therefore the transient cache will be lazy populated.
00206 
00207     mutable CdbCPtr<TFile> _tFilePtr;   //!
00208     mutable CdbCPtr<TTree> _tTreePtr;   //!
00209     mutable T*             _tObjectPtr; //!
00210 
00211     mutable std::vector< typename STORAGE_POLICY::PointerType > _tCachedObjects;  //! transient cache of objects
00212 
00213   // Persistent context
00214 
00215     UInt_t _size;     // A total number of elements in the collection
00216 
00217     ClassDefT(CdbRooRoVectorCollectionR<T>,1);
00218 };
00219 
00220 // Implementation of the methods
00221 
00222 #ifndef __CINT__
00223 #include "CdbRooReadonly/CdbRooRoVectorCollectionR.cc"
00224 #endif
00225 #endif /* CDBROOREADONLY_VECTOR_COLLECTION_R_RDL */

Generated on Mon Dec 5 18:22:11 2005 for CDB by doxygen1.3-rc3