Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Slot.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2022 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
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#ifndef TOOLBOX_UTIL_SLOT_HPP
18#define TOOLBOX_UTIL_SLOT_HPP
19
21
22namespace toolbox {
23inline namespace util {
24
25template <typename... ArgsT>
26class BasicSlot {
27 public:
28 friend constexpr bool operator==(BasicSlot lhs, BasicSlot rhs) noexcept
29 {
30 return lhs.obj_ == rhs.obj_ && lhs.fn_ == rhs.fn_;
31 }
32 friend constexpr bool operator!=(BasicSlot lhs, BasicSlot rhs) noexcept
33 {
34 return !(lhs == rhs);
35 }
36 constexpr explicit BasicSlot(std::nullptr_t = nullptr) noexcept {}
37 ~BasicSlot() = default;
38
39 // Copy.
40 constexpr BasicSlot(const BasicSlot&) noexcept = default;
41 constexpr BasicSlot& operator=(const BasicSlot&) noexcept = default;
42
43 // Move.
46
47 void invoke(ArgsT... args) const { fn_(obj_, std::forward<ArgsT>(args)...); }
48 void operator()(ArgsT... args) const { fn_(obj_, std::forward<ArgsT>(args)...); }
49 constexpr bool empty() const noexcept { return fn_ == nullptr; }
50 constexpr explicit operator bool() const noexcept { return fn_ != nullptr; }
51
52 // Free function.
53 template <void (*FnT)(ArgsT...)>
54 constexpr auto& bind() noexcept
55 {
56 obj_ = nullptr;
57 fn_ = [](void* /*obj*/, ArgsT... args) { FnT(std::forward<ArgsT>(args)...); };
58 return *this;
59 }
60 // Lambda function.
61 template <typename ClassT>
62 constexpr auto& bind(ClassT* obj) noexcept
63 {
64 obj_ = obj;
65 fn_ = [](void* obj, ArgsT... args) {
66 (*static_cast<ClassT*>(obj))(std::forward<ArgsT>(args)...);
67 };
68 return *this;
69 }
70 // Member function.
71 template <auto MemFnT, typename ClassT = typename FunctionTraits<decltype(MemFnT)>::ClassType>
72 constexpr auto& bind(ClassT* obj) noexcept
73 {
74 obj_ = obj;
75 fn_ = [](void* obj, ArgsT... args) {
76 (static_cast<ClassT*>(obj)->*MemFnT)(std::forward<ArgsT>(args)...);
77 };
78 return *this;
79 }
80 void reset(std::nullptr_t = nullptr) noexcept
81 {
82 obj_ = nullptr;
83 fn_ = nullptr;
84 }
85
86 private:
87 void* obj_{nullptr};
88 void (*fn_)(void*, ArgsT...){nullptr};
89};
90
91template <auto FnT>
92constexpr auto bind() noexcept
93{
94 using Traits = FunctionTraits<decltype(FnT)>;
95 using Slot = typename Traits::template Pack<BasicSlot>;
96 return Slot{}.template bind<FnT>();
97}
98
99template <typename ClassT>
100constexpr auto bind(ClassT* obj) noexcept
101{
102 using Traits = FunctionTraits<decltype(&ClassT::operator())>;
103 using Slot = typename Traits::template Pack<BasicSlot>;
104 return Slot{}.bind(obj);
105}
106
107template <auto MemFnT, typename ClassT = typename FunctionTraits<decltype(MemFnT)>::ClassType>
108constexpr auto bind(ClassT* obj) noexcept
109{
110 using Traits = FunctionTraits<decltype(MemFnT)>;
111 using Slot = typename Traits::template Pack<BasicSlot>;
112 return Slot{}.template bind<MemFnT>(obj);
113}
114
115} // namespace util
116} // namespace toolbox
117
118#endif // TOOLBOX_UTIL_SLOT_HPP
void invoke(ArgsT... args) const
Definition Slot.hpp:47
constexpr BasicSlot & operator=(const BasicSlot &) noexcept=default
constexpr BasicSlot(BasicSlot &&) noexcept=default
void operator()(ArgsT... args) const
Definition Slot.hpp:48
constexpr BasicSlot(std::nullptr_t=nullptr) noexcept
Definition Slot.hpp:36
friend constexpr bool operator==(BasicSlot lhs, BasicSlot rhs) noexcept
Definition Slot.hpp:28
constexpr auto & bind(ClassT *obj) noexcept
Definition Slot.hpp:72
constexpr auto & bind(ClassT *obj) noexcept
Definition Slot.hpp:62
constexpr auto & bind() noexcept
Definition Slot.hpp:54
friend constexpr bool operator!=(BasicSlot lhs, BasicSlot rhs) noexcept
Definition Slot.hpp:32
constexpr BasicSlot(const BasicSlot &) noexcept=default
constexpr bool empty() const noexcept
Definition Slot.hpp:49
void reset(std::nullptr_t=nullptr) noexcept
Definition Slot.hpp:80
constexpr auto bind() noexcept
Definition Slot.hpp:92
Default case for functors and lambdas.
Definition Traits.hpp:27