Teko Version of the Day
Teko_CloneFactory.hpp
00001 /*
00002 // @HEADER
00003 // 
00004 // ***********************************************************************
00005 // 
00006 //      Teko: A package for block and physics based preconditioning
00007 //                  Copyright 2010 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 // Redistribution and use in source and binary forms, with or without
00013 // modification, are permitted provided that the following conditions are
00014 // met:
00015 //  
00016 // 1. Redistributions of source code must retain the above copyright
00017 // notice, this list of conditions and the following disclaimer.
00018 //  
00019 // 2. Redistributions in binary form must reproduce the above copyright
00020 // notice, this list of conditions and the following disclaimer in the
00021 // documentation and/or other materials provided with the distribution.
00022 //  
00023 // 3. Neither the name of the Corporation nor the names of the
00024 // contributors may be used to endorse or promote products derived from
00025 // this software without specific prior written permission. 
00026 //  
00027 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00028 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00029 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00030 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00031 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00032 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00033 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00034 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00035 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
00036 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00037 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00038 //  
00039 // Questions? Contact Eric C. Cyr (eccyr@sandia.gov)
00040 // 
00041 // ***********************************************************************
00042 // 
00043 // @HEADER
00044 
00045 */
00046 
00047 #ifndef __Teko_CloneFactory_hpp__
00048 #define __Teko_CloneFactory_hpp__
00049 
00050 #include "Teuchos_RCP.hpp"
00051 
00052 namespace Teko {
00053 
00055 class Cloneable {
00056 public:
00057    virtual ~Cloneable() {}
00058 
00066    virtual Teuchos::RCP<Cloneable> clone() const = 0;
00067 };
00068 
00072 class AutoCloneDummy {};
00073 
00092 template <class CloneType,class BaseType=AutoCloneDummy>
00093 class AutoClone : public Cloneable, public BaseType {
00094 public:
00095    virtual ~AutoClone() {}
00096 
00100    AutoClone() 
00101       : BaseType()
00102    { }
00103    
00111    virtual Teuchos::RCP<Cloneable> clone() const
00112    { return Teuchos::rcp(new AutoClone<CloneType,CloneType>()); }
00113 
00114 private:
00115    // hidden
00116    AutoClone(const AutoClone<BaseType> & );
00117 };
00118 
00128 template <class CloneBaseType>
00129 class CloneFactory {
00130 public:
00132    CloneFactory() {}
00133 
00135    CloneFactory(const CloneFactory<CloneBaseType> & cf) : parentClones_(cf.parentClones_) {}
00136 
00137    virtual ~CloneFactory() {}
00138 
00153    virtual Teuchos::RCP<CloneBaseType> build(const std::string & str) const
00154    { 
00155       std::map<std::string,Teuchos::RCP<const Cloneable> >::const_iterator itr 
00156             = parentClones_.find(str);
00157       if(itr==parentClones_.end()) return Teuchos::null;
00158       return Teuchos::rcp_dynamic_cast<CloneBaseType>(itr->second->clone(),true); 
00159    }
00160 
00170    virtual void addClone(const std::string & str,const Teuchos::RCP<Cloneable> & clone)
00171    { parentClones_[str] = clone; }
00172 
00174    virtual int cloneCount() const
00175    { return parentClones_.size(); }
00176 
00181    void getCloneNames(std::vector<std::string> & names) const
00182    {
00183       std::map<std::string,Teuchos::RCP<const Cloneable> >::const_iterator itr;
00184       for(itr=parentClones_.begin();itr!=parentClones_.end();++itr)
00185          names.push_back(itr->first);
00186    }
00187 
00188 protected:
00190    std::map<std::string,Teuchos::RCP<const Cloneable> > parentClones_;
00191 };
00192 
00193 }
00194 
00195 #endif
 All Classes Files Functions Variables