libebml_ng
double.h
Go to the documentation of this file.
1 #ifndef EBML_NG_DOUBLE_H
2 #define EBML_NG_DOUBLE_H
3 
4 #include "libebml_ng/struct.h"
5 
6 namespace ebml {
7  FIXED_SIZE(float, 4)
8  FIXED_SIZE(double, 8)
9 
10  DEF_PACK_ALT(float) {
11  memcpy(dest, &value, sizeof(float));
12 
13  if (std::endian::native == std::endian::little) {
14  _reverse(dest, 0, sizeof(float) - 1);
15  }
16 
17  return sizeof(float);
18  }
19 
20  DEF_PACK_ALT(double) {
21  memcpy(dest, &value, sizeof(double));
22 
23  if (std::endian::native == std::endian::little) {
24  _reverse(dest, 0, sizeof(double) - 1);
25  }
26 
27  return sizeof(double);
28  }
29 
30  DEF_UNPACK(float) {
31  float ret = 0;
32 
33  if (size == 0) {return 0;}
34 
35  if (size != 4) {
36  throw std::invalid_argument("unpack<float>: invalid value for size");
37  }
38 
39  memcpy(&ret, src, sizeof(float));
40 
41  if (std::endian::native == std::endian::little) {
42  _reverse((char*)&ret, 0, sizeof(float) - 1);
43  }
44 
45  return ret;
46  }
47 
48  DEF_UNPACK(double) {
49  double ret = 0;
50 
51  if (size == 0) {return 0;}
52 
53  if (size == 4) {return unpack<float>(src, size);}
54 
55  if (size != 8) {
56  throw std::invalid_argument("unpack<double>: invalid value for size");
57  }
58 
59  memcpy(&ret, src, sizeof(double));
60 
61  if (std::endian::native == std::endian::little) {
62  _reverse((char*)&ret, 0, sizeof(double) - 1);
63  }
64 
65  return ret;
66  }
67 }
68 
69 #endif
DEF_UNPACK(float)
Definition: double.h:30
#define FIXED_SIZE(T, s)
Definition: struct.h:139
size_t size(const std::string &value)
Definition: binary.h:8
void _reverse(char *s, unsigned int j, unsigned int k)
Reverses the contents of a character array between indices [j, k].
Definition: struct.h:26
Definition: basictypes.h:40
DEF_PACK_ALT(float)
Definition: double.h:10