Sacado_TemplateManager.hpp

Go to the documentation of this file.
00001 // $Id$ 
00002 // $Source$ 
00003 // @HEADER
00004 // ***********************************************************************
00005 // 
00006 //                           Sacado Package
00007 //                 Copyright (2006) Sandia Corporation
00008 // 
00009 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00010 // the U.S. Government retains certain rights in this software.
00011 // 
00012 // This library is free software; you can redistribute it and/or modify
00013 // it under the terms of the GNU Lesser General Public License as
00014 // published by the Free Software Foundation; either version 2.1 of the
00015 // License, or (at your option) any later version.
00016 //  
00017 // This library is distributed in the hope that it will be useful, but
00018 // WITHOUT ANY WARRANTY; without even the implied warranty of
00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 // Lesser General Public License for more details.
00021 //  
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License along with this library; if not, write to the Free Software
00024 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00025 // USA
00026 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
00027 // (etphipp@sandia.gov).
00028 // 
00029 // ***********************************************************************
00030 // @HEADER
00031 
00032 #ifndef SACADO_TEMPLATEMANAGER_HPP
00033 #define SACADO_TEMPLATEMANAGER_HPP
00034 
00035 #include <vector>
00036 #include <typeinfo>
00037 
00038 #include "Teuchos_RCP.hpp"
00039 
00040 #include "Sacado_mpl_size.hpp"
00041 #include "Sacado_mpl_find.hpp"
00042 #include "Sacado_mpl_for_each.hpp"
00043 #include "Sacado_mpl_apply.hpp"
00044 
00045 #include "Sacado_TemplateIterator.hpp"
00046 
00047 namespace Sacado {
00048 
00050 
00070   template <typename TypeSeq, typename BaseT, typename ObjectT>
00071   class TemplateManager {
00072 
00074     struct type_info_less {
00075       bool operator() (const std::type_info* a, const std::type_info* b) {
00076         return a->before(*b);
00077       }
00078     };
00079 
00080     template <typename BuilderOpT>
00081     struct BuildObject {
00082       mutable std::vector< Teuchos::RCP<BaseT> >& objects;
00083       const BuilderOpT& builder;
00084       BuildObject(std::vector< Teuchos::RCP<BaseT> >& objects_,
00085                   const BuilderOpT& builder_) :
00086         objects(objects_), builder(builder_) {}
00087       template <typename T> void operator()(T) const {
00088         int idx = mpl::find<TypeSeq,T>::value;
00089         objects[idx] = builder.template build<T>();
00090       }
00091     };
00092 
00093   public:
00094 
00096     typedef TemplateIterator<BaseT> iterator;
00097 
00099     typedef ConstTemplateIterator<BaseT> const_iterator;
00100 
00102     struct DefaultBuilderOp {
00103 
00105       template<class ScalarT>
00106       Teuchos::RCP<BaseT> build() const {
00107         typedef typename Sacado::mpl::apply<ObjectT,ScalarT>::type type;
00108         return Teuchos::rcp( dynamic_cast<BaseT*>( new type ) );
00109       }
00110 
00111     };
00112 
00114     TemplateManager();
00115 
00117     ~TemplateManager();
00118 
00120     template <typename BuilderOpT>
00121     void buildObjects(const BuilderOpT& builder);
00122 
00124     void buildObjects();
00125 
00127     template<typename ScalarT>
00128     Teuchos::RCP<BaseT> getAsBase();
00129 
00131     template<typename ScalarT>
00132     Teuchos::RCP<const BaseT> getAsBase() const;
00133 
00135     template<typename ScalarT>
00136     Teuchos::RCP< typename Sacado::mpl::apply<ObjectT,ScalarT>::type > getAsObject();
00137 
00139     template<typename ScalarT>
00140     Teuchos::RCP< const typename Sacado::mpl::apply<ObjectT,ScalarT>::type > getAsObject() const;
00141 
00143     typename Sacado::TemplateManager<TypeSeq,BaseT,ObjectT>::iterator begin();
00144 
00146     typename Sacado::TemplateManager<TypeSeq,BaseT,ObjectT>::const_iterator 
00147     begin() const;
00148 
00150     typename Sacado::TemplateManager<TypeSeq,BaseT,ObjectT>::iterator end();
00151 
00153     typename Sacado::TemplateManager<TypeSeq,BaseT,ObjectT>::const_iterator 
00154     end() const;
00155 
00156   private:
00157 
00159     std::vector< Teuchos::RCP<BaseT> > objects;
00160 
00161   };
00162 
00163 }
00164 
00165 // Include template definitions
00166 #include "Sacado_TemplateManagerImp.hpp"
00167 
00168 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 10:19:36 2011 for Sacado Package Browser (Single Doxygen Collection) by  doxygen 1.6.3