comparison C++/Pack.h @ 267:bc9b5e7421a7

Pack: * Remove endian from TypeInfo * Don't use rvalue and perfect forwarding
author David Demelier <markand@malikania.fr>
date Tue, 14 Oct 2014 13:14:36 +0200
parents 41bdde9027c0
children b5d795389387
comparison
equal deleted inserted replaced
266:41bdde9027c0 267:bc9b5e7421a7
74 static inline T convert(T value, Endian endian) 74 static inline T convert(T value, Endian endian)
75 { 75 {
76 static_assert(TypeInfo<T>::supported, "unsupported type"); 76 static_assert(TypeInfo<T>::supported, "unsupported type");
77 77
78 if (endian != mode) 78 if (endian != mode)
79 return TypeInfo<T>::convert(value, endian); 79 return TypeInfo<T>::convert(value);
80 80
81 return value; 81 return value;
82 } 82 }
83 83
84 /** 84 /**
154 * @param endian the endian mode 154 * @param endian the endian mode
155 * @param args the arguments 155 * @param args the arguments
156 * @throw std::runtime_exception on error 156 * @throw std::runtime_exception on error
157 */ 157 */
158 template <typename... Args> 158 template <typename... Args>
159 static void read(const std::string &path, Endian endian, Args&&... args) 159 static void read(const std::string &path, Endian endian, Args&... args)
160 { 160 {
161 std::ifstream in; 161 std::ifstream in;
162 162
163 in.open(path, std::ios_base::binary); 163 in.open(path, std::ios_base::binary);
164 if (!in.is_open()) 164 if (!in.is_open())
171 template <> 171 template <>
172 struct Pack::TypeInfo<uint8_t> { 172 struct Pack::TypeInfo<uint8_t> {
173 static constexpr const bool supported = true; 173 static constexpr const bool supported = true;
174 static constexpr const size_t size = sizeof (uint8_t); 174 static constexpr const size_t size = sizeof (uint8_t);
175 175
176 static constexpr uint8_t convert(uint8_t v, Endian endian) 176 static constexpr uint8_t convert(uint8_t v)
177 { 177 {
178 return v; 178 return v;
179 } 179 }
180 }; 180 };
181 181
182 template <> 182 template <>
183 struct Pack::TypeInfo<uint16_t> { 183 struct Pack::TypeInfo<uint16_t> {
184 static constexpr const bool supported = true; 184 static constexpr const bool supported = true;
185 static constexpr const size_t size = sizeof (uint16_t); 185 static constexpr const size_t size = sizeof (uint16_t);
186 186
187 static constexpr uint16_t convert(uint16_t v, Endian endian) 187 static constexpr uint16_t convert(uint16_t v)
188 { 188 {
189 return (((v >> 8) & 0x00FFL) | ((v << 8) & 0xFF00L)); 189 return (((v >> 8) & 0x00FFL) | ((v << 8) & 0xFF00L));
190 } 190 }
191 }; 191 };
192 192
193 template <> 193 template <>
194 struct Pack::TypeInfo<uint32_t> { 194 struct Pack::TypeInfo<uint32_t> {
195 static constexpr const bool supported = true; 195 static constexpr const bool supported = true;
196 static constexpr const size_t size = sizeof (uint32_t); 196 static constexpr const size_t size = sizeof (uint32_t);
197 197
198 static constexpr uint32_t convert(uint32_t v, Endian endian) 198 static constexpr uint32_t convert(uint32_t v)
199 { 199 {
200 return ((((v) >> 24) & 0x000000FFL) 200 return ((((v) >> 24) & 0x000000FFL)
201 | (((v) >> 8) & 0x0000FF00L) 201 | (((v) >> 8) & 0x0000FF00L)
202 | (((v) << 8) & 0x00FF0000L) 202 | (((v) << 8) & 0x00FF0000L)
203 | (((v) << 24) & 0xFF000000L)); 203 | (((v) << 24) & 0xFF000000L));
207 template <> 207 template <>
208 struct Pack::TypeInfo<uint64_t> { 208 struct Pack::TypeInfo<uint64_t> {
209 static constexpr const bool supported = true; 209 static constexpr const bool supported = true;
210 static constexpr const size_t size = sizeof (uint64_t); 210 static constexpr const size_t size = sizeof (uint64_t);
211 211
212 static constexpr uint64_t convert(uint64_t v, Endian endian) 212 static constexpr uint64_t convert(uint64_t v)
213 { 213 {
214 return ((((v) & 0xff00000000000000ull) >> 56) 214 return ((((v) & 0xff00000000000000ull) >> 56)
215 | (((v) & 0x00ff000000000000ull) >> 40) 215 | (((v) & 0x00ff000000000000ull) >> 40)
216 | (((v) & 0x0000ff0000000000ull) >> 24) 216 | (((v) & 0x0000ff0000000000ull) >> 24)
217 | (((v) & 0x000000ff00000000ull) >> 8 ) 217 | (((v) & 0x000000ff00000000ull) >> 8 )