Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 130 additions & 10 deletions include/slo/impl/union/recursive.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ union RecursiveUnion<T> {
template <typename... Args>
constexpr explicit RecursiveUnion(std::in_place_index_t<0>, Args&&... args) : value{std::forward<Args>(args)...} {}

[[nodiscard]] constexpr std::size_t index() const
requires(has_index<T>)
{
return value.tag;
}
[[nodiscard]] constexpr std::size_t index() const requires(has_index<T>) { return value.tag; }

template <std::size_t N, typename Self>
constexpr decltype(auto) get(this Self&& self) {
Expand All @@ -33,8 +29,7 @@ union RecursiveUnion<T> {
};

template <typename T0, typename... Ts>
requires(sizeof...(Ts) != 0)
union RecursiveUnion<T0, Ts...> {
requires(sizeof...(Ts) != 0) union RecursiveUnion<T0, Ts...> {
T0 alternative_0;
RecursiveUnion<Ts...> tail;

Expand All @@ -48,9 +43,7 @@ union RecursiveUnion<T0, Ts...> {
constexpr explicit RecursiveUnion(std::in_place_index_t<N>, Args&&... args)
: tail{std::in_place_index_t<N - 1>{}, std::forward<Args>(args)...} {}

[[nodiscard]] constexpr std::size_t index() const
requires(has_index<T0>)
{
[[nodiscard]] constexpr std::size_t index() const requires(has_index<T0>) {
if (compat::is_within_lifetime(&alternative_0.tag)) {
return alternative_0.tag;
} else {
Expand All @@ -72,4 +65,131 @@ union RecursiveUnion<T0, Ts...> {
}
};

template <typename T0, typename T1, typename... Ts>
requires(sizeof...(Ts) != 0) union RecursiveUnion<T0, T1, Ts...> {
T0 alternative_0;
T1 alternative_1;
RecursiveUnion<Ts...> tail;

constexpr ~RecursiveUnion() {}

template <typename... Args>
constexpr explicit RecursiveUnion(std::in_place_index_t<0>, Args&&... args)
: alternative_0{std::forward<Args>(args)...} {}

template <typename... Args>
constexpr explicit RecursiveUnion(std::in_place_index_t<1>, Args&&... args)
: alternative_1{std::forward<Args>(args)...} {}

template <std::size_t N, typename... Args>
constexpr explicit RecursiveUnion(std::in_place_index_t<N>, Args&&... args)
: tail{std::in_place_index_t<N - 2>{}, std::forward<Args>(args)...} {}

[[nodiscard]] constexpr std::size_t index() const requires(has_index<T0>&& has_index<T1>) {
if (compat::is_within_lifetime(&alternative_0.tag)) {
return alternative_0.tag;
} else if (compat::is_within_lifetime(&alternative_1.tag)) {
return alternative_1.tag;
} else {
return tail.index();
}
}

template <std::size_t N, typename Self>
constexpr decltype(auto) get(this Self&& self) {
if constexpr (N == 0) {
if constexpr (has_unwrap<T0>) {
return std::forward<Self>(self).alternative_0.unwrap();
} else {
return compat::forward_like<Self>(std::forward<Self>(self).alternative_0);
}
} else if constexpr (N == 1) {
if constexpr (has_unwrap<T1>) {
return std::forward<Self>(self).alternative_1.unwrap();
} else {
return compat::forward_like<Self>(std::forward<Self>(self).alternative_1);
}
} else {
return std::forward<Self>(self).tail.template get<N - 2>();
}
}
};

template <typename T0, typename T1, typename T2, typename T3, typename... Ts>
requires(sizeof...(Ts) != 0) union RecursiveUnion<T0, T1, T2, T3, Ts...> {
T0 alternative_0;
T1 alternative_1;
T2 alternative_2;
T3 alternative_3;
RecursiveUnion<Ts...> tail;

constexpr ~RecursiveUnion() {}

template <typename... Args>
constexpr explicit RecursiveUnion(std::in_place_index_t<0>, Args&&... args)
: alternative_0{std::forward<Args>(args)...} {}

template <typename... Args>
constexpr explicit RecursiveUnion(std::in_place_index_t<1>, Args&&... args)
: alternative_1{std::forward<Args>(args)...} {}

template <typename... Args>
constexpr explicit RecursiveUnion(std::in_place_index_t<2>, Args&&... args)
: alternative_2{std::forward<Args>(args)...} {}

template <typename... Args>
constexpr explicit RecursiveUnion(std::in_place_index_t<3>, Args&&... args)
: alternative_3{std::forward<Args>(args)...} {}

template <std::size_t N, typename... Args>
constexpr explicit RecursiveUnion(std::in_place_index_t<N>, Args&&... args)
: tail{std::in_place_index_t<N - 4>{}, std::forward<Args>(args)...} {}

[[nodiscard]] constexpr std::size_t index() const
requires(has_index<T0>&& has_index<T1>&& has_index<T2>&& has_index<T3>) {
if (compat::is_within_lifetime(&alternative_0.tag)) {
return alternative_0.tag;
} else if (compat::is_within_lifetime(&alternative_1.tag)) {
return alternative_1.tag;
} else if (compat::is_within_lifetime(&alternative_2.tag)) {
return alternative_2.tag;
} else if (compat::is_within_lifetime(&alternative_3.tag)) {
return alternative_3.tag;
} else {
return tail.index();
}
}

template <std::size_t N, typename Self>
constexpr decltype(auto) get(this Self&& self) {
if constexpr (N == 0) {
if constexpr (has_unwrap<T0>) {
return std::forward<Self>(self).alternative_0.unwrap();
} else {
return compat::forward_like<Self>(std::forward<Self>(self).alternative_0);
}
} else if constexpr (N == 1) {
if constexpr (has_unwrap<T1>) {
return std::forward<Self>(self).alternative_1.unwrap();
} else {
return compat::forward_like<Self>(std::forward<Self>(self).alternative_1);
}
} else if constexpr (N == 2) {
if constexpr (has_unwrap<T2>) {
return std::forward<Self>(self).alternative_2.unwrap();
} else {
return compat::forward_like<Self>(std::forward<Self>(self).alternative_2);
}
} else if constexpr (N == 3) {
if constexpr (has_unwrap<T3>) {
return std::forward<Self>(self).alternative_3.unwrap();
} else {
return compat::forward_like<Self>(std::forward<Self>(self).alternative_3);
}
} else {
return std::forward<Self>(self).tail.template get<N - 4>();
}
}
};

} // namespace slo::impl