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 <memory>
10 #include <string>
11 
12 #include <boost/optional.hpp>
13 
14 namespace iroha {
15  namespace to_string {
16  namespace detail {
17  const std::string kBeginBlockMarker = "[";
18  const std::string kEndBlockMarker = "]";
19  const std::string kSingleFieldsSeparator = ", ";
20  const std::string kNotSet = "(not set)";
21 
23  template <typename T>
24  inline std::string toStringDereferenced(const T &o);
25  } // namespace detail
26 
27  inline std::string toString(const std::string &o) {
28  return o;
29  }
30 
31  template <typename T>
32  inline auto toString(const T &o) -> std::enable_if_t<
33  std::is_same<decltype(std::to_string(o)), std::string>::value,
34  std::string> {
35  return std::to_string(o);
36  }
37 
38  template <typename T>
39  inline auto toString(const T &o) -> std::enable_if_t<
40  std::is_same<typename std::decay_t<decltype(o.toString())>,
41  std::string>::value,
42  std::string> {
43  return o.toString();
44  }
45 
46  template <typename... T>
47  inline std::string toString(const std::unique_ptr<T...> &o) {
49  }
50 
51  template <typename... T>
52  inline std::string toString(const std::shared_ptr<T...> &o) {
54  }
55 
56  template <typename T>
57  inline std::string toString(const T *o) {
59  }
60 
61  template <typename T>
62  inline auto toString(const T &o) -> std::enable_if_t<
63  boost::optional_detail::is_optional_related<T>::value,
64  std::string> {
66  }
67 
69  template <typename T, typename = decltype(*std::declval<T>().begin())>
70  inline std::string toString(const T &c) {
71  std::string result = detail::kBeginBlockMarker;
72  bool need_field_separator = false;
73  for (auto &o : c) {
74  if (need_field_separator) {
75  result.append(detail::kSingleFieldsSeparator);
76  }
77  result.append(toString(o));
78  need_field_separator = true;
79  }
80  result.append(detail::kEndBlockMarker);
81  return result;
82  }
83 
84  namespace detail {
86  template <typename T>
87  inline std::string toStringDereferenced(const T &o) {
88  if (o) {
90  } else {
91  return kNotSet;
92  }
93  }
94 
95  template <>
96  inline std::string toStringDereferenced<boost::none_t>(
97  const boost::none_t &) {
98  return kNotSet;
99  }
100  } // namespace detail
101  } // namespace to_string
102 } // namespace iroha
103 
104 #endif
const std::string kSingleFieldsSeparator
Definition: to_string.hpp:19
std::string toString(const T &c)
Print a plain collection.
Definition: to_string.hpp:70
const std::string kNotSet
Definition: to_string.hpp:20
const std::string kEndBlockMarker
Definition: to_string.hpp:18
Definition: block_query.hpp:14
std::string toStringDereferenced(const T &o)
Print pointers and optionals.
Definition: to_string.hpp:87
std::string toString(const std::string &o)
Definition: to_string.hpp:27
const std::string kBeginBlockMarker
Definition: to_string.hpp:17