xtd 1.0.0
Loading...
Searching...
No Matches
convert_pointer.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "any.hpp"
6#include "static.hpp"
7#include "types.hpp"
8
10template<typename new_type, typename current_type>
11new_type* __convert_value__(current_type* value);
13
15namespace xtd {
28 public:
30
42 template<typename new_type, typename current_type>
43 [[nodiscard]] static auto to_ptr(const current_type* value) -> const new_type* {
44 if (value == nullptr) return nullptr;
45 return &to_ref<new_type>(*value);
46 }
47
59 template<typename new_type, typename current_type>
60 [[nodiscard]] static auto to_ptr(current_type* value) -> new_type* {
61 if (value == nullptr) return nullptr;
62 return &to_ref<new_type>(*value);
63 }
64
76 template<typename new_type, typename current_type>
77 [[nodiscard]] static auto to_ptr(const current_type& value) -> const new_type* {
78 return &to_ref<new_type>(value);
79 }
80
92 template<typename new_type, typename current_type>
93 [[nodiscard]] static auto to_ptr(current_type& value) -> new_type* {
94 return &to_ref<new_type>(value);
95 }
96
109 template<typename new_type, typename current_type>
110 [[nodiscard]] static auto to_ptr(const xtd::ptr<current_type>& value) -> xtd::ptr<new_type> {
111 return to_shared_ptr<new_type>(value);
112 }
113
125 template<typename new_type, typename current_type>
126 [[nodiscard]] static auto to_ptr(xtd::ptr<current_type>& value) -> xtd::ptr<new_type> {
127 return to_shared_ptr<new_type>(value);
128 }
129
142 template<typename new_type, typename current_type>
143 [[nodiscard]] static auto to_ptr(xtd::ptr<current_type>&& value) -> xtd::ptr<new_type> {
144 return to_shared_ptr<new_type>(std::move(value));
145 }
146
159 template<typename new_type, typename current_type>
160 [[nodiscard]] static auto to_ref(const current_type& value) -> const new_type& {
161 try {
162 return dynamic_cast<const new_type&>(value);
163 } catch (const std::exception& e) {
165 }
167 }
168
181 template<typename new_type, typename current_type>
182 [[nodiscard]] static auto to_ref(current_type& value) -> new_type& {
183 try {
184 return dynamic_cast<new_type&>(value);
185 } catch (const std::exception& e) {
187 }
189 }
190
203 template<typename new_type, typename current_type>
204 [[nodiscard]] static auto to_ref(const current_type* value) -> const new_type& {
206 try {
207 return dynamic_cast<const new_type&>(*value);
208 } catch (const std::exception& e) {
210 }
212 }
213
226 template<typename new_type, typename current_type>
227 [[nodiscard]] static auto to_ref(current_type* value) -> new_type& {
229 try {
230 return dynamic_cast<new_type&>(*value);
231 } catch (const std::exception& e) {
233 }
235 }
236
249 template<typename new_type, typename current_type>
250 [[nodiscard]] static auto to_shared_ptr(const xtd::sptr<current_type>& value) -> xtd::sptr<new_type> {
251 try {
252 [[maybe_unused]] auto result = dynamic_cast<new_type&>(*value.get());
253 } catch (const std::exception& e) {
255 }
256 return std::dynamic_pointer_cast<new_type>(value.pointer());
257 }
258
270 template<typename new_type, typename current_type>
271 [[nodiscard]] static auto to_shared_ptr(xtd::sptr<current_type>& value) -> xtd::sptr<new_type> {
272 try {
273 [[maybe_unused]] auto& result = dynamic_cast<new_type&>(*value.get());
274 } catch (const std::exception& e) {
276 }
277 return std::dynamic_pointer_cast<new_type>(value.pointer());
278 }
279
292 template<typename new_type, typename current_type>
293 [[nodiscard]] static auto to_shared_ptr(xtd::sptr<current_type>&& value) -> xtd::sptr<new_type> {
294 try {
295 [[maybe_unused]] auto& result = dynamic_cast<new_type&>(*value.get());
296 } catch (const std::exception& e) {
298 }
299 //return std::move(value);
300 return std::dynamic_pointer_cast<new_type>(std::move(value.pointer()));
301 }
302
315 template<typename new_type, typename current_type>
316 [[nodiscard]] static auto to_sptr(const xtd::sptr<current_type>& value) -> xtd::sptr<new_type> {
317 return to_shared_ptr<new_type>(value);
318 }
319
331 template<typename new_type, typename current_type>
332 [[nodiscard]] static auto to_sptr(xtd::sptr<current_type>& value) -> xtd::sptr<new_type> {
333 return to_shared_ptr<new_type>(value);
334 }
335
348 template<typename new_type, typename current_type>
349 [[nodiscard]] static auto to_sptr(xtd::sptr<current_type>&& value) -> xtd::sptr<new_type> {
350 return to_shared_ptr<new_type>(std::move(value));
351 }
352
365 template<typename new_type, typename current_type>
366 [[nodiscard]] static auto to_unique_ptr(xtd::uptr<current_type>& value) -> xtd::uptr<new_type> {
367 auto ptr = value.release();
368 try {
369 return xtd::uptr<new_type>(__convert_value__<new_type>(ptr));
370 } catch (const std::exception& e) {
373 }
375 }
376
388 template<typename new_type, typename current_type>
389 [[nodiscard]] static auto to_unique_ptr(xtd::uptr<current_type>&& value) -> xtd::uptr<new_type> {
390 auto ptr = value.release();
391 try {
392 return xtd::uptr<new_type>(__convert_value__<new_type>(ptr));
393 } catch (const std::exception& e) {
396 }
398 }
399
412 template<typename new_type, typename current_type>
413 [[nodiscard]] static auto to_uptr(xtd::uptr<current_type>& value) -> xtd::uptr<new_type> {
414 return to_unique_ptr<new_type>(value);
415 }
416
428 template<typename new_type, typename current_type>
429 [[nodiscard]] static auto to_uptr(xtd::uptr<current_type>&& value) -> xtd::uptr<new_type> {
430 return to_unique_ptr<new_type>(std::move(value));
431 }
432
433 };
434}
Contains xtd::any type and std::bad_any_cast exception.
Represents API to convert pointers.
Definition convert_pointer.hpp:27
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
@ invalid_cast
The cast is not valid.
Definition exception_case.hpp:63
@ argument_null
The argument is null.
Definition exception_case.hpp:33
#define static_
This keyword is use to represent a static object. A static object can't be instantiated (constructors...
Definition static.hpp:38
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 auto to_ptr(const current_type *value) -> const new_type *
Casts a type into another type.
Definition convert_pointer.hpp:43
static auto to_ptr(current_type *value) -> new_type *
Casts a type into another type.
Definition convert_pointer.hpp:60
static auto to_shared_ptr(xtd::sptr< current_type > &&value) -> xtd::sptr< new_type >
Casts a type into another type.
Definition convert_pointer.hpp:293
static auto to_ptr(xtd::ptr< current_type > &value) -> xtd::ptr< new_type >
Casts a type into another type.
Definition convert_pointer.hpp:126
static auto to_ptr(xtd::ptr< current_type > &&value) -> xtd::ptr< new_type >
Casts a type into another type.
Definition convert_pointer.hpp:143
static auto to_sptr(const xtd::sptr< current_type > &value) -> xtd::sptr< new_type >
Casts a type into another type.
Definition convert_pointer.hpp:316
static auto to_shared_ptr(xtd::sptr< current_type > &value) -> xtd::sptr< new_type >
Casts a type into another type.
Definition convert_pointer.hpp:271
static auto to_unique_ptr(xtd::uptr< current_type > &&value) -> xtd::uptr< new_type >
Casts a type into another type.
Definition convert_pointer.hpp:389
static auto to_unique_ptr(xtd::uptr< current_type > &value) -> xtd::uptr< new_type >
Casts a type into another type.
Definition convert_pointer.hpp:366
static auto to_sptr(xtd::sptr< current_type > &&value) -> xtd::sptr< new_type >
Casts a type into another type.
Definition convert_pointer.hpp:349
static auto to_ptr(current_type &value) -> new_type *
Casts a type into another type.
Definition convert_pointer.hpp:93
static auto to_ref(const current_type &value) -> const new_type &
Casts a type into another type.
Definition convert_pointer.hpp:160
static auto to_ref(current_type &value) -> new_type &
Casts a type into another type.
Definition convert_pointer.hpp:182
static auto to_uptr(xtd::uptr< current_type > &value) -> xtd::uptr< new_type >
Casts a type into another type.
Definition convert_pointer.hpp:413
static auto to_shared_ptr(const xtd::sptr< current_type > &value) -> xtd::sptr< new_type >
Casts a type into another type.
Definition convert_pointer.hpp:250
static auto to_ref(const current_type *value) -> const new_type &
Casts a type into another type.
Definition convert_pointer.hpp:204
static auto to_ptr(const current_type &value) -> const new_type *
Casts a type into another type.
Definition convert_pointer.hpp:77
static auto to_sptr(xtd::sptr< current_type > &value) -> xtd::sptr< new_type >
Casts a type into another type.
Definition convert_pointer.hpp:332
static auto to_ptr(const xtd::ptr< current_type > &value) -> xtd::ptr< new_type >
Casts a type into another type.
Definition convert_pointer.hpp:110
static auto to_ref(current_type *value) -> new_type &
Casts a type into another type.
Definition convert_pointer.hpp:227
static auto to_uptr(xtd::uptr< current_type > &&value) -> xtd::uptr< new_type >
Casts a type into another type.
Definition convert_pointer.hpp:429
@ e
The E key.
Definition console_key.hpp:96
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.