xtd 0.2.0
Loading...
Searching...
No Matches
convert_pointer.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "any.hpp"
8#include "static.hpp"
9#include "types.hpp"
10
12template<class new_type, class current_type>
13new_type* __convert_value__(current_type* value);
15
17namespace xtd {
30 public:
32
44 template<class new_type, class current_type>
45 static const new_type* to_ptr(const current_type* value) {
46 if (value == nullptr) return nullptr;
47 return &to_ref<new_type>(*value);
48 }
49
61 template<class new_type, class current_type>
62 static new_type* to_ptr(current_type* value) {
63 if (value == nullptr) return nullptr;
64 return &to_ref<new_type>(*value);
65 }
66
78 template<class new_type, class current_type>
79 static const new_type* to_ptr(const current_type& value) {
80 return &to_ref<new_type>(value);
81 }
82
94 template<class new_type, class current_type>
95 static new_type* to_ptr(current_type& value) {
96 return &to_ref<new_type>(value);
97 }
98
111 template<class new_type, class current_type>
113 return to_shared_ptr<new_type>(value);
114 }
115
127 template<class new_type, class current_type>
131
144 template<class new_type, class current_type>
146 return to_shared_ptr<new_type>(std::move(value));
147 }
148
161 template<class new_type, class current_type>
162 static const new_type& to_ref(const current_type& value) {
163 try {
164 return dynamic_cast<const new_type&>(value);
165 } catch (const std::exception& e) {
167 }
169 }
170
183 template<class new_type, class current_type>
184 static new_type& to_ref(current_type& value) {
185 try {
186 return dynamic_cast<new_type&>(value);
187 } catch (const std::exception& e) {
189 }
191 }
192
205 template<class new_type, class current_type>
206 static const new_type& to_ref(const current_type* value) {
208 try {
209 return dynamic_cast<const new_type&>(*value);
210 } catch (const std::exception& e) {
212 }
214 }
215
228 template<class new_type, class current_type>
229 static new_type& to_ref(current_type* value) {
231 try {
232 return dynamic_cast<new_type&>(*value);
233 } catch (const std::exception& e) {
235 }
237 }
238
251 template<class new_type, class current_type>
253 try {
254 [[maybe_unused]] auto result = dynamic_cast<new_type&>(*value.get());
255 } catch (const std::exception& e) {
257 }
258 return std::dynamic_pointer_cast<new_type>(value.pointer());
259 }
260
272 template<class new_type, class current_type>
274 try {
275 [[maybe_unused]] auto& result = dynamic_cast<new_type&>(*value.get());
276 } catch (const std::exception& e) {
278 }
279 return std::dynamic_pointer_cast<new_type>(value.pointer());
280 }
281
294 template<class new_type, class current_type>
296 try {
297 [[maybe_unused]] auto& result = dynamic_cast<new_type&>(*value.get());
298 } catch (const std::exception& e) {
300 }
301 //return std::move(value);
302 return std::dynamic_pointer_cast<new_type>(std::move(value.pointer()));
303 }
304
317 template<class new_type, class current_type>
319 return to_shared_ptr<new_type>(value);
320 }
321
333 template<class new_type, class current_type>
337
350 template<class new_type, class current_type>
352 return to_shared_ptr<new_type>(std::move(value));
353 }
354
367 template<class new_type, class current_type>
369 auto ptr = value.release();
370 try {
371 return xtd::uptr<new_type>(__convert_value__<new_type>(ptr));
372 } catch (const std::exception& e) {
375 }
377 }
378
390 template<class new_type, class current_type>
392 auto ptr = value.release();
393 try {
394 return xtd::uptr<new_type>(__convert_value__<new_type>(ptr));
395 } catch (const std::exception& e) {
398 }
400 }
401
414 template<class new_type, class current_type>
418
430 template<class new_type, class current_type>
432 return to_unique_ptr<new_type>(std::move(value));
433 }
434
435 };
436}
Contains xtd::any type and std::bad_any_cast exception.
Contains xtd::argument_null_exception exception.
Represents API to convert pointers.
Definition convert_pointer.hpp:29
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
@ invalid_cast
The cast is not valid.
Definition exception_case.hpp:63
@ argument_null
The argument is null.
Definition exception_case.hpp:33
#define static_
This keyword is use to represent a static object. A static object can't be instantiated (constructors...
Definition static.hpp:37
xtd::shared_ptr_object< type_t > sptr
The xtd::sptr object is a shared pointer.
Definition sptr.hpp:25
xtd::unique_ptr_object< type_t > uptr
The xtd::uptr object is a unique pointer.
Definition uptr.hpp:25
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.hpp:27
static xtd::ptr< new_type > to_ptr(xtd::ptr< current_type > &value)
Casts a type into another type.
Definition convert_pointer.hpp:128
static xtd::uptr< new_type > to_unique_ptr(xtd::uptr< current_type > &value)
Casts a type into another type.
Definition convert_pointer.hpp:368
static xtd::uptr< new_type > to_unique_ptr(xtd::uptr< current_type > &&value)
Casts a type into another type.
Definition convert_pointer.hpp:391
static xtd::sptr< new_type > to_sptr(xtd::sptr< current_type > &&value)
Casts a type into another type.
Definition convert_pointer.hpp:351
static new_type & to_ref(current_type *value)
Casts a type into another type.
Definition convert_pointer.hpp:229
static const new_type * to_ptr(const current_type &value)
Casts a type into another type.
Definition convert_pointer.hpp:79
static const new_type * to_ptr(const current_type *value)
Casts a type into another type.
Definition convert_pointer.hpp:45
static const new_type & to_ref(const current_type &value)
Casts a type into another type.
Definition convert_pointer.hpp:162
static xtd::sptr< new_type > to_sptr(xtd::sptr< current_type > &value)
Casts a type into another type.
Definition convert_pointer.hpp:334
static xtd::sptr< new_type > to_sptr(const xtd::sptr< current_type > &value)
Casts a type into another type.
Definition convert_pointer.hpp:318
static new_type * to_ptr(current_type *value)
Casts a type into another type.
Definition convert_pointer.hpp:62
static xtd::uptr< new_type > to_uptr(xtd::uptr< current_type > &value)
Casts a type into another type.
Definition convert_pointer.hpp:415
static xtd::ptr< new_type > to_ptr(xtd::ptr< current_type > &&value)
Casts a type into another type.
Definition convert_pointer.hpp:145
static new_type * to_ptr(current_type &value)
Casts a type into another type.
Definition convert_pointer.hpp:95
static const new_type & to_ref(const current_type *value)
Casts a type into another type.
Definition convert_pointer.hpp:206
static new_type & to_ref(current_type &value)
Casts a type into another type.
Definition convert_pointer.hpp:184
static xtd::sptr< new_type > to_shared_ptr(xtd::sptr< current_type > &value)
Casts a type into another type.
Definition convert_pointer.hpp:273
static xtd::uptr< new_type > to_uptr(xtd::uptr< current_type > &&value)
Casts a type into another type.
Definition convert_pointer.hpp:431
static xtd::ptr< new_type > to_ptr(const xtd::ptr< current_type > &value)
Casts a type into another type.
Definition convert_pointer.hpp:112
static xtd::sptr< new_type > to_shared_ptr(xtd::sptr< current_type > &&value)
Casts a type into another type.
Definition convert_pointer.hpp:295
static xtd::sptr< new_type > to_shared_ptr(const xtd::sptr< current_type > &value)
Casts a type into another type.
Definition convert_pointer.hpp:252
@ e
The E key.
Definition console_key.hpp:96
Contains xtd::invalid_cast_exception exception.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Contains xtd::static_object class.
Contains xtd fundamental types.