|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Ian Petersen |
| 3 | + * Copyright (c) 2025 NVIDIA Corporation |
| 4 | + * |
| 5 | + * Licensed under the Apache License Version 2.0 with LLVM Exceptions |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://llvm.org/LICENSE.txt |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +#pragma once |
| 18 | + |
| 19 | +#include "__execution_fwd.hpp" |
| 20 | + |
| 21 | +#include "__concepts.hpp" |
| 22 | +#include "__env.hpp" |
| 23 | +#include "__queries.hpp" |
| 24 | + |
| 25 | +#include <memory> |
| 26 | + |
| 27 | +namespace stdexec { |
| 28 | + //////////////////////////////////////////////////////////////////////////////////////////////// |
| 29 | + // [exec.spawn] paragraph 9 |
| 30 | + // [exec.spawn.future] paragraph 15 |
| 31 | + // |
| 32 | + // spawn and spawn_future both have to choose an allocator and an injected environment, and they |
| 33 | + // do it in the same way; this namespace provides a couple of functions for making those choices |
| 34 | + namespace __spawn_common { |
| 35 | + struct __choose_alloc_fn { |
| 36 | + // [exec.spawn] paragraph 9 |
| 37 | + // [exec.spawn.future] paragraph 15 |
| 38 | + // (9/15.1) -- if the expression get_allocator(env) is well-formed, then alloc is |
| 39 | + // the result of get_allocator(env) |
| 40 | + template <class _Env, class _SenderEnv> |
| 41 | + requires __callable<get_allocator_t, _Env> |
| 42 | + auto operator()(const _Env& __env, const _SenderEnv&) const { |
| 43 | + return get_allocator(__env); |
| 44 | + } |
| 45 | + |
| 46 | + // [exec.spawn] paragraph 9 |
| 47 | + // [exec.spawn.future] paragraph 15 |
| 48 | + // (9/15.2) -- otherwise, if the expression get_allocator(get_env(new_sender)) is |
| 49 | + // well-formed, then alloc is the result of get_allocator(get_env(new_sender)) |
| 50 | + template <class _Env, class _SenderEnv> |
| 51 | + requires(!__callable<get_allocator_t, const _Env&>) |
| 52 | + && __callable<get_allocator_t, const _SenderEnv&> |
| 53 | + auto operator()(const _Env&, const _SenderEnv& __env) const { |
| 54 | + return get_allocator(__env); |
| 55 | + } |
| 56 | + |
| 57 | + // [exec.spawn] paragraph 9 |
| 58 | + // [exec.spawn.future] paragraph 15 |
| 59 | + // (9/15.3) -- otherwise, alloc is allocator<void>() |
| 60 | + template <class _Env, class _SenderEnv> |
| 61 | + requires(!__callable<get_allocator_t, const _Env&>) |
| 62 | + && (!__callable<get_allocator_t, const _SenderEnv&>) |
| 63 | + std::allocator<void> operator()(const _Env&, const _SenderEnv&) const { |
| 64 | + return std::allocator<void>(); |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + inline constexpr __choose_alloc_fn __choose_alloc{}; |
| 69 | + |
| 70 | + struct __choose_senv_fn { |
| 71 | + // [exec.spawn] paragraph 9 |
| 72 | + // [exec.spawn.future] paragraph 15 |
| 73 | + // (9/15.1) -- if the expression get_allocator(env) is well-formed, then ... |
| 74 | + // senv is the expression env; |
| 75 | + template <class _Env, class _SenderEnv> |
| 76 | + requires __callable<get_allocator_t, const _Env&> |
| 77 | + const _Env& operator()(const _Env& __env, const _SenderEnv&) const { |
| 78 | + return __env; |
| 79 | + } |
| 80 | + |
| 81 | + // [exec.spawn] paragraph 9 |
| 82 | + // [exec.spawn.future] paragraph 15 |
| 83 | + // (9/15.2) -- otherwise, if the expression get_allocator(get_env(new_sender)) is |
| 84 | + // well-formed, then ... senv is the expression |
| 85 | + // JOIN-ENV(prop(get_allocator, alloc), env); |
| 86 | + template <class _Env, class _SenderEnv> |
| 87 | + requires(!__callable<get_allocator_t, const _Env&>) |
| 88 | + && __callable<get_allocator_t, const _SenderEnv&> |
| 89 | + auto operator()(const _Env& __env, const _SenderEnv& __sndrEnv) const { |
| 90 | + return __env::__join(prop(get_allocator, get_allocator(__sndrEnv)), __env); |
| 91 | + } |
| 92 | + |
| 93 | + // [exec.spawn] paragraph 9 |
| 94 | + // [exec.spawn.future] paragraph 15 |
| 95 | + // (9/15.3) -- otherwise, ... senv is the expression env. |
| 96 | + template <class _Env, class _SenderEnv> |
| 97 | + requires(!__callable<get_allocator_t, const _Env&>) |
| 98 | + && (!__callable<get_allocator_t, const _SenderEnv&>) |
| 99 | + const _Env& operator()(const _Env& __env, const _SenderEnv&) const { |
| 100 | + return __env; |
| 101 | + } |
| 102 | + }; |
| 103 | + |
| 104 | + inline constexpr __choose_senv_fn __choose_senv{}; |
| 105 | + } // namespace __spawn_common |
| 106 | +} // namespace stdexec |
0 commit comments