PLCnext API Documentation 24.0.0.71
slim_delegate.hxx
1
2//
3// Copyright PHOENIX CONTACT Electronics GmbH
4//
6#pragma once
8
9namespace Arp
10{
11
12template <typename T> class slim_delegate;
13
15// class slim_delegate is implemented completely implicite inline to avoid
16// code bloat in commonly used Core classe and therefore reduce compile time
17
19template<class R, class ...A>
20class slim_delegate<R(A...)>
21{
22private: // typedefs/usings
23 using InvokerPtr = R(*)(void*, A && ...);
24
25public: // construction/destruction/assignment
26 slim_delegate(const slim_delegate&) = default;
27 slim_delegate(slim_delegate&&)noexcept = default;
28 slim_delegate& operator=(const slim_delegate&) = default;
29 slim_delegate& operator=(slim_delegate&&)noexcept = default;
30
31public: // static factory operations
32
34 template <R(*TFunction)(A...)>
35 ARP_DEPRECATED("This class is not supported any more.")
36 static slim_delegate create(void) noexcept
37 {
38 return{ functionInvoker<TFunction> };
39 }
40
42 template <class C, R(C::*TMethod)(A...)>
43 ARP_DEPRECATED("This class is not supported any more.")
44 static slim_delegate create(C* pObject) noexcept
45 {
46 return{ pObject, methodInvoker<C, TMethod> };
47 }
48
50 template <class C, R(C::*TMethod)(A...)>
51 ARP_DEPRECATED("This class is not supported any more.")
52 static slim_delegate create(C& obj) noexcept
53 {
54 return{ &obj, methodInvoker<C, TMethod> };
55 }
56
58 template <class C, R(C::*TMethod)(A...)const>
59 ARP_DEPRECATED("This class is not supported any more.")
60 static slim_delegate create(const C* pObject) noexcept
61 {
62 return{ const_cast<C*>(pObject), constMethodInvoker<C, TMethod> };
63 }
64
66 template <class C, R(C::*TMethod)(A...)const>
67 ARP_DEPRECATED("This class is not supported any more.")
68 static slim_delegate create(const C& obj) noexcept
69 {
70 return{ const_cast<C*>(&obj), constMethodInvoker<C, TMethod> };
71 }
72
73public: // operators
74 bool operator==(const slim_delegate& rhs)const noexcept
75 {
76 if (this->is_static())
77 {
78 return this->pInvoker == rhs.pInvoker;
79 }
80 // else
81 return (this->pInstance == rhs.pInstance) && (this->pInvoker == rhs.pInvoker);
82 }
83
84 bool operator!=(const slim_delegate& rhs)const noexcept
85 {
86 return !(operator==(rhs));
87 }
88
89public: // nullptr and bool support
90 slim_delegate(std::nullptr_t) noexcept : slim_delegate(nullptr, nullptr) { }
91
92 bool operator==(std::nullptr_t)const noexcept
93 {
94 if (this->is_static())
95 {
96 return this->pInvoker == nullptr;
97 }
98 // else
99 return this->pInstance == nullptr || this->pInvoker == nullptr;
100 }
101
102 bool operator!=(std::nullptr_t)const noexcept
103 {
104 return !(*this == nullptr);
105 }
106
107 explicit operator bool()const noexcept
108 {
109 return *this != nullptr;
110 }
111
112public: // functor operator ()
113 R operator()(A... args) const
114 {
115 return pInvoker(this->pInstance, std::forward<A>(args)...);
116 }
117
118private: // construction
119 slim_delegate(InvokerPtr invokerPtr) noexcept
120 : pInstance(this), pInvoker(invokerPtr) // set pInstance to this a make operator bool work correctly
121 {
122 }
123
124 slim_delegate(void* pObject, InvokerPtr invokerPtr) noexcept
125 : pInstance(pObject), pInvoker(invokerPtr)
126 {
127 }
128
129 bool is_static(void)const
130 {
131 return this->pInstance == this;
132 }
133
134private: // invoker methods stored by pInvoker
135 template <R(*TFunction)(A...)>
136 static R functionInvoker(void* const, A&& ... args)
137 {
138 return TFunction(std::move(args)...);
139 }
140
141 template <class C, R(C::*TMethod)(A...)>
142 static R methodInvoker(void* pObject, A&& ... args)
143 {
144 return (static_cast<C*>(pObject)->*TMethod)(std::move(args)...);
145 }
146
147 template <class C, R(C::*TMethod)(A...) const>
148 static R constMethodInvoker(void* pObject, A&& ... args)
149 {
150 return (static_cast<const C*>(pObject)->*TMethod)(std::move(args)...);
151 }
152
153private: // fields
154 void* pInstance;
155 InvokerPtr pInvoker;
156};
157
158} // end of namespace Arp
Definition: slim_delegate.hxx:12
bool operator!=(const BasicString< CharType, Alloc > &left, const BasicString< CharType, Alloc > &right)
Compares the left string to the right string on inequality.
Definition: BasicString.hxx:1944
bool operator==(const BasicString< CharType, Alloc > &left, const BasicString< CharType, Alloc > &right)
Compares the left string to the right string on equality.
Definition: BasicString.hxx:1908
Root namespace for the PLCnext API
class ARP_DEPRECATED("Use Arp::Enum<T> instead.") EnumStrings
Deprecated! The class implements an adapter for enums to define the string literals of the enum entri...
Definition: EnumStrings.hxx:38