PLCnext API Documentation 25.0.2.69
propagate_const.hxx
1
2//
3// Copyright Phoenix Contact GmbH & Co. KG
4//
6#pragma once
8#include <type_traits>
9#include <utility>
10#include <memory>
11namespace Arp { namespace Base { namespace Core
12{
13
24template<class T>
26{
27public:
29 using element_type = typename std::pointer_traits<T>::element_type;
30
31private:
32 template <class U>
33 struct is_propagate_const : std::false_type
34 {
35 };
36
37 template <class U>
38 struct is_propagate_const<propagate_const<U>> : std::true_type
39 {
40 };
41
42public:
44 constexpr propagate_const() = default;
46 propagate_const(const propagate_const& other) = delete;
48 constexpr propagate_const(propagate_const&& other) noexcept = default;
49
51 template < class U, class = std::enable_if_t < std::is_constructible_v < T, U && >>>
52 explicit constexpr propagate_const(propagate_const<U>&& other)
53 : t(std::move(other.t))
54 {
55 }
56
59 template < class U, class = std::enable_if_t < std::is_constructible_v < T, U && > && !is_propagate_const<std::decay_t<U>>::value >>
60 explicit constexpr propagate_const(U && other)
61 : t(std::forward<U>(other))
62 {
63 }
64
66 propagate_const& operator=(const propagate_const& other) = delete;
67
69 propagate_const& operator=(propagate_const&& other) noexcept = default;
70
72 template<class U, class = std::enable_if_t<std::is_convertible_v<U, T>>>
74 {
75 this->t = std::move<U>(other.t);
76 return *this;
77 }
78
81 template < class U, class = std::enable_if_t < std::is_convertible_v<U, T>&& !is_propagate_const<std::decay_t<U>>::value >>
83 {
84 this->t = std::forward<U>(other);
85 return *this;
86 }
87
90 void swap(propagate_const& other)noexcept
91 {
92 std::swap(this->t, other.t);
93 }
94
97 {
98 return GetPointer(this->t);
99 }
100
102 const element_type* get()const
103 {
104 return GetPointer(this->t);
105 }
106
108 explicit operator bool()const
109 {
110 return this->get() != nullptr;
111 }
112
115 {
116 return *this->get();
117 }
118
121 {
122 return *this->get();
123 }
124
127 {
128 return this->get();
129 }
130
133 {
134 return this->get();
135 }
136
139 template <class T_ = T, class U = std::enable_if_t<std::is_convertible_v<const T_, const element_type*>>>
140 operator element_type * ()
141 {
142 return this->get();
143 }
144
147 template <class T_ = T, class U = std::enable_if_t<std::is_convertible_v<const T_, const element_type*>>>
148 operator const element_type * ()const
149 {
150 return this->get();
151 }
152
154 friend constexpr bool operator==(const propagate_const& lhs, std::nullptr_t)
155 {
156 return lhs.t == nullptr;
157 }
158
160 friend constexpr bool operator==(std::nullptr_t, const propagate_const& rhs)
161 {
162 return nullptr == rhs.t;
163 }
164
166 friend constexpr bool operator!=(const propagate_const& lhs, std::nullptr_t)
167 {
168 return lhs.t != nullptr;
169 }
170
172 friend constexpr bool operator!=(std::nullptr_t, const propagate_const& rhs)
173 {
174 return nullptr != rhs.t;
175 }
176
178 template<class U>
179 friend constexpr bool operator==(const propagate_const& lhs, const propagate_const<U>& rhs)
180 {
181 return lhs.t == rhs.t;
182 }
183
185 template<class U>
186 friend constexpr bool operator!=(const propagate_const& lhs, const propagate_const<U>& rhs)
187 {
188 return lhs.t != rhs.t;
189 }
190
192 template<class U>
193 friend constexpr bool operator==(const propagate_const& lhs, const U& rhs)
194 {
195 return lhs.t == rhs;
196 }
197
199 template<class U>
200 friend constexpr bool operator!=(const propagate_const& lhs, const U& rhs)
201 {
202 return lhs.t != rhs;
203 }
204
206 template<class U>
207 friend constexpr bool operator==(const U& lhs, const propagate_const& rhs)
208 {
209 return lhs.t == rhs;
210 }
211
213 template<class U>
214 friend constexpr bool operator!=(const U& lhs, const propagate_const& rhs)
215 {
216 return lhs.t != rhs;
217 }
218
219private: // methods
220 template <class U>
221 static constexpr element_type* GetPointer(U* u)
222 {
223 return u;
224 }
225
226 template <class U>
227 static constexpr element_type* GetPointer(U& u)
228 {
229 return GetPointer(u.get());
230 }
231
232 template <class U>
233 static constexpr const element_type* GetPointer(const U* u)
234 {
235 return u;
236 }
237
238 template <class U>
239 static constexpr const element_type* GetPointer(const U& u)
240 {
241 return GetPointer(u.get());
242 }
243
244private: // fields
245 T t{};
246};
247
248}}} // end of namespace Arp::Base::Core
249
250namespace Arp {
253}
Encapsulates a (smart) pointer to ensure const-correctness even on pointer.
Definition: propagate_const.hxx:26
friend constexpr bool operator!=(std::nullptr_t, const propagate_const &rhs)
Equality comparison with nullptr
Definition: propagate_const.hxx:172
element_type * operator->()
Returns a raw pointer to the pointee
Definition: propagate_const.hxx:126
propagate_const & operator=(propagate_const< U > &&other)
Move assign from a propagate_const object of compatible type
Definition: propagate_const.hxx:73
void swap(propagate_const &other) noexcept
Swaps the contents of this and other
Definition: propagate_const.hxx:90
const element_type * operator->() const
Returns a const raw pointer to the pointee
Definition: propagate_const.hxx:132
friend constexpr bool operator!=(const propagate_const &lhs, std::nullptr_t)
Equality comparison with nullptr
Definition: propagate_const.hxx:166
friend constexpr bool operator==(const propagate_const &lhs, const propagate_const< U > &rhs)
Equality comparison with other propagate_const object
Definition: propagate_const.hxx:179
propagate_const & operator=(U &&other)
Assignment from an object of compatible type
Definition: propagate_const.hxx:82
propagate_const & operator=(propagate_const &&other) noexcept=default
Move assignment operator
element_type & operator*()
Returns a reference to the pointee
Definition: propagate_const.hxx:114
const element_type & operator*() const
Returns a const reference to the pointee
Definition: propagate_const.hxx:120
friend constexpr bool operator!=(const U &lhs, const propagate_const &rhs)
Equality comparison with underlying object type
Definition: propagate_const.hxx:214
friend constexpr bool operator==(std::nullptr_t, const propagate_const &rhs)
Equality comparison with nullptr
Definition: propagate_const.hxx:160
friend constexpr bool operator==(const propagate_const &lhs, const U &rhs)
Equality comparison with underlying object type
Definition: propagate_const.hxx:193
friend constexpr bool operator!=(const propagate_const &lhs, const U &rhs)
Equality comparison with underlying object type
Definition: propagate_const.hxx:200
propagate_const & operator=(const propagate_const &other)=delete
Copy assignment operator
friend constexpr bool operator!=(const propagate_const &lhs, const propagate_const< U > &rhs)
Equality comparison with other propagate_const object
Definition: propagate_const.hxx:186
constexpr propagate_const(propagate_const &&other) noexcept=default
Move constructor
constexpr propagate_const(U &&other)
Construct from an object of compatible type
Definition: propagate_const.hxx:60
propagate_const(const propagate_const &other)=delete
Copy constructor
constexpr propagate_const()=default
Creates object pointing to nullptr
element_type * get()
Returns a raw pointer to the pointee
Definition: propagate_const.hxx:96
typename std::pointer_traits< T >::element_type element_type
Type of the object pointed to by T
Definition: propagate_const.hxx:29
constexpr propagate_const(propagate_const< U > &&other)
Construct from a propagate_const object of compatible type
Definition: propagate_const.hxx:52
const element_type * get() const
Returns a const raw pointer to the pointee
Definition: propagate_const.hxx:102
friend constexpr bool operator==(const U &lhs, const propagate_const &rhs)
Equality comparison with underlying object type
Definition: propagate_const.hxx:207
friend constexpr bool operator==(const propagate_const &lhs, std::nullptr_t)
Equality comparison with nullptr
Definition: propagate_const.hxx:154
Root namespace for the PLCnext API
Namespace of the C++ standard library