Skip to content

Instantly share code, notes, and snippets.

@jonwis
Created August 9, 2023 21:01
Show Gist options
  • Save jonwis/6a5fdfd6226529c0f16eb1c615d51553 to your computer and use it in GitHub Desktop.
Save jonwis/6a5fdfd6226529c0f16eb1c615d51553 to your computer and use it in GitHub Desktop.
More constexpr of hstring?
WINRT_EXPORT namespace winrt::param
{
struct hstring
{
#ifdef _MSC_VER
#pragma warning(suppress: 26495)
#endif
hstring() noexcept : m_handle(nullptr) {}
hstring(hstring const& values) = delete;
hstring& operator=(hstring const& values) = delete;
hstring(std::nullptr_t) = delete;
#ifdef _MSC_VER
#pragma warning(suppress: 26495)
#endif
hstring(winrt::hstring const& value) noexcept : m_handle(get_abi(value))
{
}
constexpr hstring(std::wstring const& value) noexcept : hstring(std::wstring_view(value))
{
}
constexpr hstring(std::wstring_view value) noexcept
{
WINRT_ASSERT(value.size() < UINT32_MAX);
if (value.size() == 0)
{
m_handle = nullptr;
}
else
{
m_header.length = static_cast<uint32_t>(value.size());
m_header.flags = impl::hstring_reference_flag;
m_header.ptr = value.data();
m_handle = &m_header;
}
}
template<uint32_t N> constexpr hstring(wchar_t const (&value)[N]) noexcept : hstring(std::wstring_view(value))
{
}
constexpr hstring(wchar_t const* const value) noexcept : hstring(std::wstring_view(value))
{
}
operator winrt::hstring const&() const noexcept
{
return *reinterpret_cast<winrt::hstring const*>(this);
}
private:
void* m_handle;
impl::hstring_header m_header;
};
inline void* get_abi(hstring const& object) noexcept
{
return *(void**)(&object);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment