mirror of
https://github.com/matrix-construct/construct
synced 2024-11-16 15:00:51 +01:00
ircd::util: Add an endian bswap; add more data()/size() participants.
This commit is contained in:
parent
eab178b97f
commit
f37311a3f1
1 changed files with 40 additions and 0 deletions
|
@ -404,6 +404,13 @@ size(const std::array<char, SIZE> &buf)
|
|||
return SIZE;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
constexpr typename std::enable_if<std::is_pod<T>::value, size_t>::type
|
||||
size(const T &val)
|
||||
{
|
||||
return sizeof(T);
|
||||
}
|
||||
|
||||
|
||||
template<size_t SIZE>
|
||||
constexpr const char *
|
||||
|
@ -419,6 +426,20 @@ data(char (&buf)[SIZE])
|
|||
return buf;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
constexpr typename std::enable_if<std::is_pod<T>::value, const uint8_t *>::type
|
||||
data(const T &val)
|
||||
{
|
||||
return reinterpret_cast<const uint8_t *>(&val);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
constexpr typename std::enable_if<std::is_pod<T>::value, uint8_t *>::type
|
||||
data(T &val)
|
||||
{
|
||||
return reinterpret_cast<uint8_t *>(&val);
|
||||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
auto
|
||||
|
@ -1044,5 +1065,24 @@ is_powerof2(const long long v)
|
|||
}
|
||||
|
||||
|
||||
template<class T>
|
||||
T &
|
||||
bswap(T *const &val)
|
||||
{
|
||||
assert(val != nullptr);
|
||||
std::reverse(data(*val), data(*val) + size(*val));
|
||||
return *val;
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T
|
||||
bswap(const T &val)
|
||||
{
|
||||
T ret;
|
||||
std::reverse_copy(data(val), data(val) + size(val), data(ret));
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
} // namespace util
|
||||
} // namespace ircd
|
||||
|
|
Loading…
Reference in a new issue