hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
to_string.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_LIBS_TO_STRING_HPP
7 #define IROHA_LIBS_TO_STRING_HPP
8 
9 #include <functional>
10 #include <memory>
11 #include <optional>
12 #include <string>
13 #include <string_view>
14 
15 #include <boost/optional.hpp>
16 
17 namespace iroha {
18  namespace to_string {
19  namespace detail {
20  const std::string kBeginBlockMarker = "[";
21  const std::string kEndBlockMarker = "]";
22  const std::string kSingleFieldsSeparator = ", ";
23  const std::string kNotSet = "(not set)";
24 
26  template <typename T>
27  inline std::string toStringDereferenced(const T &o);
28  } // namespace detail
29 
30  inline std::string toString(std::string const &o) {
31  return o;
32  }
33 
34  inline std::string toString(std::string_view o) {
35  return std::string{o};
36  }
37 
38  template <typename T>
39  inline auto toString(const T &o) -> std::enable_if_t<
40  std::is_same<decltype(std::to_string(o)), std::string>::value,
41  std::string> {
42  return std::to_string(o);
43  }
44 
45  template <typename T>
46  inline auto toString(const T &o) -> std::enable_if_t<
47  std::is_same<typename std::decay_t<decltype(o.toString())>,
48  std::string>::value,
49  std::string> {
50  return o.toString();
51  }
52 
53  template <typename... T>
54  inline std::string toString(const std::reference_wrapper<T...> &o) {
56  }
57 
58  template <typename... T>
59  inline std::string toString(const std::optional<T...> &o) {
61  }
62 
63  template <typename... T>
64  inline std::string toString(const std::unique_ptr<T...> &o) {
66  }
67 
68  template <typename... T>
69  inline std::string toString(const std::shared_ptr<T...> &o) {
71  }
72 
73  template <typename T>
74  inline std::string toString(const T *o) {
76  }
77 
78  template <typename T>
79  inline auto toString(const T &o) -> std::enable_if_t<
80  boost::optional_detail::is_optional_related<T>::value,
81  std::string> {
83  }
84 
86  template <typename T, typename = decltype(*std::declval<T>().begin())>
87  inline std::string toString(const T &c) {
88  std::string result = detail::kBeginBlockMarker;
89  bool need_field_separator = false;
90  for (auto &o : c) {
91  if (need_field_separator) {
92  result.append(detail::kSingleFieldsSeparator);
93  }
94  result.append(toString(o));
95  need_field_separator = true;
96  }
97  result.append(detail::kEndBlockMarker);
98  return result;
99  }
100 
101  namespace detail {
103  template <typename T>
104  inline std::string toStringDereferenced(const T &o) {
105  if (o) {
107  } else {
108  return kNotSet;
109  }
110  }
111 
112  template <>
113  inline std::string toStringDereferenced<boost::none_t>(
114  const boost::none_t &) {
115  return kNotSet;
116  }
117 
118  template <>
119  inline std::string toStringDereferenced<std::nullopt_t>(
120  const std::nullopt_t &) {
121  return kNotSet;
122  }
123  } // namespace detail
124  } // namespace to_string
125 } // namespace iroha
126 
127 #endif
const std::string kSingleFieldsSeparator
Definition: to_string.hpp:22
std::string toString(const T &c)
Print a plain collection.
Definition: to_string.hpp:87
const std::string kNotSet
Definition: to_string.hpp:23
const std::string kEndBlockMarker
Definition: to_string.hpp:21
Definition: block_query.hpp:15
std::string toString(const ::iroha::utility_service::Status &val)
Definition: proto_status_tools.cpp:56
std::string toStringDereferenced(const T &o)
Print pointers and optionals.
Definition: to_string.hpp:104
const std::string kBeginBlockMarker
Definition: to_string.hpp:20