casacore
serializable.h
Go to the documentation of this file.
1 #ifndef DYSCO_SERIALIZABLE_H
2 #define DYSCO_SERIALIZABLE_H
3 
4 #include <complex>
5 #include <iostream>
6 
7 #include <stdint.h>
8 
9 class Serializable {
10  public:
11  virtual ~Serializable() {}
12  virtual void Serialize(std::ostream &stream) const = 0;
13  virtual void Unserialize(std::istream &stream) = 0;
14 
15  template <typename T>
16  static void SerializeToUInt64(std::ostream &stream, T value) {
17  uint64_t val64t = value;
18  stream.write(reinterpret_cast<char *>(&val64t), sizeof(val64t));
19  }
20 
21  template <typename T>
22  static void SerializeToUInt32(std::ostream &stream, T value) {
23  uint32_t val32t = value;
24  stream.write(reinterpret_cast<char *>(&val32t), sizeof(val32t));
25  }
26 
27  template <typename T>
28  static void SerializeToUInt16(std::ostream &stream, T value) {
29  uint16_t val16t = value;
30  stream.write(reinterpret_cast<char *>(&val16t), sizeof(val16t));
31  }
32 
33  template <typename T>
34  static void SerializeToUInt8(std::ostream &stream, T value) {
35  uint8_t val8t = value;
36  stream.write(reinterpret_cast<char *>(&val8t), sizeof(val8t));
37  }
38 
39  static void SerializeToBool8(std::ostream &stream, bool value) {
40  uint8_t val8t = value;
41  stream.write(reinterpret_cast<char *>(&val8t), sizeof(val8t));
42  }
43 
44  static void SerializeToFloat(std::ostream &stream, float value) {
45  stream.write(reinterpret_cast<char *>(&value), sizeof(value));
46  }
47 
48  static void SerializeToDouble(std::ostream &stream, double value) {
49  stream.write(reinterpret_cast<char *>(&value), sizeof(value));
50  }
51 
52  static void SerializeToLDouble(std::ostream &stream, long double value) {
53  stream.write(reinterpret_cast<char *>(&value), sizeof(value));
54  }
55 
56  static void SerializeToLDoubleC(std::ostream &stream,
57  std::complex<long double> value) {
58  stream.write(reinterpret_cast<char *>(&value), sizeof(value));
59  }
60 
61  static void SerializeToString(std::ostream &stream, const std::string &str) {
62  SerializeToUInt64(stream, str.size());
63  stream.write(str.c_str(), str.size());
64  }
65 
66  static void SerializeTo32bString(std::ostream &stream,
67  const std::string &str) {
68  SerializeToUInt32(stream, str.size());
69  stream.write(str.c_str(), str.size());
70  }
71 
72  static uint64_t UnserializeUInt64(std::istream &stream) {
73  return Unserialize<uint64_t>(stream);
74  }
75 
76  static uint32_t UnserializeUInt32(std::istream &stream) {
77  return Unserialize<uint32_t>(stream);
78  }
79 
80  static uint16_t UnserializeUInt16(std::istream &stream) {
81  return Unserialize<uint16_t>(stream);
82  }
83 
84  static uint8_t UnserializeUInt8(std::istream &stream) {
85  return Unserialize<uint8_t>(stream);
86  }
87 
88  static bool UnserializeBool8(std::istream &stream) {
89  return (bool)Unserialize<uint8_t>(stream);
90  }
91 
92  static double UnserializeFloat(std::istream &stream) {
93  return Unserialize<float>(stream);
94  }
95 
96  static double UnserializeDouble(std::istream &stream) {
97  return Unserialize<double>(stream);
98  }
99 
100  static long double UnserializeLDouble(std::istream &stream) {
101  return Unserialize<long double>(stream);
102  }
103 
104  static std::complex<long double> UnserializeLDoubleC(std::istream &stream) {
105  return Unserialize<std::complex<long double>>(stream);
106  }
107 
108  static void UnserializeString(std::istream &stream, std::string &destStr) {
109  size_t size = UnserializeUInt64(stream);
110  char *str = new char[size];
111  stream.read(str, size);
112  destStr = std::string(str, size);
113  delete[] str;
114  }
115 
116  static void Unserialize32bString(std::istream &stream, std::string &destStr) {
117  size_t size = UnserializeUInt32(stream);
118  char *str = new char[size];
119  stream.read(str, size);
120  destStr = std::string(str, size);
121  delete[] str;
122  }
123 
124  private:
125  template <typename T>
126  static T Unserialize(std::istream &stream) {
127  T val;
128  stream.read(reinterpret_cast<char *>(&val), sizeof(val));
129  return val;
130  }
131 };
132 
133 #endif
virtual void Unserialize(std::istream &stream)=0
static T Unserialize(std::istream &stream)
Definition: serializable.h:126
static uint16_t UnserializeUInt16(std::istream &stream)
Definition: serializable.h:80
static void SerializeToUInt16(std::ostream &stream, T value)
Definition: serializable.h:28
static void SerializeToFloat(std::ostream &stream, float value)
Definition: serializable.h:44
static void SerializeToDouble(std::ostream &stream, double value)
Definition: serializable.h:48
static long double UnserializeLDouble(std::istream &stream)
Definition: serializable.h:100
virtual void Serialize(std::ostream &stream) const =0
static void SerializeToLDouble(std::ostream &stream, long double value)
Definition: serializable.h:52
static uint32_t UnserializeUInt32(std::istream &stream)
Definition: serializable.h:76
static void SerializeToUInt8(std::ostream &stream, T value)
Definition: serializable.h:34
static void SerializeTo32bString(std::ostream &stream, const std::string &str)
Definition: serializable.h:66
static void SerializeToUInt64(std::ostream &stream, T value)
Definition: serializable.h:16
static void UnserializeString(std::istream &stream, std::string &destStr)
Definition: serializable.h:108
static void Unserialize32bString(std::istream &stream, std::string &destStr)
Definition: serializable.h:116
static uint64_t UnserializeUInt64(std::istream &stream)
Definition: serializable.h:72
static double UnserializeDouble(std::istream &stream)
Definition: serializable.h:96
static double UnserializeFloat(std::istream &stream)
Definition: serializable.h:92
static void SerializeToBool8(std::ostream &stream, bool value)
Definition: serializable.h:39
static void SerializeToString(std::ostream &stream, const std::string &str)
Definition: serializable.h:61
static std::complex< long double > UnserializeLDoubleC(std::istream &stream)
Definition: serializable.h:104
virtual ~Serializable()
Definition: serializable.h:11
static uint8_t UnserializeUInt8(std::istream &stream)
Definition: serializable.h:84
static bool UnserializeBool8(std::istream &stream)
Definition: serializable.h:88
static void SerializeToUInt32(std::ostream &stream, T value)
Definition: serializable.h:22
static void SerializeToLDoubleC(std::ostream &stream, std::complex< long double > value)
Definition: serializable.h:56
LatticeExprNode value(const LatticeExprNode &expr)
This function returns the value of the expression without a mask.