Wolframe, 0.0.3

propertyTree.hpp
Go to the documentation of this file.
1 /************************************************************************
2 
3  Copyright (C) 2011 - 2014 Project Wolframe.
4  All rights reserved.
5 
6  This file is part of Project Wolframe.
7 
8  Commercial Usage
9  Licensees holding valid Project Wolframe Commercial licenses may
10  use this file in accordance with the Project Wolframe
11  Commercial License Agreement provided with the Software or,
12  alternatively, in accordance with the terms contained
13  in a written agreement between the licensee and Project Wolframe.
14 
15  GNU General Public License Usage
16  Alternatively, you can redistribute this file and/or modify it
17  under the terms of the GNU General Public License as published by
18  the Free Software Foundation, either version 3 of the License, or
19  (at your option) any later version.
20 
21  Wolframe is distributed in the hope that it will be useful,
22  but WITHOUT ANY WARRANTY; without even the implied warranty of
23  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  GNU General Public License for more details.
25 
26  You should have received a copy of the GNU General Public License
27  along with Wolframe. If not, see <http://www.gnu.org/licenses/>.
28 
29  If you have questions regarding the use of this file, please contact
30  Project Wolframe.
31 
32 ************************************************************************/
35 
36 #ifndef _WOLFRAME_PROPERTY_TREE_HPP_INCLUDED
37 #define _WOLFRAME_PROPERTY_TREE_HPP_INCLUDED
38 #include "utils/fileLineInfo.hpp"
39 #include <string>
40 #include <vector>
41 #include <cstdlib>
42 #include <cstring>
43 #include <sstream>
44 #include <iostream>
45 #include <boost/property_tree/ptree.hpp>
46 #include <boost/shared_ptr.hpp>
47 #include <boost/algorithm/string.hpp>
48 
49 namespace _Wolframe {
50 namespace utils {
51  struct PrintFormat;
52 }
53 namespace types {
54 
58 {
59 public:
64 
67  class Value
68  :public std::string
69  {
70  public:
72  Value(){}
74  Value( const std::string& token, Position position_=Position())
75  :std::string(token),position(position_){}
77  Value( const Value& o)
78  :std::string(o),position(o.position){}
79 
81  std::string string() const {return *this;}
82  };
83 
86  class Node
87  {
88  public:
89  typedef std::pair<std::string,Node> Assignment;
90 
91  public:
93  explicit Node( const Position& pos=Position())
94  {
95  m_position = pos;
96  }
97 
99  explicit Node( const std::string& val, const Position& pos=Position())
100  :m_position(pos),m_data(val){}
101 
103  explicit Node( const boost::property_tree::ptree& pt);
104 
106  Node( const Node& o);
107 
109  void setValue( const std::string& val) {m_data = val;}
110 
112  void setPosition( const Position& p) {m_position = p;}
113 
115  void recursiveSetFileName( const FileName& filename)
116  {
117  recursiveSetFileName( *this, filename);
118  }
119 
121  std::string tostring( const utils::PrintFormat* pformat=0) const;
122 
125  const Node& getChild( const std::string& nodename) const;
126 
129  std::vector<const Node*> getChildren( const std::string& nodename) const;
130 
133  Node getChildrenJoined( const std::string& nodename) const;
134 
136  void add_child( const std::string& name_, const Node& node_)
137  {
138  m_subnodes.push_back( Assignment( name_, node_));
139  }
140 
141  bool empty() const {return m_subnodes.empty();}
142  const std::string& data() const {return m_data;}
143  const Position& position() const {return m_position;}
144 
145  typedef std::vector<Assignment>::const_iterator const_iterator;
146  typedef std::vector<Assignment>::iterator iterator;
147 
148  std::vector<Assignment>::const_iterator begin() const {return m_subnodes.begin();}
149  std::vector<Assignment>::const_iterator end() const {return m_subnodes.end();}
150  std::vector<Assignment>::iterator begin() {return m_subnodes.begin();}
151  std::vector<Assignment>::iterator end() {return m_subnodes.end();}
152 
153  private:
155  static void print( std::ostringstream& out, const Node& nd, int indent, const utils::PrintFormat* pformat=0);
156 
158  static void recursiveSetFileName( Node& pt, const FileName& filename);
159 
160  private:
161  std::vector<Assignment> m_subnodes;
163  std::string m_data;
164  };
165 
166 public:
171  :m_root(o.m_root){}
172 
174  PropertyTree( const boost::property_tree::ptree& o, const std::string& filename_)
175  :m_root(o)
176  {
178  }
179 
181  PropertyTree( const Node& root_)
182  :m_root(root_){}
183 
185  const Node& root() const {return m_root;}
189  Node::const_iterator end() const {return m_root.end();}
193  Node::iterator end() {return m_root.end();}
194 
196  std::string tostring( const utils::PrintFormat* pformat=0) const
197  {
198  return m_root.tostring( pformat);
199  }
200 
201 private:
203 };
204 
205 }}//namespace
206 #endif
207 
208 
209 
PropertyTree(const boost::property_tree::ptree &o, const std::string &filename_)
Constructor.
Definition: propertyTree.hpp:174
Defines a data structure to hold positional info in a source file for error messsages etc...
std::vector< Assignment > m_subnodes
child node list
Definition: propertyTree.hpp:161
PropertyTree()
Constructor.
Definition: propertyTree.hpp:168
const Node & root() const
Get the root node of the tree.
Definition: propertyTree.hpp:185
std::pair< std::string, Node > Assignment
Definition: propertyTree.hpp:89
void setValue(const std::string &val)
Set the value of the node.
Definition: propertyTree.hpp:109
Property tree node.
Definition: propertyTree.hpp:86
Value(const std::string &token, Position position_=Position())
Constructor.
Definition: propertyTree.hpp:74
Position m_position
position of node definition start in source
Definition: propertyTree.hpp:162
void recursiveSetFileName(const FileName &filename)
Set the file name of the definition of this node in source recursively for all sub nodes where the fi...
Definition: propertyTree.hpp:115
Position position
position of node definition start in source
Definition: propertyTree.hpp:80
const std::string & data() const
Definition: propertyTree.hpp:142
Property tree node value.
Definition: propertyTree.hpp:67
Hierarchical data structure for representing configuration data trees.
Definition: propertyTree.hpp:57
std::vector< const Node * > getChildren(const std::string &nodename) const
Get a all children nodes with the a certain name.
void setPosition(const Position &p)
Set the position of the node in source.
Definition: propertyTree.hpp:112
Structure with info for uniform printing for a type of message (log format as compact one liner...
Definition: printFormats.hpp:45
std::string tostring(const utils::PrintFormat *pformat=0) const
Get the property sub tree of this node as string.
Value()
Default constructor.
Definition: propertyTree.hpp:72
Node m_root
root node of the property tree
Definition: propertyTree.hpp:202
const Position & position() const
Definition: propertyTree.hpp:143
std::string string() const
cast to string (cast operator leads to problems here, so we define it with an explicit method call) ...
Definition: propertyTree.hpp:81
Value(const Value &o)
Copy constructor.
Definition: propertyTree.hpp:77
Node::const_iterator begin() const
Get the start iterator of the root node children.
Definition: propertyTree.hpp:187
PropertyTree(const Node &root_)
Constructor.
Definition: propertyTree.hpp:181
std::vector< Assignment >::const_iterator end() const
Definition: propertyTree.hpp:149
std::vector< Assignment >::const_iterator begin() const
Definition: propertyTree.hpp:148
std::vector< Assignment >::iterator iterator
Definition: propertyTree.hpp:146
Source line info with filename reference for error messages.
Definition: fileLineInfo.hpp:47
utils::FileLineInfo::FileName FileName
Filename of the source where the node was defined.
Definition: propertyTree.hpp:63
Node(const Position &pos=Position())
Constructor.
Definition: propertyTree.hpp:93
void add_child(const std::string &name_, const Node &node_)
Add child reference.
Definition: propertyTree.hpp:136
Node getChildrenJoined(const std::string &nodename) const
Get a all children nodes with the a certain name joined to one node.
std::string tostring(const utils::PrintFormat *pformat=0) const
Get this property tree as string.
Definition: propertyTree.hpp:196
std::vector< Assignment >::const_iterator const_iterator
Definition: propertyTree.hpp:145
Node::iterator begin()
Get the start iterator of the root node children.
Definition: propertyTree.hpp:191
std::vector< Assignment >::iterator end()
Definition: propertyTree.hpp:151
utils::FileLineInfo Position
Position in the source file where the node was defined.
Definition: propertyTree.hpp:61
Node::const_iterator end() const
Get the end iterator of the root node children.
Definition: propertyTree.hpp:189
boost::shared_ptr< char > FileName
Definition: fileLineInfo.hpp:51
Node(const std::string &val, const Position &pos=Position())
Constructor.
Definition: propertyTree.hpp:99
Node::iterator end()
Get the end iterator of the root node children.
Definition: propertyTree.hpp:193
bool empty() const
Definition: propertyTree.hpp:141
static void print(std::ostringstream &out, const Node &nd, int indent, const utils::PrintFormat *pformat=0)
Print the property sub tree of this node to a stream.
PropertyTree(const PropertyTree &o)
Copy constructor.
Definition: propertyTree.hpp:170
std::string m_data
node value
Definition: propertyTree.hpp:163
const Node & getChild(const std::string &nodename) const
Get a child node by name.
std::vector< Assignment >::iterator begin()
Definition: propertyTree.hpp:150
static FileName getFileName(const std::string &name)