Skip to content

Commit 122464a

Browse files
committed
Add is and as with null_ptr
1 parent e5ce31e commit 122464a

File tree

7 files changed

+336
-312
lines changed

7 files changed

+336
-312
lines changed

src/xtd.core/include/xtd/as.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@
5151
#undef __XTD_CORE_INTERNAL__
5252

5353
/// @cond
54-
template<class new_type_t, class current_type_t>
55-
new_type_t* __convert_value__(current_type_t* value) {
56-
return xtd::as<new_type_t>(value);
54+
template<class new_type, class current_type>
55+
new_type* __convert_value__(current_type* value) {
56+
return xtd::as<new_type>(value);
5757
}
5858

5959
template<class result_t, class source_t>

src/xtd.core/include/xtd/convert_pointer.hpp

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include "types.hpp"
1010

1111
/// @cond
12-
template<class new_type_t, class current_type_t>
13-
new_type_t* __convert_value__(current_type_t* value);
12+
template<class new_type, class current_type>
13+
new_type* __convert_value__(current_type* value);
1414
/// @endcond
1515

1616
/// @brief The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
@@ -41,10 +41,10 @@ namespace xtd {
4141
/// const button* value = new xtd::forms::button();
4242
/// const control* result = convert::to_ptr<xtd::forms::control>(value);
4343
/// ```
44-
template<class new_type_t, class current_type_t>
45-
static const new_type_t* to_ptr(const current_type_t* value) {
44+
template<class new_type, class current_type>
45+
static const new_type* to_ptr(const current_type* value) {
4646
if (value == nullptr) return nullptr;
47-
return &to_ref<new_type_t>(*value);
47+
return &to_ref<new_type>(*value);
4848
}
4949

5050
/// @brief Casts a type into another type.
@@ -58,10 +58,10 @@ namespace xtd {
5858
/// button* value = new xtd::forms::button();
5959
/// control* result = convert::to_ptr<xtd::forms::control>(value);
6060
/// ```
61-
template<class new_type_t, class current_type_t>
62-
static new_type_t* to_ptr(current_type_t* value) {
61+
template<class new_type, class current_type>
62+
static new_type* to_ptr(current_type* value) {
6363
if (value == nullptr) return nullptr;
64-
return &to_ref<new_type_t>(*value);
64+
return &to_ref<new_type>(*value);
6565
}
6666

6767
/// @brief Casts a type into another type.
@@ -75,9 +75,9 @@ namespace xtd {
7575
/// const button* value = new xtd::forms::button();
7676
/// const control* result = convert::to_ptr<xtd::forms::control>(value);
7777
/// ```
78-
template<class new_type_t, class current_type_t>
79-
static const new_type_t* to_ptr(const current_type_t& value) {
80-
return &to_ref<new_type_t>(value);
78+
template<class new_type, class current_type>
79+
static const new_type* to_ptr(const current_type& value) {
80+
return &to_ref<new_type>(value);
8181
}
8282

8383
/// @brief Casts a type into another type.
@@ -91,9 +91,9 @@ namespace xtd {
9191
/// button* value = new xtd::forms::button();
9292
/// control* result = convert::to_ptr<xtd::forms::control>(value);
9393
/// ```
94-
template<class new_type_t, class current_type_t>
95-
static new_type_t* to_ptr(current_type_t& value) {
96-
return &to_ref<new_type_t>(value);
94+
template<class new_type, class current_type>
95+
static new_type* to_ptr(current_type& value) {
96+
return &to_ref<new_type>(value);
9797
}
9898

9999
/// @brief Casts a type into another type.
@@ -108,9 +108,9 @@ namespace xtd {
108108
/// const xtd::sptr<control> result = as<xtd::forms::control>(value);
109109
/// ```
110110
/// @exception xtd::invalid_cast_exception the parameters is bad cast.
111-
template<class new_type_t, class current_type_t>
112-
static xtd::ptr<new_type_t> to_ptr(const xtd::ptr<current_type_t>& value) {
113-
return to_shared_ptr<new_type_t>(value);
111+
template<class new_type, class current_type>
112+
static xtd::ptr<new_type> to_ptr(const xtd::ptr<current_type>& value) {
113+
return to_shared_ptr<new_type>(value);
114114
}
115115

116116
/// @brief Casts a type into another type.
@@ -124,9 +124,9 @@ namespace xtd {
124124
/// xtd::sptr<control> result = as<xtd::forms::control>(xtd::new_sptr<xtd::forms::button>());
125125
/// ```
126126
/// @exception xtd::invalid_cast_exception the parameters is bad cast.
127-
template<class new_type_t, class current_type_t>
128-
static xtd::ptr<new_type_t> to_ptr(xtd::ptr<current_type_t>& value) {
129-
return to_shared_ptr<new_type_t>(value);
127+
template<class new_type, class current_type>
128+
static xtd::ptr<new_type> to_ptr(xtd::ptr<current_type>& value) {
129+
return to_shared_ptr<new_type>(value);
130130
}
131131

132132
/// @brief Casts a type into another type.
@@ -141,9 +141,9 @@ namespace xtd {
141141
/// const xtd::sptr<control> result = as<xtd::forms::control>(value);
142142
/// ```
143143
/// @exception xtd::invalid_cast_exception the parameters is bad cast.
144-
template<class new_type_t, class current_type_t>
145-
static xtd::ptr<new_type_t> to_ptr(xtd::ptr<current_type_t>&& value) {
146-
return to_shared_ptr<new_type_t>(std::move(value));
144+
template<class new_type, class current_type>
145+
static xtd::ptr<new_type> to_ptr(xtd::ptr<current_type>&& value) {
146+
return to_shared_ptr<new_type>(std::move(value));
147147
}
148148

149149
/// @brief Casts a type into another type.
@@ -158,10 +158,10 @@ namespace xtd {
158158
/// const control& result = convert::to_ref<xtd::forms::control>(*value);
159159
/// ```
160160
/// @exception xtd::invalid_cast_exception the parameters is bad cast.
161-
template<class new_type_t, class current_type_t>
162-
static const new_type_t& to_ref(const current_type_t& value) {
161+
template<class new_type, class current_type>
162+
static const new_type& to_ref(const current_type& value) {
163163
try {
164-
return dynamic_cast<const new_type_t&>(value);
164+
return dynamic_cast<const new_type&>(value);
165165
} catch (const std::exception& e) {
166166
xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_cast);
167167
}
@@ -180,10 +180,10 @@ namespace xtd {
180180
/// control& result = convert::to_ref<xtd::forms::control>(*value);
181181
/// ```
182182
/// @exception xtd::invalid_cast_exception the parameters is bad cast.
183-
template<class new_type_t, class current_type_t>
184-
static new_type_t& to_ref(current_type_t& value) {
183+
template<class new_type, class current_type>
184+
static new_type& to_ref(current_type& value) {
185185
try {
186-
return dynamic_cast<new_type_t&>(value);
186+
return dynamic_cast<new_type&>(value);
187187
} catch (const std::exception& e) {
188188
xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_cast);
189189
}
@@ -202,11 +202,11 @@ namespace xtd {
202202
/// const control& result = convert::to_ref<xtd::forms::control>(value);
203203
/// ```
204204
/// @exception xtd::invalid_cast_exception the parameters is bad cast.
205-
template<class new_type_t, class current_type_t>
206-
static const new_type_t& to_ref(const current_type_t* value) {
205+
template<class new_type, class current_type>
206+
static const new_type& to_ref(const current_type* value) {
207207
if (value == nullptr) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument_null);
208208
try {
209-
return dynamic_cast<const new_type_t&>(*value);
209+
return dynamic_cast<const new_type&>(*value);
210210
} catch (const std::exception& e) {
211211
xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_cast);
212212
}
@@ -225,11 +225,11 @@ namespace xtd {
225225
/// control& result = convert::to_ref<xtd::forms::control>(value);
226226
/// ```
227227
/// @exception xtd::invalid_cast_exception the parameters is bad cast.
228-
template<class new_type_t, class current_type_t>
229-
static new_type_t& to_ref(current_type_t* value) {
228+
template<class new_type, class current_type>
229+
static new_type& to_ref(current_type* value) {
230230
if (value == nullptr) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument_null);
231231
try {
232-
return dynamic_cast<new_type_t&>(*value);
232+
return dynamic_cast<new_type&>(*value);
233233
} catch (const std::exception& e) {
234234
xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_cast);
235235
}
@@ -248,14 +248,14 @@ namespace xtd {
248248
/// const xtd::sptr<control> result = as<xtd::forms::control>(value);
249249
/// ```
250250
/// @exception xtd::invalid_cast_exception the parameters is bad cast.
251-
template<class new_type_t, class current_type_t>
252-
static xtd::sptr<new_type_t> to_shared_ptr(const xtd::sptr<current_type_t>& value) {
251+
template<class new_type, class current_type>
252+
static xtd::sptr<new_type> to_shared_ptr(const xtd::sptr<current_type>& value) {
253253
try {
254-
[[maybe_unused]] auto result = dynamic_cast<new_type_t&>(*value.get());
254+
[[maybe_unused]] auto result = dynamic_cast<new_type&>(*value.get());
255255
} catch (const std::exception& e) {
256256
xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_cast);
257257
}
258-
return std::dynamic_pointer_cast<new_type_t>(value.pointer());
258+
return std::dynamic_pointer_cast<new_type>(value.pointer());
259259
}
260260

261261
/// @brief Casts a type into another type.
@@ -269,14 +269,14 @@ namespace xtd {
269269
/// xtd::sptr<control> result = as<xtd::forms::control>(xtd::new_sptr<xtd::forms::button>());
270270
/// ```
271271
/// @exception xtd::invalid_cast_exception the parameters is bad cast.
272-
template<class new_type_t, class current_type_t>
273-
static xtd::sptr<new_type_t> to_shared_ptr(xtd::sptr<current_type_t>& value) {
272+
template<class new_type, class current_type>
273+
static xtd::sptr<new_type> to_shared_ptr(xtd::sptr<current_type>& value) {
274274
try {
275-
[[maybe_unused]] auto& result = dynamic_cast<new_type_t&>(*value.get());
275+
[[maybe_unused]] auto& result = dynamic_cast<new_type&>(*value.get());
276276
} catch (const std::exception& e) {
277277
xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_cast);
278278
}
279-
return std::dynamic_pointer_cast<new_type_t>(value.pointer());
279+
return std::dynamic_pointer_cast<new_type>(value.pointer());
280280
}
281281

282282
/// @brief Casts a type into another type.
@@ -291,15 +291,15 @@ namespace xtd {
291291
/// const xtd::sptr<control> result = as<xtd::forms::control>(value);
292292
/// ```
293293
/// @exception xtd::invalid_cast_exception the parameters is bad cast.
294-
template<class new_type_t, class current_type_t>
295-
static xtd::sptr<new_type_t> to_shared_ptr(xtd::sptr<current_type_t>&& value) {
294+
template<class new_type, class current_type>
295+
static xtd::sptr<new_type> to_shared_ptr(xtd::sptr<current_type>&& value) {
296296
try {
297-
[[maybe_unused]] auto& result = dynamic_cast<new_type_t&>(*value.get());
297+
[[maybe_unused]] auto& result = dynamic_cast<new_type&>(*value.get());
298298
} catch (const std::exception& e) {
299299
xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_cast);
300300
}
301301
//return std::move(value);
302-
return std::dynamic_pointer_cast<new_type_t>(std::move(value.pointer()));
302+
return std::dynamic_pointer_cast<new_type>(std::move(value.pointer()));
303303
}
304304

305305
/// @brief Casts a type into another type.
@@ -314,9 +314,9 @@ namespace xtd {
314314
/// const xtd::sptr<control> result = as<xtd::forms::control>(value);
315315
/// ```
316316
/// @exception xtd::invalid_cast_exception the parameters is bad cast.
317-
template<class new_type_t, class current_type_t>
318-
static xtd::sptr<new_type_t> to_sptr(const xtd::sptr<current_type_t>& value) {
319-
return to_shared_ptr<new_type_t>(value);
317+
template<class new_type, class current_type>
318+
static xtd::sptr<new_type> to_sptr(const xtd::sptr<current_type>& value) {
319+
return to_shared_ptr<new_type>(value);
320320
}
321321

322322
/// @brief Casts a type into another type.
@@ -330,9 +330,9 @@ namespace xtd {
330330
/// xtd::sptr<control> result = as<xtd::forms::control>(xtd::new_sptr<xtd::forms::button>());
331331
/// ```
332332
/// @exception xtd::invalid_cast_exception the parameters is bad cast.
333-
template<class new_type_t, class current_type_t>
334-
static xtd::sptr<new_type_t> to_sptr(xtd::sptr<current_type_t>& value) {
335-
return to_shared_ptr<new_type_t>(value);
333+
template<class new_type, class current_type>
334+
static xtd::sptr<new_type> to_sptr(xtd::sptr<current_type>& value) {
335+
return to_shared_ptr<new_type>(value);
336336
}
337337

338338
/// @brief Casts a type into another type.
@@ -347,9 +347,9 @@ namespace xtd {
347347
/// const xtd::sptr<control> result = as<xtd::forms::control>(value);
348348
/// ```
349349
/// @exception xtd::invalid_cast_exception the parameters is bad cast.
350-
template<class new_type_t, class current_type_t>
351-
static xtd::sptr<new_type_t> to_sptr(xtd::sptr<current_type_t>&& value) {
352-
return to_shared_ptr<new_type_t>(std::move(value));
350+
template<class new_type, class current_type>
351+
static xtd::sptr<new_type> to_sptr(xtd::sptr<current_type>&& value) {
352+
return to_shared_ptr<new_type>(std::move(value));
353353
}
354354

355355
/// @brief Casts a type into another type.
@@ -364,13 +364,13 @@ namespace xtd {
364364
/// xtd::uptr<control> result = as<xtd::forms::control>(value);
365365
/// ```
366366
/// @exception xtd::invalid_cast_exception the parameters is bad cast.
367-
template<class new_type_t, class current_type_t>
368-
static xtd::uptr<new_type_t> to_unique_ptr(xtd::uptr<current_type_t>& value) {
367+
template<class new_type, class current_type>
368+
static xtd::uptr<new_type> to_unique_ptr(xtd::uptr<current_type>& value) {
369369
auto ptr = value.release();
370370
try {
371-
return xtd::uptr<new_type_t>(__convert_value__<new_type_t>(ptr));
371+
return xtd::uptr<new_type>(__convert_value__<new_type>(ptr));
372372
} catch (const std::exception& e) {
373-
value = xtd::uptr<current_type_t>(ptr);
373+
value = xtd::uptr<current_type>(ptr);
374374
xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_cast);
375375
}
376376
xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_cast);
@@ -387,13 +387,13 @@ namespace xtd {
387387
/// xtd::uptr<control> result = as<xtd::forms::control>(xtd::new_uptr<xtd::forms::button>());
388388
/// ```
389389
/// @exception xtd::invalid_cast_exception the parameters is bad cast.
390-
template<class new_type_t, class current_type_t>
391-
static xtd::uptr<new_type_t> to_unique_ptr(xtd::uptr<current_type_t>&& value) {
390+
template<class new_type, class current_type>
391+
static xtd::uptr<new_type> to_unique_ptr(xtd::uptr<current_type>&& value) {
392392
auto ptr = value.release();
393393
try {
394-
return xtd::uptr<new_type_t>(__convert_value__<new_type_t>(ptr));
394+
return xtd::uptr<new_type>(__convert_value__<new_type>(ptr));
395395
} catch (const std::exception& e) {
396-
value = xtd::uptr<current_type_t>(ptr);
396+
value = xtd::uptr<current_type>(ptr);
397397
xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_cast);
398398
}
399399
xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_cast);
@@ -411,9 +411,9 @@ namespace xtd {
411411
/// xtd::uptr<control> result = as<xtd::forms::control>(value);
412412
/// ```
413413
/// @exception xtd::invalid_cast_exception the parameters is bad cast.
414-
template<class new_type_t, class current_type_t>
415-
static xtd::uptr<new_type_t> to_uptr(xtd::uptr<current_type_t>& value) {
416-
return to_unique_ptr<new_type_t>(value);
414+
template<class new_type, class current_type>
415+
static xtd::uptr<new_type> to_uptr(xtd::uptr<current_type>& value) {
416+
return to_unique_ptr<new_type>(value);
417417
}
418418

419419
/// @brief Casts a type into another type.
@@ -427,9 +427,9 @@ namespace xtd {
427427
/// xtd::uptr<control> result = as<xtd::forms::control>(xtd::new_uptr<xtd::forms::button>());
428428
/// ```
429429
/// @exception xtd::invalid_cast_exception the parameters is bad cast.
430-
template<class new_type_t, class current_type_t>
431-
static xtd::uptr<new_type_t> to_uptr(xtd::uptr<current_type_t>&& value) {
432-
return to_unique_ptr<new_type_t>(std::move(value));
430+
template<class new_type, class current_type>
431+
static xtd::uptr<new_type> to_uptr(xtd::uptr<current_type>&& value) {
432+
return to_unique_ptr<new_type>(std::move(value));
433433
}
434434
/// @}
435435
};

src/xtd.core/include/xtd/internal/__as_any_object.hpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,15 @@ namespace xtd {
6060
if (is<box<type_t>>(o.value())) return as<box<type_t >> (o.value()).value;
6161
return __polymorphic_any_object__<type_t, typename std::is_polymorphic<type_t>::type> {}(o);
6262
}
63-
63+
64+
/// @cond
65+
template<>
66+
[[nodiscard]] inline auto as<xtd::null_ptr>(any_object& o) -> xtd::null_ptr {
67+
if (o.has_value()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_cast);
68+
return xtd::null;
69+
}
70+
/// @endcond
71+
6472
/// @brief Casts a type into another type.
6573
/// @param value object to convert.
6674
/// @return A new bool object converted from value.
@@ -84,7 +92,15 @@ namespace xtd {
8492
if (is<box<type_t>>(o.value())) return as<box<type_t >> (o.value()).value;
8593
return __polymorphic_any_object__<type_t, typename std::is_polymorphic<type_t>::type> {}(o);
8694
}
87-
95+
96+
/// @cond
97+
template<>
98+
[[nodiscard]] inline auto as<xtd::null_ptr>(const any_object& o) -> xtd::null_ptr {
99+
if (o.has_value()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_cast);
100+
return xtd::null;
101+
}
102+
/// @endcond
103+
88104
/// @brief Casts a type into another type.
89105
/// @param value object to convert.
90106
/// @return A new xtd::ulong object converted from value.

0 commit comments

Comments
 (0)