LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
StdUtils.h
Go to the documentation of this file.
1 
8 #ifndef LARCOREALG_COREUTILS_STDUTILS_H
9 #define LARCOREALG_COREUTILS_STDUTILS_H
10 
11 // LArSoft libraries
12 #include "larcorealg/CoreUtils/MetaUtils.h" // util::is_basic_string_type_v ...
13 
14 // C/C++ standard libraries
15 #include <iterator> // std::begin(), std::end(), ...
16 #include <string> // std::to_string()
17 #include <utility> // std::get()
18 
19 namespace util {
20 
59 
62  template <typename T>
63  constexpr decltype(auto) to_string(T&& obj);
64  // { using std::to_string; return to_string(std::forward<T>(obj)); }
65 
66  // --- BEGIN --- Containers and iterators ------------------------------------
68  template <typename T>
69  constexpr decltype(auto) begin(T&& obj)
70  {
71  using std::begin;
72  return begin(std::forward<T>(obj));
73  }
74 
76  template <typename T>
77  constexpr decltype(auto) end(T&& obj)
78  {
79  using std::end;
80  return end(std::forward<T>(obj));
81  }
82 
84  template <typename T>
85  constexpr decltype(auto) cbegin(T&& obj)
86  {
87  using std::cbegin;
88  return cbegin(std::forward<T>(obj));
89  }
90 
92  template <typename T>
93  constexpr decltype(auto) cend(T&& obj)
94  {
95  using std::cend;
96  return cend(std::forward<T>(obj));
97  }
98 
100  template <typename T>
101  constexpr decltype(auto) size(T&& obj)
102  {
103  using std::size;
104  return size(std::forward<T>(obj));
105  }
106 
108  template <typename T>
109  constexpr decltype(auto) empty(T&& obj)
110  {
111  using std::empty;
112  return empty(std::forward<T>(obj));
113  }
114 
115  // --- END --- Containers and iterators --------------------------------------
116 
117  // --- BEGIN --- tuples ------------------------------------------------------
118 
119  template <std::size_t I, typename T>
120  decltype(auto) get(T&& obj)
121  {
122  using std::get;
123  return get<I>(std::forward<T>(obj));
124  }
125 
126  // --- END --- tuples --------------------------------------------------------
127 
129 
130 } // namespace util
131 
132 // -----------------------------------------------------------------------------
133 // --- template implementation
134 // -----------------------------------------------------------------------------
135 namespace util::details {
136 
137  // ---------------------------------------------------------------------------
138  template <typename T, typename = void>
139  struct ToStringImpl {
140 
141  template <typename U>
142  static std::string to_string(U&& obj)
143  {
144  using std::to_string;
145  return to_string(std::forward<U>(obj));
146  }
147 
148  }; // struct ToStringImpl
149 
150  // ---------------------------------------------------------------------------
151  template <typename T>
152  struct ToStringImpl<T, std::enable_if_t<util::is_basic_string_type_v<T>>> {
153 
154  template <typename U>
155  static std::string to_string(U&& obj)
156  {
157  return obj;
158  }
159 
160  }; // struct ToStringImpl<string>
161 
162  // ---------------------------------------------------------------------------
163  template <typename T>
164  struct ToStringImpl<T, std::enable_if_t<util::is_basic_string_view_type_v<T>>> {
165 
166  template <typename U>
167  static std::string to_string(U&& obj)
168  {
169  return {obj.begin(), obj.end()};
170  }
171 
172  }; // struct ToStringImpl<string_view>
173 
174  // ---------------------------------------------------------------------------
175 
176 } // namespace util::details
177 
178 // -----------------------------------------------------------------------------
179 template <typename T>
180 constexpr decltype(auto) util::to_string(T&& obj)
181 {
182  return util::details::ToStringImpl<T>::to_string(std::forward<T>(obj));
183 }
184 // { using std::to_string; return to_string(std::forward<T>(obj)); }
185 
186 // -----------------------------------------------------------------------------
187 
188 #endif // LARCOREALG_COREUTILS_STDUTILS_H
Namespace for general, non-LArSoft-specific utilities.
Definition: PIDAAlg.h:26
decltype(auto) constexpr cend(T &&obj)
ADL-aware version of std::cend.
Definition: StdUtils.h:93
Basic C++ metaprogramming utilities.
STL namespace.
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:77
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:101
decltype(auto) constexpr to_string(T &&obj)
ADL-aware version of std::to_string.
static std::string to_string(U &&obj)
Definition: StdUtils.h:142
decltype(auto) get(T &&obj)
ADL-aware version of std::to_string.
Definition: StdUtils.h:120
decltype(auto) constexpr cbegin(T &&obj)
ADL-aware version of std::cbegin.
Definition: StdUtils.h:85
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:69
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
Definition: StdUtils.h:109