xtd 0.2.0
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#include "unused.hpp"
11
13template<class new_type_t, class current_type_t>
14new_type_t* __convert_value__(current_type_t* value);
16
18namespace xtd {
31 public:
33
45 template<class new_type_t, class current_type_t>
46 static const new_type_t* to_ptr(const current_type_t* value) {
47 if (value == nullptr) return nullptr;
48 return &to_ref<new_type_t>(*value);
49 }
50
62 template<class new_type_t, class current_type_t>
63 static new_type_t* to_ptr(current_type_t* value) {
64 if (value == nullptr) return nullptr;
65 return &to_ref<new_type_t>(*value);
66 }
67
79 template<class new_type_t, class current_type_t>
80 static const new_type_t* to_ptr(const current_type_t& value) {
81 return &to_ref<new_type_t>(value);
82 }
83
95 template<class new_type_t, class current_type_t>
96 static new_type_t* to_ptr(current_type_t& value) {
97 return &to_ref<new_type_t>(value);
98 }
99
112 template<class new_type_t, class current_type_t>
114 return to_shared_ptr<new_type_t>(value);
115 }
116
128 template<class new_type_t, class current_type_t>
132
145 template<class new_type_t, class current_type_t>
147 return to_shared_ptr<new_type_t>(std::move(value));
148 }
149
162 template<class new_type_t, class current_type_t>
163 static const new_type_t& to_ref(const current_type_t& value) {
164 try {
165 return dynamic_cast<const new_type_t&>(value);
166 } catch (const std::exception& e) {
168 }
170 }
171
184 template<class new_type_t, class current_type_t>
185 static new_type_t& to_ref(current_type_t& value) {
186 try {
187 return dynamic_cast<new_type_t&>(value);
188 } catch (const std::exception& e) {
190 }
192 }
193
206 template<class new_type_t, class current_type_t>
207 static const new_type_t& to_ref(const current_type_t* value) {
209 try {
210 return dynamic_cast<const new_type_t&>(*value);
211 } catch (const std::exception& e) {
213 }
215 }
216
229 template<class new_type_t, class current_type_t>
230 static new_type_t& to_ref(current_type_t* value) {
232 try {
233 return dynamic_cast<new_type_t&>(*value);
234 } catch (const std::exception& e) {
236 }
238 }
239
252 template<class new_type_t, class current_type_t>
254 try {
255 unused_(dynamic_cast<new_type_t&>(*value.get()));
256 } catch (const std::exception& e) {
258 }
259 return std::dynamic_pointer_cast<new_type_t>(value.pointer());
260 }
261
273 template<class new_type_t, class current_type_t>
275 try {
276 unused_(dynamic_cast<new_type_t&>(*value.get()));
277 } catch (const std::exception& e) {
279 }
280 return std::dynamic_pointer_cast<new_type_t>(value.pointer());
281 }
282
295 template<class new_type_t, class current_type_t>
297 try {
298 unused_(dynamic_cast<new_type_t&>(*value.get()));
299 } catch (const std::exception& e) {
301 }
302 //return std::move(value);
303 return std::dynamic_pointer_cast<new_type_t>(std::move(value.pointer()));
304 }
305
318 template<class new_type_t, class current_type_t>
322
334 template<class new_type_t, class current_type_t>
338
351 template<class new_type_t, class current_type_t>
353 return to_shared_ptr<new_type_t>(std::move(value));
354 }
355
368 template<class new_type_t, class current_type_t>
370 auto ptr = value.release();
371 try {
372 return xtd::uptr<new_type_t>(__convert_value__<new_type_t>(ptr));
373 } catch (const std::exception& e) {
376 }
378 }
379
391 template<class new_type_t, class current_type_t>
393 auto ptr = value.release();
394 try {
395 return xtd::uptr<new_type_t>(__convert_value__<new_type_t>(ptr));
396 } catch (const std::exception& e) {
399 }
401 }
402
415 template<class new_type_t, class current_type_t>
419
431 template<class new_type_t, class current_type_t>
433 return to_unique_ptr<new_type_t>(std::move(value));
434 }
435
436 };
437}
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:30
static void throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current())
Throws an exption with specified exception case.
@ invalid_cast
The cast is not valid.
Definition exception_case.hpp:61
@ argument_null
The argument is null.
Definition exception_case.hpp:33
#define unused_
It may be used to suppress the "unused variable" or "unused local typedefs" compiler warnings when th...
Definition unused.hpp:30
#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:80
static const new_type_t * to_ptr(const current_type_t *value)
Casts a type into another type.
Definition convert_pointer.hpp:46
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:319
static xtd::sptr< new_type_t > to_sptr(xtd::sptr< current_type_t > &&value)
Casts a type into another type.
Definition convert_pointer.hpp:352
static new_type_t & to_ref(current_type_t *value)
Casts a type into another type.
Definition convert_pointer.hpp:230
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:392
static new_type_t * to_ptr(current_type_t &value)
Casts a type into another type.
Definition convert_pointer.hpp:96
static xtd::ptr< new_type_t > to_ptr(xtd::ptr< current_type_t > &&value)
Casts a type into another type.
Definition convert_pointer.hpp:146
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:296
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:253
static xtd::sptr< new_type_t > to_sptr(xtd::sptr< current_type_t > &value)
Casts a type into another type.
Definition convert_pointer.hpp:335
static new_type_t * to_ptr(current_type_t *value)
Casts a type into another type.
Definition convert_pointer.hpp:63
static const new_type_t & to_ref(const current_type_t &value)
Casts a type into another type.
Definition convert_pointer.hpp:163
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:369
static const new_type_t & to_ref(const current_type_t *value)
Casts a type into another type.
Definition convert_pointer.hpp:207
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:274
static new_type_t & to_ref(current_type_t &value)
Casts a type into another type.
Definition convert_pointer.hpp:185
static xtd::uptr< new_type_t > to_uptr(xtd::uptr< current_type_t > &value)
Casts a type into another type.
Definition convert_pointer.hpp:416
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:113
static xtd::uptr< new_type_t > to_uptr(xtd::uptr< current_type_t > &&value)
Casts a type into another type.
Definition convert_pointer.hpp:432
static xtd::ptr< new_type_t > to_ptr(xtd::ptr< current_type_t > &value)
Casts a type into another type.
Definition convert_pointer.hpp:129
@ 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.
Contains unused_ keyword.