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::RefCountPtr<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 /*
00134 #ifdef _MIPS_CXX
00135   typedef typename MemMngPack::AbstractFactory<T_itfc>::obj_ptr_t   obj_ptr_t;
00136     typedef T_PostMod                                                 post_mod_t;
00137     typedef T_Allocator                                               allocator_t;
00138 #endif
00139 */
00140 
00142   AbstractFactoryStd( const T_PostMod& post_mod = T_PostMod(), const T_Allocator& alloc = T_Allocator() );
00143 
00147   obj_ptr_t create() const;
00149 
00150 private:
00151   T_PostMod    post_mod_;
00152   T_Allocator  alloc_;
00153 
00154 }; // end class AbstractFactorStd
00155 
00157 template<class T_itfc, class T_impl, class T_Allocator >
00158 const Teuchos::RefCountPtr<const AbstractFactory<T_itfc> >
00159 abstract_factory_std_alloc(
00160   const T_Allocator&  alloc = T_Allocator()
00161   )
00162 {
00163   return rcp(
00164     new AbstractFactoryStd<T_itfc,T_impl,PostModNothing<T_impl>,T_Allocator>(
00165       PostModNothing<T_impl>(), alloc )
00166     );
00167 }
00168 
00169 // ///////////////////////////////////////////////////////
00170 // Template member definitions
00171 
00172 template<class T_itfc, class T_impl, class T_PostMod, class T_Allocator>
00173 inline
00174 AbstractFactoryStd<T_itfc,T_impl,T_PostMod,T_Allocator>::AbstractFactoryStd(
00175   const T_PostMod& post_mod, const T_Allocator& alloc
00176   )
00177   :post_mod_(post_mod)
00178   ,alloc_(alloc)
00179 {}
00180 
00181 template<class T_itfc, class T_impl, class T_PostMod, class T_Allocator>
00182 inline
00183 typename AbstractFactoryStd<T_itfc,T_impl,T_PostMod,T_Allocator>::obj_ptr_t
00184 AbstractFactoryStd<T_itfc,T_impl,T_PostMod,T_Allocator>::create() const
00185 {
00186   typename T_Allocator::ptr_t
00187     ptr = alloc_.allocate();
00188   post_mod_.initialize(ptr.get());
00189   return ptr;
00190 }
00191 
00192 } // end Teuchos
00193 
00194 #endif // TEUCHOS_ABSTRACT_FACTORY_STD_HPP

Generated on Thu Sep 18 12:39:09 2008 for Teuchos - Trilinos Tools Package by doxygen 1.3.9.1