@@ -19,12 +19,6 @@ 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-
2822 template <size_t Size, typename T, size_t ... Index>
2923 constexpr std::array<T, Size> to_array (T const * value, std::index_sequence<Index...> const ) noexcept
3024 {
@@ -38,7 +32,7 @@ namespace winrt::impl
3832 }
3933
4034 template <size_t Size>
41- constexpr auto to_array (char_type const (&value)[Size]) noexcept
35+ constexpr auto to_array (char const (&value)[Size]) noexcept
4236 {
4337 return to_array<Size - 1 >(value, std::make_index_sequence<Size - 1 >());
4438 }
@@ -433,7 +427,7 @@ namespace winrt::impl
433427 }
434428
435429 template <size_t Size>
436- constexpr guid generate_guid (std::array<char_type , Size> const & value) noexcept
430+ constexpr guid generate_guid (std::array<char , Size> const & value) noexcept
437431 {
438432 guid namespace_guid = { 0xd57af411 , 0x737b , 0xc042 ,{ 0xab , 0xae , 0x87 , 0x8b , 0x1e , 0x16 , 0xad , 0xee } };
439433
@@ -447,7 +441,7 @@ namespace winrt::impl
447441 template <typename TArg, typename ... TRest>
448442 struct arg_collection
449443 {
450- constexpr static auto data{ combine (to_array (signature<TArg>::data), u8 " ;" , arg_collection<TRest...>::data) };
444+ constexpr static auto data{ combine (to_array (signature<TArg>::data), " ;" , arg_collection<TRest...>::data) };
451445 };
452446
453447 template <typename TArg>
@@ -473,8 +467,8 @@ namespace winrt::impl
473467 {
474468 combine
475469 (
476- to_array<char_type >(guid_of<T>()),
477- std::array<char_type , 1 >{ ' \0 ' }
470+ to_array<wchar_t >(guid_of<T>()),
471+ std::array<wchar_t , 1 >{ L ' \0 ' }
478472 )
479473 };
480474
@@ -493,48 +487,98 @@ namespace winrt::impl
493487 return 3 ;
494488 }
495489
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+
496540 template <typename T>
497541 constexpr guid generic_guid_v{};
498542
499543 template <typename T>
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" ;
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" ;
538582
539583 template <> struct category <bool > { using type = basic_category; };
540584 template <> struct category <int8_t > { using type = basic_category; };
@@ -565,36 +609,57 @@ namespace winrt::impl
565609 struct category_signature <enum_category, T>
566610 {
567611 using enum_type = std::underlying_type_t <T>;
568- constexpr static auto data{ combine (u8 " enum(" , name_v <T>, u8 " ;" , signature<enum_type>::data, u8 " )" ) };
612+ constexpr static auto data{ combine (" enum(" , to_utf8 <T>(), " ;" , signature<enum_type>::data, " )" ) };
569613 };
570614
571615 template <typename ... Fields, typename T>
572616 struct category_signature <struct_category<Fields...>, T>
573617 {
574- constexpr static auto data{ combine (u8 " struct(" , name_v <T>, u8 " ;" , arg_collection<Fields...>::data, u8 " )" ) };
618+ constexpr static auto data{ combine (" struct(" , to_utf8 <T>(), " ;" , arg_collection<Fields...>::data, " )" ) };
575619 };
576620
577621 template <typename T>
578622 struct category_signature <class_category, T>
579623 {
580- constexpr static auto data{ combine (u8 " rc(" , name_v <T>, u8 " ;" , signature<winrt::default_interface<T>>::data, u8 " )" ) };
624+ constexpr static auto data{ combine (" rc(" , to_utf8 <T>(), " ;" , signature<winrt::default_interface<T>>::data, " )" ) };
581625 };
582626
583627 template <typename ... Args, typename T>
584628 struct category_signature <generic_category<Args...>, T>
585629 {
586- constexpr static auto data{ combine (u8 " pinterface(" , to_array<char_type >(generic_guid_v<T>), u8 " ;" , arg_collection<Args...>::data, u8 " )" ) };
630+ constexpr static auto data{ combine (" pinterface(" , to_array<char >(generic_guid_v<T>), " ;" , arg_collection<Args...>::data, " )" ) };
587631 };
588632
589633 template <typename T>
590634 struct category_signature <interface_category, T>
591635 {
592- constexpr static auto data{ to_array<char_type >(guid_of<T>()) };
636+ constexpr static auto data{ to_array<char >(guid_of<T>()) };
593637 };
594638
595639 template <typename T>
596640 struct category_signature <delegate_category, T>
597641 {
598- constexpr static auto data{ combine (u8 " delegate(" , to_array<char_type >(guid_of<T>()), u8 " )" ) };
642+ constexpr static auto data{ combine (" delegate(" , to_array<char >(guid_of<T>()), " )" ) };
599643 };
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+ }
600665}
0 commit comments