Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
move_only_function.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <concepts>
6#include <cstddef>
7#include <functional>
8#include <memory>
9#include <type_traits>
10#include <utility>
11
12namespace portals::detail {
13
17template <class Signature> class BasicMoveOnlyFunction;
18
26template <class R, class... Args> class BasicMoveOnlyFunction<R(Args...)> {
27public:
29 BasicMoveOnlyFunction() noexcept = default;
30
34 BasicMoveOnlyFunction(std::nullptr_t empty) noexcept {
35 (void)empty;
36 }
37
42 // Reject noncallable conversion candidates before inspecting their potentially recursive constructors.
43 template <class F>
44 requires(!std::same_as<std::remove_cvref_t<F>, BasicMoveOnlyFunction> &&
45 std::is_invocable_r_v<R, std::decay_t<F> &, Args...> && std::constructible_from<std::decay_t<F>, F>)
46 BasicMoveOnlyFunction(F &&callable) {
47 if constexpr (std::is_pointer_v<std::decay_t<F>> || std::is_member_pointer_v<std::decay_t<F>>) {
48 if (callable == nullptr) {
49 return;
50 }
51 }
52
53 target_ = std::make_unique<Target<std::decay_t<F>>>(std::forward<F>(callable));
54 }
55
59 BasicMoveOnlyFunction(BasicMoveOnlyFunction &&other) noexcept = default;
60
66
69
74 BasicMoveOnlyFunction &operator=(std::nullptr_t empty) noexcept {
75 (void)empty;
76 target_.reset();
77
78 return *this;
79 }
80
82 explicit operator bool() const noexcept {
83 return static_cast<bool>(target_);
84 }
85
92 R operator()(Args... args) {
93 return target_->invoke(std::forward<Args>(args)...);
94 }
95
96private:
97 struct TargetBase {
98 virtual ~TargetBase() = default;
99 virtual R invoke(Args... args) = 0;
100 };
101
102 template <class F> struct Target final : TargetBase {
103 template <class Input> explicit Target(Input &&callable) : callable_(std::forward<Input>(callable)) {
104 }
105
106 R invoke(Args... args) override {
107 if constexpr (std::is_void_v<R>) {
108 std::invoke(callable_, std::forward<Args>(args)...);
109 } else {
110 return std::invoke(callable_, std::forward<Args>(args)...);
111 }
112 }
113
114 F callable_;
115 };
116
117 std::unique_ptr<TargetBase> target_;
118};
119
123#if defined(__cpp_lib_move_only_function) && __cpp_lib_move_only_function >= 202110L
124template <class Signature> using MoveOnlyFunction = std::move_only_function<Signature>;
125#else
126template <class Signature> using MoveOnlyFunction = BasicMoveOnlyFunction<Signature>;
127#endif
128
129} // namespace portals::detail
BasicMoveOnlyFunction(BasicMoveOnlyFunction &&other) noexcept=default
Transfer the target, leaving the source empty.
BasicMoveOnlyFunction & operator=(std::nullptr_t empty) noexcept
Release the owned callable and its captures.
Definition move_only_function.hpp:74
BasicMoveOnlyFunction() noexcept=default
Construct an empty callable without allocation.
BasicMoveOnlyFunction & operator=(BasicMoveOnlyFunction &&other) noexcept=default
Release the old target and transfer ownership from another callable.
R operator()(Args... args)
Invoke the owned target without consuming it.
Definition move_only_function.hpp:92
BasicMoveOnlyFunction(F &&callable)
Own a callable by copying or moving its decayed value.
Definition move_only_function.hpp:46
Internal owning callable fallback for libraries without C++23 move_only_function.
Definition move_only_function.hpp:17