Skip to content

Commit 37cfc7d

Browse files
committed
Remove source pointer support from cache, cleaup aliases
1 parent 866121f commit 37cfc7d

File tree

9 files changed

+97
-84
lines changed

9 files changed

+97
-84
lines changed

include/kangaru/detail/cache.hpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "ctti.hpp"
77
#include "concepts.hpp"
88
#include "cache_types.hpp"
9-
#include "source_helper.hpp"
9+
#include "source_rebind.hpp"
1010

1111
#include <type_traits>
1212
#include <concepts>
@@ -36,7 +36,7 @@ namespace kangaru {
3636
using cache_type = Cache;
3737

3838
private:
39-
using unwrapped_cache_type = maybe_wrapped_t<cache_type>;
39+
using unwrapped_cache_type = std::remove_reference_t<maybe_unwrap_result_t<cache_type>>;
4040

4141
public:
4242
explicit constexpr with_cache_asymmetric(source_type source) noexcept
@@ -134,8 +134,8 @@ namespace kangaru {
134134
}
135135

136136
template<forwarded<with_cache_asymmetric> Original, forwarded_source NewLeaf>
137-
static constexpr auto rebind(Original&& original, NewLeaf&& new_leaf) noexcept -> with_cache_asymmetric<rebind_wrapped_source_result_t<Original, NewLeaf>, source_ref_t<Cache>, CacheFrom> {
138-
return with_cache_asymmetric<rebind_wrapped_source_result_t<Original, NewLeaf>, source_ref_t<Cache>, CacheFrom>{
137+
static constexpr auto rebind(Original&& original, NewLeaf&& new_leaf) noexcept -> with_cache_asymmetric<wrapped_source_rebind_result_t<Original, NewLeaf>, ref_result_t<Cache>, CacheFrom> {
138+
return with_cache_asymmetric<wrapped_source_rebind_result_t<Original, NewLeaf>, ref_result_t<Cache>, CacheFrom>{
139139
kangaru::rebind(KANGARU5_FWD(original).source, KANGARU5_FWD(new_leaf)),
140140
KANGARU5_NO_ADL(ref)(original.cache)
141141
};
@@ -207,9 +207,9 @@ namespace kangaru {
207207

208208
template<forwarded<with_cache> Original, forwarded_source NewLeaf>
209209
static constexpr auto rebind(Original&& original, NewLeaf&& new_leaf) noexcept
210-
-> with_cache<rebind_wrapped_source_result_t<detail::utility::forward_like_t<Original, parent_t>, NewLeaf>, source_ref_t<Cache>>
210+
-> with_cache<wrapped_source_rebind_result_t<detail::utility::forward_like_t<Original, parent_t>, NewLeaf>, ref_result_t<Cache>>
211211
{
212-
return with_cache<rebind_wrapped_source_result_t<detail::utility::forward_like_t<Original, parent_t>, NewLeaf>, source_ref_t<Cache>>{
212+
return with_cache<wrapped_source_rebind_result_t<detail::utility::forward_like_t<Original, parent_t>, NewLeaf>, ref_result_t<Cache>>{
213213
parent_t::rebind(static_cast<detail::utility::forward_like_t<Original, parent_t>&&>(original), KANGARU5_FWD(new_leaf))
214214
};
215215
}
@@ -236,9 +236,11 @@ namespace kangaru {
236236
static_assert(cache_map<with_cache<none_source, source_reference_wrapper<with_cache<none_source>>>>);
237237

238238
template<template<unqualified_object> typename SourceType>
239-
struct cached_pointer_to_source {
239+
struct cached_reference_to_source {
240240
template<injectable T>
241-
using source = SourceType<std::remove_cvref_t<T>>*;
241+
struct ttype {
242+
using type = SourceType<std::remove_cvref_t<T>>&;
243+
};
242244
};
243245

244246
template<source Source, template<typename> typename SourceFor>
@@ -251,12 +253,7 @@ namespace kangaru {
251253
)
252254
constexpr KANGARU5_PROVIDE_FUNCTION_DECL(Self&& source) -> T {
253255
decltype(auto) source_for_t = kangaru::provide<SourceFor<T>>(KANGARU5_FWD(source).source);
254-
// TODO: Can we avoid this pointer thing?
255-
if constexpr (std::is_pointer_v<decltype(source_for_t)>) {
256-
return kangaru::provide<T>(*KANGARU5_FWD(source_for_t));
257-
} else {
258-
return kangaru::provide<T>(KANGARU5_FWD(source_for_t));
259-
}
256+
return kangaru::provide<T>(KANGARU5_FWD(source_for_t));
260257
}
261258

262259
Source source;

include/kangaru/detail/concepts.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ namespace kangaru {
1616
template<typename T>
1717
concept object = std::is_object_v<T>;
1818

19+
template<typename T>
20+
concept forwarded_object = object<std::remove_reference_t<T>>;
21+
1922
template<typename T>
2023
concept unqualified_object = object<T> and not std::is_const_v<T> and not std::is_volatile_v<T>;
2124

include/kangaru/detail/container.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
namespace kangaru {
1616
template<injectable T>
17-
using cached_pointer_to_injectable_reference_source =
18-
typename cached_pointer_to_source<injectable_reference_source>::template source<T>;
17+
using cached_reference_to_injectable_reference_source =
18+
detail::utility::ttype_t<cached_reference_to_source<injectable_reference_source>, T>;
1919

2020
template<
2121
rebindable_source Source,
@@ -53,11 +53,13 @@ namespace kangaru {
5353
make_source_with_exhaustive_construction(
5454
with_alternative{
5555
with_recursion{
56-
make_source_with_cache_using_source<cached_pointer_to_injectable_reference_source>(
57-
KANGARU5_NO_ADL(fwd_ref)(KANGARU5_FWD(source))
56+
make_source_with_cache_using_source<cached_reference_to_injectable_reference_source>(
57+
with_dereference{
58+
KANGARU5_NO_ADL(fwd_ref)(KANGARU5_FWD(source))
59+
}
5860
)
5961
},
60-
external_reference_source{self}
62+
KANGARU5_NO_ADL(concat)(external_reference_source{self}, KANGARU5_NO_ADL(fwd_ref)(KANGARU5_FWD(source)))
6163
}
6264
)
6365
};

include/kangaru/detail/heap_storage.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "source_types.hpp"
55
#include "source.hpp"
66
#include "allocator.hpp"
7-
#include "source_helper.hpp"
7+
#include "source_rebind.hpp"
88

99
#include <concepts>
1010
#include <vector>
@@ -185,8 +185,8 @@ namespace kangaru {
185185
}
186186

187187
template<forwarded<with_heap_storage> Original, forwarded_source NewSource>
188-
static constexpr auto rebind(Original&& original, NewSource&& new_leaf) noexcept -> with_heap_storage<rebind_wrapped_source_result_t<Original, NewSource>, source_ref_t<Storage>> {
189-
return with_heap_storage<rebind_wrapped_source_result_t<Original, NewSource>, source_ref_t<Storage>>{
188+
static constexpr auto rebind(Original&& original, NewSource&& new_leaf) noexcept -> with_heap_storage<wrapped_source_rebind_result_t<Original, NewSource>, ref_result_t<Storage>> {
189+
return with_heap_storage<wrapped_source_rebind_result_t<Original, NewSource>, ref_result_t<Storage>>{
190190
kangaru::rebind(KANGARU5_FWD(original).source, new_leaf),
191191
KANGARU5_NO_ADL(ref)(original.storage)
192192
};

include/kangaru/detail/recursive_source.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "source.hpp"
99
#include "source_types.hpp"
1010
#include "source_reference_wrapper.hpp"
11-
#include "source_helper.hpp"
11+
#include "source_rebind.hpp"
1212

1313
#include <type_traits>
1414
#include <cstdlib>
@@ -206,7 +206,7 @@ namespace kangaru {
206206
template<source Source, construction Construction>
207207
struct with_construction {
208208
private:
209-
using construction_type = maybe_wrapped_t<Construction>;
209+
using construction_type = std::remove_reference_t<maybe_unwrap_result_t<Construction>>;
210210

211211
public:
212212
explicit constexpr with_construction(Source source) noexcept
@@ -236,8 +236,8 @@ namespace kangaru {
236236
}
237237

238238
template<forwarded<with_construction> Original, forwarded_source NewLeaf>
239-
static constexpr auto rebind(Original&& original, NewLeaf&& new_leaf) noexcept -> with_construction<rebind_wrapped_source_result_t<Original, NewLeaf>, Construction> {
240-
return with_construction<rebind_wrapped_source_result_t<Original, NewLeaf>, Construction>{
239+
static constexpr auto rebind(Original&& original, NewLeaf&& new_leaf) noexcept -> with_construction<wrapped_source_rebind_result_t<Original, NewLeaf>, Construction> {
240+
return with_construction<wrapped_source_rebind_result_t<Original, NewLeaf>, Construction>{
241241
kangaru::rebind(KANGARU5_FWD(original).source, KANGARU5_FWD(new_leaf)),
242242
original.construction
243243
};
@@ -322,17 +322,17 @@ namespace kangaru {
322322
// also yield false if it would result in infinite recursion
323323
// Yes, this requires expression will yield different result depending on the metastate of the compiler!
324324
detail::recursive_source::source_of_sfinae_wrapper<
325-
rebind_wrapped_source_result_t<
325+
wrapped_source_rebind_result_t<
326326
Self&,
327-
detail::recursive_source::leaf_as_alternative<with_recursion<source_ref_t<wrapped_source_t<Self>>>>
327+
detail::recursive_source::leaf_as_alternative<with_recursion<ref_result_t<wrapped_source_t<Self>>>>
328328
>,
329329
T
330330
>::value
331331
) {
332332
return kangaru::provide<T>(
333333
kangaru::rebind(
334334
source,
335-
detail::recursive_source::leaf_as_alternative<with_recursion<source_ref_t<wrapped_source_t<Self>>>>{
335+
detail::recursive_source::leaf_as_alternative<with_recursion<ref_result_t<wrapped_source_t<Self>>>>{
336336
KANGARU5_NO_ADL(ref)(source.source)
337337
}
338338
)
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef KANGARU5_DETAIL_SOURCE_HELPER_HPP
2-
#define KANGARU5_DETAIL_SOURCE_HELPER_HPP
1+
#ifndef KANGARU5_DETAIL_SOURCE_REBIND_HPP
2+
#define KANGARU5_DETAIL_SOURCE_REBIND_HPP
33

44
#include "source.hpp"
55
#include "source_reference_wrapper.hpp"
@@ -11,7 +11,7 @@
1111
#include "define.hpp"
1212

1313
namespace kangaru {
14-
namespace detail::source_helper {
14+
namespace detail::source_rebind {
1515
template<wrapping_source>
1616
struct rebind_wrapper {};
1717

@@ -46,14 +46,15 @@ namespace kangaru {
4646
using type = Branch<NewSource, Param>;
4747
};
4848
};
49-
} // namespace detail::source_helper
49+
} // namespace detail::source_rebind
5050

5151
template<typename Source>
5252
concept transparent_rebindable_wrapping_source =
5353
wrapping_source<Source>
5454
and requires(Source source) {
5555
// Here we need to used ttype_t instead of directly using ::ttype<...>::type because GCC 12 has issues with it.
56-
typename detail::utility::ttype_t<detail::source_helper::rebind_wrapper<std::remove_cv_t<Source>>,
56+
typename detail::utility::ttype_t<
57+
detail::source_rebind::rebind_wrapper<std::remove_cv_t<Source>>,
5758
std::decay_t<decltype(source.source)>
5859
>;
5960
};
@@ -70,16 +71,17 @@ namespace kangaru {
7071
transparent_rebindable_wrapping_source<Source>
7172
or stateful_rebindable_wrapping_source<Source>;
7273

73-
namespace detail::source_helper {
74+
namespace detail::source_rebind {
7475
struct rebind_function {
7576
template<forwarded_source Wrapper> requires (rebindable_wrapping_source<std::remove_reference_t<Wrapper>> and not forwarded_reference_wrapper<Wrapper>)
76-
constexpr auto operator()(Wrapper&& source, forwarded_source auto&& new_leaf) const noexcept {
77+
constexpr auto operator()(Wrapper&& source, forwarded_object auto&& new_leaf) const noexcept {
7778
if constexpr (stateful_rebindable_wrapping_source<std::remove_reference_t<Wrapper>>) {
7879
return std::decay_t<Wrapper>::rebind(source, new_leaf);
7980
} else if constexpr (transparent_rebindable_wrapping_source<std::remove_reference_t<Wrapper>>) {
80-
using rebound = typename detail::source_helper::rebind_wrapper<std::remove_cvref_t<Wrapper>>::template ttype<
81+
using rebound = typename detail::utility::ttype_t<
82+
detail::source_rebind::rebind_wrapper<std::remove_cvref_t<Wrapper>>,
8183
decltype(operator()(source.source, KANGARU5_FWD(new_leaf)))
82-
>::type;
84+
>;
8385
return rebound{
8486
operator()(source.source, KANGARU5_FWD(new_leaf))
8587
};
@@ -89,9 +91,7 @@ namespace kangaru {
8991
}
9092

9193
template<forwarded_reference_wrapper Wrapper>
92-
constexpr auto operator()(Wrapper&& wrapper, forwarded_source auto&& new_leaf) const noexcept {
93-
// TODO: Do we need to use forwarding refs for wrappers?
94-
// TODO: Do we need to unwrap references at all? Does it even make sense?
94+
constexpr auto operator()(Wrapper&& wrapper, forwarded_object auto&& new_leaf) const noexcept {
9595
decltype(auto) unwrapped = KANGARU5_FWD(wrapper).unwrap();
9696
return operator()(KANGARU5_FWD(unwrapped), KANGARU5_FWD(new_leaf));
9797
}
@@ -101,7 +101,7 @@ namespace kangaru {
101101
and not rebindable_wrapping_source<std::remove_reference_t<Leaf>>
102102
and not forwarded_wrapping_source<Leaf>
103103
)
104-
constexpr auto operator()(Leaf&& leaf, forwarded_source auto&& new_leaf) const noexcept {
104+
constexpr auto operator()(Leaf&& leaf, forwarded_object auto&& new_leaf) const noexcept {
105105
// We do not forward new_leaf here, since it may be called multiple times
106106
return new_leaf(KANGARU5_FWD(leaf));
107107
}
@@ -120,24 +120,24 @@ namespace kangaru {
120120
inline constexpr auto is_rebindable_v<Leaf> = true;
121121

122122
namespace niebloid {
123-
inline constexpr auto rebind = detail::source_helper::rebind_function{};
123+
inline constexpr auto rebind = detail::source_rebind::rebind_function{};
124124
}
125-
} // namespace detail::source_helper
125+
} // namespace detail::source_rebind
126126

127127
inline namespace niebloid {
128-
using namespace detail::source_helper::niebloid;
128+
using namespace detail::source_rebind::niebloid;
129129
}
130130

131131
template<typename Source>
132-
concept rebindable_source = source<Source> and detail::source_helper::is_rebindable_v<Source>;
132+
concept rebindable_source = source<Source> and detail::source_rebind::is_rebindable_v<Source>;
133133

134134
template<source Source, forwarded_source Leaf>
135135
using rebind_result_t = decltype(kangaru::rebind(std::declval<Source>(), std::declval<Leaf>()));
136136

137137
template<forwarded_wrapping_source Source, forwarded_source Leaf>
138-
using rebind_wrapped_source_result_t = decltype(kangaru::rebind(std::declval<forwarded_wrapped_source_t<Source>>(), std::declval<Leaf>()));
138+
using wrapped_source_rebind_result_t = decltype(kangaru::rebind(std::declval<forwarded_wrapped_source_t<Source>>(), std::declval<Leaf>()));
139139
} // namespace kangaru
140140

141141
#include "undef.hpp"
142142

143-
#endif
143+
#endif // KANGARU5_DETAIL_SOURCE_REBIND_HPP

include/kangaru/detail/source_reference_wrapper.hpp

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
#include "define.hpp"
1010

1111
namespace kangaru {
12-
template<object Source> requires source<std::remove_const_t<Source>>
12+
template<typename T>
13+
concept reference_wrapper = source<T> and requires(T ref) {
14+
{ ref.unwrap() } -> reference;
15+
};
16+
17+
template<object Source> requires (source<std::remove_const_t<Source>> and not reference_wrapper<std::remove_const_t<Source>>)
1318
struct source_reference_wrapper {
1419
explicit constexpr source_reference_wrapper(Source& source) noexcept : source{std::addressof(source)} {}
1520

@@ -28,7 +33,7 @@ namespace kangaru {
2833
Source* source;
2934
};
3035

31-
template<source_ref Source>
36+
template<source_ref Source> requires (not reference_wrapper<std::remove_cvref_t<Source>>)
3237
struct source_forwarding_reference_wrapper {
3338
explicit constexpr source_forwarding_reference_wrapper(Source source) noexcept : source{std::addressof(source)} {}
3439

@@ -61,14 +66,12 @@ namespace kangaru {
6166
template<forwarded_source Source>
6267
source_forwarding_reference_wrapper(Source&&) -> source_forwarding_reference_wrapper<Source&&>;
6368

64-
template<typename T>
65-
concept reference_wrapper = source<T> and requires(T ref) {
66-
{ ref.unwrap() } -> reference;
67-
};
68-
6969
template<typename T>
7070
concept forwarded_reference_wrapper = reference_wrapper<std::remove_reference_t<T>>;
7171

72+
template<reference_wrapper Wrapper>
73+
using source_reference_wrapped_type = std::remove_reference_t<decltype(std::declval<Wrapper>().unwrap())>;
74+
7275
inline constexpr auto maybe_unwrap(forwarded_reference_wrapper auto&& ref) noexcept -> decltype(auto) {
7376
return KANGARU5_FWD(ref).unwrap();
7477
}
@@ -77,14 +80,8 @@ namespace kangaru {
7780
return KANGARU5_FWD(any);
7881
}
7982

80-
template<reference_wrapper Wrapper>
81-
using source_reference_wrapped_type = std::remove_reference_t<decltype(std::declval<Wrapper>().unwrap())>;
82-
83-
template<typename MaybeWrapper>
84-
using maybe_wrapped_t = std::remove_reference_t<decltype(KANGARU5_NO_ADL(maybe_unwrap)(std::declval<MaybeWrapper>()))>;
85-
8683
template<typename MaybeWrapper>
87-
using forwarded_maybe_wrapped_t = decltype(KANGARU5_NO_ADL(maybe_unwrap)(std::declval<MaybeWrapper>()));
84+
using maybe_unwrap_result_t = decltype(KANGARU5_NO_ADL(maybe_unwrap)(std::declval<MaybeWrapper>()));
8885

8986
template<source Source> requires (not reference_wrapper<std::remove_cvref_t<Source>>)
9087
inline constexpr auto ref(Source& source) -> source_reference_wrapper<Source> {
@@ -96,6 +93,11 @@ namespace kangaru {
9693
return source;
9794
}
9895

96+
template<source_ref Source> requires (not reference_wrapper<std::remove_cvref_t<Source>>)
97+
inline constexpr auto ref(source_forwarding_reference_wrapper<Source> source) -> source_reference_wrapper<std::remove_reference_t<Source>> {
98+
return source_reference_wrapper<std::remove_reference_t<Source>>{source.unwrap()};
99+
}
100+
99101
template<forwarded_source Source> requires (not forwarded_reference_wrapper<Source>)
100102
inline constexpr auto fwd_ref(Source&& source) -> source_forwarding_reference_wrapper<Source&&> {
101103
return source_forwarding_reference_wrapper<Source&&>{KANGARU5_FWD(source)};
@@ -106,11 +108,16 @@ namespace kangaru {
106108
return source;
107109
}
108110

111+
template<forwarded_source Source> requires (not reference_wrapper<std::remove_cvref_t<Source>>)
112+
inline constexpr auto fwd_ref(source_reference_wrapper<Source> source) -> source_forwarding_reference_wrapper<Source&> {
113+
return source_forwarding_reference_wrapper<Source&>{source.unwrap()};
114+
}
115+
109116
template<source Source>
110-
using source_ref_t = decltype(KANGARU5_NO_ADL(ref)(std::declval<Source&>()));
117+
using ref_result_t = decltype(KANGARU5_NO_ADL(ref)(std::declval<Source&>()));
111118

112119
template<source_ref Source>
113-
using source_fwd_ref_t = decltype(KANGARU5_NO_ADL(fwd_ref)(std::declval<Source>()));
120+
using fwd_ref_result_t = decltype(KANGARU5_NO_ADL(fwd_ref)(std::declval<Source>()));
114121
} // namespace kangaru
115122

116123
#include "undef.hpp"

include/kangaru/detail/source_types.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "constructor.hpp"
99
#include "source_reference_wrapper.hpp"
1010
#include "tag.hpp"
11-
#include "source_helper.hpp"
11+
#include "source_rebind.hpp"
1212

1313
#include <tuple>
1414
#include <concepts>
@@ -256,8 +256,8 @@ namespace kangaru {
256256
}
257257

258258
template<forwarded<with_alternative> Original, forwarded_source NewLeaf>
259-
static constexpr auto rebind(Original&& original, NewLeaf&& new_leaf) noexcept -> with_alternative<rebind_wrapped_source_result_t<Original, NewLeaf>, source_fwd_ref_t<detail::utility::forward_like_t<Original, Alternative>>> {
260-
return with_alternative<rebind_wrapped_source_result_t<Original, NewLeaf>, source_fwd_ref_t<detail::utility::forward_like_t<Original, Alternative>>>{
259+
static constexpr auto rebind(Original&& original, NewLeaf&& new_leaf) noexcept -> with_alternative<wrapped_source_rebind_result_t<Original, NewLeaf>, fwd_ref_result_t<detail::utility::forward_like_t<Original, Alternative>>> {
260+
return with_alternative<wrapped_source_rebind_result_t<Original, NewLeaf>, fwd_ref_result_t<detail::utility::forward_like_t<Original, Alternative>>>{
261261
kangaru::rebind(KANGARU5_FWD(original).source, KANGARU5_FWD(new_leaf)),
262262
KANGARU5_NO_ADL(fwd_ref)(maybe_unwrap(KANGARU5_FWD(original).alternative))
263263
};

0 commit comments

Comments
 (0)