Skip to content

Commit

Permalink
utf-8 encoding of runtime names (#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr authored May 7, 2020
1 parent 7551187 commit 08eca8b
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 180 deletions.
6 changes: 3 additions & 3 deletions cppwinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ namespace cppwinrt
}
else
{
w.write(R"(, L", ")");
w.write(R"(, u8", ")");
}
w.write(", name_v<%>", param.Name());
}
Expand All @@ -366,14 +366,14 @@ namespace cppwinrt

if (empty(generics))
{
auto format = R"( template <> inline constexpr auto& name_v<%> = L"%.%";
auto format = R"( template <> inline constexpr auto& name_v<%> = u8"%.%";
)";

w.write(format, type, type_name.name_space, type_name.name);
}
else
{
auto format = R"( template <%> inline constexpr auto name_v<%> = zcombine(L"%.%<"%, L">");
auto format = R"( template <%> inline constexpr auto name_v<%> = zcombine(u8"%.%<"%, u8">");
)";

w.write(format,
Expand Down
20 changes: 10 additions & 10 deletions strings/base_foundation.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ WINRT_EXPORT namespace winrt::Windows::Foundation

namespace winrt::impl
{
template <> inline constexpr auto& name_v<Windows::Foundation::Point> = L"Windows.Foundation.Point";
template <> inline constexpr auto& name_v<Windows::Foundation::Size> = L"Windows.Foundation.Size";
template <> inline constexpr auto& name_v<Windows::Foundation::Rect> = L"Windows.Foundation.Rect";
template <> inline constexpr auto& name_v<Windows::Foundation::Point> = u8"Windows.Foundation.Point";
template <> inline constexpr auto& name_v<Windows::Foundation::Size> = u8"Windows.Foundation.Size";
template <> inline constexpr auto& name_v<Windows::Foundation::Rect> = u8"Windows.Foundation.Rect";

template <> struct category<Windows::Foundation::Point>
{
Expand All @@ -123,13 +123,13 @@ namespace winrt::impl

#ifdef WINRT_IMPL_NUMERICS

template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::float2> = L"Windows.Foundation.Numerics.Vector2";
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::float3> = L"Windows.Foundation.Numerics.Vector3";
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::float4> = L"Windows.Foundation.Numerics.Vector4";
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::float3x2> = L"Windows.Foundation.Numerics.Matrix3x2";
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::float4x4> = L"Windows.Foundation.Numerics.Matrix4x4";
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::quaternion> = L"Windows.Foundation.Numerics.Quaternion";
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::plane> = L"Windows.Foundation.Numerics.Plane";
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::float2> = u8"Windows.Foundation.Numerics.Vector2";
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::float3> = u8"Windows.Foundation.Numerics.Vector3";
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::float4> = u8"Windows.Foundation.Numerics.Vector4";
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::float3x2> = u8"Windows.Foundation.Numerics.Matrix3x2";
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::float4x4> = u8"Windows.Foundation.Numerics.Matrix4x4";
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::quaternion> = u8"Windows.Foundation.Numerics.Quaternion";
template <> inline constexpr auto& name_v<Windows::Foundation::Numerics::plane> = u8"Windows.Foundation.Numerics.Plane";

template <> struct category<Windows::Foundation::Numerics::float2>
{
Expand Down
175 changes: 55 additions & 120 deletions strings/base_identity.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ WINRT_EXPORT namespace winrt

namespace winrt::impl
{
#ifdef __cpp_char8_t
using char_type = char8_t;
#else
using char_type = char;
#endif

template <size_t Size, typename T, size_t... Index>
constexpr std::array<T, Size> to_array(T const* value, std::index_sequence<Index...> const) noexcept
{
Expand All @@ -32,7 +38,7 @@ namespace winrt::impl
}

template <size_t Size>
constexpr auto to_array(char const(&value)[Size]) noexcept
constexpr auto to_array(char_type const(&value)[Size]) noexcept
{
return to_array<Size - 1>(value, std::make_index_sequence<Size - 1>());
}
Expand Down Expand Up @@ -427,7 +433,7 @@ namespace winrt::impl
}

template <size_t Size>
constexpr guid generate_guid(std::array<char, Size> const& value) noexcept
constexpr guid generate_guid(std::array<char_type, Size> const& value) noexcept
{
guid namespace_guid = { 0xd57af411, 0x737b, 0xc042,{ 0xab, 0xae, 0x87, 0x8b, 0x1e, 0x16, 0xad, 0xee } };

Expand All @@ -441,7 +447,7 @@ namespace winrt::impl
template <typename TArg, typename... TRest>
struct arg_collection
{
constexpr static auto data{ combine(to_array(signature<TArg>::data), ";", arg_collection<TRest...>::data) };
constexpr static auto data{ combine(to_array(signature<TArg>::data), u8";", arg_collection<TRest...>::data) };
};

template <typename TArg>
Expand All @@ -467,8 +473,8 @@ namespace winrt::impl
{
combine
(
to_array<wchar_t>(guid_of<T>()),
std::array<wchar_t, 1>{ L'\0' }
to_array<char_type>(guid_of<T>()),
std::array<char_type, 1>{ '\0' }
)
};

Expand All @@ -487,98 +493,48 @@ namespace winrt::impl
return 3;
}

constexpr size_t to_utf8(wchar_t const value, char* buffer) noexcept
{
if (value <= 0x7F)
{
*buffer = static_cast<char>(value);
return 1;
}

if (value <= 0x7FF)
{
*buffer = static_cast<char>(0xC0 | (value >> 6));
*(buffer + 1) = 0x80 | (value & 0x3F);
return 2;
}

*buffer = 0xE0 | (value >> 12);
*(buffer + 1) = 0x80 | ((value >> 6) & 0x3F);
*(buffer + 2) = 0x80 | (value & 0x3F);
return 3;
}

template <typename T>
constexpr size_t to_utf8_size() noexcept
{
auto input = to_array(name_v<T>);
size_t length = 0;

for (wchar_t const element : input)
{
length += to_utf8_size(element);
}

return length;
}

template <typename T>
constexpr auto to_utf8() noexcept
{
auto input = to_array(name_v<T>);
std::array<char, to_utf8_size<T>()> output{};
size_t offset{};

for (wchar_t const element : input)
{
offset += to_utf8(element, &output[offset]);
}

return output;
}

template <typename T>
constexpr guid generic_guid_v{};

template <typename T>
constexpr auto& basic_signature_v = "";

template <> inline constexpr auto& basic_signature_v<bool> = "b1";
template <> inline constexpr auto& basic_signature_v<int8_t> = "i1";
template <> inline constexpr auto& basic_signature_v<int16_t> = "i2";
template <> inline constexpr auto& basic_signature_v<int32_t> = "i4";
template <> inline constexpr auto& basic_signature_v<int64_t> = "i8";
template <> inline constexpr auto& basic_signature_v<uint8_t> = "u1";
template <> inline constexpr auto& basic_signature_v<uint16_t> = "u2";
template <> inline constexpr auto& basic_signature_v<uint32_t> = "u4";
template <> inline constexpr auto& basic_signature_v<uint64_t> = "u8";
template <> inline constexpr auto& basic_signature_v<float> = "f4";
template <> inline constexpr auto& basic_signature_v<double> = "f8";
template <> inline constexpr auto& basic_signature_v<char16_t> = "c2";
template <> inline constexpr auto& basic_signature_v<guid> = "g16";
template <> inline constexpr auto& basic_signature_v<hstring> = "string";
template <> inline constexpr auto& basic_signature_v<Windows::Foundation::IInspectable> = "cinterface(IInspectable)";

template <> inline constexpr auto& name_v<bool> = L"Boolean";
template <> inline constexpr auto& name_v<int8_t> = L"Int8";
template <> inline constexpr auto& name_v<int16_t> = L"Int16";
template <> inline constexpr auto& name_v<int32_t> = L"Int32";
template <> inline constexpr auto& name_v<int64_t> = L"Int64";
template <> inline constexpr auto& name_v<uint8_t> = L"UInt8";
template <> inline constexpr auto& name_v<uint16_t> = L"UInt16";
template <> inline constexpr auto& name_v<uint32_t> = L"UInt32";
template <> inline constexpr auto& name_v<uint64_t> = L"UInt64";
template <> inline constexpr auto& name_v<float> = L"Single";
template <> inline constexpr auto& name_v<double> = L"Double";
template <> inline constexpr auto& name_v<char16_t> = L"Char16";
template <> inline constexpr auto& name_v<guid> = L"Guid";
template <> inline constexpr auto& name_v<hstring> = L"String";
template <> inline constexpr auto& name_v<hresult> = L"Windows.Foundation.HResult";
template <> inline constexpr auto& name_v<event_token> = L"Windows.Foundation.EventRegistrationToken";
template <> inline constexpr auto& name_v<Windows::Foundation::IInspectable> = L"Object";
template <> inline constexpr auto& name_v<Windows::Foundation::TimeSpan> = L"Windows.Foundation.TimeSpan";
template <> inline constexpr auto& name_v<Windows::Foundation::DateTime> = L"Windows.Foundation.DateTime";
template <> inline constexpr auto& name_v<IAgileObject> = L"IAgileObject";
constexpr auto& basic_signature_v = u8"";

template <> inline constexpr auto& basic_signature_v<bool> = u8"b1";
template <> inline constexpr auto& basic_signature_v<int8_t> = u8"i1";
template <> inline constexpr auto& basic_signature_v<int16_t> = u8"i2";
template <> inline constexpr auto& basic_signature_v<int32_t> = u8"i4";
template <> inline constexpr auto& basic_signature_v<int64_t> = u8"i8";
template <> inline constexpr auto& basic_signature_v<uint8_t> = u8"u1";
template <> inline constexpr auto& basic_signature_v<uint16_t> = u8"u2";
template <> inline constexpr auto& basic_signature_v<uint32_t> = u8"u4";
template <> inline constexpr auto& basic_signature_v<uint64_t> = u8"u8";
template <> inline constexpr auto& basic_signature_v<float> = u8"f4";
template <> inline constexpr auto& basic_signature_v<double> = u8"f8";
template <> inline constexpr auto& basic_signature_v<char16_t> = u8"c2";
template <> inline constexpr auto& basic_signature_v<guid> = u8"g16";
template <> inline constexpr auto& basic_signature_v<hstring> = u8"string";
template <> inline constexpr auto& basic_signature_v<Windows::Foundation::IInspectable> = u8"cinterface(IInspectable)";

template <> inline constexpr auto& name_v<bool> = u8"Boolean";
template <> inline constexpr auto& name_v<int8_t> = u8"Int8";
template <> inline constexpr auto& name_v<int16_t> = u8"Int16";
template <> inline constexpr auto& name_v<int32_t> = u8"Int32";
template <> inline constexpr auto& name_v<int64_t> = u8"Int64";
template <> inline constexpr auto& name_v<uint8_t> = u8"UInt8";
template <> inline constexpr auto& name_v<uint16_t> = u8"UInt16";
template <> inline constexpr auto& name_v<uint32_t> = u8"UInt32";
template <> inline constexpr auto& name_v<uint64_t> = u8"UInt64";
template <> inline constexpr auto& name_v<float> = u8"Single";
template <> inline constexpr auto& name_v<double> = u8"Double";
template <> inline constexpr auto& name_v<char16_t> = u8"Char16";
template <> inline constexpr auto& name_v<guid> = u8"Guid";
template <> inline constexpr auto& name_v<hstring> = u8"String";
template <> inline constexpr auto& name_v<hresult> = u8"Windows.Foundation.HResult";
template <> inline constexpr auto& name_v<event_token> = u8"Windows.Foundation.EventRegistrationToken";
template <> inline constexpr auto& name_v<Windows::Foundation::IInspectable> = u8"Object";
template <> inline constexpr auto& name_v<Windows::Foundation::TimeSpan> = u8"Windows.Foundation.TimeSpan";
template <> inline constexpr auto& name_v<Windows::Foundation::DateTime> = u8"Windows.Foundation.DateTime";
template <> inline constexpr auto& name_v<IAgileObject> = u8"IAgileObject";

template <> struct category<bool> { using type = basic_category; };
template <> struct category<int8_t> { using type = basic_category; };
Expand Down Expand Up @@ -609,57 +565,36 @@ namespace winrt::impl
struct category_signature<enum_category, T>
{
using enum_type = std::underlying_type_t<T>;
constexpr static auto data{ combine("enum(", to_utf8<T>(), ";", signature<enum_type>::data, ")") };
constexpr static auto data{ combine(u8"enum(", name_v<T>, u8";", signature<enum_type>::data, u8")") };
};

template <typename... Fields, typename T>
struct category_signature<struct_category<Fields...>, T>
{
constexpr static auto data{ combine("struct(", to_utf8<T>(), ";", arg_collection<Fields...>::data, ")") };
constexpr static auto data{ combine(u8"struct(", name_v<T>, u8";", arg_collection<Fields...>::data, u8")") };
};

template <typename T>
struct category_signature<class_category, T>
{
constexpr static auto data{ combine("rc(", to_utf8<T>(), ";", signature<winrt::default_interface<T>>::data, ")") };
constexpr static auto data{ combine(u8"rc(", name_v<T>, u8";", signature<winrt::default_interface<T>>::data, u8")") };
};

template <typename... Args, typename T>
struct category_signature<generic_category<Args...>, T>
{
constexpr static auto data{ combine("pinterface(", to_array<char>(generic_guid_v<T>), ";", arg_collection<Args...>::data, ")") };
constexpr static auto data{ combine(u8"pinterface(", to_array<char_type>(generic_guid_v<T>), u8";", arg_collection<Args...>::data, u8")") };
};

template <typename T>
struct category_signature<interface_category, T>
{
constexpr static auto data{ to_array<char>(guid_of<T>()) };
constexpr static auto data{ to_array<char_type>(guid_of<T>()) };
};

template <typename T>
struct category_signature<delegate_category, T>
{
constexpr static auto data{ combine("delegate(", to_array<char>(guid_of<T>()), ")") };
constexpr static auto data{ combine(u8"delegate(", to_array<char_type>(guid_of<T>()), u8")") };
};

template <size_t Size>
constexpr std::wstring_view to_wstring_view(std::array<wchar_t, Size> const& value) noexcept
{
return { value.data(), Size - 1 };
}

template <size_t Size>
constexpr std::wstring_view to_wstring_view(wchar_t const (&value)[Size]) noexcept
{
return { value, Size - 1 };
}
}

WINRT_EXPORT namespace winrt
{
template <typename T>
constexpr auto name_of() noexcept
{
return impl::to_wstring_view(impl::name_v<T>);
}
}
2 changes: 1 addition & 1 deletion strings/base_implements.h
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ namespace winrt::impl
{
static hstring get()
{
return hstring{ name_of<I>() };
return name_of<I>();
}
};

Expand Down
24 changes: 24 additions & 0 deletions strings/base_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -638,3 +638,27 @@ WINRT_EXPORT namespace winrt
return result;
}
}

namespace winrt::impl
{
template <size_t Size>
hstring literal_to_hstring(std::array<char_type, Size> const& value) noexcept
{
return to_hstring(std::string_view(reinterpret_cast<char const*>(value.data()), Size - 1));
}

template <size_t Size>
hstring literal_to_hstring(char_type const (&value)[Size]) noexcept
{
return to_hstring(std::string_view(reinterpret_cast<char const*>(value), Size - 1));
}
}

WINRT_EXPORT namespace winrt
{
template <typename T>
hstring name_of() noexcept
{
return impl::literal_to_hstring(impl::name_v<T>);
}
}
3 changes: 3 additions & 0 deletions strings/base_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ WINRT_EXPORT namespace winrt
struct hstring;
struct clock;

template <typename T>
hstring name_of() noexcept;

struct hresult
{
int32_t value{};
Expand Down
4 changes: 2 additions & 2 deletions strings/base_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace winrt::impl

struct diagnostics_info
{
std::map<std::wstring_view, uint32_t> queries;
std::map<std::wstring_view, factory_diagnostics_info> factories;
std::map<hstring, uint32_t> queries;
std::map<hstring, factory_diagnostics_info> factories;
};

struct diagnostics_cache
Expand Down
Loading

0 comments on commit 08eca8b

Please sign in to comment.