opm-simulators
PropertyTree.hpp
1 /*
2  Copyright 2019 SINTEF Digital, Mathematics and Cybernetics.
3 
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef OPM_PROPERTYTREE_HEADER_INCLUDED
21 #define OPM_PROPERTYTREE_HEADER_INCLUDED
22 
23 #include <functional>
24 #include <iosfwd>
25 #include <memory>
26 #include <optional>
27 #include <string>
28 #include <vector>
29 
30 namespace boost::property_tree {
31  template<class T1, class T2, class T3> class basic_ptree;
33 } // namespace boost::property_tree
34 
35 namespace Opm {
36 
39 {
40 public:
44  PropertyTree();
45 
53  explicit PropertyTree(const std::string& jsonFile);
54 
58  PropertyTree(const PropertyTree& tree);
59 
61  ~PropertyTree();
62 
68  PropertyTree& operator=(const PropertyTree& tree);
69 
79  template<class T>
80  void put(const std::string& key, const T& data);
81 
91  template<class T>
92  T get(const std::string& key) const;
93 
107  template<class T>
108  T get(const std::string& key, const T& defValue) const;
109 
119  PropertyTree get_child(const std::string& key) const;
120 
129  std::optional<PropertyTree>
130  get_child_optional(const std::string& key) const;
131 
135  std::vector<std::string> get_child_keys() const;
136 
150  template <typename T>
151  std::optional<std::vector<T>>
152  get_child_items_as_vector(const std::string& child) const;
153 
162  void write_json(std::ostream& os, bool pretty) const;
163 
164 protected:
171 
173  std::unique_ptr<boost::property_tree::ptree> tree_;
174 };
175 
176 } // namespace Opm
177 
178 #endif // OPM_PROPERTYTREE_HEADER_INCLUDED
PropertyTree get_child(const std::string &key) const
Retrieve copy of sub tree rooted at node.
Definition: PropertyTree.cpp:82
~PropertyTree()
Destructor.
void put(const std::string &key, const T &data)
Insert key/value pair into property tree.
Definition: PropertyTree.cpp:71
std::unique_ptr< boost::property_tree::ptree > tree_
Internal representation of the property tree.
Definition: PropertyTree.hpp:173
void write_json(std::ostream &os, bool pretty) const
Emit a textual representation of the property tree in JSON form.
Definition: PropertyTree.cpp:76
PropertyTree & operator=(const PropertyTree &tree)
Assignment operator.
Definition: PropertyTree.cpp:133
PropertyTree()
Default constructor.
Definition: PropertyTree.cpp:35
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45
std::optional< PropertyTree > get_child_optional(const std::string &key) const
Retrieve copy of sub tree rooted at node.
Definition: PropertyTree.cpp:90
Definition: PropertyTree.hpp:31
std::optional< std::vector< T > > get_child_items_as_vector(const std::string &child) const
Retrieve node items as linearised vector.
Definition: PropertyTree.cpp:110
std::vector< std::string > get_child_keys() const
Retrieve all child keys of this property tree node.
Definition: PropertyTree.cpp:99
Definition: PropertyTree.hpp:30
Hierarchical collection of key/value pairs.
Definition: PropertyTree.hpp:38