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 
14 #include <boost/optional.hpp>
15 
16 namespace iroha {
17  namespace to_string {
18  namespace detail {
19  const std::string kBeginBlockMarker = "[";
20  const std::string kEndBlockMarker = "]";
21  const std::string kSingleFieldsSeparator = ", ";
22  const std::string kNotSet = "(not set)";
23 
25  template <typename T>
26  inline std::string toStringDereferenced(const T &o);
27  } // namespace detail
28 
29  inline std::string toString(const std::string &o) {
30  return o;
31  }
32 
33  template <typename T>
34  inline auto toString(const T &o) -> std::enable_if_t<
35  std::is_same<decltype(std::to_string(o)), std::string>::value,
36  std::string> {
37  return std::to_string(o);
38  }
39 
40  template <typename T>
41  inline auto toString(const T &o) -> std::enable_if_t<
42  std::is_same<typename std::decay_t<decltype(o.toString())>,
43  std::string>::value,
44  std::string> {
45  return o.toString();
46  }
47 
48  template <typename... T>
49  inline std::string toString(const std::reference_wrapper<T...> &o) {
51  }
52 
53  template <typename... T>
54  inline std::string toString(const std::optional<T...> &o) {
56  }
57 
58  template <typename... T>
59  inline std::string toString(const std::unique_ptr<T...> &o) {
61  }
62 
63  template <typename... T>
64  inline std::string toString(const std::shared_ptr<T...> &o) {
66  }
67 
68  template <typename T>
69  inline std::string toString(const T *o) {
71  }
72 
73  template <typename T>
74  inline auto toString(const T &o) -> std::enable_if_t<
75  boost::optional_detail::is_optional_related<T>::value,
76  std::string> {
78  }
79 
81  template <typename T, typename = decltype(*std::declval<T>().begin())>
82  inline std::string toString(const T &c) {
83  std::string result = detail::kBeginBlockMarker;
84  bool need_field_separator = false;
85  for (auto &o : c) {
86  if (need_field_separator) {
87  result.append(detail::kSingleFieldsSeparator);
88  }
89  result.append(toString(o));
90  need_field_separator = true;
91  }
92  result.append(detail::kEndBlockMarker);
93  return result;
94  }
95 
96  namespace detail {
98  template <typename T>
99  inline std::string toStringDereferenced(const T &o) {
100  if (o) {
102  } else {
103  return kNotSet;
104  }
105  }
106 
107  template <>
108  inline std::string toStringDereferenced<boost::none_t>(
109  const boost::none_t &) {
110  return kNotSet;
111  }
112 
113  template <>
114  inline std::string toStringDereferenced<std::nullopt_t>(
115  const std::nullopt_t &) {
116  return kNotSet;
117  }
118  } // namespace detail
119  } // namespace to_string
120 } // namespace iroha
121 
122 #endif
const std::string kSingleFieldsSeparator
Definition: to_string.hpp:21
std::string toString(const T &c)
Print a plain collection.
Definition: to_string.hpp:82
const std::string kNotSet
Definition: to_string.hpp:22
const std::string kEndBlockMarker
Definition: to_string.hpp:20
Definition: block_query.hpp:15
std::string toStringDereferenced(const T &o)
Print pointers and optionals.
Definition: to_string.hpp:99
std::string toString(const std::string &o)
Definition: to_string.hpp:29
const std::string kBeginBlockMarker
Definition: to_string.hpp:19