QMCPACK
OhmmsElementBase.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: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
8 // Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
9 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
10 //
11 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
12 //////////////////////////////////////////////////////////////////////////////////////
13 
14 
15 #ifndef OHMMS_XMLDATA_H
16 #define OHMMS_XMLDATA_H
17 /**@file OhmmsElementBase.h
18  *@brief Declaration of OhmmsElementBase and define xml-related macros.
19  */
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23 
24 #include "OhmmsData/libxmldefs.h"
25 
26 /**\class OhmmsElementBase
27  *\brief Abstract class to provide xml-compatible I/O interfaces for the derived classes.
28  *
29  *Generic interfaces using std::iostream are much preferred. However,
30  *there isn't any pure c++ xml parser that is based on std::iostream alone.
31  *After evaluating several xml parsers, JK chose libxml
32  *(The XML C parser and toolkit of gnome, http://www.xmlsoft.org)
33  *based on its performance and availability on many platforms.
34  *
35  *The base class is written to be able to handle DTD or Schema in
36  *future. Current implementation assumes that each OhmmsElementBase
37  *object handles a node and its child nodes. However, it does not
38  *specify how the derived classes hanlde the child nodes.
39  */
41 {
42 public:
43  ///enumeration to choose the xml parser
44  enum
45  {
46  useLIBXML = 0, /*!< using libxml2 library */
47  useLIBXMLPP, /*!< using libxml++ library */
48  usePLAIN /*!< using ascii parser */
49  };
50 
51  ///constructor with a name
52  OhmmsElementBase(const char* aname = "none") : myIOMode(useLIBXML), myName(aname) {}
53 
54  ///destructor
55  virtual ~OhmmsElementBase() {}
56 
57  ///return the name
58  inline const std::string& getName() const { return myName; }
59 
60  ///set name
61  inline void setName(const std::string& aname) { myName = aname; }
62 
63  ///set iomode
64  inline void setIOMode(int imode) { myIOMode = imode; }
65 
66  ///write to a std::ostream
67  virtual bool get(std::ostream&) const = 0;
68 
69  ///read from std::istream
70  virtual bool put(std::istream&) = 0;
71 
72  ///read from an xmlNode
73  virtual bool put(xmlNodePtr cur) = 0;
74 
75  ///reset member data
76  virtual void reset() = 0;
77 
78  ///add a xmlNode to the children list of parent
79  virtual bool add(xmlNodePtr parent) { return true; }
80 
81  ///read from string
82  void put(const std::string& s)
83  {
84  std::istringstream stream(s);
85  put(stream);
86  }
87 
88  ///write the start of a node
89  virtual void begin_node(std::ostream& os) const {}
90 
91  ///write the end of a node
92  virtual void end_node(std::ostream& os) const {}
93 
94 protected:
95  ///the type of IO mode: default is useLIBXML
96  int myIOMode;
97 
98  ///the name of the node, corresponds to the xml tag
99  std::string myName;
100 };
101 
102 #endif
const std::string & getName() const
return the name
std::string myName
the name of the node, corresponds to the xml tag
void setName(const std::string &aname)
set name
A collection of put/get functions to read from or write to a xmlNode defined in libxml2.
void put(const std::string &s)
read from string
Abstract class to provide xml-compatible I/O interfaces for the derived classes.
virtual ~OhmmsElementBase()
destructor
int myIOMode
the type of IO mode: default is useLIBXML
virtual void begin_node(std::ostream &os) const
write the start of a node
virtual void reset()=0
reset member data
OhmmsElementBase(const char *aname="none")
constructor with a name
void setIOMode(int imode)
set iomode
virtual bool add(xmlNodePtr parent)
add a xmlNode to the children list of parent
virtual void end_node(std::ostream &os) const
write the end of a node
virtual bool put(std::istream &)=0
read from std::istream