15 memset(&s, 0,
sizeof(S));
22 throw std::logic_error(
"size of "+std::string(
typeid(S).name())+
" not a multiple of 2.");
30 std::transform(reinterpret_cast<uint16_t*>(&s),
31 reinterpret_cast<uint16_t*>(&s)+
sizeof(S)/2,
32 reinterpret_cast<uint16_t*>(&s),
39 std::transform(reinterpret_cast<uint16_t*>(&s),
40 reinterpret_cast<uint16_t*>(&s)+
sizeof(S)/2,
41 reinterpret_cast<uint16_t*>(&s),
62 void ntohcpy(
const std::vector<uint16_t> &vec, S &s)
64 if (
sizeof(S)!=vec.size()*2)
65 throw std::logic_error(
"ntohcpy: size of vector mismatch "+std::string(
typeid(S).name()));
67 std::transform(vec.begin(), vec.end(),
68 reinterpret_cast<uint16_t*
>(&s), ntohs);
75 throw std::logic_error(
"htoncpy: size of "+std::string(
typeid(S).name())+
" not a multiple of 2");
77 std::vector<uint16_t> v(
sizeof(S)/2);
79 std::transform(reinterpret_cast<const uint16_t*>(&s),
80 reinterpret_cast<const uint16_t*>(&s)+
sizeof(S)/2,
86 template<
typename T,
typename S>
87 void bitcpy(T *target,
size_t ntarget,
const S *source,
size_t nsource,
size_t ss=0,
size_t ts=0)
89 const size_t targetsize = ts==0 ?
sizeof(T)*8 : std::min(ts,
sizeof(T)*8);
90 const size_t sourcesize = ss==0 ?
sizeof(S)*8 : std::min(ss,
sizeof(S)*8);
92 const S *
const ends = source + nsource;
93 const T *
const endt = target + ntarget;
98 memset(t, 0,
sizeof(T)*ntarget);
100 size_t targetpos = 0;
101 size_t sourcepos = 0;
103 while (s<ends && t<endt)
106 *t |= (*s>>sourcepos)<<targetpos;
109 const int ncopy = std::min(sourcesize-sourcepos, targetsize-targetpos);
114 if (sourcepos>=sourcesize)
116 sourcepos %= sourcesize;
120 if (targetpos>=targetsize)
122 targetpos %= targetsize;
std::vector< uint16_t > htoncpy(const S &s)
void ntohcpy(const std::vector< uint16_t > &vec, S &s)
void bitcpy(T *target, size_t ntarget, const S *source, size_t nsource, size_t ss=0, size_t ts=0)