xtd - Reference Guide  0.2.0
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
convert_pointer.h
Go to the documentation of this file.
1 #pragma once
5 #include <any>
6 #include "static.h"
7 #include "types.h"
9 
11 namespace xtd {
20  public:
33  template<typename type_t>
34  static const type_t& to_ref(const type_t& value) {
35  return value;
36  }
37 
50  template<typename type_t>
51  static type_t& to_ref(type_t& value) {
52  return value;
53  }
66  template<typename type_t>
67  static const type_t& to_ref(const type_t* value) {
68  return *value;
69  }
82  template<typename type_t>
83  static type_t& to_ref(type_t* value) {
84  return *value;
85  }
86 
99  template<typename new_type_t, typename current_type_t>
100  static const new_type_t& to_ref(const current_type_t& value) {
101  try {
102  return dynamic_cast<const new_type_t&>(value);
103  } catch (const std::exception& e) {
104  throw invalid_cast_exception(e.what(), csf_);
105  }
107  }
108 
121  template<typename new_type_t, typename current_type_t>
122  static new_type_t& to_ref(current_type_t& value) {
123  try {
124  return dynamic_cast<new_type_t&>(value);
125  } catch (const std::exception& e) {
126  throw invalid_cast_exception(e.what(), csf_);
127  }
129  }
130 
143  template<typename new_type_t, typename current_type_t>
144  static const new_type_t& to_ref(const current_type_t* value) {
145  try {
146  return dynamic_cast<const new_type_t&>(*value);
147  } catch (const std::exception& e) {
148  throw invalid_cast_exception(e.what(), csf_);
149  }
151  }
152 
165  template<typename new_type_t, typename current_type_t>
166  static new_type_t& to_ref(current_type_t* value) {
167  try {
168  return dynamic_cast<new_type_t&>(*value);
169  } catch (const std::exception& e) {
170  throw invalid_cast_exception(e.what(), csf_);
171  }
173  }
174 
186  template<typename type_t>
187  static const type_t* to_ptr(const type_t* value) {
188  return value;
189  }
190 
202  template<typename type_t>
203  static type_t* to_ptr(type_t* value) {
204  return value;
205  }
206 
218  template<typename new_type_t, typename current_type_t>
219  static const new_type_t* to_ptr(const current_type_t* value) {
220  if (value == nullptr) return nullptr;
221  return &to_ref<new_type_t>(*value);
222  }
223 
235  template<typename new_type_t, typename current_type_t>
236  static new_type_t* to_ptr(current_type_t* value) {
237  if (value == nullptr) return nullptr;
238  return &to_ref<new_type_t>(*value);
239  }
240 
252  template<typename new_type_t, typename current_type_t>
253  static const new_type_t* to_ptr(const current_type_t& value) {
254  if (value == nullptr) return nullptr;
255  return &to_ref<new_type_t>(value);
256  }
257 
269  template<typename new_type_t, typename current_type_t>
270  static new_type_t* to_ptr(current_type_t& value) {
271  if (value == nullptr) return nullptr;
272  return &to_ref<new_type_t>(value);
273  }
274 
287  template<typename new_type_t, typename current_type_t>
288  static std::unique_ptr<new_type_t> to_unique_ptr(std::unique_ptr<current_type_t>& value) {
289  try {
290  //return move(value);
291  return std::unique_ptr<new_type_t>(as<new_type_t>(value.release()));
292  } catch (const std::exception& e) {
293  throw invalid_cast_exception(e.what(), csf_);
294  }
296  }
297 
309  template<typename new_type_t, typename current_type_t>
310  static std::unique_ptr<new_type_t> to_unique_ptr(std::unique_ptr<current_type_t>&& value) {
311  try {
312  //return move(value);
313  return std::unique_ptr<new_type_t>(as<new_type_t>(value.release()));
314  } catch (const std::exception& e) {
315  throw invalid_cast_exception(e.what(), csf_);
316  }
318  }
319 
332  template<typename new_type_t, typename current_type_t>
333  static std::shared_ptr<new_type_t> to_shared_ptr(const std::shared_ptr<current_type_t>& value) {
334  try {
335  return dynamic_pointer_cast<new_type_t>(value);
336  } catch (const std::exception& e) {
337  throw invalid_cast_exception(e.what(), csf_);
338  }
340  }
341 
353  template<typename new_type_t, typename current_type_t>
354  static std::shared_ptr<new_type_t> to_shared_ptr(std::shared_ptr<current_type_t>& value) {
355  try {
356  return dynamic_pointer_cast<new_type_t>(value);
357  } catch (const std::exception& e) {
358  throw invalid_cast_exception(e.what(), csf_);
359  }
361  }
362 
375  template<typename new_type_t, typename current_type_t>
376  static std::shared_ptr<new_type_t> to_shared_ptr(std::shared_ptr<current_type_t>&& value) {
377  try {
378  return move(value);
379  } catch (const std::exception& e) {
380  throw invalid_cast_exception(e.what(), csf_);
381  }
383  }
384  };
385 }
Contains xtd::static_object class.
Contains xtd fundamental types.
static new_type_t * to_ptr(current_type_t *value)
Casts a type into another type.
Definition: convert_pointer.h:236
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:333
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:376
#define static_
This keyword is use to represent a static object. A static object can&#39;t be instantiated (constructors...
Definition: static.h:38
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:288
static new_type_t * to_ptr(current_type_t &value)
Casts a type into another type.
Definition: convert_pointer.h:270
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
Represents API to convert pointers.
Definition: convert_pointer.h:19
static const type_t * to_ptr(const type_t *value)
Casts a type into another type.
Definition: convert_pointer.h:187
#define csf_
Provides information about the current stack frame.
Definition: stack_frame.h:213
static const new_type_t & to_ref(const current_type_t &value)
Casts a type into another type.
Definition: convert_pointer.h:100
static new_type_t & to_ref(current_type_t *value)
Casts a type into another type.
Definition: convert_pointer.h:166
static const new_type_t * to_ptr(const current_type_t &value)
Casts a type into another type.
Definition: convert_pointer.h:253
static const new_type_t * to_ptr(const current_type_t *value)
Casts a type into another type.
Definition: convert_pointer.h:219
static const new_type_t & to_ref(const current_type_t *value)
Casts a type into another type.
Definition: convert_pointer.h:144
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:310
static const type_t & to_ref(const type_t &value)
Casts a type into another type.
Definition: convert_pointer.h:34
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:354
static new_type_t & to_ref(current_type_t &value)
Casts a type into another type.
Definition: convert_pointer.h:122
The exception that is thrown for invalid casting or explicit conversion.
Definition: invalid_cast_exception.h:18
static type_t & to_ref(type_t *value)
Casts a type into another type.
Definition: convert_pointer.h:83
Contains xtd::invalid_cast_exception exception.
static type_t & to_ref(type_t &value)
Casts a type into another type.
Definition: convert_pointer.h:51
static type_t * to_ptr(type_t *value)
Casts a type into another type.
Definition: convert_pointer.h:203
static const type_t & to_ref(const type_t *value)
Casts a type into another type.
Definition: convert_pointer.h:67