mirror of
https://github.com/matrix-construct/construct
synced 2024-10-01 05:08:59 +02:00
ircd::util: Add hton()/ntoh() in-place swap overloads.
This commit is contained in:
parent
d1966fffb5
commit
29775f0866
1 changed files with 25 additions and 7 deletions
|
@ -17,6 +17,8 @@ namespace ircd::util
|
||||||
template<class T> T bswap(T);
|
template<class T> T bswap(T);
|
||||||
|
|
||||||
// Host <-> network endian
|
// Host <-> network endian
|
||||||
|
template<class T> T &hton(T *const &);
|
||||||
|
template<class T> T &ntoh(T *const &);
|
||||||
template<class T> T hton(T);
|
template<class T> T hton(T);
|
||||||
template<class T> T ntoh(T);
|
template<class T> T ntoh(T);
|
||||||
|
|
||||||
|
@ -85,21 +87,37 @@ template<class T>
|
||||||
T
|
T
|
||||||
ircd::util::hton(T a)
|
ircd::util::hton(T a)
|
||||||
{
|
{
|
||||||
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
return hton<T>(&a);
|
||||||
return bswap(&a);
|
|
||||||
#else
|
|
||||||
return a;
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
T
|
T
|
||||||
ircd::util::ntoh(T a)
|
ircd::util::ntoh(T a)
|
||||||
|
{
|
||||||
|
return ntoh<T>(&a);
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
T &
|
||||||
|
ircd::util::hton(T *const &a)
|
||||||
{
|
{
|
||||||
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||||
return bswap(&a);
|
return bswap(a);
|
||||||
#else
|
#else
|
||||||
return a;
|
assert(a);
|
||||||
|
return *a;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
T &
|
||||||
|
ircd::util::ntoh(T *const &a)
|
||||||
|
{
|
||||||
|
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
||||||
|
return bswap(a);
|
||||||
|
#else
|
||||||
|
assert(a);
|
||||||
|
return *a;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue