Cbeam
Loading...
Searching...
No Matches
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
28#include <cbeam/convert/string.hpp> // for cbeam::convert::indent
29
30#include <ostream> // for std::basic_ostream, std::endl, std::ostringstream
31#include <string> // for std::char_traits, std::operator<<, std::string
32#include <type_traits> // for std::enable_if, std::false_type, std::true_type, std::void_t
33
34namespace cbeam::convert
35{
45 template <typename T, typename = void>
46 struct is_nested_map : std::false_type
47 {
48 };
49
50 template <typename T>
51 struct is_nested_map<T, std::void_t<typename T::nested_tables, typename T::table_of_values>> : std::true_type
52 {
53 };
54
64 template <typename T, typename = void>
65 struct has_key_and_mapped_type : std::false_type
66 {
67 };
68
69 template <typename T>
70 struct has_key_and_mapped_type<T, std::void_t<typename T::key_type, typename T::mapped_type>> : std::true_type
71 {
72 };
73
87 template <typename T>
88 inline typename std::enable_if<has_key_and_mapped_type<T>::value && !is_nested_map<T>::value, std::string>::type to_string(const T& table, const int indentation)
89 {
90 std::ostringstream os;
91 for (const auto& it : table)
92 {
93 os << indent(indentation) << to_string(it.first)
94 << indent(1) << to_string(it.second) << std::endl;
95 }
96 return os.str();
97 }
98
111 template <typename T>
112 inline typename std::enable_if<has_key_and_mapped_type<T>::value && !is_nested_map<T>::value, std::string>::type to_string(const T& table)
113 {
114 return to_string<T>(table, 0);
115 }
116}
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
Type trait to check if a given type supports key_type and mapped_type, similar to std::map.
Definition map.hpp:66
Type trait to check if a given type is a nested map.
Definition map.hpp:47