Skip to content

Commit c664e86

Browse files
committed
refactor(allocation-value): reducing duplications
1 parent 1e8651d commit c664e86

File tree

2 files changed

+79
-68
lines changed

2 files changed

+79
-68
lines changed

include/stdsharp/memory/allocation_value.h

Lines changed: 73 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,81 @@
44
#include "allocation_traits.h"
55
#include "launder_iterator.h"
66

7-
namespace stdsharp
7+
namespace stdsharp::details
88
{
9-
template<allocator_req Allocator, typename T = Allocator::value_type>
9+
template<typename Allocator, typename T>
1010
struct allocation_value
1111
{
1212
using allocation_traits = allocation_traits<Allocator>;
1313
using allocator_traits = allocation_traits::allocator_traits;
1414
using allocator_type = allocation_traits::allocator_type;
1515
using size_type = allocator_traits::size_type;
1616

17-
static constexpr auto always_equal = true;
18-
19-
[[nodiscard]] static constexpr auto value_size() noexcept { return sizeof(T); }
20-
21-
[[nodiscard]] bool operator==(const allocation_value&) const = default;
17+
using ctor = allocator_traits::constructor;
18+
using dtor = allocator_traits::destructor;
2219

2320
static constexpr auto data = allocation_traits::template data<T>;
2421
static constexpr auto get = allocation_traits::template get<T>;
2522

26-
private:
27-
using ctor = allocator_traits::constructor;
28-
using dtor = allocator_traits::destructor;
29-
30-
constexpr void size_validate(const auto& allocation) const noexcept
23+
constexpr void size_validate(this const auto& t, const auto& allocation) noexcept
3124
{
3225
Expects(
33-
allocation.size() * sizeof(typename allocation_traits::value_type) >= value_size()
26+
allocation.size() * sizeof(typename allocation_traits::value_type) >= t.value_size()
3427
);
3528
}
3629

37-
constexpr void
38-
size_validate(const auto& src_allocation, const auto& dst_allocation) const noexcept
30+
constexpr void size_validate(
31+
this const auto& t,
32+
const auto& src_allocation,
33+
const auto& dst_allocation
34+
) noexcept
3935
{
40-
size_validate(src_allocation);
41-
size_validate(dst_allocation);
36+
t.size_validate(src_allocation);
37+
t.size_validate(dst_allocation);
4238
}
4339

44-
public:
4540
static constexpr struct default_construct_t
4641
{
4742
} default_construct{};
43+
};
44+
}
4845

46+
namespace stdsharp
47+
{
48+
template<allocator_req Allocator, typename T = Allocator::value_type>
49+
struct allocation_value : private details::allocation_value<Allocator, T>
50+
{
51+
using m_base = details::allocation_value<Allocator, T>;
52+
53+
public:
54+
using typename m_base::allocation_traits;
55+
using typename m_base::allocator_traits;
56+
using typename m_base::allocator_type;
57+
using typename m_base::size_type;
58+
using typename m_base::default_construct_t;
59+
using m_base::default_construct;
60+
using m_base::data;
61+
using m_base::get;
62+
63+
static constexpr auto always_equal = true;
64+
65+
[[nodiscard]] static constexpr auto value_size() noexcept { return sizeof(T); }
66+
67+
[[nodiscard]] bool operator==(const allocation_value&) const = default;
68+
69+
private:
70+
using typename m_base::ctor;
71+
using typename m_base::dtor;
72+
73+
public:
4974
constexpr void operator()(
5075
allocator_type& allocator,
5176
const allocation<Allocator> auto& allocation,
5277
const default_construct_t /*unused*/
5378
) const noexcept(nothrow_invocable<ctor, allocator_type&, T*>)
5479
requires std::invocable<ctor, allocator_type&, T*>
5580
{
56-
size_validate(allocation);
81+
this->size_validate(allocation);
5782
ctor{}(allocator, data(allocation));
5883
}
5984

@@ -64,7 +89,7 @@ namespace stdsharp
6489
) const noexcept(nothrow_invocable<ctor, allocator_type&, T*, const T&>)
6590
requires std::invocable<ctor, allocator_type&, T*, const T&>
6691
{
67-
size_validate(src_allocation, dst_allocation);
92+
this->size_validate(src_allocation, dst_allocation);
6893
ctor{}(allocator, data(dst_allocation), get(src_allocation));
6994
}
7095

@@ -75,7 +100,7 @@ namespace stdsharp
75100
) const noexcept(nothrow_invocable<ctor, allocator_type&, T*, T>)
76101
requires std::invocable<ctor, allocator_type&, T*, T>
77102
{
78-
size_validate(src_allocation, dst_allocation);
103+
this->size_validate(src_allocation, dst_allocation);
79104
ctor{}(allocator, data(dst_allocation), cpp_move(get(src_allocation)));
80105
}
81106

@@ -85,7 +110,7 @@ namespace stdsharp
85110
) const noexcept(nothrow_copy_assignable<T>)
86111
requires copy_assignable<T>
87112
{
88-
size_validate(src_allocation, dst_allocation);
113+
this->size_validate(src_allocation, dst_allocation);
89114
get(dst_allocation) = get(src_allocation);
90115
}
91116

@@ -95,7 +120,7 @@ namespace stdsharp
95120
) const noexcept(nothrow_move_assignable<T>)
96121
requires move_assignable<T>
97122
{
98-
size_validate(src_allocation, dst_allocation);
123+
this->size_validate(src_allocation, dst_allocation);
99124
get(dst_allocation) = cpp_move(get(src_allocation));
100125
}
101126

@@ -104,44 +129,34 @@ namespace stdsharp
104129
const allocation<Allocator> auto& allocation
105130
) const noexcept
106131
{
107-
size_validate(allocation);
132+
this->size_validate(allocation);
108133
dtor{}(allocator, data(allocation));
109134
}
110135
};
111136

112137
template<allocator_req Allocator, typename T>
113-
struct allocation_value<Allocator, T[]> // NOLINT(*-c-arrays)
138+
struct allocation_value<Allocator, T[]> :// NOLINT(*-c-arrays)
139+
private details::allocation_value<Allocator, T>
114140
{
115-
public:
116-
using allocation_traits = allocation_traits<Allocator>;
117-
using allocator_traits = allocation_traits::allocator_traits;
118-
using allocator_type = allocation_traits::allocator_type;
119-
using size_type = allocator_traits::size_type;
141+
using m_base = details::allocation_value<Allocator, T>;
120142

121-
static constexpr auto data = allocation_traits::template data<T>;
122-
static constexpr auto get = allocation_traits::template get<T>;
143+
public:
144+
using typename m_base::allocation_traits;
145+
using typename m_base::allocator_traits;
146+
using typename m_base::allocator_type;
147+
using typename m_base::size_type;
148+
using typename m_base::default_construct_t;
149+
using m_base::default_construct;
150+
using m_base::data;
151+
using m_base::get;
123152

124153
private:
125-
using ctor = allocator_traits::constructor;
126-
using dtor = allocator_traits::destructor;
154+
using typename m_base::ctor;
155+
using typename m_base::dtor;
127156
using size_t = std::size_t;
128157

129158
size_t size_;
130159

131-
constexpr void size_validate(const auto& allocation) const noexcept
132-
{
133-
Expects(
134-
allocation.size() * sizeof(typename allocation_traits::value_type) >= value_size()
135-
);
136-
}
137-
138-
constexpr void
139-
size_validate(const auto& src_allocation, const auto& dst_allocation) const noexcept
140-
{
141-
size_validate(src_allocation);
142-
size_validate(dst_allocation);
143-
}
144-
145160
[[nodiscard]] constexpr auto launder_begin(const auto& allocation) const noexcept
146161
{
147162
return launder_iterator{data(allocation)};
@@ -161,20 +176,16 @@ namespace stdsharp
161176

162177
[[nodiscard]] bool operator==(const allocation_value&) const = default;
163178

164-
static constexpr struct default_construct_t
165-
{
166-
} default_construct{};
167-
168179
constexpr void operator()(
169180
allocator_type& allocator,
170181
const allocation<Allocator> auto& allocation,
171182
const default_construct_t /*unused*/
172183
) const noexcept(nothrow_invocable<ctor, allocator_type&, T*>)
173184
requires std::invocable<ctor, allocator_type&, T*>
174185
{
175-
size_validate(allocation);
186+
this->size_validate(allocation);
176187

177-
auto p = data(allocation);
188+
auto&& p = data(allocation);
178189
const auto count = size();
179190
for(size_t i = 0; i < count; ++i, ++p) ctor{}(allocator, p);
180191
}
@@ -186,9 +197,9 @@ namespace stdsharp
186197
) const noexcept(nothrow_invocable<ctor, allocator_type&, T*, const T&>)
187198
requires std::invocable<ctor, allocator_type&, T*, const T&>
188199
{
189-
size_validate(src_allocation, dst_allocation);
200+
this->size_validate(src_allocation, dst_allocation);
190201

191-
auto dst_begin = data(dst_allocation);
202+
auto&& dst_begin = data(dst_allocation);
192203
const auto& src_begin = launder_begin(src_allocation);
193204
const auto count = size();
194205

@@ -203,9 +214,9 @@ namespace stdsharp
203214
) const noexcept(nothrow_invocable<ctor, allocator_type&, T*, T>)
204215
requires std::invocable<ctor, allocator_type&, T*, T>
205216
{
206-
size_validate(src_allocation, dst_allocation);
217+
this->size_validate(src_allocation, dst_allocation);
207218

208-
auto dst_begin = data(dst_allocation);
219+
auto&& dst_begin = data(dst_allocation);
209220
const auto& src_begin = launder_begin(src_allocation);
210221
const auto count = size();
211222

@@ -219,7 +230,7 @@ namespace stdsharp
219230
) const noexcept(nothrow_copy_assignable<T>)
220231
requires copy_assignable<T>
221232
{
222-
size_validate(src_allocation, dst_allocation);
233+
this->size_validate(src_allocation, dst_allocation);
223234
std::ranges::copy_n( //
224235
launder_begin(src_allocation),
225236
size(),
@@ -233,7 +244,7 @@ namespace stdsharp
233244
) const noexcept(nothrow_move_assignable<T>)
234245
requires move_assignable<T>
235246
{
236-
size_validate(src_allocation, dst_allocation);
247+
this->size_validate(src_allocation, dst_allocation);
237248
move_n(launder_begin(src_allocation), size(), launder_begin(dst_allocation));
238249
}
239250

@@ -242,9 +253,9 @@ namespace stdsharp
242253
const allocation<Allocator> auto& allocation
243254
) const noexcept
244255
{
245-
size_validate(allocation);
256+
this->size_validate(allocation);
246257

247-
auto iter = launder_begin(allocation);
258+
auto&& iter = launder_begin(allocation);
248259
const auto count = size();
249260
for(size_t i = 0; i < count; ++i, ++iter) dtor{}(allocator, iter.data());
250261
}

include/stdsharp/memory/box.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace stdsharp::details
3939
dispatcher<req.move_assign, void, const allocation_type&, const allocation_type&>,
4040
dispatcher<expr_req::no_exception, void, allocator_type&, const allocation_type&>>;
4141

42-
using allocation_value = allocation_value<allocator_type, box_traits>;
42+
using allocation_value = stdsharp::allocation_value<allocator_type, box_traits>;
4343

4444
using allocations_type = std::array<allocation_type, 1>;
4545
using callocations_type =
@@ -49,11 +49,11 @@ namespace stdsharp::details
4949
template<lifetime_req Req, typename Alloc, typename T>
5050
concept box_type_compatible = std::constructible_from<
5151
typename box_traits<Req, Alloc>::dispatchers,
52-
allocation_value<Alloc, T>,
53-
allocation_value<Alloc, T>,
54-
allocation_value<Alloc, T>,
55-
allocation_value<Alloc, T>,
56-
allocation_value<Alloc, T>>;
52+
stdsharp::allocation_value<Alloc, T>,
53+
stdsharp::allocation_value<Alloc, T>,
54+
stdsharp::allocation_value<Alloc, T>,
55+
stdsharp::allocation_value<Alloc, T>,
56+
stdsharp::allocation_value<Alloc, T>>;
5757

5858
template<lifetime_req Req, typename Alloc, lifetime_req OtherReq>
5959
concept box_compatible =

0 commit comments

Comments
 (0)