xtd 0.2.0
convert_pointer.h
Go to the documentation of this file.
1 #pragma once
5 #include "any.h"
8 #include "static.h"
9 #include "types.h"
10 #include "unused.h"
11 
13 template<typename new_type_t, typename current_type_t>
14 new_type_t* __convert_value__(current_type_t* value);
16 
18 namespace 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  try {
336  return std::unique_ptr<new_type_t>(__convert_value__<new_type_t>(value.release()));
337  } catch (const std::exception& e) {
338  throw invalid_cast_exception(e.what(), csf_);
339  }
341  }
342 
354  template<typename new_type_t, typename current_type_t>
355  static std::unique_ptr<new_type_t> to_unique_ptr(std::unique_ptr<current_type_t>&& value) {
356  try {
357  return std::unique_ptr<new_type_t>(__convert_value__<new_type_t>(value.release()));
358  } catch (const std::exception& e) {
359  throw invalid_cast_exception(e.what(), csf_);
360  }
362  }
363 
376  template<typename new_type_t, typename current_type_t>
377  static std::shared_ptr<new_type_t> to_shared_ptr(const std::shared_ptr<current_type_t>& value) {
378  try {
379  unused_(dynamic_cast<new_type_t&>(*value.get()));
380  } catch (const std::exception& e) {
381  throw invalid_cast_exception(e.what(), csf_);
382  }
383  return std::dynamic_pointer_cast<new_type_t>(value);
384  }
385 
397  template<typename new_type_t, typename current_type_t>
398  static std::shared_ptr<new_type_t> to_shared_ptr(std::shared_ptr<current_type_t>& value) {
399  try {
400  unused_(dynamic_cast<new_type_t&>(*value.get()));
401  } catch (const std::exception& e) {
402  throw invalid_cast_exception(e.what(), csf_);
403  }
404  return std::dynamic_pointer_cast<new_type_t>(value);
405  }
406 
419  template<typename new_type_t, typename current_type_t>
420  static std::shared_ptr<new_type_t> to_shared_ptr(std::shared_ptr<current_type_t>&& value) {
421  try {
422  unused_(dynamic_cast<new_type_t&>(*value.get()));
423  } catch (const std::exception& e) {
424  throw invalid_cast_exception(e.what(), csf_);
425  }
426  //return std::move(value);
427  return std::dynamic_pointer_cast<new_type_t>(std::move(value));
428  }
430  };
431 }
The exception that is thrown when one of the arguments provided to a method is null.
Definition: argument_null_exception.h:20
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:126
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:377
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:420
#define static_
This keyword is use to represent a static object. A static object can&#39;t be instantiated (constructors...
Definition: static.h:37
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 new_type_t * to_ptr(current_type_t &value)
Casts a type into another type.
Definition: convert_pointer.h:159
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:28
static const type_t * to_ptr(const type_t *value)
Casts a type into another type.
Definition: convert_pointer.h:77
#define csf_
Provides information about the current stack frame.
Definition: current_stack_frame.h:30
static const type_t * to_ptr(const type_t &value)
Casts a type into another type.
Definition: convert_pointer.h:45
static const new_type_t & to_ref(const current_type_t &value)
Casts a type into another type.
Definition: convert_pointer.h:244
static new_type_t & to_ref(current_type_t *value)
Casts a type into another type.
Definition: convert_pointer.h:311
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 new_type_t * to_ptr(const current_type_t *value)
Casts a type into another type.
Definition: convert_pointer.h:109
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::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:355
static const type_t & to_ref(const type_t &value)
Casts a type into another type.
Definition: convert_pointer.h:176
Contains std::any type and std::bad_any_cast exception.
Contains xtd::argument_null_exception exception.
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:398
static type_t * to_ptr(type_t &value)
Casts a type into another type.
Definition: convert_pointer.h:61
static new_type_t & to_ref(current_type_t &value)
Casts a type into another type.
Definition: convert_pointer.h:266
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:226
Contains xtd::invalid_cast_exception exception.
#define unused_
It may be used to suppress the "unused variable" or "unused local typedefs" compiler warnings when th...
Definition: unused.h:30
static type_t & to_ref(type_t &value)
Casts a type into another type.
Definition: convert_pointer.h:193
static type_t * to_ptr(type_t *value)
Casts a type into another type.
Definition: convert_pointer.h:93
static const type_t & to_ref(const type_t *value)
Casts a type into another type.
Definition: convert_pointer.h:209
Contains unused_ keyword.