Skip to content

Commit 605fb95

Browse files
authored
Merge pull request #2 from beman-project/get_allocator
added get_allocator
2 parents a83a0b3 + 974c65b commit 605fb95

9 files changed

+161
-6
lines changed

docs/TODO.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"execution.queryable": { "code":true, "test":true, "doc":true, "comment":"empty" },
66
"execution.queryable.general]": { "code":true, "test":true, "doc":false, "comment":"nothing testable" },
77
"execution.forwarding_query": { "code":true, "test":true, "doc":false },
8+
"execution.get_allocator": { "code":true, "test":true, "doc":false },
89
"execution.queryable.concepts": { "code":true, "test":true, "doc":false },
910
"execution.receivers": { "code":true, "test":true, "doc":true, "comment":"empty" },
1011
"execution.senders": { "code":true, "test":true, "doc":true, "comment":"empty" },

docs/TODO.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
| [[execution.syn](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2300r10.html#execution.syn)] | 🔴 | 🔴 | 🔴 | |
5151
| [[execution.queries](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2300r10.html#execution.queries)] | ✅ | ✅ | ✅ | empty |
5252
| [[execution.forwarding_query](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2300r10.html#execution.forwarding_query)] | ✅ | ✅ | 🔴 | |
53-
| [[execution.get_allocator](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2300r10.html#execution.get_allocator)] | 🔴 | 🔴 | 🔴 | |
53+
| [[execution.get_allocator](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2300r10.html#execution.get_allocator)] | ✅ | ✅ | 🔴 | |
5454
| [[execution.get_stop_token](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2300r10.html#execution.get_stop_token)] | 🔴 | 🔴 | 🔴 | |
5555
| [[execution.environment.get_env](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2300r10.html#execution.environment.get_env)] | 🔴 | 🔴 | 🔴 | |
5656
| [[execution.get_domain](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2024/p2300r10.html#execution.get_domain)] | 🔴 | 🔴 | 🔴 | |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// include/Beman/Execution26/detail/get_allocator.hpp -*-C++-*-
2+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
#ifndef INCLUDED_BEMAN_EXECUTION26_DETAIL_GET_ALLOCATOR
5+
#define INCLUDED_BEMAN_EXECUTION26_DETAIL_GET_ALLOCATOR
6+
7+
#include <Beman/Execution26/detail/common.hpp>
8+
#include <Beman/Execution26/detail/forwarding_query.hpp>
9+
#include <Beman/Execution26/detail/simple_allocator.hpp>
10+
#include <concepts>
11+
#include <utility>
12+
13+
// ----------------------------------------------------------------------------
14+
15+
namespace Beman::Execution26
16+
{
17+
struct get_allocator_t
18+
{
19+
template <typename Object>
20+
requires (not requires(Object&& object, get_allocator_t const& tag)
21+
{
22+
::std::as_const(object).query(tag);
23+
})
24+
auto operator()(Object&&) const
25+
= BEMAN_EXECUTION26_DELETE("the object requires a const query(get_allocator_t) overload");
26+
template <typename Object>
27+
requires (not requires(Object const& object, get_allocator_t const& tag)
28+
{
29+
object.query(tag);
30+
})
31+
auto operator()(Object&&) const
32+
= BEMAN_EXECUTION26_DELETE("the object requires a query(get_allocator_t) overload");
33+
template <typename Object>
34+
requires (not requires(Object const& object, get_allocator_t const& tag)
35+
{
36+
{ object.query(tag) } noexcept;
37+
})
38+
auto operator()(Object&&) const
39+
= BEMAN_EXECUTION26_DELETE("the query(get_allocator_t) overload needs to be noexcept");
40+
template <typename Object>
41+
requires (not requires(Object const& object, get_allocator_t const& tag)
42+
{
43+
{ object.query(tag) } noexcept -> ::Beman::Execution26::Detail::simple_allocator<>;
44+
})
45+
auto operator()(Object&&) const
46+
= BEMAN_EXECUTION26_DELETE("the query(get_allocator_t) overload needs to be noexcept");
47+
48+
template <typename Object>
49+
auto operator()(Object&& object) const
50+
{
51+
return ::std::as_const(object).query(*this);
52+
}
53+
54+
constexpr auto query(::Beman::Execution26::forwarding_query_t const&) const noexcept -> bool
55+
{
56+
return true;
57+
}
58+
};
59+
60+
inline constexpr get_allocator_t get_allocator{};
61+
}
62+
63+
// ----------------------------------------------------------------------------
64+
65+
#endif

include/Beman/Execution26/detail/simple-allocator.hpp include/Beman/Execution26/detail/simple_allocator.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// include/Beman/Execution26/detail/simple-allocator.hpp -*-C++-*-
1+
// include/Beman/Execution26/detail/simple_allocator.hpp -*-C++-*-
22
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33

44
#ifndef INCLUDED_BEMAN_EXECUTION26_DETAIL_SIMPLE_ALLOCATOR
@@ -11,7 +11,7 @@
1111

1212
namespace Beman::Execution26::Detail
1313
{
14-
template <typename Alloc>
14+
template <typename Alloc = bool>
1515
concept simple_allocator
1616
= requires(Alloc alloc, ::std::size_t n)
1717
{

include/Beman/Execution26/execution.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// ----------------------------------------------------------------------------
88

99
#include <Beman/Execution26/detail/queryable.hpp>
10+
#include <Beman/Execution26/detail/forwarding_query.hpp>
1011
#include <Beman/Execution26/detail/get_env.hpp>
1112

1213
#include <Beman/Execution26/detail/completion_signature.hpp>

src/Beman/Execution26/tests/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33

44
list(APPEND execution_tests
5+
exec-get-allocator.pass
56
exec-fwd-env.pass
67
exec-opstate.pass
78
exec-opstate-start.pass

src/Beman/Execution26/tests/allocator-requirements-general.pass.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// src/Beman/Execution26/tests/allocator-requirements-general.pass.cpp -*-C++-*-
22
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33

4-
#include <Beman/Execution26/detail/simple-allocator.hpp>
4+
#include <Beman/Execution26/detail/simple_allocator.hpp>
55
#include <test/execution.hpp>
66

77
// ----------------------------------------------------------------------------
@@ -116,4 +116,4 @@ auto main() -> int
116116
static_assert(not test_detail::simple_allocator<not_equality_comparable>);
117117

118118
static_assert(test_detail::simple_allocator<allocator>);
119-
}
119+
}

src/Beman/Execution26/tests/exec-fwd-env.pass.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// src/Beman/Execution26/tests/exec-fwd-env.pass.cpp -*-C++-*-
22
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
33

4-
#include <Beman/Execution26/detail/forwarding_query.hpp>
54
#include <Beman/Execution26/execution.hpp>
65
#include <test/execution.hpp>
76
#include <concepts>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// src/Beman/Execution26/tests/exec.get.allocator.pass.cpp -*-C++-*-
2+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
#include <Beman/Execution26/detail/get_allocator.hpp>
5+
#include <Beman/Execution26/execution.hpp>
6+
#include <test/execution.hpp>
7+
#include <concepts>
8+
9+
// ----------------------------------------------------------------------------
10+
11+
namespace
12+
{
13+
struct non_allocator
14+
{
15+
};
16+
struct allocator
17+
{
18+
int tag{};
19+
using value_type = int;
20+
auto allocate(std::size_t n) -> int* { return new value_type[n]; }
21+
auto deallocate(int* ptr, std::size_t) -> void { delete[] ptr; }
22+
auto operator== (allocator const&) const -> bool = default;
23+
};
24+
25+
struct no_allocator
26+
{
27+
int tag{};
28+
};
29+
30+
template <bool Noexcept>
31+
struct throwing_allocator
32+
{
33+
auto query(test_std::get_allocator_t const&) const noexcept(Noexcept) -> allocator
34+
{
35+
return {};
36+
}
37+
};
38+
39+
template <typename Result>
40+
struct allocator_object
41+
{
42+
int tag{};
43+
auto query(test_std::get_allocator_t const&) const noexcept -> Result
44+
{
45+
return Result{this->tag};
46+
}
47+
};
48+
49+
struct non_const_get_allocator
50+
{
51+
auto query(test_std::get_allocator_t const&) noexcept -> allocator
52+
{
53+
return {};
54+
}
55+
};
56+
57+
58+
template <bool Expect, typename Object>
59+
auto test_get_allocator(Object&& object) -> void
60+
{
61+
static_assert(Expect == requires{ test_std::get_allocator(::std::forward<Object>(object)); });
62+
}
63+
}
64+
65+
auto main() -> int
66+
{
67+
static_assert(std::same_as<test_std::get_allocator_t const,
68+
decltype(test_std::get_allocator)>);
69+
static_assert(test_std::forwarding_query(test_std::get_allocator));
70+
71+
test_get_allocator<false>(no_allocator());
72+
test_get_allocator<true>(throwing_allocator<true>());
73+
test_get_allocator<false>(throwing_allocator<false>());
74+
75+
static_assert(test_detail::simple_allocator<allocator>);
76+
static_assert(not test_detail::simple_allocator<non_allocator>);
77+
test_get_allocator<true>(allocator_object<allocator>());
78+
test_get_allocator<false>(allocator_object<non_allocator>());
79+
80+
test_get_allocator<false>(non_const_get_allocator());
81+
non_const_get_allocator alloc;
82+
test_get_allocator<false>(alloc);
83+
non_const_get_allocator calloc;
84+
test_get_allocator<false>(calloc);
85+
86+
static_assert(std::same_as<allocator, decltype(test_std::get_allocator(allocator_object<allocator>()))>);
87+
assert(43 == test_std::get_allocator(allocator_object<allocator>{43}).tag);
88+
}

0 commit comments

Comments
 (0)