Skip to content

Commit 7e81ca1

Browse files
committed
Changed stateful rebind to be done from inside types instead of from outside
1 parent aff277f commit 7e81ca1

File tree

5 files changed

+39
-64
lines changed

5 files changed

+39
-64
lines changed

include/kangaru/detail/cache.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,14 @@ namespace kangaru {
131131
std::ranges::swap(cache, other.cache);
132132
}
133133

134+
template<forwarded<with_cache> Original, forwarded_source NewSource>
135+
static constexpr auto rebind(Original&& original, NewSource&& new_source) -> with_cache<std::decay_t<NewSource>, source_reference_wrapper<unwrapped_cache_type>> {
136+
return with_cache<std::decay_t<NewSource>, source_reference_wrapper<unwrapped_cache_type>>{
137+
KANGARU5_FWD(new_source),
138+
KANGARU5_NO_ADL(ref)(original.cache)
139+
};
140+
}
141+
134142
private:
135143
template<typename To>
136144
static constexpr auto cast(detail::cache::adl_castable_to<To> auto&& any) -> To {

include/kangaru/detail/heap_storage.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,14 @@ namespace kangaru {
183183
return KANGARU5_NO_ADL(maybe_unwrap)(storage).emplace_from(function);
184184
}
185185

186+
template<forwarded<with_heap_storage> Original, forwarded_source NewSource>
187+
static constexpr auto rebind(Original&& original, NewSource&& new_source) -> with_heap_storage<std::decay_t<NewSource>, source_reference_wrapper<maybe_wrapped_t<Storage>>> {
188+
return with_heap_storage<std::decay_t<NewSource>, source_reference_wrapper<maybe_wrapped_t<Storage>>>{
189+
KANGARU5_FWD(new_source),
190+
KANGARU5_NO_ADL(ref)(original.storage)
191+
};
192+
}
193+
186194
private:
187195
template<pointer T> requires source_of<source_type, std::remove_pointer_t<T>>
188196
friend constexpr auto provide(forwarded<with_heap_storage> auto&& source) -> T {

include/kangaru/detail/recursive_source.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ namespace kangaru {
235235
}
236236
}
237237

238+
template<forwarded<with_construction> Original, forwarded_source NewSource>
239+
static constexpr auto rebind(Original&& original, NewSource&& new_source) -> with_construction<std::decay_t<NewSource>, Construction> {
240+
return with_construction<std::decay_t<NewSource>, Construction>{KANGARU5_FWD(new_source), original.construction};
241+
}
242+
238243
Source source;
239244

240245
private:

include/kangaru/detail/source_helper.hpp

Lines changed: 10 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -20,45 +20,18 @@ namespace kangaru {
2020
};
2121
};
2222

23-
template<template<typename, typename> typename Branch, kangaru::source Source, typename State>
24-
struct rebind_wrapper<Branch<Source, State>> {
25-
template<kangaru::source NewSource, typename NewState = State> requires requires { typename Branch<NewSource, NewState>; }
26-
struct ttype {
27-
using type = Branch<NewSource, NewState>;
28-
};
29-
};
30-
3123
template<template<typename> typename Branch, kangaru::source Source>
3224
struct rebind_wrapper<Branch<Source> const> {
3325
template<kangaru::source NewSource> requires requires { typename Branch<NewSource>; }
3426
struct ttype {
3527
using type = Branch<NewSource>;
3628
};
3729
};
38-
39-
template<template<typename, typename> typename Branch, kangaru::source Source, typename State>
40-
struct rebind_wrapper<Branch<Source, State> const> {
41-
template<kangaru::source NewSource, typename NewState = State> requires requires { typename Branch<NewSource, NewState>; }
42-
struct ttype {
43-
using type = Branch<NewSource, NewState>;
44-
};
45-
};
4630
} // namespace detail::source_helper
4731

4832
template<typename Source>
49-
concept weak_rebindable_wrapping_source =
33+
concept transparent_rebindable_wrapping_source =
5034
wrapping_source<Source>
51-
and requires {
52-
typename std::type_identity_t<
53-
typename detail::source_helper::rebind_wrapper<Source>::template ttype<
54-
wrapped_source_t<Source>
55-
>::type
56-
>;
57-
};
58-
59-
template<typename Source>
60-
concept stateless_rebindable_wrapping_source =
61-
weak_rebindable_wrapping_source<Source>
6235
and requires(Source source) {
6336
typename std::type_identity_t<
6437
typename detail::source_helper::rebind_wrapper<Source>::template ttype<
@@ -69,34 +42,24 @@ namespace kangaru {
6942
typename detail::source_helper::rebind_wrapper<Source>::template ttype<std::decay_t<decltype(source.source)>>::type,
7043
std::decay_t<decltype(source.source)>
7144
>;
72-
};;
45+
};
7346

7447
template<typename Source>
7548
concept stateful_rebindable_wrapping_source =
76-
weak_rebindable_wrapping_source<Source>
49+
wrapping_source<Source>
7750
and requires(Source source) {
78-
typename std::type_identity_t<
79-
typename detail::source_helper::rebind_wrapper<Source>::template ttype<
80-
std::decay_t<decltype(source.source)>,
81-
decltype(KANGARU5_NO_ADL(ref)(source))
82-
>::type
83-
>;
84-
requires std::constructible_from<
85-
typename detail::source_helper::rebind_wrapper<Source>::template ttype<
86-
std::decay_t<decltype(source.source)>,
87-
decltype(KANGARU5_NO_ADL(ref)(source))
88-
>::type,
89-
std::decay_t<decltype(source.source)>,
90-
decltype(KANGARU5_NO_ADL(ref)(source))
91-
>;
51+
Source::rebind(source, none_source{});
9252
};
9353

9454
template<typename Source>
9555
concept rebindable_wrapping_source =
96-
stateless_rebindable_wrapping_source<Source>
56+
transparent_rebindable_wrapping_source<Source>
9757
or stateful_rebindable_wrapping_source<Source>;
9858

9959
namespace detail::source_helper {
60+
// TODO: Design an interface where rebinding might branch off in composed sources
61+
// and allow rebinding with a transformed leaf instead of ignoring it.
62+
10063
// Forward declaration because otherwise the top overloads cannot find the bottom ones.
10164
template<kangaru::source Leaf> requires (not reference_wrapper<Leaf> and not rebindable_wrapping_source<Leaf>)
10265
constexpr auto rebind_source_tree(forwarded_source auto&& new_leaf, Leaf&) noexcept;
@@ -124,15 +87,8 @@ namespace kangaru {
12487
template<rebindable_wrapping_source Wrapper> requires (not reference_wrapper<Wrapper>)
12588
constexpr auto rebind_source_tree(forwarded_source auto&& new_leaf, Wrapper& source) noexcept {
12689
if constexpr (stateful_rebindable_wrapping_source<Wrapper>) {
127-
using rebound = typename detail::source_helper::rebind_wrapper<Wrapper>::template ttype<
128-
decltype(KANGARU5_NO_ADL(rebind_source_tree)(KANGARU5_FWD(new_leaf), source.source)),
129-
decltype(KANGARU5_NO_ADL(ref)(source))
130-
>::type;
131-
return rebound{
132-
KANGARU5_NO_ADL(rebind_source_tree)(KANGARU5_FWD(new_leaf), source.source),
133-
KANGARU5_NO_ADL(ref)(source),
134-
};
135-
} else if constexpr (stateless_rebindable_wrapping_source<Wrapper>) {
90+
return Wrapper::rebind(source, KANGARU5_NO_ADL(rebind_source_tree)(KANGARU5_FWD(new_leaf), source.source));
91+
} else if constexpr (transparent_rebindable_wrapping_source<Wrapper>) {
13692
using rebound = typename detail::source_helper::rebind_wrapper<Wrapper>::template ttype<
13793
decltype(KANGARU5_NO_ADL(rebind_source_tree)(KANGARU5_FWD(new_leaf), source.source))
13894
>::type;
@@ -143,16 +99,6 @@ namespace kangaru {
14399
static_assert(not std::same_as<Wrapper, Wrapper>, "exhaustive");
144100
}
145101
}
146-
147-
template<typename Source> requires (forwarded_source<Source> and not wrapping_source<std::remove_reference_t<Source>>)
148-
constexpr auto source_tree_leaf(Source&& source) -> auto&& {
149-
return KANGARU5_FWD(source);
150-
}
151-
152-
template<typename Source> requires (wrapping_source<std::remove_reference_t<Source>>)
153-
constexpr auto source_tree_leaf(Source&& source) -> auto&& {
154-
return KANGARU5_NO_ADL(source_tree_leaf)(KANGARU5_FWD(source).source);
155-
}
156102
} // namespace detail::source_helper
157103
} // namespace kangaru
158104

include/kangaru/detail/source_types.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,14 @@ namespace kangaru {
250250
return kangaru::provide<T>(KANGARU5_FWD(source).source);
251251
}
252252

253+
template<forwarded<with_alternative> Original, forwarded_source NewSource>
254+
static constexpr auto rebind(Original&& original, NewSource&& new_source) -> with_alternative<std::decay_t<NewSource>, source_reference_wrapper<maybe_wrapped_t<Alternative>>> {
255+
return with_alternative<std::decay_t<NewSource>, source_reference_wrapper<maybe_wrapped_t<Alternative>>>{
256+
KANGARU5_FWD(new_source),
257+
KANGARU5_NO_ADL(ref)(original.alternative)
258+
};
259+
}
260+
253261
private:
254262
Alternative alternative;
255263
};

0 commit comments

Comments
 (0)