Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_CloneFactory.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Teko: A package for block and physics based preconditioning
4 //
5 // Copyright 2010 NTESS and the Teko contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef __Teko_CloneFactory_hpp__
11 #define __Teko_CloneFactory_hpp__
12 
13 #include "Teuchos_RCP.hpp"
14 
15 namespace Teko {
16 
18 class Cloneable {
19  public:
20  virtual ~Cloneable() {}
21 
29  virtual Teuchos::RCP<Cloneable> clone() const = 0;
30 };
31 
35 class AutoCloneDummy {};
36 
55 template <class CloneType, class BaseType = AutoCloneDummy>
56 class AutoClone : public Cloneable, public BaseType {
57  public:
58  virtual ~AutoClone() {}
59 
63  AutoClone() : BaseType() {}
64 
72  virtual Teuchos::RCP<Cloneable> clone() const {
73  return Teuchos::rcp(new AutoClone<CloneType, CloneType>());
74  }
75 
76  private:
77  // hidden
79 };
80 
90 template <class CloneBaseType>
91 class CloneFactory {
92  public:
95 
98 
99  virtual ~CloneFactory() {}
100 
115  virtual Teuchos::RCP<CloneBaseType> build(const std::string& str) const {
116  std::map<std::string, Teuchos::RCP<const Cloneable> >::const_iterator itr =
117  parentClones_.find(str);
118  if (itr == parentClones_.end()) return Teuchos::null;
119  return Teuchos::rcp_dynamic_cast<CloneBaseType>(itr->second->clone(), true);
120  }
121 
131  virtual void addClone(const std::string& str, const Teuchos::RCP<Cloneable>& clone) {
132  parentClones_[str] = clone;
133  }
134 
136  virtual int cloneCount() const { return parentClones_.size(); }
137 
142  void getCloneNames(std::vector<std::string>& names) const {
143  std::map<std::string, Teuchos::RCP<const Cloneable> >::const_iterator itr;
144  for (itr = parentClones_.begin(); itr != parentClones_.end(); ++itr)
145  names.push_back(itr->first);
146  }
147 
148  protected:
150  std::map<std::string, Teuchos::RCP<const Cloneable> > parentClones_;
151 };
152 
153 } // namespace Teko
154 
155 #endif
virtual int cloneCount() const
Return the number of clones stored in this factory.
void getCloneNames(std::vector< std::string > &names) const
CloneFactory()
Default constructor.
virtual void addClone(const std::string &str, const Teuchos::RCP< Cloneable > &clone)
CloneFactory(const CloneFactory< CloneBaseType > &cf)
Copy constructor.
std::map< std::string, Teuchos::RCP< const Cloneable > > parentClones_
stores the clonable objects
virtual Teuchos::RCP< CloneBaseType > build(const std::string &str) const
virtual Teuchos::RCP< Cloneable > clone() const
virtual Teuchos::RCP< Cloneable > clone() const =0