Simpatico  v1.10
ParamComponent.h
1 #ifndef UTIL_PARAM_COMPONENT_H
2 #define UTIL_PARAM_COMPONENT_H
3 
4 /*
5 * Util Package - C++ Utilities for Scientific Computation
6 *
7 * Copyright 2010 - 2017, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include <util/mpi/MpiFileIo.h> // base class
12 #include <util/archives/Serializable.h> // base class (typedef)
13 #include <util/global.h>
14 
15 #include <iostream>
16 #include <sstream>
17 #include <string>
18 
19 namespace Util
20 {
21 
31  class ParamComponent : public Serializable, public MpiFileIo
32  {
33 
34  public:
35 
39  virtual ~ParamComponent();
40 
46  virtual void readParam(std::istream& in) = 0;
47 
53  virtual void writeParam(std::ostream& out) = 0;
54 
61  virtual void load(Serializable::IArchive &ar)
62  {}
63 
70  virtual void save(Serializable::OArchive &ar)
71  {}
72 
79  virtual void resetParam()
80  {}
81 
91  void setIndent(const ParamComponent& parent, bool next = true);
92 
96  std::string indent() const;
97 
104  template <class Archive>
105  void serialize(Archive& ar, const unsigned int version);
106 
107  // Public static methods
108 
112  static void initStatic();
113 
124  static void setEcho(bool echo = true);
125 
131  static bool echo();
132 
133  protected:
134 
144  ParamComponent();
145 
149  ParamComponent(const ParamComponent& other);
150 
151  private:
152 
154  std::string indent_;
155 
157  static bool echo_;
158 
159  // friend:
160 
161  #ifdef UTIL_MPI
162  template <class T> friend class Factory;
163 
164  /*
165  * Note: This friend declaration allows the Factory::readObject()
166  * method to set the Factory ioCommunicator to be that of the
167  * parent ParamComposite.
168  */
169  #endif
170 
171  };
172 
173  /*
174  * Serialize a ParamComponent as a string.
175  */
176  template <class Archive>
177  void ParamComponent::serialize(Archive& ar, const unsigned int version)
178  {
179  std::string str;
180  if (Archive::is_saving()) {
181  std::ostringstream buffer;
182  writeParam(buffer);
183  str = buffer.str();
184  }
185  ar & str;
186  if (Archive::is_loading()) {
187  std::istringstream buffer(str);
188  readParam(buffer);
189  }
190  }
191 
192 }
193 #endif
virtual void load(Serializable::IArchive &ar)
Load internal state from an archive.
virtual void writeParam(std::ostream &out)=0
Read parameter(s) to file.
virtual void resetParam()
Nontrivial implementation provided by ParamComposite subclass.
static void setEcho(bool echo=true)
Enable or disable echoing for all subclasses of ParamComponent.
void serialize(Archive &ar, const unsigned int version)
Serialize this ParamComponent as a string.
virtual ~ParamComponent()
Destructor.
File containing preprocessor macros for error handling.
static void initStatic()
Initialize static echo member to false.
Identifies whether this processor may do file I/O.
Definition: MpiFileIo.h:33
virtual void save(Serializable::OArchive &ar)
Save internal state to an archive.
Saving / output archive for binary ostream.
virtual void readParam(std::istream &in)=0
Read parameter(s) from file.
ParamComponent()
Constructor.
Utility classes for scientific computation.
Definition: accumulators.mod:1
static bool echo()
Get echo parameter.
Abstract base class for classes that input and ouput parameters to file.
void setIndent(const ParamComponent &parent, bool next=true)
Set indent level.
Saving archive for binary istream.
Abstract class for serializable objects.
Definition: Serializable.h:34
Factory template.
Definition: Factory.h:32
std::string indent() const
Return indent string for this object (string of spaces).