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_t, class current_type_t>
13new_type_t* __convert_value__(current_type_t* value);
15
17namespace xtd {
30 public:
32
44 template<class new_type_t, class current_type_t>
45 static const new_type_t* to_ptr(const current_type_t* value) {
46 if (value == nullptr) return nullptr;
47 return &to_ref<new_type_t>(*value);
48 }
49
61 template<class new_type_t, class current_type_t>
62 static new_type_t* to_ptr(current_type_t* value) {
63 if (value == nullptr) return nullptr;
64 return &to_ref<new_type_t>(*value);
65 }
66
78 template<class new_type_t, class current_type_t>
79 static const new_type_t* to_ptr(const current_type_t& value) {
80 return &to_ref<new_type_t>(value);
81 }
82
94 template<class new_type_t, class current_type_t>
95 static new_type_t* to_ptr(current_type_t& value) {
96 return &to_ref<new_type_t>(value);
97 }
98
111 template<class new_type_t, class current_type_t>
113 return to_shared_ptr<new_type_t>(value);
114 }
115
127 template<class new_type_t, class current_type_t>
131
144 template<class new_type_t, class current_type_t>
146 return to_shared_ptr<new_type_t>(std::move(value));
147 }
148
161 template<class new_type_t, class current_type_t>
162 static const new_type_t& to_ref(const current_type_t& value) {
163 try {
164 return dynamic_cast<const new_type_t&>(value);
165 } catch (const std::exception& e) {
167 }
169 }
170
183 template<class new_type_t, class current_type_t>
184 static new_type_t& to_ref(current_type_t& value) {
185 try {
186 return dynamic_cast<new_type_t&>(value);
187 } catch (const std::exception& e) {
189 }
191 }
192
205 template<class new_type_t, class current_type_t>
206 static const new_type_t& to_ref(const current_type_t* value) {
208 try {
209 return dynamic_cast<const new_type_t&>(*value);
210 } catch (const std::exception& e) {
212 }
214 }
215
228 template<class new_type_t, class current_type_t>
229 static new_type_t& to_ref(current_type_t* value) {
231 try {
232 return dynamic_cast<new_type_t&>(*value);
233 } catch (const std::exception& e) {
235 }
237 }
238
251 template<class new_type_t, class current_type_t>
253 try {
254 [[maybe_unused]] auto result = dynamic_cast<new_type_t&>(*value.get());
255 } catch (const std::exception& e) {
257 }
258 return std::dynamic_pointer_cast<new_type_t>(value.pointer());
259 }
260
272 template<class new_type_t, class current_type_t>
274 try {
275 [[maybe_unused]] auto& result = dynamic_cast<new_type_t&>(*value.get());
276 } catch (const std::exception& e) {
278 }
279 return std::dynamic_pointer_cast<new_type_t>(value.pointer());
280 }
281
294 template<class new_type_t, class current_type_t>
296 try {
297 [[maybe_unused]] auto& result = dynamic_cast<new_type_t&>(*value.get());
298 } catch (const std::exception& e) {
300 }
301 //return std::move(value);
302 return std::dynamic_pointer_cast<new_type_t>(std::move(value.pointer()));
303 }
304
317 template<class new_type_t, class current_type_t>
321
333 template<class new_type_t, class current_type_t>
337
350 template<class new_type_t, class current_type_t>
352 return to_shared_ptr<new_type_t>(std::move(value));
353 }
354
367 template<class new_type_t, class current_type_t>
369 auto ptr = value.release();
370 try {
371 return xtd::uptr<new_type_t>(__convert_value__<new_type_t>(ptr));
372 } catch (const std::exception& e) {
375 }
377 }
378
390 template<class new_type_t, class current_type_t>
392 auto ptr = value.release();
393 try {
394 return xtd::uptr<new_type_t>(__convert_value__<new_type_t>(ptr));
395 } catch (const std::exception& e) {
398 }
400 }
401
414 template<class new_type_t, class current_type_t>
418
430 template<class new_type_t, class current_type_t>
432 return to_unique_ptr<new_type_t>(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 const new_type_t * to_ptr(const current_type_t &value)
Casts a type into another type.
Definition convert_pointer.hpp:79
static const new_type_t * to_ptr(const current_type_t *value)
Casts a type into another type.
Definition convert_pointer.hpp:45
static xtd::sptr< new_type_t > to_sptr(const xtd::sptr< current_type_t > &value)
Casts a type into another type.
Definition convert_pointer.hpp:318
static xtd::sptr< new_type_t > to_sptr(xtd::sptr< current_type_t > &&value)
Casts a type into another type.
Definition convert_pointer.hpp:351
static new_type_t & to_ref(current_type_t *value)
Casts a type into another type.
Definition convert_pointer.hpp:229
static xtd::uptr< new_type_t > to_unique_ptr(xtd::uptr< current_type_t > &&value)
Casts a type into another type.
Definition convert_pointer.hpp:391
static new_type_t * to_ptr(current_type_t &value)
Casts a type into another type.
Definition convert_pointer.hpp:95
static xtd::ptr< new_type_t > to_ptr(xtd::ptr< current_type_t > &&value)
Casts a type into another type.
Definition convert_pointer.hpp:145
static xtd::sptr< new_type_t > to_shared_ptr(xtd::sptr< current_type_t > &&value)
Casts a type into another type.
Definition convert_pointer.hpp:295
static xtd::sptr< new_type_t > to_shared_ptr(const xtd::sptr< current_type_t > &value)
Casts a type into another type.
Definition convert_pointer.hpp:252
static xtd::sptr< new_type_t > to_sptr(xtd::sptr< current_type_t > &value)
Casts a type into another type.
Definition convert_pointer.hpp:334
static new_type_t * to_ptr(current_type_t *value)
Casts a type into another type.
Definition convert_pointer.hpp:62
static const new_type_t & to_ref(const current_type_t &value)
Casts a type into another type.
Definition convert_pointer.hpp:162
static xtd::uptr< new_type_t > to_unique_ptr(xtd::uptr< current_type_t > &value)
Casts a type into another type.
Definition convert_pointer.hpp:368
static const new_type_t & to_ref(const current_type_t *value)
Casts a type into another type.
Definition convert_pointer.hpp:206
static xtd::sptr< new_type_t > to_shared_ptr(xtd::sptr< current_type_t > &value)
Casts a type into another type.
Definition convert_pointer.hpp:273
static new_type_t & to_ref(current_type_t &value)
Casts a type into another type.
Definition convert_pointer.hpp:184
static xtd::uptr< new_type_t > to_uptr(xtd::uptr< current_type_t > &value)
Casts a type into another type.
Definition convert_pointer.hpp:415
static xtd::ptr< new_type_t > to_ptr(const xtd::ptr< current_type_t > &value)
Casts a type into another type.
Definition convert_pointer.hpp:112
static xtd::uptr< new_type_t > to_uptr(xtd::uptr< current_type_t > &&value)
Casts a type into another type.
Definition convert_pointer.hpp:431
static xtd::ptr< new_type_t > to_ptr(xtd::ptr< current_type_t > &value)
Casts a type into another type.
Definition convert_pointer.hpp:128
@ 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.