dispenso 1.4.1
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
15#pragma once
16
17#include <utility>
18
19#include <dispenso/detail/once_callable_impl.h>
20#include <dispenso/platform.h>
21
22namespace dispenso {
23
24#if DISPENSO_HAS_CONCEPTS
33template <typename F>
34concept OnceCallableFunc = std::invocable<F>;
35#endif // DISPENSO_HAS_CONCEPTS
36
37namespace detail {
38template <typename Result>
39class FutureBase;
40template <typename Result>
41class FutureImplBase;
42} // namespace detail
43
54 public:
59#if defined DISPENSO_DEBUG
60 : onceCallable_(nullptr)
61#endif // DISPENSO_DEBUG
62 {
63 }
64
72 template <typename F>
73 DISPENSO_REQUIRES(OnceCallableFunc<F>)
74 OnceFunction(F&& f) : onceCallable_(detail::createOnceCallable(std::forward<F>(f))) {}
75
76 OnceFunction(const OnceFunction& other) = delete;
77
79 OnceFunction(OnceFunction&& other) : onceCallable_(other.onceCallable_) {
80#if defined DISPENSO_DEBUG
81 other.onceCallable_ = nullptr;
82#endif // DISPENSO_DEBUG
83 }
84
85 OnceFunction& operator=(OnceFunction&& other) {
86 onceCallable_ = other.onceCallable_;
87#if defined DISPENSO_DEBUG
88 if (&other != this) {
89 other.onceCallable_ = nullptr;
90 }
91#endif // DISPENSO_DEBUG
92 return *this;
93 }
94
99 void operator()() const {
100#if defined DISPENSO_DEBUG
101 assert(onceCallable_ != nullptr && "Must not use OnceFunction more than once!");
102#endif // DISPENSO_DEBUG
103
104 onceCallable_->run();
105
106#if defined DISPENSO_DEBUG
107 onceCallable_ = nullptr;
108#endif // DISPENSO_DEBUG
109 }
110
111 private:
112 OnceFunction(detail::OnceCallable* func, bool) : onceCallable_(func) {}
113
114 mutable detail::OnceCallable* onceCallable_;
115
116 template <typename Result>
117 friend class detail::FutureBase;
118 template <typename Result>
119 friend class detail::FutureImplBase;
120};
121
122} // namespace dispenso
OnceFunction(OnceFunction &&other)