mirror of
https://github.com/matrix-construct/construct
synced 2024-12-25 15:04:10 +01:00
ircd: Add template option to skip runtime conditional in byte_view conversion.
This commit is contained in:
parent
bd3d34adb2
commit
a9c37908ec
1 changed files with 22 additions and 10 deletions
|
@ -13,7 +13,10 @@
|
|||
|
||||
namespace ircd
|
||||
{
|
||||
template<class T = string_view> struct byte_view;
|
||||
template<class T = string_view,
|
||||
bool safety = true>
|
||||
struct byte_view;
|
||||
|
||||
template<> struct byte_view<string_view>;
|
||||
}
|
||||
|
||||
|
@ -39,19 +42,13 @@ struct ircd::byte_view<ircd::string_view>
|
|||
};
|
||||
|
||||
/// string_view -> bytes
|
||||
template<class T>
|
||||
template<class T,
|
||||
bool safety>
|
||||
struct ircd::byte_view
|
||||
{
|
||||
string_view s;
|
||||
|
||||
operator const T &() const
|
||||
{
|
||||
assert(sizeof(T) <= size(s));
|
||||
if(unlikely(sizeof(T) > s.size()))
|
||||
throw std::bad_cast();
|
||||
|
||||
return *reinterpret_cast<const T *>(s.data());
|
||||
}
|
||||
operator const T &() const;
|
||||
|
||||
byte_view(const string_view &s = {})
|
||||
:s{s}
|
||||
|
@ -62,3 +59,18 @@ struct ircd::byte_view
|
|||
:s{byte_view<string_view>{t}}
|
||||
{}
|
||||
};
|
||||
|
||||
template<class T,
|
||||
bool safety>
|
||||
inline ircd::byte_view<T, safety>::operator
|
||||
const T &()
|
||||
const
|
||||
{
|
||||
assert(sizeof(T) <= size(s));
|
||||
|
||||
if constexpr(safety)
|
||||
if(unlikely(sizeof(T) > s.size()))
|
||||
throw std::bad_cast();
|
||||
|
||||
return *reinterpret_cast<const T *>(s.data());
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue