xtd 0.2.0
Loading...
Searching...
No Matches
convert_pointer.h
Go to the documentation of this file.
1
4#pragma once
5#include "any.h"
8#include "static.h"
9#include "types.h"
10#include "unused.h"
11
13template<typename new_type_t, typename current_type_t>
14new_type_t* __convert_value__(current_type_t* value);
16
18namespace xtd {
31 public:
33
45 template<typename new_type_t, typename 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<typename new_type_t, typename 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<typename new_type_t, typename 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<typename new_type_t, typename 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<typename new_type_t, typename current_type_t>
113 static const new_type_t& to_ref(const current_type_t& value) {
114 try {
115 return dynamic_cast<const new_type_t&>(value);
116 } catch (const std::exception& e) {
117 throw invalid_cast_exception(e.what());
118 }
119 throw invalid_cast_exception {};
120 }
121
134 template<typename new_type_t, typename current_type_t>
135 static new_type_t& to_ref(current_type_t& value) {
136 try {
137 return dynamic_cast<new_type_t&>(value);
138 } catch (const std::exception& e) {
139 throw invalid_cast_exception(e.what());
140 }
141 throw invalid_cast_exception {};
142 }
143
156 template<typename new_type_t, typename current_type_t>
157 static const new_type_t& to_ref(const current_type_t* value) {
158 if (value == nullptr) throw argument_null_exception {};
159 try {
160 return dynamic_cast<const new_type_t&>(*value);
161 } catch (const std::exception& e) {
162 throw invalid_cast_exception(e.what());
163 }
164 throw invalid_cast_exception {};
165 }
166
179 template<typename new_type_t, typename current_type_t>
180 static new_type_t& to_ref(current_type_t* value) {
181 if (value == nullptr) throw argument_null_exception {};
182 try {
183 return dynamic_cast<new_type_t&>(*value);
184 } catch (const std::exception& e) {
185 throw invalid_cast_exception(e.what());
186 }
187 throw invalid_cast_exception {};
188 }
189
202 template<typename new_type_t, typename current_type_t>
204 auto ptr = value.release();
205 try {
206 return xtd::uptr<new_type_t>(__convert_value__<new_type_t>(ptr));
207 } catch (const std::exception& e) {
209 throw invalid_cast_exception(e.what());
210 }
211 throw invalid_cast_exception {};
212 }
213
225 template<typename new_type_t, typename current_type_t>
227 auto ptr = value.release();
228 try {
229 return xtd::uptr<new_type_t>(__convert_value__<new_type_t>(ptr));
230 } catch (const std::exception& e) {
232 throw invalid_cast_exception(e.what());
233 }
234 throw invalid_cast_exception {};
235 }
236
249 template<typename new_type_t, typename current_type_t>
251 try {
252 unused_(dynamic_cast<new_type_t&>(*value.get()));
253 } catch (const std::exception& e) {
254 throw invalid_cast_exception(e.what());
255 }
256 return std::dynamic_pointer_cast<new_type_t>(value);
257 }
258
270 template<typename new_type_t, typename current_type_t>
272 try {
273 unused_(dynamic_cast<new_type_t&>(*value.get()));
274 } catch (const std::exception& e) {
275 throw invalid_cast_exception(e.what());
276 }
277 return std::dynamic_pointer_cast<new_type_t>(value);
278 }
279
292 template<typename new_type_t, typename current_type_t>
294 try {
295 unused_(dynamic_cast<new_type_t&>(*value.get()));
296 } catch (const std::exception& e) {
297 throw invalid_cast_exception(e.what());
298 }
299 //return std::move(value);
300 return std::dynamic_pointer_cast<new_type_t>(std::move(value));
301 }
303 };
304}
Contains std::any type and std::bad_any_cast exception.
Contains xtd::argument_null_exception exception.
The exception that is thrown when one of the arguments provided to a method is null.
Definition argument_null_exception.h:23
Represents API to convert pointers.
Definition convert_pointer.h:30
The exception that is thrown for invalid casting or explicit conversion.
Definition invalid_cast_exception.h:19
#define unused_
It may be used to suppress the "unused variable" or "unused local typedefs" compiler warnings when th...
Definition unused.h:30
#define static_
This keyword is use to represent a static object. A static object can't be instantiated (constructors...
Definition static.h:37
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.h:27
std::unique_ptr< type_t > uptr
The xtd::uptr object is a unique pointer.
Definition uptr.h:25
std::shared_ptr< type_t > sptr
The xtd::sptr object is a shared pointer.
Definition sptr.h:25
static const new_type_t * to_ptr(const current_type_t &value)
Casts a type into another type.
Definition convert_pointer.h:80
static const new_type_t * to_ptr(const current_type_t *value)
Casts a type into another type.
Definition convert_pointer.h:46
static new_type_t & to_ref(current_type_t *value)
Casts a type into another type.
Definition convert_pointer.h:180
static xtd::uptr< new_type_t > to_unique_ptr(xtd::uptr< current_type_t > &&value)
Casts a type into another type.
Definition convert_pointer.h:226
static new_type_t * to_ptr(current_type_t &value)
Casts a type into another type.
Definition convert_pointer.h:96
static xtd::sptr< new_type_t > to_shared_ptr(xtd::sptr< current_type_t > &&value)
Casts a type into another type.
Definition convert_pointer.h:293
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.h:250
static new_type_t * to_ptr(current_type_t *value)
Casts a type into another type.
Definition convert_pointer.h:63
static const new_type_t & to_ref(const current_type_t &value)
Casts a type into another type.
Definition convert_pointer.h:113
static xtd::uptr< new_type_t > to_unique_ptr(xtd::uptr< current_type_t > &value)
Casts a type into another type.
Definition convert_pointer.h:203
static const new_type_t & to_ref(const current_type_t *value)
Casts a type into another type.
Definition convert_pointer.h:157
static xtd::sptr< new_type_t > to_shared_ptr(xtd::sptr< current_type_t > &value)
Casts a type into another type.
Definition convert_pointer.h:271
static new_type_t & to_ref(current_type_t &value)
Casts a type into another type.
Definition convert_pointer.h:135
@ e
The E key.
Contains xtd::invalid_cast_exception exception.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Contains xtd::static_object class.
Contains unused_ keyword.
Contains xtd fundamental types.