PLCnext API Documentation 25.0.2.69
slim_delegate.hxx
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
7
8#ifndef ARP_USE_ARP_SYSTEM_CORE
9
10// #error "Do not use this file any more"
11
12#else
13
14#include "Arp/System/Core/Arp.h"
15
16namespace Arp
17{
18
19template <typename T> class slim_delegate;
20
22// class slim_delegate is implemented completely implicite inline to avoid
23// code bloat in commonly used Core classe and therefore reduce compile time
24
26ARP_DEPRECATED("This class is not supported any more.")
27template<class R, class ...A>
28class slim_delegate<R(A...)>
29{
30private: // typedefs/usings
31 using InvokerPtr = R(*)(void*, A && ...);
32
33public: // construction/destruction/assignment
34 slim_delegate(const slim_delegate&) = default;
35 slim_delegate(slim_delegate&&)noexcept = default;
36 slim_delegate& operator=(const slim_delegate&) = default;
37 slim_delegate& operator=(slim_delegate&&)noexcept = default;
38
39public: // static factory operations
40
42 template <R(*TFunction)(A...)>
43 ARP_DEPRECATED("This class is not supported any more.")
44 static slim_delegate create(void) noexcept
45 {
46 return{ functionInvoker<TFunction> };
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* pObject) noexcept
53 {
54 return{ pObject, methodInvoker<C, TMethod> };
55 }
56
58 template <class C, R(C::*TMethod)(A...)>
59 ARP_DEPRECATED("This class is not supported any more.")
60 static slim_delegate create(C& obj) noexcept
61 {
62 return{ &obj, methodInvoker<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* pObject) noexcept
69 {
70 return{ const_cast<C*>(pObject), constMethodInvoker<C, TMethod> };
71 }
72
74 template <class C, R(C::*TMethod)(A...)const>
75 ARP_DEPRECATED("This class is not supported any more.")
76 static slim_delegate create(const C& obj) noexcept
77 {
78 return{ const_cast<C*>(&obj), constMethodInvoker<C, TMethod> };
79 }
80
81public: // operators
82 bool operator==(const slim_delegate& rhs)const noexcept
83 {
84 if (this->is_static())
85 {
86 return this->pInvoker == rhs.pInvoker;
87 }
88 // else
89 return (this->pInstance == rhs.pInstance) && (this->pInvoker == rhs.pInvoker);
90 }
91
92 bool operator!=(const slim_delegate& rhs)const noexcept
93 {
94 return !(operator==(rhs));
95 }
96
97public: // nullptr and bool support
98 slim_delegate(std::nullptr_t) noexcept : slim_delegate(nullptr, nullptr) { }
99
100 bool operator==(std::nullptr_t)const noexcept
101 {
102 if (this->is_static())
103 {
104 return this->pInvoker == nullptr;
105 }
106 // else
107 return this->pInstance == nullptr || this->pInvoker == nullptr;
108 }
109
110 bool operator!=(std::nullptr_t)const noexcept
111 {
112 return !(*this == nullptr);
113 }
114
115 explicit operator bool()const noexcept
116 {
117 return *this != nullptr;
118 }
119
120public: // functor operator ()
121 R operator()(A... args) const
122 {
123 return pInvoker(this->pInstance, std::forward<A>(args)...);
124 }
125
126private: // construction
127 slim_delegate(InvokerPtr invokerPtr) noexcept
128 : pInstance(this), pInvoker(invokerPtr) // set pInstance to this a make operator bool work correctly
129 {
130 }
131
132 slim_delegate(void* pObject, InvokerPtr invokerPtr) noexcept
133 : pInstance(pObject), pInvoker(invokerPtr)
134 {
135 }
136
137 bool is_static(void)const
138 {
139 return this->pInstance == this;
140 }
141
142private: // invoker methods stored by pInvoker
143 template <R(*TFunction)(A...)>
144 static R functionInvoker(void* const, A&& ... args)
145 {
146 return TFunction(std::move(args)...);
147 }
148
149 template <class C, R(C::*TMethod)(A...)>
150 static R methodInvoker(void* pObject, A&& ... args)
151 {
152 return (static_cast<C*>(pObject)->*TMethod)(std::move(args)...);
153 }
154
155 template <class C, R(C::*TMethod)(A...) const>
156 static R constMethodInvoker(void* pObject, A&& ... args)
157 {
158 return (static_cast<const C*>(pObject)->*TMethod)(std::move(args)...);
159 }
160
161private: // fields
162 void* pInstance;
163 InvokerPtr pInvoker;
164};
165
166} // end of namespace Arp
167
168#endif // ndef ARP_USE_ARP_SYSTEM_CORE
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