00001 #if !defined(__BUILDER_HPP)
00002 #define __BUILDER_HPP
00003
00004 /*
00005 CoreLinux++
00006 Copyright (C) 1999,2000 CoreLinux Consortium
00007
00008 The CoreLinux++ Library is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU Library General Public License as
00010 published by the Free Software Foundation; either version 2 of the
00011 License, or (at your option) any later version.
00012
00013 The CoreLinux++ Library Library is distributed in the hope that it will
00014 be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016 Library General Public License for more details.
00017
00018 You should have received a copy of the GNU Library General Public
00019 License along with the GNU C Library; see the file COPYING.LIB. If not,
00020 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00021 Boston, MA 02111-1307, USA.
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 // Constructors and destructor
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 ; // do nothing
00095 }
00096
00097 //
00098 // Operator overloads
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 ; // do nothing
00123 }
00124
00125 return ( *this );
00126 }
00127
00134 bool operator==( const Builder &aRef )
00135 {
00136 return ( this == &aRef );
00137 }
00138
00139 //
00140 // Accessors
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 // Builder methods
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 ; // do nothing
00228 }
00229 }
00230
00231 protected:
00232
00233 //
00234 // Constructor
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 // Mutators
00251 //
00252
00254
00255 void incrementCreates( void )
00256 {
00257 ++theProductCreates;
00258 }
00259
00261
00262 void incrementDestroys( void )
00263 {
00264 ++theProductDestroys;
00265 }
00266
00267 //
00268 // Factory methods
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 // Data members
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 Common rcs information do not modify
00308 $Author: prudhomm $
00309 $Revision: 1.1 $
00310 $Date: 2000/04/23 20:43:13 $
00311 $Locker: $
00312 */
00313