- Timestamp:
- 05/13/11 15:07:00 (14 years ago)
- Location:
- trunk/FACT++/src
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/HeadersFTM.h
r10691 r10702 2 2 #define FACT_HeadersFTM 3 3 4 #include <string.h> 5 #include <algorithm> 6 #include <arpa/inet.h> 7 #include <stdexcept> 8 #include <typeinfo> 9 #include <vector> 4 #include <ostream> 10 5 11 6 // For debugging 12 7 #include <iostream> 13 8 14 namespace Header 15 { 16 template<typename S> 17 void hton(S &s) 18 { 19 std::transform(reinterpret_cast<uint16_t*>(&s), 20 reinterpret_cast<uint16_t*>(&s)+sizeof(S)/2, 21 reinterpret_cast<uint16_t*>(&s), 22 htons); 23 } 24 25 template<typename S> 26 void ntoh(S &s) 27 { 28 std::transform(reinterpret_cast<uint16_t*>(&s), 29 reinterpret_cast<uint16_t*>(&s)+sizeof(S)/2, 30 reinterpret_cast<uint16_t*>(&s), 31 ntohs); 32 } 33 34 template<typename S> 35 S NtoH(const S &s) 36 { 37 S ret(s); 38 ntoh(ret); 39 return ret; 40 } 41 42 template<typename S> 43 S HtoN(const S &s) 44 { 45 S ret(s); 46 hton(ret); 47 return ret; 48 } 49 50 template<typename S> 51 void reset(S &s) 52 { 53 memset(&s, 0, sizeof(S)); 54 } 55 56 template<typename S> 57 void init(S &s) 58 { 59 if (sizeof(S)%2!=0) 60 throw std::logic_error("size of "+std::string(typeid(S).name())+" not a multiple of 2."); 61 62 reset(s); 63 } 64 65 template<typename S> 66 void ntohcpy(const std::vector<uint16_t> &vec, S &s) 67 { 68 if (sizeof(S)!=vec.size()*2) 69 throw std::logic_error("size of vector mismatch "+std::string(typeid(S).name())); 70 71 std::transform(vec.begin(), vec.end(), 72 reinterpret_cast<uint16_t*>(&s), ntohs); 73 } 74 75 template<typename S> 76 std::vector<uint16_t> htoncpy(const S &s) 77 { 78 if (sizeof(S)%2) 79 throw std::logic_error("size of "+std::string(typeid(S).name())+" not a multiple of 2"); 80 81 std::vector<uint16_t> v(sizeof(S)/2); 82 83 std::transform(reinterpret_cast<const uint16_t*>(&s), 84 reinterpret_cast<const uint16_t*>(&s)+sizeof(S)/2, 85 v.begin(), htons); 86 87 return v; 88 } 89 90 template<typename T, typename S> 91 void bitcpy(T *target, size_t ntarget, const S *source, size_t nsource, size_t ss=0, size_t ts=0) 92 { 93 const size_t targetsize = ss==0 ? sizeof(T) : std::min(ss, sizeof(T)); 94 const size_t sourcesize = ts==0 ? sizeof(S) : std::min(ts, sizeof(S)); 95 96 const S *const ends = source + nsource; 97 const T *const endt = target + ntarget; 98 99 const S *s = source; 100 T *t = target; 101 102 memset(t, 0, sizeof(T)*ntarget); 103 104 size_t targetpos = 0; 105 size_t sourcepos = 0; 106 107 while (s<ends && t<endt) 108 { 109 // Start filling with "source size" - "position" bits 110 *t |= (*s>>sourcepos)<<targetpos; 111 112 // If not all bits fitted into targetsize the number 113 // of copied bits is the number of bits which fitted 114 const int n = std::min(sourcesize-sourcepos, targetsize-targetpos); 115 116 targetpos += n; 117 sourcepos += n; 118 119 if (sourcepos>=sourcesize) 120 s++; 121 122 if (targetpos>=targetsize) 123 t++; 124 125 sourcepos %= sourcesize; 126 targetpos %= targetsize; 127 } 128 } 129 130 } 9 #include "ByteOrder.h" 131 10 132 11 // ==================================================================== 133 134 #include <ostream>135 12 136 13 namespace FTM 137 14 { 138 using namespace Header;139 140 15 enum States 141 16 {
Note:
See TracChangeset
for help on using the changeset viewer.