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 {
29 public:
31
44 template<typename type_t>
45 static const type_t* to_ptr(const type_t& value) {
46 return &value;
47 }
48
60 template<typename type_t>
61 static type_t* to_ptr(type_t& value) {
62 return &value;
63 }
64
76 template<typename type_t>
77 static const type_t* to_ptr(const type_t* value) {
78 return value;
79 }
80
92 template<typename type_t>
93 static type_t* to_ptr(type_t* value) {
94 return value;
95 }
96
108 template<typename new_type_t, typename current_type_t>
109 static const new_type_t* to_ptr(const current_type_t* value) {
110 if (value == nullptr) return nullptr;
111 return &to_ref<new_type_t>(*value);
112 }
113
125 template<typename new_type_t, typename current_type_t>
126 static new_type_t* to_ptr(current_type_t* value) {
127 if (value == nullptr) return nullptr;
128 return &to_ref<new_type_t>(*value);
129 }
130
142 template<typename new_type_t, typename current_type_t>
143 static const new_type_t* to_ptr(const current_type_t& value) {
144 return &to_ref<new_type_t>(value);
145 }
146
158 template<typename new_type_t, typename current_type_t>
159 static new_type_t* to_ptr(current_type_t& value) {
160 return &to_ref<new_type_t>(value);
161 }
162
175 template<typename type_t>
176 static const type_t& to_ref(const type_t& value) {
177 return value;
178 }
179
192 template<typename type_t>
193 static type_t& to_ref(type_t& value) {
194 return value;
195 }
208 template<typename type_t>
209 static const type_t& to_ref(const type_t* value) {
210 if (value == nullptr) throw argument_null_exception {csf_};
211 return *value;
212 }
225 template<typename type_t>
226 static type_t& to_ref(type_t* value) { // value parameter can't be const by design.
227 if (value == nullptr) throw argument_null_exception {csf_};
228 return *value;
229 }
230
243 template<typename new_type_t, typename current_type_t>
244 static const new_type_t& to_ref(const current_type_t& value) {
245 try {
246 return dynamic_cast<const new_type_t&>(value);
247 } catch (const std::exception& e) {
248 throw invalid_cast_exception(e.what(), csf_);
249 }
251 }
252
265 template<typename new_type_t, typename current_type_t>
266 static new_type_t& to_ref(current_type_t& value) {
267 try {
268 return dynamic_cast<new_type_t&>(value);
269 } catch (const std::exception& e) {
270 throw invalid_cast_exception(e.what(), csf_);
271 }
273 }
274
287 template<typename new_type_t, typename current_type_t>
288 static const new_type_t& to_ref(const current_type_t* value) {
289 if (value == nullptr) throw argument_null_exception {csf_};
290 try {
291 return dynamic_cast<const new_type_t&>(*value);
292 } catch (const std::exception& e) {
293 throw invalid_cast_exception(e.what(), csf_);
294 }
296 }
297
310 template<typename new_type_t, typename current_type_t>
311 static new_type_t& to_ref(current_type_t* value) {
312 if (value == nullptr) throw argument_null_exception {csf_};
313 try {
314 return dynamic_cast<new_type_t&>(*value);
315 } catch (const std::exception& e) {
316 throw invalid_cast_exception(e.what(), csf_);
317 }
319 }
320
333 template<typename new_type_t, typename current_type_t>
334 static std::unique_ptr<new_type_t> to_unique_ptr(std::unique_ptr<current_type_t>& value) {
335 auto ptr = value.release();
336 try {
337 return std::unique_ptr<new_type_t>(__convert_value__<new_type_t>(ptr));
338 } catch (const std::exception& e) {
339 value = std::unique_ptr<current_type_t>(ptr);
340 throw invalid_cast_exception(e.what(), csf_);
341 }
343 }
344
356 template<typename new_type_t, typename current_type_t>
357 static std::unique_ptr<new_type_t> to_unique_ptr(std::unique_ptr<current_type_t>&& value) {
358 auto ptr = value.release();
359 try {
360 return std::unique_ptr<new_type_t>(__convert_value__<new_type_t>(ptr));
361 } catch (const std::exception& e) {
362 value = std::unique_ptr<current_type_t>(ptr);
363 throw invalid_cast_exception(e.what(), csf_);
364 }
366 }
367
380 template<typename new_type_t, typename current_type_t>
381 static std::shared_ptr<new_type_t> to_shared_ptr(const std::shared_ptr<current_type_t>& value) {
382 try {
383 unused_(dynamic_cast<new_type_t&>(*value.get()));
384 } catch (const std::exception& e) {
385 throw invalid_cast_exception(e.what(), csf_);
386 }
387 return std::dynamic_pointer_cast<new_type_t>(value);
388 }
389
401 template<typename new_type_t, typename current_type_t>
402 static std::shared_ptr<new_type_t> to_shared_ptr(std::shared_ptr<current_type_t>& value) {
403 try {
404 unused_(dynamic_cast<new_type_t&>(*value.get()));
405 } catch (const std::exception& e) {
406 throw invalid_cast_exception(e.what(), csf_);
407 }
408 return std::dynamic_pointer_cast<new_type_t>(value);
409 }
410
423 template<typename new_type_t, typename current_type_t>
424 static std::shared_ptr<new_type_t> to_shared_ptr(std::shared_ptr<current_type_t>&& value) {
425 try {
426 unused_(dynamic_cast<new_type_t&>(*value.get()));
427 } catch (const std::exception& e) {
428 throw invalid_cast_exception(e.what(), csf_);
429 }
430 //return std::move(value);
431 return std::dynamic_pointer_cast<new_type_t>(std::move(value));
432 }
434 };
435}
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:20
Represents API to convert pointers.
Definition convert_pointer.h:28
The exception that is thrown for invalid casting or explicit conversion.
Definition invalid_cast_exception.h:18
#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
#define csf_
Provides information about the current stack frame.
Definition current_stack_frame.h:30
static const new_type_t * to_ptr(const current_type_t &value)
Casts a type into another type.
Definition convert_pointer.h:143
static const type_t * to_ptr(const type_t *value)
Casts a type into another type.
Definition convert_pointer.h:77
static type_t & to_ref(type_t *value)
Casts a type into another type.
Definition convert_pointer.h:226
static std::unique_ptr< new_type_t > to_unique_ptr(std::unique_ptr< current_type_t > &&value)
Casts a type into another type.
Definition convert_pointer.h:357
static const new_type_t * to_ptr(const current_type_t *value)
Casts a type into another type.
Definition convert_pointer.h:109
static new_type_t & to_ref(current_type_t *value)
Casts a type into another type.
Definition convert_pointer.h:311
static type_t * to_ptr(type_t *value)
Casts a type into another type.
Definition convert_pointer.h:93
static std::shared_ptr< new_type_t > to_shared_ptr(std::shared_ptr< current_type_t > &value)
Casts a type into another type.
Definition convert_pointer.h:402
static new_type_t * to_ptr(current_type_t &value)
Casts a type into another type.
Definition convert_pointer.h:159
static const type_t & to_ref(const type_t *value)
Casts a type into another type.
Definition convert_pointer.h:209
static const type_t & to_ref(const type_t &value)
Casts a type into another type.
Definition convert_pointer.h:176
static new_type_t * to_ptr(current_type_t *value)
Casts a type into another type.
Definition convert_pointer.h:126
static const new_type_t & to_ref(const current_type_t &value)
Casts a type into another type.
Definition convert_pointer.h:244
static std::shared_ptr< new_type_t > to_shared_ptr(const std::shared_ptr< current_type_t > &value)
Casts a type into another type.
Definition convert_pointer.h:381
static const new_type_t & to_ref(const current_type_t *value)
Casts a type into another type.
Definition convert_pointer.h:288
static std::shared_ptr< new_type_t > to_shared_ptr(std::shared_ptr< current_type_t > &&value)
Casts a type into another type.
Definition convert_pointer.h:424
static type_t & to_ref(type_t &value)
Casts a type into another type.
Definition convert_pointer.h:193
static const type_t * to_ptr(const type_t &value)
Casts a type into another type.
Definition convert_pointer.h:45
static new_type_t & to_ref(current_type_t &value)
Casts a type into another type.
Definition convert_pointer.h:266
static std::unique_ptr< new_type_t > to_unique_ptr(std::unique_ptr< current_type_t > &value)
Casts a type into another type.
Definition convert_pointer.h:334
static type_t * to_ptr(type_t &value)
Casts a type into another type.
Definition convert_pointer.h:61
@ 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 xtd fundamental types.
Contains unused_ keyword.