Ginkgo Generated from branch based on main. Ginkgo version 1.9.0
A numerical linear algebra library targeting many-core architectures
 
Loading...
Searching...
No Matches
property_tree.hpp
1// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#ifndef GKO_PUBLIC_CORE_CONFIG_PROPERTY_TREE_HPP_
6#define GKO_PUBLIC_CORE_CONFIG_PROPERTY_TREE_HPP_
7
8
9#include <cstdint>
10#include <limits>
11#include <map>
12#include <stdexcept>
13#include <string>
14#include <type_traits>
15#include <vector>
16
17
18namespace gko {
19namespace config {
20
21
28class pnode final {
29public:
30 using key_type = std::string;
31 using map_type = std::map<key_type, pnode>;
32 using array_type = std::vector<pnode>;
33
37 enum class tag_t { empty, array, boolean, real, integer, string, map };
38
42 explicit pnode();
43
49 explicit pnode(bool boolean);
50
58 template <typename T,
59 std::enable_if_t<std::is_integral<T>::value>* = nullptr>
60 explicit pnode(T integer);
61
67 explicit pnode(const std::string& str);
68
74 explicit pnode(const char* str);
75
81 explicit pnode(double real);
82
88 explicit pnode(const array_type& array);
89
95 explicit pnode(const map_type& map);
96
100 explicit operator bool() const noexcept;
101
107 tag_t get_tag() const;
108
115 const array_type& get_array() const;
116
123 const map_type& get_map() const;
124
132 bool get_boolean() const;
133
141 std::int64_t get_integer() const;
142
149 double get_real() const;
150
157 const std::string& get_string() const;
158
169 const pnode& get(const std::string& key) const;
170
179 const pnode& get(int index) const;
180
181private:
182 void throw_if_not_contain(tag_t tag) const;
183
184 static const pnode& empty_node();
185
186 tag_t tag_;
187 array_type array_; // for array
188 map_type map_; // for map
189 // value
190 std::string str_;
191 union {
192 std::int64_t integer_;
193 double real_;
194 bool boolean_;
195 } union_data_;
196};
197
198
199template <typename T, std::enable_if_t<std::is_integral<T>::value>*>
200pnode::pnode(T integer) : tag_(tag_t::integer)
201{
202 if (integer > std::numeric_limits<std::int64_t>::max() ||
203 (std::is_signed<T>::value &&
204 integer < std::numeric_limits<std::int64_t>::min())) {
205 throw std::runtime_error("The input is out of the range of int64_t.");
206 }
207 union_data_.integer_ = static_cast<std::int64_t>(integer);
208}
209
210
211} // namespace config
212} // namespace gko
213
214#endif // GKO_PUBLIC_CORE_CONFIG_PROPERTY_TREE_HPP_
tag_t
tag_t is the indicator for the current node storage.
Definition property_tree.hpp:37
pnode(const array_type &array)
Constructor for array.
pnode(const map_type &map)
Constructor for map.
bool get_boolean() const
Access the boolean value stored in this property node.
const map_type & get_map() const
Access the map stored in this property node.
double get_real() const
Access the real floating point value stored in this property node.
const pnode & get(const std::string &key) const
This function is to access the data under the map.
pnode(double real)
Constructor for double (and also float)
const std::string & get_string() const
Access the string stored in this property node.
pnode(const std::string &str)
Constructor for string.
pnode(bool boolean)
Constructor for bool.
tag_t get_tag() const
Get the current node tag.
pnode()
Default constructor: create an empty node.
const array_type & get_array() const
Access the array stored in this property node.
pnode(const char *str)
Constructor for char* (otherwise, it will use bool)
std::int64_t get_integer() const
The Ginkgo namespace.
Definition abstract_factory.hpp:20
STL namespace.