xtd 0.2.0
Loading...
Searching...
No Matches
__as_generic.h
Go to the documentation of this file.
1
3#pragma once
4
6#if !defined(__XTD_CORE_INTERNAL__)
7#error "Do not include this file: Internal use only"
8#endif
10
11#define __XTD_STD_INTERNAL__
12#include "__xtd_std_version.h"
13#undef __XTD_STD_INTERNAL__
14
16namespace xtd {
35 template<typename new_type_t>
36 new_type_t as(std::any value) {
37 try {
38 return std::any_cast<new_type_t>(value);
39 } catch (const std::exception& e) {
40 throw invalid_cast_exception(e.what());
41 }
42 }
43
61 template<typename new_type_t>
62 new_type_t as(bool value) {
63 return static_cast<new_type_t>(value);
64 }
65
83 template<typename new_type_t>
84 new_type_t as(xtd::byte value) {
85 return static_cast<new_type_t>(value);
86 }
87
105 template<typename new_type_t>
106 new_type_t as(char value) {
107 return static_cast<new_type_t>(value);
108 }
109
110#if defined(__xtd__cpp_lib_char8_t)
128 template<typename new_type_t>
129 new_type_t as(char8 value) {
130 return static_cast<new_type_t>(value);
131 }
132#endif
133
151 template<typename new_type_t>
152 new_type_t as(char16 value) {
153 return static_cast<new_type_t>(value);
154 }
155
173 template<typename new_type_t>
174 new_type_t as(char32 value) {
175 return static_cast<new_type_t>(value);
176 }
177
195 template<typename new_type_t>
196 new_type_t as(wchar value) {
197 return static_cast<new_type_t>(value);
198 }
199
217 template<typename new_type_t>
218 new_type_t as(decimal value) {
219 return static_cast<new_type_t>(value);
220 }
221
239 template<typename new_type_t>
240 new_type_t as(double value) {
241 return static_cast<new_type_t>(value);
242 }
243
261 template<typename new_type_t>
262 new_type_t as(float value) {
263 return static_cast<new_type_t>(value);
264 }
265
283 template<typename new_type_t>
284 new_type_t as(int16 value) {
285 return static_cast<new_type_t>(value);
286 }
287
305 template<typename new_type_t>
306 new_type_t as(int32 value) {
307 return static_cast<new_type_t>(value);
308 }
309
327 template<typename new_type_t>
328 new_type_t as(int64 value) {
329 return static_cast<new_type_t>(value);
330 }
331
349 template<typename new_type_t>
350 new_type_t as(slong value) {
351 return static_cast<new_type_t>(value);
352 }
353
371 template<typename new_type_t>
372 new_type_t as(sbyte value) {
373 return static_cast<new_type_t>(value);
374 }
375
393 template<typename new_type_t>
394 new_type_t as(uint16 value) {
395 return static_cast<new_type_t>(value);
396 }
397
415 template<typename new_type_t>
416 new_type_t as(uint32 value) {
417 return static_cast<new_type_t>(value);
418 }
419
437 template<typename new_type_t>
438 new_type_t as(uint64 value) {
439 return static_cast<new_type_t>(value);
440 }
441
459 template<typename new_type_t>
460 new_type_t as(xtd::ulong value) {
461 return static_cast<new_type_t>(value);
462 }
463
482 template<typename new_type_t>
483 new_type_t as(const xtd::string& value) {
484 try {
485 return dynamic_cast<const new_type_t&>(value);
486 } catch (const std::exception& e) {
487 throw invalid_cast_exception(e.what());
488 }
489 throw std::bad_cast();
490 }
491
510 template<typename new_type_t>
511 new_type_t as(xtd::string& value) {
512 try {
513 return dynamic_cast<new_type_t&>(value);
514 } catch (const std::exception& e) {
515 throw invalid_cast_exception(e.what());
516 }
517 throw std::bad_cast();
518 }
519
521 template<typename new_type_t>
522 new_type_t as(const xtd::string& value, xtd::byte from_base) {
523 throw invalid_cast_exception {};
524 }
525 template<typename new_type_t>
526 new_type_t as(xtd::byte value, xtd::byte from_base) {
527 throw invalid_cast_exception {};
528 }
529 template<typename new_type_t>
530 new_type_t as(int16 value, xtd::byte from_base) {
531 throw invalid_cast_exception {};
532 }
533 template<typename new_type_t>
534 new_type_t as(int32 value, xtd::byte from_base) {
535 throw invalid_cast_exception {};
536 }
537 template<typename new_type_t>
538 new_type_t as(int64 value, xtd::byte from_base) {
539 throw invalid_cast_exception {};
540 }
541 template<typename new_type_t>
542 new_type_t as(slong value, xtd::byte from_base) {
543 throw invalid_cast_exception {};
544 }
545 template<typename new_type_t>
546 new_type_t as(sbyte value, xtd::byte from_base) {
547 throw invalid_cast_exception {};
548 }
549 template<typename new_type_t>
550 new_type_t as(uint16 value, xtd::byte from_base) {
551 throw invalid_cast_exception {};
552 }
553 template<typename new_type_t>
554 new_type_t as(uint32 value, xtd::byte from_base) {
555 throw invalid_cast_exception {};
556 }
557 template<typename new_type_t>
558 new_type_t as(uint64 value, xtd::byte from_base) {
559 throw invalid_cast_exception {};
560 }
561 template<typename new_type_t>
562 new_type_t as(xtd::ulong value, xtd::byte from_base) {
563 throw invalid_cast_exception {};
564 }
566
580 template<typename new_type_t>
581 new_type_t as(const std::string& value) {
582 try {
583 return dynamic_cast<const new_type_t&>(value);
584 } catch (const std::exception& e) {
585 throw invalid_cast_exception(e.what());
586 }
587 throw std::bad_cast();
588 }
589
603 template<typename new_type_t>
604 new_type_t as(std::string& value) {
605 try {
606 return dynamic_cast<new_type_t&>(value);
607 } catch (const std::exception& e) {
608 throw invalid_cast_exception(e.what());
609 }
610 throw std::bad_cast();
611 }
612
613#if defined(__xtd__cpp_lib_char8_t)
627 template<typename new_type_t>
628 new_type_t as(const std::u8string& value) {
629 try {
630 return dynamic_cast<const new_type_t&>(value);
631 } catch (const std::exception& e) {
632 throw invalid_cast_exception(e.what());
633 }
634 throw std::bad_cast();
635 }
636
650 template<typename new_type_t>
651 new_type_t as(std::u8string& value) {
652 try {
653 return dynamic_cast<new_type_t&>(value);
654 } catch (const std::exception& e) {
655 throw invalid_cast_exception(e.what());
656 }
657 throw std::bad_cast();
658 }
659#endif
660
674 template<typename new_type_t>
675 new_type_t as(const std::u16string& value) {
676 try {
677 return dynamic_cast<const new_type_t&>(value);
678 } catch (const std::exception& e) {
679 throw invalid_cast_exception(e.what());
680 }
681 throw std::bad_cast();
682 }
683
697 template<typename new_type_t>
698 new_type_t as(std::u16string& value) {
699 try {
700 return dynamic_cast<new_type_t&>(value);
701 } catch (const std::exception& e) {
702 throw invalid_cast_exception(e.what());
703 }
704 throw std::bad_cast();
705 }
706
720 template<typename new_type_t>
721 new_type_t as(const std::u32string& value) {
722 try {
723 return dynamic_cast<const new_type_t&>(value);
724 } catch (const std::exception& e) {
725 throw invalid_cast_exception(e.what());
726 }
727 throw std::bad_cast();
728 }
729
743 template<typename new_type_t>
744 new_type_t as(std::u32string& value) {
745 try {
746 return dynamic_cast<new_type_t&>(value);
747 } catch (const std::exception& e) {
748 throw invalid_cast_exception(e.what());
749 }
750 throw std::bad_cast();
751 }
752
766 template<typename new_type_t>
767 new_type_t as(const std::wstring& value) {
768 try {
769 return dynamic_cast<const new_type_t&>(value);
770 } catch (const std::exception& e) {
771 throw invalid_cast_exception(e.what());
772 }
773 throw std::bad_cast();
774 }
775
789 template<typename new_type_t>
790 new_type_t as(std::wstring& value) {
791 try {
792 return dynamic_cast<new_type_t&>(value);
793 } catch (const std::exception& e) {
794 throw invalid_cast_exception(e.what());
795 }
796 throw std::bad_cast();
797 }
798
816 template<typename new_type_t>
817 new_type_t as(const char* value) {
818 return reinterpret_cast<new_type_t>(value);
819 }
820
838 template<typename new_type_t>
839 new_type_t as(char* value) {
840 return reinterpret_cast<new_type_t>(value);
841 }
842
843#if defined(__xtd__cpp_lib_char8_t)
861 template<typename new_type_t>
862 new_type_t as(const char8* value) {
863 return reinterpret_cast<new_type_t>(value);
864 }
865
883 template<typename new_type_t>
884 new_type_t as(char8* value) {
885 return reinterpret_cast<new_type_t>(value);
886 }
887#endif
888
906 template<typename new_type_t>
907 new_type_t as(const char16* value) {
908 return reinterpret_cast<new_type_t>(value);
909 }
910
928 template<typename new_type_t>
929 new_type_t as(char16* value) {
930 return reinterpret_cast<new_type_t>(value);
931 }
932
950 template<typename new_type_t>
951 new_type_t as(const char32* value) {
952 return reinterpret_cast<new_type_t>(value);
953 }
954
972 template<typename new_type_t>
973 new_type_t as(char32* value) {
974 return reinterpret_cast<new_type_t>(value);
975 }
976
994 template<typename new_type_t>
995 new_type_t as(const wchar* value) {
996 return reinterpret_cast<new_type_t>(value);
997 }
998
1016 template<typename new_type_t>
1017 new_type_t as(wchar* value) {
1018 return reinterpret_cast<new_type_t>(value);
1019 }
1020}
Contains __xtd_std_version definitions.
Represents text as a sequence of character units.
Definition basic_string.h:79
The exception that is thrown for invalid casting or explicit conversion.
Definition invalid_cast_exception.h:19
int16_t int16
Represents a 16-bit signed integer.
Definition int16.h:23
char8_t char8
Represents a 8-bit unicode character.
Definition char8.h:27
wchar_t wchar
Represents a wide character.
Definition wchar.h:24
int32_t int32
Represents a 32-bit signed integer.
Definition int32.h:23
long double decimal
Represents a decimal-precision floating-point number.
Definition decimal.h:23
int64_t int64
Represents a 64-bit signed integer.
Definition int64.h:23
__ulong__ ulong
Represents a 32-bit or 64-bit unsigned integer.
Definition ulong.h:27
int8_t sbyte
Represents a 8-bit signed integer.
Definition sbyte.h:23
char16_t char16
Represents a 16-bit unicode character.
Definition char16.h:26
uint32_t uint32
Represents a 32-bit unsigned integer.
Definition uint32.h:23
char32_t char32
Represents a 32-bit unicode character.
Definition char32.h:26
uint64_t uint64
Represents a 64-bit unsigned integer.
Definition uint64.h:23
uint8_t byte
Represents a 8-bit unsigned integer.
Definition byte.h:23
uint16_t uint16
Represents a 16-bit unsigned integer.
Definition uint16.h:23
__slong__ slong
Represents a 32-bit or 64-bit signed integer.
Definition slong.h:27
type_t as(any_object &o)
Casts a type into another type.
Definition __as_any_object.h:57
@ e
The E key.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10