Skip to content

Commit

Permalink
use Boost.Endian library
Browse files Browse the repository at this point in the history
  • Loading branch information
jclehner committed Jun 29, 2023
1 parent aac72d6 commit d5bcbac
Showing 1 changed file with 21 additions and 24 deletions.
45 changes: 21 additions & 24 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

#ifndef BCM2UTILS_UTIL_H
#define BCM2UTILS_UTIL_H
#include <type_traits>
#include <boost/endian/conversion.hpp>
#include <system_error>
#include <type_traits>
#include <functional>
#include <stdexcept>
#include <typeinfo>
Expand Down Expand Up @@ -222,29 +223,25 @@ class mstimer

std::string transform(const std::string& str, std::function<int(int)> f);

template<class T> T be_to_h(T n);
template<class T> T le_to_h(T n);
template<class T> T h_to_be(T n);
template<class T> T h_to_le(T n);

#define BCM2UTILS_DEF_BSWAPPER(type, bits) \
template<> inline type be_to_h(type n) \
{ return be ## bits ## toh(n); } \
template<> inline type le_to_h(type n) \
{ return le ## bits ## toh(n); } \
template<> inline type h_to_be(type n) \
{ return htobe ## bits(n); } \
template<> inline type h_to_le(type n) \
{ return htole ## bits(n); }

BCM2UTILS_DEF_BSWAPPER(uint16_t, 16);
BCM2UTILS_DEF_BSWAPPER(int16_t, 16);
BCM2UTILS_DEF_BSWAPPER(uint32_t, 32);
BCM2UTILS_DEF_BSWAPPER(int32_t, 32);
BCM2UTILS_DEF_BSWAPPER(uint64_t, 64);
BCM2UTILS_DEF_BSWAPPER(int64_t, 64);

#undef BCM2UTILS_DEF_BSWAPPER
template<class T> inline T be_to_h(T n)
{
return boost::endian::big_to_native(n);
}

template<class T> inline T le_to_h(T n)
{
return boost::endian::little_to_native(n);
}

template<class T> inline T h_to_be(T n)
{
return boost::endian::native_to_big(n);
}

template<class T> inline T h_to_le(T n)
{
return boost::endian::native_to_little(n);
}

template<typename T> using sp = std::shared_ptr<T>;
template<typename T> using csp = std::shared_ptr<const T>;
Expand Down

0 comments on commit d5bcbac

Please sign in to comment.