@@ -19,6 +19,12 @@ WINRT_EXPORT namespace winrt
1919
2020namespace winrt ::impl
2121{
22+ #ifdef __cpp_char8_t
23+ using char_type = char8_t ;
24+ #else
25+ using char_type = char ;
26+ #endif
27+
2228 template <size_t Size, typename T, size_t ... Index>
2329 constexpr std::array<T, Size> to_array (T const * value, std::index_sequence<Index...> const ) noexcept
2430 {
@@ -32,7 +38,7 @@ namespace winrt::impl
3238 }
3339
3440 template <size_t Size>
35- constexpr auto to_array (char const (&value)[Size]) noexcept
41+ constexpr auto to_array (char_type const (&value)[Size]) noexcept
3642 {
3743 return to_array<Size - 1 >(value, std::make_index_sequence<Size - 1 >());
3844 }
@@ -427,7 +433,7 @@ namespace winrt::impl
427433 }
428434
429435 template <size_t Size>
430- constexpr guid generate_guid (std::array<char , Size> const & value) noexcept
436+ constexpr guid generate_guid (std::array<char_type , Size> const & value) noexcept
431437 {
432438 guid namespace_guid = { 0xd57af411 , 0x737b , 0xc042 ,{ 0xab , 0xae , 0x87 , 0x8b , 0x1e , 0x16 , 0xad , 0xee } };
433439
@@ -441,7 +447,7 @@ namespace winrt::impl
441447 template <typename TArg, typename ... TRest>
442448 struct arg_collection
443449 {
444- constexpr static auto data{ combine (to_array (signature<TArg>::data), " ;" , arg_collection<TRest...>::data) };
450+ constexpr static auto data{ combine (to_array (signature<TArg>::data), u8 " ;" , arg_collection<TRest...>::data) };
445451 };
446452
447453 template <typename TArg>
@@ -467,8 +473,8 @@ namespace winrt::impl
467473 {
468474 combine
469475 (
470- to_array<wchar_t >(guid_of<T>()),
471- std::array<wchar_t , 1 >{ L ' \0 ' }
476+ to_array<char_type >(guid_of<T>()),
477+ std::array<char_type , 1 >{ ' \0 ' }
472478 )
473479 };
474480
@@ -487,98 +493,48 @@ namespace winrt::impl
487493 return 3 ;
488494 }
489495
490- constexpr size_t to_utf8 (wchar_t const value, char * buffer) noexcept
491- {
492- if (value <= 0x7F )
493- {
494- *buffer = static_cast <char >(value);
495- return 1 ;
496- }
497-
498- if (value <= 0x7FF )
499- {
500- *buffer = static_cast <char >(0xC0 | (value >> 6 ));
501- *(buffer + 1 ) = 0x80 | (value & 0x3F );
502- return 2 ;
503- }
504-
505- *buffer = 0xE0 | (value >> 12 );
506- *(buffer + 1 ) = 0x80 | ((value >> 6 ) & 0x3F );
507- *(buffer + 2 ) = 0x80 | (value & 0x3F );
508- return 3 ;
509- }
510-
511- template <typename T>
512- constexpr size_t to_utf8_size () noexcept
513- {
514- auto input = to_array (name_v<T>);
515- size_t length = 0 ;
516-
517- for (wchar_t const element : input)
518- {
519- length += to_utf8_size (element);
520- }
521-
522- return length;
523- }
524-
525- template <typename T>
526- constexpr auto to_utf8 () noexcept
527- {
528- auto input = to_array (name_v<T>);
529- std::array<char , to_utf8_size<T>()> output{};
530- size_t offset{};
531-
532- for (wchar_t const element : input)
533- {
534- offset += to_utf8 (element, &output[offset]);
535- }
536-
537- return output;
538- }
539-
540496 template <typename T>
541497 constexpr guid generic_guid_v{};
542498
543499 template <typename T>
544- constexpr auto & basic_signature_v = " " ;
545-
546- template <> inline constexpr auto & basic_signature_v<bool > = " b1" ;
547- template <> inline constexpr auto & basic_signature_v<int8_t > = " i1" ;
548- template <> inline constexpr auto & basic_signature_v<int16_t > = " i2" ;
549- template <> inline constexpr auto & basic_signature_v<int32_t > = " i4" ;
550- template <> inline constexpr auto & basic_signature_v<int64_t > = " i8" ;
551- template <> inline constexpr auto & basic_signature_v<uint8_t > = " u1" ;
552- template <> inline constexpr auto & basic_signature_v<uint16_t > = " u2" ;
553- template <> inline constexpr auto & basic_signature_v<uint32_t > = " u4" ;
554- template <> inline constexpr auto & basic_signature_v<uint64_t > = " u8" ;
555- template <> inline constexpr auto & basic_signature_v<float > = " f4" ;
556- template <> inline constexpr auto & basic_signature_v<double > = " f8" ;
557- template <> inline constexpr auto & basic_signature_v<char16_t > = " c2" ;
558- template <> inline constexpr auto & basic_signature_v<guid> = " g16" ;
559- template <> inline constexpr auto & basic_signature_v<hstring> = " string" ;
560- template <> inline constexpr auto & basic_signature_v<Windows::Foundation::IInspectable> = " cinterface(IInspectable)" ;
561-
562- template <> inline constexpr auto & name_v<bool > = L " Boolean" ;
563- template <> inline constexpr auto & name_v<int8_t > = L " Int8" ;
564- template <> inline constexpr auto & name_v<int16_t > = L " Int16" ;
565- template <> inline constexpr auto & name_v<int32_t > = L " Int32" ;
566- template <> inline constexpr auto & name_v<int64_t > = L " Int64" ;
567- template <> inline constexpr auto & name_v<uint8_t > = L " UInt8" ;
568- template <> inline constexpr auto & name_v<uint16_t > = L " UInt16" ;
569- template <> inline constexpr auto & name_v<uint32_t > = L " UInt32" ;
570- template <> inline constexpr auto & name_v<uint64_t > = L " UInt64" ;
571- template <> inline constexpr auto & name_v<float > = L " Single" ;
572- template <> inline constexpr auto & name_v<double > = L " Double" ;
573- template <> inline constexpr auto & name_v<char16_t > = L " Char16" ;
574- template <> inline constexpr auto & name_v<guid> = L " Guid" ;
575- template <> inline constexpr auto & name_v<hstring> = L " String" ;
576- template <> inline constexpr auto & name_v<hresult> = L " Windows.Foundation.HResult" ;
577- template <> inline constexpr auto & name_v<event_token> = L " Windows.Foundation.EventRegistrationToken" ;
578- template <> inline constexpr auto & name_v<Windows::Foundation::IInspectable> = L " Object" ;
579- template <> inline constexpr auto & name_v<Windows::Foundation::TimeSpan> = L " Windows.Foundation.TimeSpan" ;
580- template <> inline constexpr auto & name_v<Windows::Foundation::DateTime> = L " Windows.Foundation.DateTime" ;
581- template <> inline constexpr auto & name_v<IAgileObject> = L " IAgileObject" ;
500+ constexpr auto & basic_signature_v = u8 "" ;
501+
502+ template <> inline constexpr auto & basic_signature_v<bool > = u8 " b1" ;
503+ template <> inline constexpr auto & basic_signature_v<int8_t > = u8 " i1" ;
504+ template <> inline constexpr auto & basic_signature_v<int16_t > = u8 " i2" ;
505+ template <> inline constexpr auto & basic_signature_v<int32_t > = u8 " i4" ;
506+ template <> inline constexpr auto & basic_signature_v<int64_t > = u8 " i8" ;
507+ template <> inline constexpr auto & basic_signature_v<uint8_t > = u8 " u1" ;
508+ template <> inline constexpr auto & basic_signature_v<uint16_t > = u8 " u2" ;
509+ template <> inline constexpr auto & basic_signature_v<uint32_t > = u8 " u4" ;
510+ template <> inline constexpr auto & basic_signature_v<uint64_t > = u8 " u8" ;
511+ template <> inline constexpr auto & basic_signature_v<float > = u8 " f4" ;
512+ template <> inline constexpr auto & basic_signature_v<double > = u8 " f8" ;
513+ template <> inline constexpr auto & basic_signature_v<char16_t > = u8 " c2" ;
514+ template <> inline constexpr auto & basic_signature_v<guid> = u8 " g16" ;
515+ template <> inline constexpr auto & basic_signature_v<hstring> = u8 " string" ;
516+ template <> inline constexpr auto & basic_signature_v<Windows::Foundation::IInspectable> = u8 " cinterface(IInspectable)" ;
517+
518+ template <> inline constexpr auto & name_v<bool > = u8 " Boolean" ;
519+ template <> inline constexpr auto & name_v<int8_t > = u8 " Int8" ;
520+ template <> inline constexpr auto & name_v<int16_t > = u8 " Int16" ;
521+ template <> inline constexpr auto & name_v<int32_t > = u8 " Int32" ;
522+ template <> inline constexpr auto & name_v<int64_t > = u8 " Int64" ;
523+ template <> inline constexpr auto & name_v<uint8_t > = u8 " UInt8" ;
524+ template <> inline constexpr auto & name_v<uint16_t > = u8 " UInt16" ;
525+ template <> inline constexpr auto & name_v<uint32_t > = u8 " UInt32" ;
526+ template <> inline constexpr auto & name_v<uint64_t > = u8 " UInt64" ;
527+ template <> inline constexpr auto & name_v<float > = u8 " Single" ;
528+ template <> inline constexpr auto & name_v<double > = u8 " Double" ;
529+ template <> inline constexpr auto & name_v<char16_t > = u8 " Char16" ;
530+ template <> inline constexpr auto & name_v<guid> = u8 " Guid" ;
531+ template <> inline constexpr auto & name_v<hstring> = u8 " String" ;
532+ template <> inline constexpr auto & name_v<hresult> = u8 " Windows.Foundation.HResult" ;
533+ template <> inline constexpr auto & name_v<event_token> = u8 " Windows.Foundation.EventRegistrationToken" ;
534+ template <> inline constexpr auto & name_v<Windows::Foundation::IInspectable> = u8 " Object" ;
535+ template <> inline constexpr auto & name_v<Windows::Foundation::TimeSpan> = u8 " Windows.Foundation.TimeSpan" ;
536+ template <> inline constexpr auto & name_v<Windows::Foundation::DateTime> = u8 " Windows.Foundation.DateTime" ;
537+ template <> inline constexpr auto & name_v<IAgileObject> = u8 " IAgileObject" ;
582538
583539 template <> struct category <bool > { using type = basic_category; };
584540 template <> struct category <int8_t > { using type = basic_category; };
@@ -609,57 +565,36 @@ namespace winrt::impl
609565 struct category_signature <enum_category, T>
610566 {
611567 using enum_type = std::underlying_type_t <T>;
612- constexpr static auto data{ combine (" enum(" , to_utf8 <T>(), " ;" , signature<enum_type>::data, " )" ) };
568+ constexpr static auto data{ combine (u8 " enum(" , name_v <T>, u8 " ;" , signature<enum_type>::data, u8 " )" ) };
613569 };
614570
615571 template <typename ... Fields, typename T>
616572 struct category_signature <struct_category<Fields...>, T>
617573 {
618- constexpr static auto data{ combine (" struct(" , to_utf8 <T>(), " ;" , arg_collection<Fields...>::data, " )" ) };
574+ constexpr static auto data{ combine (u8 " struct(" , name_v <T>, u8 " ;" , arg_collection<Fields...>::data, u8 " )" ) };
619575 };
620576
621577 template <typename T>
622578 struct category_signature <class_category, T>
623579 {
624- constexpr static auto data{ combine (" rc(" , to_utf8 <T>(), " ;" , signature<winrt::default_interface<T>>::data, " )" ) };
580+ constexpr static auto data{ combine (u8 " rc(" , name_v <T>, u8 " ;" , signature<winrt::default_interface<T>>::data, u8 " )" ) };
625581 };
626582
627583 template <typename ... Args, typename T>
628584 struct category_signature <generic_category<Args...>, T>
629585 {
630- constexpr static auto data{ combine (" pinterface(" , to_array<char >(generic_guid_v<T>), " ;" , arg_collection<Args...>::data, " )" ) };
586+ constexpr static auto data{ combine (u8 " pinterface(" , to_array<char_type >(generic_guid_v<T>), u8 " ;" , arg_collection<Args...>::data, u8 " )" ) };
631587 };
632588
633589 template <typename T>
634590 struct category_signature <interface_category, T>
635591 {
636- constexpr static auto data{ to_array<char >(guid_of<T>()) };
592+ constexpr static auto data{ to_array<char_type >(guid_of<T>()) };
637593 };
638594
639595 template <typename T>
640596 struct category_signature <delegate_category, T>
641597 {
642- constexpr static auto data{ combine (" delegate(" , to_array<char >(guid_of<T>()), " )" ) };
598+ constexpr static auto data{ combine (u8 " delegate(" , to_array<char_type >(guid_of<T>()), u8 " )" ) };
643599 };
644-
645- template <size_t Size>
646- constexpr std::wstring_view to_wstring_view (std::array<wchar_t , Size> const & value) noexcept
647- {
648- return { value.data (), Size - 1 };
649- }
650-
651- template <size_t Size>
652- constexpr std::wstring_view to_wstring_view (wchar_t const (&value)[Size]) noexcept
653- {
654- return { value, Size - 1 };
655- }
656- }
657-
658- WINRT_EXPORT namespace winrt
659- {
660- template <typename T>
661- constexpr auto name_of () noexcept
662- {
663- return impl::to_wstring_view (impl::name_v<T>);
664- }
665600}
0 commit comments