CoreLinux++  0.4.32
Builder.hpp
1 #if !defined(__BUILDER_HPP)
2 #define __BUILDER_HPP
3 
4 /*
5  CoreLinux++
6  Copyright (C) 1999,2000 CoreLinux Consortium
7 
8  The CoreLinux++ Library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU Library General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version.
12 
13  The CoreLinux++ Library Library is distributed in the hope that it will
14  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Library General Public License for more details.
17 
18  You should have received a copy of the GNU Library General Public
19  License along with the GNU C Library; see the file COPYING.LIB. If not,
20  write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  Boston, MA 02111-1307, USA.
22 */
23 
24 #if !defined(__COMMON_HPP)
25 #include <Common.hpp>
26 #endif
27 
28 #if !defined(__ABSTRACTFACTORY_HPP)
29 #include <AbstractFactory.hpp>
30 #endif
31 
32 namespace corelinux
33 {
43  template< class ProductImpl, class UniqueId >
44  class Builder
45  {
46  public:
47 
48  //
49  // Constructors and destructor
50  //
51 
58  Builder
59  (
60  AbstractFactory<UniqueId> *aAbstractFactory
61  )
62  throw( Assertion )
63  :
64  theCurrentProduct( NULLPTR ),
65  theFactory( NULLPTR ),
66  theProductCreates( 0 ),
68  {
69  REQUIRE( aAbstractFactory != NULLPTR );
70  theFactory = aAbstractFactory ;
71  }
72 
79  Builder( const Builder &aBuilder )
81  :
82  theCurrentProduct( NULLPTR ),
83  theFactory( aBuilder.getFactory() ),
84  theProductCreates( 0 ),
86  {
87  REQUIRE( theFactory != NULLPTR );
88  }
89 
91 
92  virtual ~Builder( void )
93  {
94  ; // do nothing
95  }
96 
97  //
98  // Operator overloads
99  //
100 
108  Builder & operator=( const Builder &aRef )
109  {
110  if( this != &aRef )
111  {
112  this->destroy( theCurrentProduct );
113 
114  theFactory = aRef.getFactory();
115 
116  theCurrentProduct = NULLPTR;
117  theProductCreates = 0;
118  theProductDestroys = 0;
119  }
120  else
121  {
122  ; // do nothing
123  }
124 
125  return ( *this );
126  }
127 
134  bool operator==( const Builder &aRef )
135  {
136  return ( this == &aRef );
137  }
138 
139  //
140  // Accessors
141  //
147  virtual ProductImpl * getCurrentProduct( void ) const
148  {
149  return theCurrentProduct;
150  }
151 
153 
154  virtual CountCref getProductCreates( void ) const
155  {
156  return theProductCreates;
157  }
158 
160 
161  virtual CountCref getProductDestroys( void ) const
162  {
163  return theProductDestroys;
164  }
165 
167 
168  virtual AbstractFactory<UniqueId> * getFactory( void ) const
169  {
170  return theFactory;
171  }
172  //
173  // Builder methods
174  //
181  virtual ProductImpl * create( void )
182  {
183  ProductImpl *aPtr( NULLPTR );
184 
185  try
186  {
187  aPtr = createProduct();
188 
189  CHECK( aPtr != NULLPTR );
190 
191  theCurrentProduct = aPtr;
192 
194  }
195  catch( ... )
196  {
197  throw;
198  }
199 
200  return aPtr;
201  }
202 
209  virtual void destroy( ProductImpl * aPtr )
210  {
211  REQUIRE( aPtr != NULLPTR );
212  try
213  {
214  destroyProduct( aPtr );
216  }
217  catch( ... )
218  {
219  throw;
220  }
221  if( aPtr == theCurrentProduct )
222  {
223  theCurrentProduct = NULLPTR;
224  }
225  else
226  {
227  ; // do nothing
228  }
229  }
230 
231  protected:
232 
233  //
234  // Constructor
235  //
236 
238 
240  :
241  theCurrentProduct( NULLPTR ),
242  theFactory( NULLPTR ),
243  theProductCreates( 0 ),
244  theProductDestroys( 0 )
245  {
246  NEVER_GET_HERE;
247  }
248 
249  //
250  // Mutators
251  //
252 
254 
255  void incrementCreates( void )
256  {
258  }
259 
261 
262  void incrementDestroys( void )
263  {
265  }
266 
267  //
268  // Factory methods
269  //
270 
272 
273  virtual ProductImpl * createProduct( void ) const = 0;
274 
276 
277  virtual void destroyProduct( ProductImpl * ) const = 0;
278 
279  protected:
280 
281  //
282  // Data members
283  //
284 
286 
287  ProductImpl *theCurrentProduct;
288 
290 
292 
294 
296 
298 
300 
301  };
302 }
303 
304 #endif // if !defined(__BUILDER_HPP)
305 
306 /*
307  Common rcs information do not modify
308  $Author: prudhomm $
309  $Revision: 1.1 $
310  $Date: 2000/04/23 20:43:13 $
311  $Locker: $
312 */
313 
Count theProductCreates
The count of creates.
Definition: Builder.hpp:295
Builder(void)
Default constructor not supported.
Definition: Builder.hpp:239
Builder & operator=(const Builder &aRef)
Operation assignment.
Definition: Builder.hpp:108
virtual CountCref getProductDestroys(void) const
Retrieve the product destroy counts.
Definition: Builder.hpp:161
void incrementCreates(void)
Increment the creates.
Definition: Builder.hpp:255
virtual void destroy(ProductImpl *aPtr)
Default destroy routine invokes the implementation destroyProduct method.
Definition: Builder.hpp:209
virtual AbstractFactory< UniqueId > * getFactory(void) const
Retrieve the AbstractFactory.
Definition: Builder.hpp:168
ProductImpl * theCurrentProduct
The product that was most recently built.
Definition: Builder.hpp:287
Count theProductDestroys
The count of destroys.
Definition: Builder.hpp:299
virtual ProductImpl * createProduct(void) const =0
Pure virtual createProduct.
virtual ~Builder(void)
Virtual destructor.
Definition: Builder.hpp:92
virtual void destroyProduct(ProductImpl *) const =0
Pure virtual destroyProduct.
AbstractFactory< UniqueId > * theFactory
The factory for creating parts.
Definition: Builder.hpp:291
bool operator==(const Builder &aRef)
Equality operator.
Definition: Builder.hpp:134
throw(Assertion)
Copy constructor creates a new instance of the reference abstract factory.
Definition: Builder.hpp:80
virtual ProductImpl * create(void)
Default create routine invokes the implementation createProduct method.
Definition: Builder.hpp:181
Assertion is-a Exception created when an assertion fails.
Definition: Assertion.hpp:423
virtual ProductImpl * getCurrentProduct(void) const
Retrieves the current product.
Definition: Builder.hpp:147
void incrementDestroys(void)
Increment the destroys.
Definition: Builder.hpp:262
AbstractFactory provides an interface for creating families of related or dependent objects without s...
Definition: AbstractFactory.hpp:60
Builder seperates the construction of a complex object from its representation so that the same const...
Definition: Builder.hpp:44
virtual CountCref getProductCreates(void) const
Retrieve the product create counts.
Definition: Builder.hpp:154

This is the CoreLinux++ reference manual
Provided by The CoreLinux Consortium