repr 0.1
Reconstructable string representations and more
Loading...
Searching...
No Matches
reftuple.h
Go to the documentation of this file.
1#pragma once
2#include <utility>
3#include <cstddef>
4#include <memory>
5#include <librepr/util/util.h>
6#include "list.h"
7
8namespace librepr {
9template <typename... Types>
10class RefTuple {
11private:
12 void* data[sizeof...(Types)]{};
13
14public:
15 static constexpr auto size = sizeof...(Types);
16 using types = TypeList<Types...>;
17
18 template <detail::ref_convertible_to<Types>... U>
19 requires (sizeof...(U) != 1 || !(std::same_as<RefTuple, std::remove_cvref_t<U>> || ...))
20 explicit RefTuple(U&&... values) noexcept((noexcept(detail::convert_ref<Types>(std::forward<U>(values))) && ...))
21 : data{static_cast<void*>(const_cast<std::remove_cvref_t<Types>*>(
22 std::addressof(detail::convert_ref<Types>(std::forward<U>(values)))))...} {}
23
29
30 template <std::size_t Idx>
33 using return_type = typename types::template get<Idx>;
34 return *static_cast<std::remove_reference_t<return_type>*>(data[Idx]);
35 }
36};
37
38template <>
39class RefTuple<> {
40public:
41 RefTuple() = default;
42};
43
44template <typename... Types>
46
47namespace detail {
49 template <typename... Ts>
50 auto operator()(Ts&&... refs) const {
51 return RefTuple{std::forward<Ts>(refs)...};
52 }
53};
54} // namespace detail
55
56constexpr inline auto make_reftuple = detail::make_reftuple_impl{};
57
58template <std::size_t Idx, typename... Ts>
59constexpr decltype(auto) get(RefTuple<Ts...> const& tuple) noexcept {
60 return tuple.template get<Idx>();
61}
62
63} // namespace librepr
64
65template <std::size_t I, typename... Ts>
66struct std::tuple_element<I, librepr::RefTuple<Ts...>> {
67 using type = typename librepr::TypeList<Ts...>::template get<I>;
68};
69
70template <typename... Ts>
71struct std::tuple_size<librepr::RefTuple<Ts...>> : std::integral_constant<std::size_t, sizeof...(Ts)> {};
Definition reftuple.h:10
RefTuple(RefTuple &&) noexcept=default
static constexpr auto size
Definition reftuple.h:15
constexpr decltype(auto) get() const noexcept
Definition reftuple.h:32
RefTuple(U &&... values) noexcept((noexcept(detail::convert_ref< Types >(std::forward< U >(values))) &&...))
Definition reftuple.h:20
Definition ctvi/ctvi.h:9
constexpr auto make_reftuple
Definition reftuple.h:56
std::string code_for()
Definition repr:39
RefTuple(Types &...) -> RefTuple< Types... >
constexpr decltype(auto) get(RefTuple< Ts... > const &tuple) noexcept
Definition reftuple.h:59
STL namespace.
Definition list.h:64
Definition reftuple.h:48
auto operator()(Ts &&... refs) const
Definition reftuple.h:50