Cbeam
Loading...
Searching...
No Matches
nested_map.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2025 acrion innovations GmbH
3Authors: Stefan Zipproth, s.zipproth@acrion.ch
4
5This file is part of Cbeam, see https://github.com/acrion/cbeam and https://cbeam.org
6
7Cbeam is offered under a commercial and under the AGPL license.
8For commercial licensing, contact us at https://acrion.ch/sales. For AGPL licensing, see below.
9
10AGPL licensing:
11
12Cbeam is free software: you can redistribute it and/or modify
13it under the terms of the GNU Affero General Public License as published by
14the Free Software Foundation, either version 3 of the License, or
15(at your option) any later version.
16
17Cbeam is distributed in the hope that it will be useful,
18but WITHOUT ANY WARRANTY; without even the implied warranty of
19MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20GNU Affero General Public License for more details.
21
22You should have received a copy of the GNU Affero General Public License
23along with Cbeam. If not, see <https://www.gnu.org/licenses/>.
24*/
25
26#pragma once
27
29#include <cbeam/convert/map.hpp> // for cbeam::convert::is_nested_map
30#include <cbeam/convert/string.hpp> // for cbeam::convert::indent
31
32#include <ostream> // for std::basic_ostream, std::endl, std::ostringstream
33#include <string> // for std::char_traits, std::string, std::operator<<
34#include <type_traits> // for std::decay_t, std::enable_if
35
36namespace cbeam::convert
37{
38 template <typename Key, typename Value>
39 inline std::string to_string(const container::nested_map<Key, Value>& map, const int indentation);
40
41 template <typename Key, typename Value>
42 inline std::string to_string(container::nested_map<Key, Value>& map, const int indentation);
43
57 template <typename Key, typename Value>
58 inline std::string to_string(const std::map<Key, container::nested_map<Key, Value>>& nested_maps, const int indentation)
59 {
60 std::ostringstream os;
61 for (const auto& it : nested_maps)
62 {
63 os << indent(indentation) << to_string(it.first) << std::endl
64 << to_string<Key, Value>(it.second, indentation + 1);
65 }
66 return os.str();
67 }
68
81 template <typename Key, typename Value>
82 inline std::string to_string(const std::map<Key, container::nested_map<Key, Value>>& nested_maps)
83 {
84 return to_string<Key, Value>(nested_maps, 0);
85 }
86
99 template <typename Key, typename Value>
100 inline std::string to_string(const container::nested_map<Key, Value>& map, const int indentation)
101 {
102 return to_string(map.data, indentation + 1)
103 + to_string(map.sub_tables, indentation + 1);
104 }
105
117 template <typename Key, typename Value>
118 inline std::string to_string(const container::nested_map<Key, Value>& map)
119 {
120 return to_string(map, -1);
121 }
122
134 template <typename T>
135 inline typename std::enable_if<is_nested_map<T>::value, std::string>::type to_string(const T& table)
136 {
137 using DataTableType = typename std::decay_t<decltype(table)>;
138 using KeyType = typename DataTableType::key_type;
139 using ValueType = typename DataTableType::mapped_type;
140
142 }
143}
Contains conversion utilities to transform data between different formats and types....
Definition buffer.hpp:35
std::string to_string(const container::buffer &b)
Creates a std::string from the contents of a container::buffer.
Definition buffer.hpp:42
std::string indent(int indentation)
Returns a string consisting of indentation tab characters.
Definition string.hpp:54
A map structure that can store nested maps of keys and values. By including serialization/nested_map....
Definition nested_map.hpp:44
nested_tables sub_tables
list of sub tables, each having a unique name
Definition nested_map.hpp:61
table_of_values data
key value pairs of Value instances that store the actual data
Definition nested_map.hpp:60