DocWire SDK
DocWire SDK: Award-winning modern data processing in C++20. SourceForge Community Choice & Microsoft support. AI-driven processing. Supports nearly 100 data formats, including email boxes and OCR. Boost efficiency in text extraction, web data extraction, data mining, document analysis. Offline processing possible for security and confidentiality
stringification.h
1 /*********************************************************************************************************************************************/
2 /* DocWire SDK: Award-winning modern data processing in C++20. SourceForge Community Choice & Microsoft support. AI-driven processing. */
3 /* Supports nearly 100 data formats, including email boxes and OCR. Boost efficiency in text extraction, web data extraction, data mining, */
4 /* document analysis. Offline processing possible for security and confidentiality */
5 /* */
6 /* Copyright (c) SILVERCODERS Ltd, http://silvercoders.com */
7 /* Project homepage: https://github.com/docwire/docwire */
8 /* */
9 /* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-DocWire-Commercial */
10 /*********************************************************************************************************************************************/
11 
12 #ifndef DOCWIRE_STRINGIFICATION_H
13 #define DOCWIRE_STRINGIFICATION_H
14 
15 #include "concepts_misc.h"
16 #include "concepts_stream.h"
17 #include "concepts_string.h"
18 #include "diagnostic_message.h"
19 #include "named.h"
20 #include "serialization_base.h"
21 #include <string>
22 #include <sstream>
23 #include <type_traits>
24 
25 namespace docwire
26 {
27 
32 template <typename T>
33 struct stringifier;
34 
35 template <typename T>
36 std::string stringify(const T& value)
37 {
38  return stringifier<T>()(value);
39 }
40 
44 template <streamable T>
45 requires (!string_method_equipped<T>) // Avoid conflict if T also has a string() method
46 struct stringifier<T>
47 {
48  std::string operator()(const T& value) const
49  {
50  std::ostringstream s;
51  s << value;
52  return s.str();
53  }
54 };
55 
59 template <string_method_equipped T>
60 struct stringifier<T>
61 {
62  std::string operator()(const T& value) const { return std::string(value.string()); }
63 };
64 
76 template <typename T>
77 requires (!string_method_equipped<T> && !streamable<T> && !strong_type_alias<T>)
78 struct stringifier<T>
79 {
80  std::string operator()(const T& value) const
81  {
82  return stringify(serialization::full(value));
83  }
84 };
85 
86 template<>
87 struct stringifier<const char*>
88 {
89  std::string operator()(const char* value) const { return value; }
90 };
91 
95 template <>
96 struct stringifier<std::exception_ptr>
97 {
98  std::string operator()(const std::exception_ptr& eptr) const { return errors::diagnostic_message(eptr); }
99 };
100 
101 // Specialization for std::pair, providing a custom string representation
102 template <typename T1, typename T2>
103 struct stringifier<std::pair<T1, T2>>
104 {
105  std::string operator()(const std::pair<T1, T2>& pair) const
106  {
107  return stringify(pair.first) + ": " + stringify(pair.second);
108  }
109 };
110 
114 template <typename T>
115 struct stringifier<named::value<T>>
116 {
117  std::string operator()(const named::value<T>& nv) const
118  {
119  return stringify(nv.name) + ": " + stringify(nv.value);
120  }
121 };
122 
123 template <strong_type_alias T>
124 requires (
125  !std::is_same_v<T, serialization::object> &&
126  !std::is_same_v<T, serialization::array>)
127 struct stringifier<T>
128 {
129  std::string operator()(const T& value) const { return stringify(value.v); }
130 };
131 
132 template<>
133 struct stringifier<std::string>
134 {
135  std::string operator()(const std::string& value) const { return value; }
136 };
137 
138 template<>
139 struct stringifier<serialization::object>
140 {
141  std::string operator()(const serialization::object& obj) const
142  {
143  return stringify(serialization::value{obj});
144  }
145 };
146 
147 template<>
148 struct stringifier<serialization::value>
149 {
150  std::string operator()(const serialization::value& s_val) const
151  {
152  return std::visit(
153  [](auto&& arg) -> std::string {
154  using T = std::decay_t<decltype(arg)>;
155  if constexpr (std::is_same_v<T, serialization::object>)
156  {
157  std::string result = "{";
158  bool first = true;
159  for (const auto& [key, val] : arg.v)
160  {
161  if (!first) result += ", ";
162  result += key + ": " + stringify(val);
163  first = false;
164  }
165  result += "}";
166  return result;
167  }
168  else if constexpr (std::is_same_v<T, serialization::array>)
169  {
170  std::string result = "[";
171  bool first = true;
172  for (const auto& val : arg.v)
173  {
174  if (!first) result += ", ";
175  result += stringify(val);
176  first = false;
177  }
178  result += "]";
179  return result;
180  }
181  else if constexpr (std::is_same_v<T, std::nullptr_t>)
182  {
183  return "nullptr";
184  }
185  else if constexpr (std::is_same_v<T, bool>)
186  {
187  return arg ? "true" : "false";
188  }
189  else
190  {
191  return stringify(arg);
192  }
193  },
194  s_val);
195  }
196 };
197 
198 } // namespace docwire
199 
200 #endif
DOCWIRE_CORE_EXPORT std::string diagnostic_message(const std::exception &e)
Generates a diagnostic message for the given nested exceptions chain.
std::variant< std::nullptr_t, bool, std::int64_t, std::uint64_t, double, std::string, array, object > value
A variant type representing any serialized value.
value full(const T &value)
Serializes a value of type T into a docwire::serialization::value.
The main namespace for the DocWire SDK.
Definition: ai_elements.h:19
requires(!string_method_equipped< T >) struct stringifier< T >
Specialization for types that are streamable to std::ostream.
A named value wrapper.
Definition: named.h:28
T value
The value of the parameter.
Definition: named.h:32
std::string_view name
The name of the parameter.
Definition: named.h:30
Represents a serialized object (map of string keys to values).
Specialization for types with a string() method.
Primary template for the stringifier.