Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_AbstractFactoryStd.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef TEUCHOS_ABSTRACT_FACTORY_STD_HPP
43 #define TEUCHOS_ABSTRACT_FACTORY_STD_HPP
44 
45 #include "Teuchos_AbstractFactory.hpp"
46 
47 namespace Teuchos {
48 
52 template<class T_impl>
54 public:
56  void initialize(T_impl* /* p */) const {} // required!
57 };
58 
62 template<class T_impl>
63 class AllocatorNew {
64 public:
66  typedef Teuchos::RCP<T_impl> ptr_t; // required!
68  const ptr_t allocate() const { return Teuchos::rcp(new T_impl()); } // required!
69 };
70 
138 template<class T_itfc, class T_impl
139  ,class T_PostMod = PostModNothing<T_impl>
140  ,class T_Allocator = AllocatorNew<T_impl>
141  >
142 class AbstractFactoryStd : public AbstractFactory<T_itfc> {
143 public:
144 
145  typedef typename Teuchos::AbstractFactory<T_itfc>::obj_ptr_t obj_ptr_t; // RAB: 20030916: G++ 3.2 complains without this
146 
148  AbstractFactoryStd( const T_PostMod& post_mod = T_PostMod(), const T_Allocator& alloc = T_Allocator() );
149 
153  obj_ptr_t create() const;
155 
156 private:
157  T_PostMod post_mod_;
158  T_Allocator alloc_;
159 
160 };
161 
162 
167 template<class T_itfc, class T_impl>
170 {
171  return rcp(
173  );
174 }
175 
176 
181 template<class T_itfc, class T_impl, class T_Allocator>
183 abstractFactoryStd( const T_Allocator& alloc = T_Allocator() )
184 {
185  return rcp(
186  new AbstractFactoryStd<T_itfc,T_impl,PostModNothing<T_impl>,T_Allocator>(
187  PostModNothing<T_impl>(), alloc
188  )
189  );
190 }
191 
192 
193 // ///////////////////////////////////////////////////////
194 // Template member definitions
195 
196 template<class T_itfc, class T_impl, class T_PostMod, class T_Allocator>
197 inline
199  const T_PostMod& post_mod, const T_Allocator& alloc
200  )
201  :post_mod_(post_mod)
202  ,alloc_(alloc)
203 {}
204 
205 template<class T_itfc, class T_impl, class T_PostMod, class T_Allocator>
206 inline
207 typename AbstractFactoryStd<T_itfc,T_impl,T_PostMod,T_Allocator>::obj_ptr_t
209 {
210  typename T_Allocator::ptr_t
211  ptr = alloc_.allocate();
212  post_mod_.initialize(ptr.get());
213  return ptr;
214 }
215 
216 } // end Teuchos
217 
218 #endif // TEUCHOS_ABSTRACT_FACTORY_STD_HPP
Simple, templated concrete subclass of universal "Abstract Factory" interface for the creation of o...
RCP< const AbstractFactory< T_itfc > > abstractFactoryStd()
Nonmember constructor for an standar abstract factory object.
AbstractFactoryStd(const T_PostMod &post_mod=T_PostMod(), const T_Allocator &alloc=T_Allocator())
RCP< const AbstractFactory< T_itfc > > abstractFactoryStd(const T_Allocator &alloc=T_Allocator())
Nonmember constructor for an standar abstract factory object.
Simple, universal "Abstract Factory" interface for the dynamic creation of objects.
Default allocation policy class for AbstractFactoryStd which returns new T_impl().
Default post-modification policy class for AbstractFactorStd which does nothing!
Smart reference counting pointer class for automatic garbage collection.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.