include/boost/capy/detail/run.hpp

100.0% Lines (4 / 4) 100.0% Functions (2 / 2)
run.hpp
f(x) Functions (2)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3 // Copyright (c) 2026 Michael Vandeberg
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Official repository: https://github.com/cppalliance/capy
9 //
10
11 #ifndef BOOST_CAPY_DETAIL_RUN_HPP
12 #define BOOST_CAPY_DETAIL_RUN_HPP
13
14 #include <boost/capy/detail/config.hpp>
15 #include <boost/capy/detail/frame_memory_resource.hpp>
16
17 #include <concepts>
18 #include <coroutine>
19 #include <cstddef>
20 #include <memory_resource>
21
22 namespace boost {
23 namespace capy {
24 namespace detail {
25
26 // A runnable that a stop ends by abandonment (quitter): its promise
27 // says what it delivers instead of its own result, and gives the
28 // handle a launcher resumes to start it.
29 template<class Task>
30 concept stops_by_abandonment = requires(typename Task::promise_type& p) {
31 Task::promise_type::stopped_result();
32 { p.launch_handle() } -> std::convertible_to<std::coroutine_handle<>>;
33 };
34
35 /// Concept for standard Allocator types.
36 template<class A>
37 concept Allocator = requires(A a, std::size_t n) {
38 typename A::value_type;
39 { a.allocate(n) } -> std::same_as<typename A::value_type*>;
40 };
41
42 /// Specialization for memory_resource* - stores pointer directly.
43 template<>
44 class frame_memory_resource<std::pmr::memory_resource*>
45 {
46 std::pmr::memory_resource* mr_;
47
48 public:
49 17x explicit frame_memory_resource(std::pmr::memory_resource* mr) noexcept
50 17x : mr_(mr)
51 {
52 17x }
53
54 17x std::pmr::memory_resource* get() noexcept { return mr_; }
55 };
56
57 /// Specialization for void - no allocator (empty).
58 template<>
59 class frame_memory_resource<void>
60 {
61 public:
62 frame_memory_resource() = default;
63
64 std::pmr::memory_resource* get() noexcept { return nullptr; }
65 };
66
67 } // namespace detail
68 } // namespace capy
69 } // namespace boost
70
71 #endif
72