Sacado_TemplateManager.hpp

Go to the documentation of this file.
00001 // $Id: Sacado_TemplateManager.hpp,v 1.4 2007/07/24 00:08:07 etphipp Exp $ 
00002 // $Source: /space/CVS/Trilinos/packages/sacado/src/template/Sacado_TemplateManager.hpp,v $ 
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 
00044 namespace Sacado {
00045 
00046   // Forward declaration
00047   template <typename TypeSeq, typename BaseT, template<typename> class ObjectT>
00048   class TemplateIterator;
00049   template <typename TypeSeq, typename BaseT, template<typename> class ObjectT>
00050   class ConstTemplateIterator;
00051 
00053 
00073   template <typename TypeSeq, typename BaseT, template<typename> class ObjectT>
00074   class TemplateManager {
00075 
00077     struct type_info_less {
00078       bool operator() (const std::type_info* a, const std::type_info* b) {
00079   return a->before(*b);
00080       }
00081     };
00082 
00083     template <typename BuilderOpT>
00084     struct BuildObject {
00085       mutable std::vector< Teuchos::RCP<BaseT> >& objects;
00086       const BuilderOpT& builder;
00087       BuildObject(std::vector< Teuchos::RCP<BaseT> >& objects_,
00088       const BuilderOpT& builder_) :
00089   objects(objects_), builder(builder_) {}
00090       template <typename T> void operator()(T) const {
00091   int idx = mpl::find<TypeSeq,T>::value;
00092   objects[idx] = builder.template build<T>();
00093       }
00094     };
00095 
00097     friend class TemplateIterator<TypeSeq,BaseT,ObjectT>;
00098 
00099   public:
00100 
00102     typedef TemplateIterator<TypeSeq,BaseT,ObjectT> iterator;
00103 
00105     typedef ConstTemplateIterator<TypeSeq,BaseT,ObjectT> const_iterator;
00106 
00108     struct DefaultBuilderOp {
00109 
00111       template<class ScalarT>
00112       Teuchos::RCP<BaseT> build() const {
00113   return Teuchos::rcp( dynamic_cast<BaseT*>( new ObjectT<ScalarT> ) );
00114       }
00115 
00116     };
00117 
00119     TemplateManager();
00120 
00122     ~TemplateManager();
00123 
00125     template <typename BuilderOpT>
00126     void buildObjects(const BuilderOpT& builder);
00127 
00129     void buildObjects();
00130 
00132     template<typename ScalarT>
00133     Teuchos::RCP<BaseT> getAsBase();
00134 
00136     template<typename ScalarT>
00137     Teuchos::RCP<const BaseT> getAsBase() const;
00138 
00140     template<typename ScalarT>
00141     Teuchos::RCP< ObjectT<ScalarT> > getAsObject();
00142 
00144     template<typename ScalarT>
00145     Teuchos::RCP< const ObjectT<ScalarT> > getAsObject() const;
00146 
00148     typename Sacado::TemplateManager<TypeSeq,BaseT,ObjectT>::iterator begin();
00149 
00151     typename Sacado::TemplateManager<TypeSeq,BaseT,ObjectT>::const_iterator 
00152     begin() const;
00153 
00155     typename Sacado::TemplateManager<TypeSeq,BaseT,ObjectT>::iterator end();
00156 
00158     typename Sacado::TemplateManager<TypeSeq,BaseT,ObjectT>::const_iterator 
00159     end() const;
00160 
00161   private:
00162 
00164     std::vector< Teuchos::RCP<BaseT> > objects;
00165 
00166   };
00167 
00168 }
00169 
00170 // Include template definitions
00171 #include "Sacado_TemplateManagerImp.hpp"
00172 
00173 // Include template iterator definition
00174 #include "Sacado_TemplateIterator.hpp"
00175 
00176 #endif

Generated on Wed May 12 21:59:06 2010 for Sacado Package Browser (Single Doxygen Collection) by  doxygen 1.4.7