dispenso
A library for task parallelism
 
Loading...
Searching...
No Matches
once_function.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
14#pragma once
15
16#include <utility>
17
18#include <dispenso/detail/once_callable_impl.h>
19
20namespace dispenso {
21namespace detail {
22template <typename Result>
23class FutureBase;
24template <typename Result>
25class FutureImplBase;
26} // namespace detail
27
38 public:
43#if defined DISPENSO_DEBUG
44 : onceCallable_(nullptr)
45#endif // DISPENSO_DEBUG
46 {
47 }
48
56 template <typename F>
57 OnceFunction(F&& f) : onceCallable_(detail::createOnceCallable(std::forward<F>(f))) {}
58
59 OnceFunction(const OnceFunction& other) = delete;
60
61 OnceFunction(OnceFunction&& other) : onceCallable_(other.onceCallable_) {
62#if defined DISPENSO_DEBUG
63 other.onceCallable_ = nullptr;
64#endif // DISPENSO_DEBUG
65 }
66
67 OnceFunction& operator=(OnceFunction&& other) {
68 onceCallable_ = other.onceCallable_;
69#if defined DISPENSO_DEBUG
70 if (&other != this) {
71 other.onceCallable_ = nullptr;
72 }
73#endif // DISPENSO_DEBUG
74 return *this;
75 }
76
81 void operator()() const {
82#if defined DISPENSO_DEBUG
83 assert(onceCallable_ != nullptr && "Must not use OnceFunction more than once!");
84#endif // DISPENSO_DEBUG
85
86 onceCallable_->run();
87
88#if defined DISPENSO_DEBUG
89 onceCallable_ = nullptr;
90#endif // DISPENSO_DEBUG
91 }
92
93 private:
94 OnceFunction(detail::OnceCallable* func, bool) : onceCallable_(func) {}
95
96 mutable detail::OnceCallable* onceCallable_;
97
98 template <typename Result>
99 friend class detail::FutureBase;
100 template <typename Result>
101 friend class detail::FutureImplBase;
102};
103
104} // namespace dispenso
detail::OpResult< T > OpResult
Definition pipeline.h:29