Skip to content

Commit 6f1c52c

Browse files
authored
Revert utf-8 encoding of runtime class names (#621)
1 parent ede55c0 commit 6f1c52c

18 files changed

+190
-128
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"(, u8", ")");
356+
w.write(R"(, L", ")");
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<%> = u8"%.%";
369+
auto format = R"( template <> inline constexpr auto& name_v<%> = L"%.%";
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(u8"%.%<"%, u8">");
376+
auto format = R"( template <%> inline constexpr auto name_v<%> = zcombine(L"%.%<"%, L">");
377377
)";
378378

379379
w.write(format,

cppwinrt/component_writers.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ namespace cppwinrt
106106
if (settings.component_opt)
107107
{
108108
auto format = R"(
109-
if (requal(name, "%.%"))
109+
if (requal(name, L"%.%"))
110110
{
111111
return winrt_make_%();
112112
}
@@ -120,7 +120,7 @@ namespace cppwinrt
120120
else
121121
{
122122
auto format = R"(
123-
if (requal(name, "%.%"))
123+
if (requal(name, L"%.%"))
124124
{
125125
return winrt::detach_abi(winrt::make<winrt::@::factory_implementation::%>());
126126
}
@@ -149,11 +149,9 @@ bool __stdcall %_can_unload_now() noexcept
149149
return true;
150150
}
151151
152-
void* __stdcall %_get_activation_factory([[maybe_unused]] std::wstring_view const& wname)
152+
void* __stdcall %_get_activation_factory([[maybe_unused]] std::wstring_view const& name)
153153
{
154-
auto name = winrt::to_string(wname);
155-
156-
auto requal = [](std::string_view const& left, std::string_view const& right) noexcept
154+
auto requal = [](std::wstring_view const& left, std::wstring_view const& right) noexcept
157155
{
158156
return std::equal(left.rbegin(), left.rend(), right.rbegin(), right.rend());
159157
};
@@ -752,7 +750,7 @@ catch (...) { return winrt::to_hresult(); }
752750
%
753751
hstring GetRuntimeClassName() const
754752
{
755-
return name_of<class_type>();
753+
return L"%.%";
756754
}
757755
%%%% };
758756
}
@@ -822,6 +820,8 @@ catch (...) { return winrt::to_hresult(); }
822820
type_name,
823821
type_name,
824822
composable_base_name,
823+
type_namespace,
824+
type_name,
825825
bind<write_component_class_override_constructors>(type),
826826
bind<write_component_override_dispatch_base>(type),
827827
bind<write_component_base_call>(type),
@@ -839,7 +839,7 @@ catch (...) { return winrt::to_hresult(); }
839839
840840
hstring GetRuntimeClassName() const
841841
{
842-
return name_of<instance_type>();
842+
return L"%.%";
843843
}
844844
% };
845845
}
@@ -851,6 +851,8 @@ catch (...) { return winrt::to_hresult(); }
851851
bind<write_component_factory_interfaces>(factories),
852852
type_namespace,
853853
type_name,
854+
type_namespace,
855+
type_name,
854856
bind<write_component_forwarders>(factories));
855857
}
856858

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> = 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";
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";
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> = 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";
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";
133133

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

strings/base_identity.h

Lines changed: 120 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ 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-
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
}

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 name_of<I>();
422+
return hstring{ name_of<I>() };
423423
}
424424
};
425425

0 commit comments

Comments
 (0)