00001 #ifndef CDBBASE_ADAPTER_ITR_HH 00002 #define CDBBASE_ADAPTER_ITR_HH 00003 00004 // File and Version Information: 00005 // $Id: CdbAdapterItr.hh,v 1.2 2005/08/26 07:36:47 gapon Exp $ 00006 00007 #include "CdbBase/CdbIItr.hh" 00008 00009 /// A proxy class retranslating values of an "input" type into the "output" one 00010 /** 00011 * This abstract class is meant to be used for rapid building of CDB API iterators 00012 * of the "output" type out of input iterators of the "input" type of elements. 00013 * The class is parameterized by two parameters: 00014 * 00015 * T - "output" type of the iterator values produced by the current iterator 00016 * I - "input" type of an input iterator's elements 00017 * 00018 * A basic idea is that a value of the I type needs to be translated into the corresponding 00019 * value of the T type. The translation is done through a special virtual method to be 00020 * overloaded by a subclass. 00021 * 00022 * In addition, the iterator provides an optional filtering mechanism to evaluate each input 00023 * object withing the iterator's "::next()" method. That would allow (if needed by a subclass) 00024 * to exclude certain objects out of the input sequence from being translated and delivered 00025 * though the "::value()" and "::toValue()" method. The name of the method is "::isAccepted()". 00026 * If the method returns "true" then the object is accepted, otherwise - it's rejected, 00027 * and the iterator will go to the next object input object and so on. By default the method 00028 * will accept all objects. 00029 * 00030 * @see CdbAdapterItr::toValue() 00031 * @see CdbAdapterItr::isAccepted() 00032 * 00033 * EXAMPLE: 00034 * 00035 * #include "CdbBase/CdbAdapterItr.hh" 00036 * 00037 * class MyItr : public CdbAdapterItr<std::string, unsigned int> { 00038 * public: 00039 * MyItr( const CdbIItr<unsigned int>& theInputItr ) : 00040 * CdbAdapterItr<std::string, unsigned int>( theInputItr ) 00041 * { } 00042 * protected: 00043 * virtual std::string toValue( const unsigned int& theValue ) const 00044 * { ... } 00045 * virtual bool isAccepted( const unsigned int& theValue ) const 00046 * { 00047 * return ( theValue >= 0 ); 00048 * } 00049 * }; 00050 * 00051 * IMPLEMENTATION NOTE: 00052 * 00053 * This particular implementation of the iterator has not been optimized to cache 00054 * the translated values locally. This feature will be implemented later. Just keep 00055 * this performance issue in mind when using the iterator. 00056 */ 00057 template< class T, 00058 class I > 00059 class CdbAdapterItr : public CdbIItr<T> { 00060 00061 private: 00062 00063 /// The default constructor (NOT IMPLEMENTED) 00064 00065 CdbAdapterItr( ); 00066 00067 /// The assignment operator (NOT IMPLEMENTED) 00068 00069 CdbAdapterItr<T,I>& operator=( const CdbAdapterItr<T,I>& theItr ); 00070 00071 protected: 00072 00073 /// The normal constructor 00074 /** 00075 * We'll take an input iterator whose values need to be retranslated 00076 * to deliver values of specified "output" type as the iterator will advance. 00077 * 00078 * DESIGN NOTE: 00079 * 00080 * The iterator will take over the specified one. 00081 */ 00082 CdbAdapterItr( CdbIItr<I>* theInputItrPtr ); 00083 00084 /// The copy constructor 00085 00086 CdbAdapterItr( const CdbAdapterItr<T,I>& theItr ); 00087 00088 public: 00089 00090 /// The destructor 00091 00092 virtual ~CdbAdapterItr( ); 00093 00094 /// Reset an iterator to its initial state. 00095 /** 00096 * This implements the corresponding method of the base class. 00097 * 00098 * @see CdbIItr::reset 00099 * @see CdbStatus 00100 */ 00101 virtual CdbStatus reset( ); 00102 00103 /// Advance an iterator to the next position. 00104 /** 00105 * This implements the corresponding method of the base class. 00106 * 00107 * @see CdbIItr::next() 00108 */ 00109 virtual bool next( ); 00110 00111 /// Obtain the currently reffered value. 00112 /** 00113 * This implements the corresponding method of the base class. 00114 * 00115 * @see CdbIItr::value() 00116 * 00117 * @return the current value the iterator is set on 00118 */ 00119 virtual T value( ); 00120 00121 /// Check if an iterator is valid. 00122 /** 00123 * This implements the corresponding method of the base class. 00124 * 00125 * @see CdbIItr::isValid() 00126 */ 00127 virtual bool isValid( ); 00128 00129 protected: 00130 00131 /// User defined translation for the currently referred value. 00132 /** 00133 * This method is being called by the current implementation of the itarator 00134 * to translate the desired information out of the currently refered element of 00135 * the input iterator passed to the class's constructor. 00136 * 00137 * The method is supposed to be implemented by a subclass. 00138 * 00139 * @return the current value the iterator is set on 00140 */ 00141 virtual T toValue( const I& ) const = 0; 00142 00143 /// Optional user defined filter for input objects 00144 /** 00145 * This method is being called by the "::next()" method of the current implementation 00146 * of the itarator for each input object in order to determine if that object needs 00147 * to be translated or filtered out. 00148 * 00149 * If the method returns "true" then the object is accepted, otherwise - it's rejected, 00150 * and the iterator will go to the next object input object and so on. 00151 * 00152 * The default implementation of the method would accept all objects. 00153 * 00154 * The method can be optionally implemented by a subclass. 00155 * 00156 * @return "true" to accept an object, and "false" - to reject. 00157 */ 00158 virtual bool isAccepted( const I& ) const { return true; } 00159 00160 private: 00161 00162 CdbIItr<I>* _myInputItrPtr; 00163 }; 00164 00165 #ifdef BABAR_COMP_INST 00166 #include "CdbBase/CdbAdapterItr.cc" 00167 #endif /* BABAR_COMP_INST */ 00168 00169 #endif // CDBBASE_ADAPTER_ITR_HH
1.3-rc3