00001 #ifndef CDBBDBSHARED_R_ALLOCATOR_P_HH 00002 #define CDBBDBSHARED_R_ALLOCATOR_P_HH 00003 00004 // File and Version Information: 00005 // $Id: CdbBdbSRallocatorP.ddl,v 1.5 2004/08/06 05:54:25 bartoldu Exp $ 00006 00007 #include "BdbUtil/Bdb.hh" 00008 00009 #include "CdbBase/CdbItr.hh" 00010 #include "CdbBdbShared/CdbBdbSBtreeP.hh" 00011 00012 #include <iostream> 00013 00014 /// Default RT_OPERATIONS_POLICY policy for a resource type class 00015 /** 00016 * The resource type is specified through the template parameter of 00017 * this class. 00018 */ 00019 template< class RT > 00020 class CdbBdbSRaDefaultPolicy { 00021 public: 00022 00023 static RT min ( ) { return 0; } 00024 static RT next ( RT value ) { return ++value; } 00025 static bool valid( const RT& value ) { return true; } 00026 }; 00027 00028 /// Generic resource allocator class 00029 /** 00030 This persistent class represents "resource allocator", whose primary 00031 focus (although not limited to) is 00032 at various sorts of identifiers. This facility is capable of managing 00033 individual values (not ranges) of resources. 00034 00035 The class has the following template parameters: 00036 00037 RT: the type of the managed resource. This class is expected 00038 to provide the following public interface: 00039 00040 - default ctor 00041 - copy ctor 00042 - destructor 00043 - assignment operator 00044 - operator== 00045 - operator< 00046 - operator << to serialize into std::ostream 00047 00048 Basically, it's the same interface, which is required to put objects 00049 of the RT type into a B-tree. 00050 00051 RT_OPERATIONS_POLICY: an optional policy for the RT type. The role 00052 of this policy is to supply an appropriate abstraction 00053 for the following operations: 00054 00055 - obtain a value of RT type in its minimum state 00056 00057 - increment a value of RT type to get to the next element in a sequence 00058 of allowed ones (logically similar to ++(value) operator). 00059 00060 IMPORTANT: It's assumed that when the iterated value will reach its maximum 00061 available state then the iterator will switch back to the minimum 00062 state as defined above. 00063 00064 - check if specified value of a resource is in a "valid" range. 00065 00066 IMPORTANT: The "Resource Allocator" neither assumes an existence nor relies at 00067 any internal state the RT_OPERATIONS_POLICY class may have. 00068 It means that the methods of a concrete policy classs supplied 00069 through this template parameter can be invoked at any sequence 00070 and they will always provide repeatable results for the same 00071 values of their input parameters. 00072 00073 See the interface of the default RT_OPERATIONS_POLICY class as a specification 00074 for the interface of user-defined policy classes. 00075 00076 This class also provides default version of the RT_OPERATIONS_POLICY class, which 00077 is sufficient enough for most primitive ("countable") data types like: d_ULong, d_UShort, 00078 etc. The default implementation of the RT_OPERATIONS_POLICY relies on the following: 00079 00080 - there is proper (and semantically meaningfull) conversion for the RT 00081 to set it to its minimum state by assigning 0 to it. 00082 00083 - there is ++(value) operator to advance the value to the "next" state 00084 00085 - the values obtained through the "operator++" are unique and sequential 00086 in a range of the available value of the RT type. 00087 00088 - all possible values of the RT type are "valid". 00089 00090 The abstract user defined types require proper policy class to be supplied 00091 along with RT type to override the default policy unless the RT type 00092 has sufficient (both formally and semantically) public interface 00093 meeting the requirements of the default implementation for OPERATIONS. 00094 00095 */ 00096 template< class RT, 00097 class RT_OPERATIONS_POLICY = CdbBdbSRaDefaultPolicy<RT> > 00098 class CdbBdbSRallocatorP : public BdbPersObj { 00099 00100 public: 00101 00102 /// Default constructor 00103 /** 00104 * Initializes an empty collectiuon 00105 */ 00106 CdbBdbSRallocatorP( ); 00107 00108 /// Destructor 00109 /** 00110 * Will also destroy all the elements. 00111 */ 00112 virtual ~CdbBdbSRallocatorP( ); 00113 00114 /// Check allocation of specified resource 00115 /** 00116 * The method will return: 00117 * 00118 * - "Success" if specified resource is allocated 00119 * - "NotFound" if specified resource is not allocated 00120 * - "Error" if specified resource is not "valid" (see RT_OPERATIONS_POLICY::valid() 00121 * method for the explanation) or in case of any other internal problem 00122 */ 00123 virtual CdbStatus isAllocated( const RT& theResource ) const; 00124 00125 /// Force allocation of specified resource 00126 /** 00127 * The method makes sure that specified resource, whose value passed to the procedure 00128 * gets allocated. The passed resource can already be allocated. 00129 * 00130 * The method will return: 00131 * 00132 * - "Success" if specified resource already was or just has been allocated 00133 * - "Error" if specified resource is not "valid" (see RT_OPERATIONS_POLICY::valid() 00134 * method for the explanation) or in case of any other internal problem 00135 */ 00136 virtual CdbStatus force( const RT& theResource ); 00137 00138 /// Allocate a resource 00139 /** 00140 * The method will return: 00141 * 00142 * - "Success" if a resource has been allocated 00143 * - "NotFound" if a resource could not be allocated 00144 */ 00145 virtual CdbStatus allocate( RT& theResource ); 00146 00147 /// Release specified resource 00148 /** 00149 * The method will return: 00150 * 00151 * - "Success" if specified resource was released 00152 * - "NotFound" if specified resource was never allocated 00153 * - "Error" if specified resource is not "valid" (see RT_OPERATIONS_POLICY::valid() 00154 * method for the explanation) or in case of any other internal problem 00155 */ 00156 virtual CdbStatus release( const RT& theResource ); 00157 00158 /// Get an instance of an iterator for allocated resources 00159 /** 00160 * This kind of iterator provides "non-sorted" access to the values 00161 * of allocated resources. 00162 */ 00163 virtual CdbItr<RT> iterator( ) const; 00164 00165 /// Dump the contents of the object 00166 /** 00167 */ 00168 virtual void dump( std::ostream& o ) const; 00169 00170 private: 00171 00172 // Two indexes: one for allocated resources and the other one - 00173 // for the spare ones. 00174 00175 BdbRef( CdbBdbSBtreeP< RT > ) _allocatedRef; 00176 BdbRef( CdbBdbSBtreeP< RT > ) _freeRef; 00177 00178 // The value of the most recently generated resource. It will be 00179 // automatically updated at an allocation request if the 00180 // index of spare resources is empty. 00181 // 00182 // NOTE: The "force" request may violate the normal sequence of resources 00183 // allocation. Therefore if the next value generated through the data member 00184 // below is already occupied (at either of indexes above) then the generator 00185 // will skip the value and go to the next one and so on until a spare one 00186 // will be found or the allocation will be exausted. 00187 00188 RT _recent; 00189 }; 00190 00191 // Explicit template instantiation for the resource allocator 00192 00193 #define EXPLICIT_INSTANTIATE_CdbBdbSRallocatorP_1( T ) \ 00194 EXPLICIT_INSTANTIATE_CdbBdbSBtreeP_1( T ); \ 00195 template class CdbBdbSRallocatorP< T > 00196 00197 #ifdef BABAR_COMP_INST 00198 #include "CdbBdbShared/CdbBdbSRallocatorP.cc" 00199 #endif /* BABAR_COMP_INST */ 00200 00201 #endif /* CDBBDBSHARED_R_ALLOCATOR_P_HH */
1.3-rc3