00001 #if !defined(__BUILDER_HPP)
00002 #define __BUILDER_HPP
00003
00004 00005 00006 00007 00008 00009 00010 00011 00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022
00023
00024 #if !defined(__COMMON_HPP)
00025 #include <Common.hpp>
00026 #endif
00027
00028 #if !defined(__ABSTRACTFACTORY_HPP)
00029 #include <AbstractFactory.hpp>
00030 #endif
00031
00032 namespace corelinux
00033 {
00043 template< class ProductImpl, class UniqueId >
00044 class Builder
00045 {
00046 public:
00047
00048
00049
00050
00051
00058 Builder
00059 (
00060 AbstractFactory<UniqueId> *aAbstractFactory
00061 )
00062 throw( Assertion )
00063 :
00064 theCurrentProduct( NULLPTR ),
00065 theFactory( NULLPTR ),
00066 theProductCreates( 0 ),
00067 theProductDestroys( 0 )
00068 {
00069 REQUIRE( aAbstractFactory != NULLPTR );
00070 theFactory = aAbstractFactory ;
00071 }
00072
00079 Builder( const Builder &aBuilder )
00080 throw( Assertion )
00081 :
00082 theCurrentProduct( NULLPTR ),
00083 theFactory( aBuilder.getFactory() ),
00084 theProductCreates( 0 ),
00085 theProductDestroys( 0 )
00086 {
00087 REQUIRE( theFactory != NULLPTR );
00088 }
00089
00091
00092 virtual ~Builder( void )
00093 {
00094 ;
00095 }
00096
00097
00098
00099
00100
00108 Builder & operator=( const Builder &aRef )
00109 {
00110 if( this != &aRef )
00111 {
00112 this->destroy( theCurrentProduct );
00113
00114 theFactory = aRef.getFactory();
00115
00116 theCurrentProduct = NULLPTR;
00117 theProductCreates = 0;
00118 theProductDestroys = 0;
00119 }
00120 else
00121 {
00122 ;
00123 }
00124
00125 return ( *this );
00126 }
00127
00134 bool operator==( const Builder &aRef )
00135 {
00136 return ( this == &aRef );
00137 }
00138
00139
00140
00141 00147 virtual ProductImpl * getCurrentProduct( void ) const
00148 {
00149 return theCurrentProduct;
00150 }
00151
00153
00154 virtual CountCref getProductCreates( void ) const
00155 {
00156 return theProductCreates;
00157 }
00158
00160
00161 virtual CountCref getProductDestroys( void ) const
00162 {
00163 return theProductDestroys;
00164 }
00165
00167
00168 virtual AbstractFactory<UniqueId> * getFactory( void ) const
00169 {
00170 return theFactory;
00171 }
00172
00173
00174 00181 virtual ProductImpl * create( void )
00182 {
00183 ProductImpl *aPtr( NULLPTR );
00184
00185 try
00186 {
00187 aPtr = createProduct();
00188
00189 CHECK( aPtr != NULLPTR );
00190
00191 theCurrentProduct = aPtr;
00192
00193 incrementCreates();
00194 }
00195 catch( ... )
00196 {
00197 throw;
00198 }
00199
00200 return aPtr;
00201 }
00202
00209 virtual void destroy( ProductImpl * aPtr )
00210 {
00211 REQUIRE( aPtr != NULLPTR );
00212 try
00213 {
00214 destroyProduct( aPtr );
00215 incrementDestroys();
00216 }
00217 catch( ... )
00218 {
00219 throw;
00220 }
00221 if( aPtr == theCurrentProduct )
00222 {
00223 theCurrentProduct = NULLPTR;
00224 }
00225 else
00226 {
00227 ;
00228 }
00229 }
00230
00231 protected:
00232
00233
00234
00235
00236
00238
00239 Builder( void ) throw(Assertion)
00240 :
00241 theCurrentProduct( NULLPTR ),
00242 theFactory( NULLPTR ),
00243 theProductCreates( 0 ),
00244 theProductDestroys( 0 )
00245 {
00246 NEVER_GET_HERE;
00247 }
00248
00249
00250
00251
00252
00254
00255 void incrementCreates( void )
00256 {
00257 ++theProductCreates;
00258 }
00259
00261
00262 void incrementDestroys( void )
00263 {
00264 ++theProductDestroys;
00265 }
00266
00267
00268
00269
00270
00272
00273 virtual ProductImpl * createProduct( void ) const = 0;
00274
00276
00277 virtual void destroyProduct( ProductImpl * ) const = 0;
00278
00279 protected:
00280
00281
00282
00283
00284
00286
00287 ProductImpl *theCurrentProduct;
00288
00290
00291 AbstractFactory<UniqueId> *theFactory;
00292
00294
00295 Count theProductCreates;
00296
00298
00299 Count theProductDestroys;
00300
00301 };
00302 }
00303
00304 #endif // if !defined(__BUILDER_HPP)
00305
00306 00307 00308 00309 00310 00311 00312
00313