CMS 3D CMS Logo

emulator_io.h
Go to the documentation of this file.
1 #ifndef FIRMWARE_utils_emulator_io_h
2 #define FIRMWARE_utils_emulator_io_h
3 
4 #include <fstream>
5 #include <vector>
7 
8 namespace l1ct {
9 
10  template <typename T>
11  inline bool writeVar(const T &src, std::fstream &to) {
12  to.write(reinterpret_cast<const char *>(&src), sizeof(T));
13  return to.good();
14  }
15 
16  template <typename T>
17  inline bool readVar(std::fstream &from, T &to) {
18  from.read(reinterpret_cast<char *>(&to), sizeof(T));
19  return from.good();
20  }
21 
22  template <typename T>
23  bool writeAP(const T &src, std::fstream &to) {
24  for (unsigned int i = 0, n = T::width; i < n; i += 32) {
25  ap_uint<32> word = src(std::min(i + 31, n - 1), i);
26  uint32_t w32 = word.to_uint();
27  if (!writeVar(w32, to))
28  return false;
29  }
30  return true;
31  }
32 
33  template <typename T>
34  bool readAP(std::fstream &from, T &to) {
35  uint32_t w32;
36  for (unsigned int i = 0, n = T::width; i < n; i += 32) {
37  if (!readVar(from, w32))
38  return false;
39  ap_uint<32> word = w32;
40  to(std::min(i + 31, n - 1), i) = word(std::min(31u, n - i - 1), 0);
41  }
42  return true;
43  }
44 
45  template <typename T>
46  bool writeObj(const T &obj, std::fstream &to) {
47  return writeAP(obj.pack(), to);
48  }
49 
50  template <typename T>
51  bool readObj(std::fstream &from, T &obj) {
52  ap_uint<T::BITWIDTH> packed;
53  if (!readAP(from, packed))
54  return false;
55  obj = T::unpack(packed);
56  return true;
57  }
58 
59  template <typename T>
60  bool writeMany(const std::vector<T> &objs, std::fstream &to) {
61  uint32_t number = objs.size();
62  writeVar(number, to);
63  for (uint32_t i = 0; i < number; ++i) {
64  objs[i].write(to);
65  }
66  return to.good();
67  }
68 
69  template <int NB>
70  bool writeMany(const std::vector<ap_uint<NB>> &objs, std::fstream &to) {
71  uint32_t number = objs.size();
72  writeVar(number, to);
73  for (uint32_t i = 0; i < number; ++i) {
74  writeAP(objs[i], to);
75  }
76  return to.good();
77  }
78 
79  template <typename T>
80  bool readMany(std::fstream &from, std::vector<T> &objs) {
81  uint32_t number = 0;
82  readVar(from, number);
83  objs.resize(number);
84  for (uint32_t i = 0; i < number; ++i)
85  objs[i].read(from);
86  return from.good();
87  }
88 
89  template <int NB>
90  bool readMany(std::fstream &from, std::vector<ap_uint<NB>> &objs) {
91  uint32_t number = 0;
92  readVar(from, number);
93  objs.resize(number);
94  for (uint32_t i = 0; i < number; ++i)
95  readAP(from, objs[i]);
96  return from.good();
97  }
98 
99 } // namespace l1ct
100 
101 #endif
bool writeObj(const T &obj, std::fstream &to)
Definition: emulator_io.h:46
bool readObj(std::fstream &from, T &obj)
Definition: emulator_io.h:51
bool readVar(std::fstream &from, T &to)
Definition: emulator_io.h:17
uint64_t word
std::pair< unsigned int, unsigned int > unpack(cond::Time_t since)
bool writeAP(const T &src, std::fstream &to)
Definition: emulator_io.h:23
bool readAP(std::fstream &from, T &to)
Definition: emulator_io.h:34
bool readMany(std::fstream &from, std::vector< T > &objs)
Definition: emulator_io.h:80
bool writeVar(const T &src, std::fstream &to)
Definition: emulator_io.h:11
bool writeMany(const std::vector< T > &objs, std::fstream &to)
Definition: emulator_io.h:60
long double T
Definition: datatypes.h:17