Skip to content

Commit 08eca8b

Browse files
authored
utf-8 encoding of runtime names (#610)
1 parent 7551187 commit 08eca8b

File tree

17 files changed

+120
-180
lines changed

17 files changed

+120
-180
lines changed

cppwinrt/code_writers.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ namespace cppwinrt
353353
}
354354
else
355355
{
356-
w.write(R"(, L", ")");
356+
w.write(R"(, u8", ")");
357357
}
358358
w.write(", name_v<%>", param.Name());
359359
}
@@ -366,14 +366,14 @@ namespace cppwinrt
366366

367367
if (empty(generics))
368368
{
369-
auto format = R"( template <> inline constexpr auto& name_v<%> = L"%.%";
369+
auto format = R"( template <> inline constexpr auto& name_v<%> = u8"%.%";
370370
)";
371371

372372
w.write(format, type, type_name.name_space, type_name.name);
373373
}
374374
else
375375
{
376-
auto format = R"( template <%> inline constexpr auto name_v<%> = zcombine(L"%.%<"%, L">");
376+
auto format = R"( template <%> inline constexpr auto name_v<%> = zcombine(u8"%.%<"%, u8">");
377377
)";
378378

379379
w.write(format,

strings/base_foundation.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ WINRT_EXPORT namespace winrt::Windows::Foundation
102102

103103
namespace winrt::impl
104104
{
105-
template <> inline constexpr auto& name_v<Windows::Foundation::Point> = L"Windows.Foundation.Point";
106-
template <> inline constexpr auto& name_v<Windows::Foundation::Size> = L"Windows.Foundation.Size";
107-
template <> inline constexpr auto& name_v<Windows::Foundation::Rect> = L"Windows.Foundation.Rect";
105+
template <> inline constexpr auto& name_v<Windows::Foundation::Point> = u8"Windows.Foundation.Point";
106+
template <> inline constexpr auto& name_v<Windows::Foundation::Size> = u8"Windows.Foundation.Size";
107+
template <> inline constexpr auto& name_v<Windows::Foundation::Rect> = u8"Windows.Foundation.Rect";
108108

109109
template <> struct category<Windows::Foundation::Point>
110110
{
@@ -123,13 +123,13 @@ namespace winrt::impl
123123

124124
#ifdef WINRT_IMPL_NUMERICS
125125

126-
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::float2> = L"Windows.Foundation.Numerics.Vector2";
127-
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::float3> = L"Windows.Foundation.Numerics.Vector3";
128-
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::float4> = L"Windows.Foundation.Numerics.Vector4";
129-
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::float3x2> = L"Windows.Foundation.Numerics.Matrix3x2";
130-
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::float4x4> = L"Windows.Foundation.Numerics.Matrix4x4";
131-
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::quaternion> = L"Windows.Foundation.Numerics.Quaternion";
132-
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::plane> = L"Windows.Foundation.Numerics.Plane";
126+
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::float2> = u8"Windows.Foundation.Numerics.Vector2";
127+
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::float3> = u8"Windows.Foundation.Numerics.Vector3";
128+
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::float4> = u8"Windows.Foundation.Numerics.Vector4";
129+
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::float3x2> = u8"Windows.Foundation.Numerics.Matrix3x2";
130+
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::float4x4> = u8"Windows.Foundation.Numerics.Matrix4x4";
131+
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::quaternion> = u8"Windows.Foundation.Numerics.Quaternion";
132+
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::plane> = u8"Windows.Foundation.Numerics.Plane";
133133

134134
template <> struct category<Windows::Foundation::Numerics::float2>
135135
{

strings/base_identity.h

Lines changed: 55 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ WINRT_EXPORT namespace winrt
1919

2020
namespace 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
}

strings/base_implements.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ namespace winrt::impl
419419
{
420420
static hstring get()
421421
{
422-
return hstring{ name_of<I>() };
422+
return name_of<I>();
423423
}
424424
};
425425

strings/base_string.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,3 +638,27 @@ WINRT_EXPORT namespace winrt
638638
return result;
639639
}
640640
}
641+
642+
namespace winrt::impl
643+
{
644+
template <size_t Size>
645+
hstring literal_to_hstring(std::array<char_type, Size> const& value) noexcept
646+
{
647+
return to_hstring(std::string_view(reinterpret_cast<char const*>(value.data()), Size - 1));
648+
}
649+
650+
template <size_t Size>
651+
hstring literal_to_hstring(char_type const (&value)[Size]) noexcept
652+
{
653+
return to_hstring(std::string_view(reinterpret_cast<char const*>(value), Size - 1));
654+
}
655+
}
656+
657+
WINRT_EXPORT namespace winrt
658+
{
659+
template <typename T>
660+
hstring name_of() noexcept
661+
{
662+
return impl::literal_to_hstring(impl::name_v<T>);
663+
}
664+
}

strings/base_types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ WINRT_EXPORT namespace winrt
2626
struct hstring;
2727
struct clock;
2828

29+
template <typename T>
30+
hstring name_of() noexcept;
31+
2932
struct hresult
3033
{
3134
int32_t value{};

strings/base_windows.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ namespace winrt::impl
1111

1212
struct diagnostics_info
1313
{
14-
std::map<std::wstring_view, uint32_t> queries;
15-
std::map<std::wstring_view, factory_diagnostics_info> factories;
14+
std::map<hstring, uint32_t> queries;
15+
std::map<hstring, factory_diagnostics_info> factories;
1616
};
1717

1818
struct diagnostics_cache

0 commit comments

Comments
 (0)