QMCPACK
IOBase.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Paul R. C. Kent, kentpr@ornl.gov, Oak Ridge National Laboratory
8 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
9 //
10 // File created by: Paul R. C. Kent, kentpr@ornl.gov, Oak Ridge National Laboratory
11 //////////////////////////////////////////////////////////////////////////////////////
12 
13 
14 // http://pathintegrals.info //
15 /////////////////////////////////////////////////////////////
16 
17 #ifndef INPUT_OUTPUT_BASE_H
18 #define INPUT_OUTPUT_BASE_H
19 
20 #include <string>
21 #include <list>
22 #include <stack>
23 
24 #include <fstream>
25 
26 #include "IOVar.h"
27 
28 namespace IO
29 {
30 /// This class stores a tree of input file sections. Each member of
31 /// the tree contains a list of tree nodes below it and a list of
32 /// variables contained in the present node.
34 {
35 protected:
36  // USE ME! I'm not being used yet.
37  bool IsModified;
39 
40 public:
41  std::list<IOVarBase*> VarList;
42  std::list<IOTreeClass*> SectionList;
43 
44  inline void MarkModified();
45  /// This is used to ensure proper ordering of sections in the HDF
46  /// version in which there is no guarantee that the sections will
47  /// come out of the file in the same order you put them in.
49  virtual void PrintTree() = 0;
50  virtual void PrintTree(int numIndent) = 0;
51  virtual IOFileType GetFileType() = 0;
52 
54  /// This is the empty std::string unless I'm the root node of some file.
55  std::string FileName;
56  std::string Name;
57  inline void InsertSection(IOTreeClass* newSec);
58  inline bool FindSection(std::string name, IOTreeClass*& sectionPtr, int num = 0);
59  inline int CountSections(std::string name);
60  inline int CountVars();
61 
62  template<class T>
63  bool ReadVar(std::string name, T& var)
64  {
65  bool readVarSuccess;
66  std::list<IOVarBase*>::iterator varIter = VarList.begin();
67  while ((varIter != VarList.end()) && ((*varIter)->GetName() != name))
68  varIter++;
69 
70  bool found = varIter != VarList.end();
71  if (found)
72  readVarSuccess = (*varIter)->Read(var);
73  else if (Parent != NULL)
74  readVarSuccess = Parent->ReadVar(name, var);
75  else
76  return false;
77 
78  return readVarSuccess;
79  }
80 
81  inline IOVarBase* GetVarPtr(std::string name)
82  {
83  std::list<IOVarBase*>::iterator iter = VarList.begin();
84  while ((iter != VarList.end()) && ((*iter)->GetName() != name))
85  {
86  iter++;
87  }
88  if (iter == VarList.end())
89  return NULL;
90  else
91  {
92  MarkModified(); // If we're getting the pointer, we're probably
93  // gonna modify the variable.
94  return *iter;
95  }
96  }
97  inline IOVarBase* GetVarPtr(int num)
98  {
99  std::list<IOVarBase*>::iterator iter = VarList.begin();
100  int i = 0;
101  while ((iter != VarList.end()) && (i != num))
102  {
103  iter++;
104  i++;
105  }
106  if (iter == VarList.end())
107  return NULL;
108  else
109  {
110  MarkModified(); // If we're getting the pointer, we're probably
111  // gonna modify the variable.
112  return *iter;
113  }
114  }
115 
116 
117  /// Write me!
118  virtual IOTreeClass* NewSection(std::string name) = 0;
119 
120  virtual bool OpenFile(std::string fileName, std::string mySectionName, IOTreeClass* parent) = 0;
121  virtual bool NewFile(std::string fileName, std::string mySectionName, IOTreeClass* parent) = 0;
122  /// Inserts a new Include directive in the present section.
123  virtual void IncludeSection(IOTreeClass*) = 0;
124  virtual void CloseFile() = 0;
125  virtual void FlushFile() = 0;
126 
127  /// These create a new variable with the given name and value:
128  template<typename T>
129  bool WriteVar(std::string name, T val);
130  template<typename T, int LEN>
131  bool WriteVar(std::string name, const TinyVector<T, LEN>& val);
132  template<typename T, int RANK>
133  bool WriteVar(std::string name, const Array<T, RANK>& val);
134  template<typename T, int RANK, int LEN>
135  bool WriteVar(std::string name, const Array<TinyVector<T, LEN>, RANK>& val);
136 
137 
138  /// Append a value to a variable of dimension of 1 higher than val.
139  /// i.e. Add a double to an blitz::Array<double,1> or add blitz::Array<double,1>
140  /// to an blitz::Array<double,2>, etc.
141  template<class T>
142  inline bool AppendVar(std::string name, T val);
143  inline void SetUnderscores(bool use) { UseUnderscores = use; }
144 
145  inline IOTreeClass() : UseUnderscores(false), FileName("")
146  {
147  // Nothing for now
148  }
149  virtual ~IOTreeClass(){}
150 };
151 
153 {
154  if (FileName != "")
155  IsModified = true;
156  else if (Parent != NULL)
157  Parent->MarkModified();
158 }
159 
160 
161 template<class T>
162 inline bool IOTreeClass::AppendVar(std::string name, T val)
163 {
164  IOVarBase* var = GetVarPtr(name);
165  if (var == NULL)
166  return false;
167  MarkModified();
168  int dim0 = var->GetExtent(0);
169  var->Resize(dim0 + 1);
170  return var->Write(val, 0);
171  // return var->Append(val);
172 }
173 
174 
175 /// Returns the number of subsections with the given name within the
176 /// present section.
177 inline int IOTreeClass::CountSections(std::string name)
178 {
179  std::list<IOTreeClass*>::iterator sectionIter;
180  sectionIter = SectionList.begin();
181  int numSections = 0;
182  while (sectionIter != SectionList.end())
183  {
184  if ((name == (*sectionIter)->Name) || (name == ""))
185  {
186  numSections++;
187  }
188  sectionIter++;
189  }
190  return numSections;
191 }
192 
194 {
195  std::list<IOVarBase*>::iterator varIter;
196  varIter = VarList.begin();
197  int numVars = 0;
198  while (varIter != VarList.end())
199  {
200  numVars++;
201  varIter++;
202  }
203  return numVars;
204 }
205 
206 
207 /// FindSection locates a subsection with the given name within the
208 /// section in contains and returns it in the pointer, sectionPtr,
209 /// which is passed a reference. Returns true if the section is
210 /// found. The final parameter, which default value "true",
211 /// optionally resets the section iterator to the beginning of the
212 /// section. Thus, one may control whether or not order is
213 /// significant.
214 inline bool IOTreeClass::FindSection(std::string name, IOTreeClass*& sectionPtr, int num)
215 {
216  std::list<IOTreeClass*>::iterator Iter = SectionList.begin();
217  int counter = 0;
218  while (counter <= num && Iter != SectionList.end())
219  {
220  if ((*Iter)->Name == name)
221  {
222  counter++;
223  }
224  if (counter <= num)
225  Iter++;
226  }
227  bool found = Iter != SectionList.end();
228  if (found)
229  {
230  sectionPtr = *Iter;
231  }
232  return (found);
233 }
234 
235 
237 {
238  std::list<IOTreeClass*>::iterator iter;
239 
240  if (SectionList.empty())
241  SectionList.push_back(newSec);
242  else
243  {
244  iter = SectionList.begin();
245  while ((iter != SectionList.end()) && ((*iter)->MyNumber < newSec->MyNumber))
246  iter++;
247  if (iter != SectionList.end())
248  SectionList.insert(iter, newSec);
249  else
250  SectionList.push_back(newSec);
251  }
252 }
253 
254 
255 } // namespace IO
256 
257 #endif
void InsertSection(IOTreeClass *newSec)
Definition: IOBase.h:236
std::string Name
Definition: IOBase.h:56
bool WriteVar(std::string name, T val)
These create a new variable with the given name and value:
Definition: IO.h:37
bool Write(const Array< T, RANK > &val, T0 s0, T1 s1, T2 s2, T3 s3, T4 s4, T5 s5, T6 s6, T7 s7, T8 s8, T9 s9, T10 s10)
Write functions ///.
Definition: IOVar.h:156
int CountSections(std::string name)
Returns the number of subsections with the given name within the present section. ...
Definition: IOBase.h:177
IOVarBase * GetVarPtr(int num)
Definition: IOBase.h:97
IOVarBase * GetVarPtr(std::string name)
Definition: IOBase.h:81
bool FindSection(std::string name, IOTreeClass *&sectionPtr, int num=0)
FindSection locates a subsection with the given name within the section in contains and returns it in...
Definition: IOBase.h:214
This class stores a tree of input file sections.
Definition: IOBase.h:33
bool IsModified
Definition: IOBase.h:37
void MarkModified()
Definition: IOBase.h:152
virtual bool OpenFile(std::string fileName, std::string mySectionName, IOTreeClass *parent)=0
IOFileType
Definition: IOVarBase.h:35
virtual int GetExtent(int dim)=0
Definition: IO.h:24
std::list< IOVarBase * > VarList
Definition: IOBase.h:41
void SetUnderscores(bool use)
Definition: IOBase.h:143
virtual void FlushFile()=0
virtual IOTreeClass * NewSection(std::string name)=0
Write me!
virtual void IncludeSection(IOTreeClass *)=0
Inserts a new Include directive in the present section.
bool ReadVar(std::string name, T &var)
Definition: IOBase.h:63
virtual IOFileType GetFileType()=0
IOTreeClass * Parent
Definition: IOBase.h:53
int CountVars()
Definition: IOBase.h:193
std::string FileName
This is the empty std::string unless I&#39;m the root node of some file.
Definition: IOBase.h:55
std::list< IOTreeClass * > SectionList
Definition: IOBase.h:42
int CurrSecNum
Definition: IOBase.h:48
virtual bool NewFile(std::string fileName, std::string mySectionName, IOTreeClass *parent)=0
int MyNumber
This is used to ensure proper ordering of sections in the HDF version in which there is no guarantee ...
Definition: IOBase.h:48
virtual void PrintTree()=0
virtual void CloseFile()=0
bool UseUnderscores
Definition: IOBase.h:38
bool AppendVar(std::string name, T val)
Append a value to a variable of dimension of 1 higher than val.
Definition: IOBase.h:162
virtual void Resize(int n)=0
Resizes the first dimension of the variable.
virtual ~IOTreeClass()
Definition: IOBase.h:149