Spicy
tuple.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <string>
6 #include <tuple>
7 #include <utility>
8 
9 #include <hilti/rt/extension-points.h>
10 #include <hilti/rt/util.h>
11 
12 namespace hilti::rt {
13 
14 namespace tuple {
15 
16 template<typename Tuple, size_t Idx>
17 ptrdiff_t elementOffset() {
18  // This is pretty certainly not well-defined, but seems to work for us ...
19  Tuple t; // requires all elements to be default constructable, which should be the case for us
20  return reinterpret_cast<const char*>(&std::get<Idx>(t)) - reinterpret_cast<const char*>(&t);
21 }
22 
23 } // namespace tuple
24 
25 namespace detail::adl {
26 template<typename... Ts>
27 inline std::string to_string(const std::tuple<Ts...>& x, adl::tag /*unused*/) {
28  auto y = rt::map_tuple(x, [&](auto& v) { return hilti::rt::to_string(v); });
29  return fmt("(%s)", rt::join_tuple_for_print(std::move(y)));
30 }
31 
32 template<typename... Ts>
33 inline std::string to_string_for_print(const std::tuple<Ts...>& x, adl::tag /*unused*/) {
34  auto y = rt::map_tuple(x, [&](auto& v) { return hilti::rt::to_string(v); });
35  return fmt("(%s)", rt::join_tuple_for_print(std::move(y)));
36 }
37 } // namespace detail::adl
38 
39 } // namespace hilti::rt
40 
41 namespace std {
42 
43 template<typename... Ts>
44 inline std::ostream& operator<<(std::ostream& out, const std::tuple<Ts...>& x) {
45  return out << hilti::rt::to_string_for_print(x);
46 }
47 
48 } // namespace std
std::string to_string(T &&x)
Definition: extension-points.h:26
std::string to_string_for_print(const T &x)
Definition: extension-points.h:45
Definition: any.h:7
Definition: optional.h:79
constexpr auto map_tuple(T &&tup, F f)
Definition: util.h:472
auto join_tuple_for_print(T &&tup)
Definition: util.h:494
std::string fmt(const char *fmt, const Args &... args)
Definition: fmt.h:13