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 namespace Sacado {
00046 
00047   // Forward declaration
00048   template <typename TypeSeq, typename BaseT, typename ObjectT>
00049   class TemplateIterator;
00050   template <typename TypeSeq, typename BaseT, typename ObjectT>
00051   class ConstTemplateIterator;
00052 
00054 
00074   template <typename TypeSeq, typename BaseT, typename ObjectT>
00075   class TemplateManager {
00076 
00078     struct type_info_less {
00079       bool operator() (const std::type_info* a, const std::type_info* b) {
00080         return a->before(*b);
00081       }
00082     };
00083 
00084     template <typename BuilderOpT>
00085     struct BuildObject {
00086       mutable std::vector< Teuchos::RCP<BaseT> >& objects;
00087       const BuilderOpT& builder;
00088       BuildObject(std::vector< Teuchos::RCP<BaseT> >& objects_,
00089                   const BuilderOpT& builder_) :
00090         objects(objects_), builder(builder_) {}
00091       template <typename T> void operator()(T) const {
00092         int idx = mpl::find<TypeSeq,T>::value;
00093         objects[idx] = builder.template build<T>();
00094       }
00095     };
00096 
00098     friend class TemplateIterator<TypeSeq,BaseT,ObjectT>;
00099 
00100   public:
00101 
00103     typedef TemplateIterator<TypeSeq,BaseT,ObjectT> iterator;
00104 
00106     typedef ConstTemplateIterator<TypeSeq,BaseT,ObjectT> const_iterator;
00107 
00109     struct DefaultBuilderOp {
00110 
00112       template<class ScalarT>
00113       Teuchos::RCP<BaseT> build() const {
00114         typedef typename Sacado::mpl::apply<ObjectT,ScalarT>::type type;
00115         return Teuchos::rcp( dynamic_cast<BaseT*>( new type ) );
00116       }
00117 
00118     };
00119 
00121     TemplateManager();
00122 
00124     ~TemplateManager();
00125 
00127     template <typename BuilderOpT>
00128     void buildObjects(const BuilderOpT& builder);
00129 
00131     void buildObjects();
00132 
00134     template<typename ScalarT>
00135     Teuchos::RCP<BaseT> getAsBase();
00136 
00138     template<typename ScalarT>
00139     Teuchos::RCP<const BaseT> getAsBase() const;
00140 
00142     template<typename ScalarT>
00143     Teuchos::RCP< typename Sacado::mpl::apply<ObjectT,ScalarT>::type > getAsObject();
00144 
00146     template<typename ScalarT>
00147     Teuchos::RCP< const typename Sacado::mpl::apply<ObjectT,ScalarT>::type > getAsObject() const;
00148 
00150     typename Sacado::TemplateManager<TypeSeq,BaseT,ObjectT>::iterator begin();
00151 
00153     typename Sacado::TemplateManager<TypeSeq,BaseT,ObjectT>::const_iterator 
00154     begin() const;
00155 
00157     typename Sacado::TemplateManager<TypeSeq,BaseT,ObjectT>::iterator end();
00158 
00160     typename Sacado::TemplateManager<TypeSeq,BaseT,ObjectT>::const_iterator 
00161     end() const;
00162 
00163   private:
00164 
00166     std::vector< Teuchos::RCP<BaseT> > objects;
00167 
00168   };
00169 
00170 }
00171 
00172 // Include template definitions
00173 #include "Sacado_TemplateManagerImp.hpp"
00174 
00175 // Include template iterator definition
00176 #include "Sacado_TemplateIterator.hpp"
00177 
00178 #endif

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