Teuchos_AbstractFactoryStd.hpp

00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #ifndef TEUCHOS_ABSTRACT_FACTORY_STD_HPP
00030 #define TEUCHOS_ABSTRACT_FACTORY_STD_HPP
00031 
00032 #include "Teuchos_AbstractFactory.hpp"
00033 
00034 namespace Teuchos {
00035 
00039 template<class T_impl>
00040 class PostModNothing {
00041 public:
00043   void initialize(T_impl* p) const {} // required!
00044 };
00045 
00049 template<class T_impl>
00050 class AllocatorNew {
00051 public:
00053   typedef Teuchos::RCP<T_impl>  ptr_t;                         // required!
00055   const ptr_t allocate() const { return Teuchos::rcp(new T_impl()); }  // required!
00056 };
00057 
00125 template<class T_itfc, class T_impl
00126          ,class T_PostMod = PostModNothing<T_impl>
00127          ,class T_Allocator = AllocatorNew<T_impl>
00128         >
00129 class AbstractFactoryStd : public AbstractFactory<T_itfc> {
00130 public:
00131 
00132   typedef typename Teuchos::AbstractFactory<T_itfc>::obj_ptr_t   obj_ptr_t;  // RAB: 20030916: G++ 3.2 complains without this
00133 
00135   AbstractFactoryStd( const T_PostMod& post_mod = T_PostMod(), const T_Allocator& alloc = T_Allocator() );
00136 
00140   obj_ptr_t create() const;
00142 
00143 private:
00144   T_PostMod    post_mod_;
00145   T_Allocator  alloc_;
00146 
00147 };
00148 
00149 
00154 template<class T_itfc, class T_impl>
00155 RCP<const AbstractFactory<T_itfc> >
00156 abstractFactoryStd()
00157 {
00158   return rcp(
00159     new AbstractFactoryStd<T_itfc,T_impl,PostModNothing<T_impl>,AllocatorNew<T_impl> >()
00160     );
00161 }
00162 
00163 
00168 template<class T_itfc, class T_impl, class T_Allocator>
00169 RCP<const AbstractFactory<T_itfc> >
00170 abstractFactoryStd( const T_Allocator& alloc = T_Allocator() )
00171 {
00172   return rcp(
00173     new AbstractFactoryStd<T_itfc,T_impl,PostModNothing<T_impl>,T_Allocator>(
00174       PostModNothing<T_impl>(), alloc
00175       )
00176     );
00177 }
00178 
00179 
00180 // ///////////////////////////////////////////////////////
00181 // Template member definitions
00182 
00183 template<class T_itfc, class T_impl, class T_PostMod, class T_Allocator>
00184 inline
00185 AbstractFactoryStd<T_itfc,T_impl,T_PostMod,T_Allocator>::AbstractFactoryStd(
00186   const T_PostMod& post_mod, const T_Allocator& alloc
00187   )
00188   :post_mod_(post_mod)
00189   ,alloc_(alloc)
00190 {}
00191 
00192 template<class T_itfc, class T_impl, class T_PostMod, class T_Allocator>
00193 inline
00194 typename AbstractFactoryStd<T_itfc,T_impl,T_PostMod,T_Allocator>::obj_ptr_t
00195 AbstractFactoryStd<T_itfc,T_impl,T_PostMod,T_Allocator>::create() const
00196 {
00197   typename T_Allocator::ptr_t
00198     ptr = alloc_.allocate();
00199   post_mod_.initialize(ptr.get());
00200   return ptr;
00201 }
00202 
00203 } // end Teuchos
00204 
00205 #endif // TEUCHOS_ABSTRACT_FACTORY_STD_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 09:57:43 2011 for Teuchos - Trilinos Tools Package by  doxygen 1.6.3