xtd 1.0.0
Loading...
Searching...
No Matches
is.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "any.hpp"
6#include "parse.hpp"
7#include "types.hpp"
8#include <limits>
9#include <memory>
10#include <stdexcept>
11#include <variant>
12
14namespace xtd {
16 template<typename value_t>
17 [[nodiscard]] inline auto is(bool value) -> bool {return false;}
18 template<typename value_t>
19 [[nodiscard]] inline auto is(char value) -> bool {return false;}
20 template<typename value_t>
21 [[nodiscard]] inline auto is(char16 value) -> bool {return false;}
22 template<typename value_t>
23 [[nodiscard]] inline auto is(char32 value) -> bool {return false;}
24 template<typename value_t>
25 [[nodiscard]] inline auto is(char8 value) -> bool {return false;}
26 template<typename value_t>
27 [[nodiscard]] inline auto is(wchar value) -> bool {return false;}
28 template<typename value_t>
29 [[nodiscard]] inline auto is(decimal value) -> bool {return false;}
30 template<typename value_t>
31 [[nodiscard]] inline auto is(double value) -> bool {return false;}
32 template<typename value_t>
33 [[nodiscard]] inline auto is(float value) -> bool {return false;}
34 template<typename value_t>
35 [[nodiscard]] inline auto is(sbyte value) -> bool {return false;}
36 template<typename value_t>
37 [[nodiscard]] inline auto is(int16 value) -> bool {return false;}
38 template<typename value_t>
39 [[nodiscard]] inline auto is(int32 value) -> bool {return false;}
40 template<typename value_t>
41 [[nodiscard]] inline auto is(int64 value) -> bool {return false;}
42 template<typename value_t>
43 [[nodiscard]] inline auto is(slong value) -> bool {return false;}
44 template<typename value_t>
45 [[nodiscard]] inline auto is(xtd::byte value) -> bool {return false;}
46 template<typename value_t>
47 [[nodiscard]] inline auto is(uint16 value) -> bool {return false;}
48 template<typename value_t>
49 [[nodiscard]] inline auto is(uint32 value) -> bool {return false;}
50 template<typename value_t>
51 [[nodiscard]] inline auto is(uint64 value) -> bool {return false;}
52 template<typename value_t>
53 [[nodiscard]] inline auto is(xtd::ulong value) -> bool {return false;}
55
73 template<>
74 [[nodiscard]] inline auto is<bool>(bool value) -> bool {
75 return true;
76 }
77
95 template<>
96 [[nodiscard]] inline auto is<char>(char value) -> bool {
97 return true;
98 }
99
117 template<>
118 [[nodiscard]] inline auto is<char16>(char16 value) -> bool {
119 return true;
120 }
121
139 template<>
140 [[nodiscard]] inline auto is<char32>(char32 value) -> bool {
141 return true;
142 }
143
161 template<>
162 [[nodiscard]] inline auto is<char8>(char8 value) -> bool {
163 return true;
164 }
165
183 template<>
184 [[nodiscard]] inline auto is<wchar>(wchar value) -> bool {
185 return true;
186 }
187
205 template<>
206 [[nodiscard]] inline auto is<decimal>(decimal value) -> bool {
207 return true;
208 }
209
227 template<>
228 [[nodiscard]] inline auto is<double>(double value) -> bool {
229 return true;
230 }
231
249 template<>
250 [[nodiscard]] inline auto is<float>(float value) -> bool {
251 return true;
252 }
253
271 template<>
272 [[nodiscard]] inline auto is<sbyte>(sbyte value) -> bool {
273 return true;
274 }
275
293 template<>
294 [[nodiscard]] inline auto is<int16>(int16 value) -> bool {
295 return true;
296 }
297
315 template<>
316 [[nodiscard]] inline auto is<int32>(int32 value) -> bool {
317 return true;
318 }
319
337 template<>
338 [[nodiscard]] inline auto is<int64>(int64 value) -> bool {
339 return true;
340 }
341
359 template<>
360 [[nodiscard]] inline auto is<slong>(slong value) -> bool {
361 return true;
362 }
363
381 template<>
382 [[nodiscard]] inline auto is<xtd::byte>(xtd::byte value) -> bool {
383 return true;
384 }
385
403 template<>
404 [[nodiscard]] inline auto is<uint16>(uint16 value) -> bool {
405 return true;
406 }
407
425 template<>
426 [[nodiscard]] inline auto is<uint32>(uint32 value) -> bool {
427 return true;
428 }
429
447 template<>
448 [[nodiscard]] inline auto is<uint64>(uint64 value) -> bool {
449 return true;
450 }
451
469 template<>
470 [[nodiscard]] inline auto is<xtd::ulong>(xtd::ulong value) -> bool {
471 return true;
472 }
473
484 template<typename type_t>
485 [[nodiscard]] auto is(xtd::any value) -> bool {
486 try {
488 return true;
489 } catch (const std::bad_cast&) {
490 return false;
491 }
492 }
493
495 template<>
496 [[nodiscard]] inline auto is<xtd::null_ptr>(xtd::any value) -> bool {
497 return !value.has_value();
498 }
500
511 template<>
512 [[nodiscard]] inline auto is<xtd::any>(xtd::any value) -> bool {
513 return true;
514 }
515
526 template<typename type_t, typename param_t>
527 [[nodiscard]] auto is(const param_t* value) -> bool {
528 try {
529 if (value == nullptr) return false;
530 return dynamic_cast<const type_t*>(value) != nullptr;
531 } catch (const std::bad_cast&) {
532 return false;
533 }
534 }
535
546 template<typename type_t, typename param_t>
547 [[nodiscard]] auto is(const param_t& value) -> bool {
548 return is<type_t>(&value);
549 }
550
561 template<typename type_t, typename param_t>
562 requires (std::is_polymorphic_v<type_t> && std::is_polymorphic_v<param_t> && !std::is_null_pointer_v<type_t>)
563 [[nodiscard]] auto is(param_t* value) -> bool {
564 if (value == nullptr) return false;
565 return dynamic_cast<type_t*>(value) != nullptr;
566 }
567
569 template<typename type_t, typename param_t>
570 requires ((!std::is_polymorphic_v<type_t> || !std::is_polymorphic_v<param_t>) && !std::is_null_pointer_v<type_t>)
571 [[nodiscard]] auto is(param_t* value) -> bool {
572 if (value == nullptr) return false;
573 return typeid(type_t).name() == typeid(param_t).name();
574 }
575
576 template<typename type_t, typename param_t>
577 requires std::is_null_pointer_v<type_t>
578 [[nodiscard]] auto is(param_t* value) -> bool {
579 return value == nullptr;
580 }
581
582 template<typename type_t, typename ...args_t>
583 [[nodiscard]] inline auto is(const std::variant<args_t...>& value) -> bool {
584 auto result = false;
585 std::visit([&](auto&& arg) {if constexpr (std::is_same_v<std::remove_cvref_t<std::decay_t<decltype(arg)>>, std::remove_cvref_t<type_t>>) result = true;}, value);
586 return result;
587 }
588
589 template<typename type_t, typename ...args_t>
590 [[nodiscard]] inline auto is(std::variant<args_t...>& value) -> bool {
591 auto result = false;
592 std::visit([&](auto&& arg) {if constexpr (std::is_same_v<std::remove_cvref_t<std::decay_t<decltype(arg)>>, std::remove_cvref_t<type_t>>) result = true;}, value);
593 return result;
594 }
596
607 template<typename type_t, typename param_t>
608 [[nodiscard]] auto is(param_t& value) -> bool {
609 return is<type_t>(&value);
610 }
611
622 template<typename new_type, typename current_type>
623 [[nodiscard]] auto is(xtd::sptr<current_type>& value) -> bool {
624 auto result = std::dynamic_pointer_cast<new_type>(value.pointer());
625 if (result) return true;
626 return false;
627 }
628}
Contains xtd::any type and std::bad_any_cast exception.
constexpr xtd::expressions::placeholder< index > arg
The xtd::expressions::arg instance is the index argument used by expression.
Definition arg.hpp:61
@ value
Represnets the constant operator precedence (42).
Definition operator_precedence.hpp:30
std::int64_t int64
Represents a 64-bit signed integer.
Definition int64.hpp:23
std::uint32_t uint32
Represents a 32-bit unsigned integer.
Definition uint32.hpp:23
char8_t char8
Represents a 8-bit unicode character.
Definition char8.hpp:26
xtd::shared_ptr_object< type_t > sptr
The xtd::sptr object is a shared pointer.
Definition sptr.hpp:25
__slong__ slong
Represents a 32-bit or 64-bit signed integer.
Definition slong.hpp:27
std::int8_t sbyte
Represents a 8-bit signed integer.
Definition sbyte.hpp:23
wchar_t wchar
Represents a wide character.
Definition wchar.hpp:24
__ulong__ ulong
Represents a 32-bit or 64-bit unsigned integer.
Definition ulong.hpp:27
long double decimal
Represents a decimal-precision floating-point number.
Definition decimal.hpp:23
std::uint16_t uint16
Represents a 16-bit unsigned integer.
Definition uint16.hpp:23
char16_t char16
Represents a 16-bit unicode character.
Definition char16.hpp:26
std::int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:25
std::int16_t int16
Represents a 16-bit signed integer.
Definition int16.hpp:23
std::uint64_t uint64
Represents a 64-bit unsigned integer.
Definition uint64.hpp:23
std::uint8_t byte
Represents a 8-bit unsigned integer.
Definition byte.hpp:23
char32_t char32
Represents a 32-bit unicode character.
Definition char32.hpp:23
std::any any
Represents the any alias on std::any.
Definition any.hpp:24
auto is< sbyte >(sbyte value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:272
auto is< decimal >(decimal value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:206
auto is< uint16 >(uint16 value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:404
auto is< xtd::any >(xtd::any value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:512
auto is< wchar >(wchar value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:184
auto is< char32 >(char32 value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:140
auto any_cast(const xtd::any &operand) -> type_t
Performs type-safe access to the contained object.
Definition any_cast.hpp:22
auto is< int16 >(int16 value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:294
auto is< double >(double value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:228
auto is< float >(float value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:250
auto is(xtd::any value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:485
auto is< int32 >(int32 value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:316
auto is< uint32 >(uint32 value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:426
auto is< xtd::byte >(xtd::byte value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:382
auto is< bool >(bool value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:74
auto is< char16 >(char16 value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:118
auto is< slong >(slong value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:360
auto is< char8 >(char8 value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:162
auto is< uint64 >(uint64 value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:448
auto is< int64 >(int64 value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:338
auto is< xtd::ulong >(xtd::ulong value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:470
auto is< char >(char value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:96
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Contains xtd::parse methods.
Contains xtd fundamental types.