Belle II Software development
Reader.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8#include "daq/slc/base/Reader.h"
9
10#include "daq/slc/base/Serializable.h"
11
12#include <unistd.h>
13
14#define __ENDIAN_L__
15
16using namespace Belle2;
17
18void Reader::reverse(void* buf, size_t len)
19{
20 for (int c = 0 ; c < (int)len / 2 ; c ++) {
21 char tmp = *((char*)buf + c);
22 *((char*)buf + c) = *((char*)buf + len - 1 - c);
23 *((char*)buf + len - 1 - c) = tmp;
24 }
25}
26
27bool Reader::readBool()
28{
29 bool v;
30 read(&v, sizeof(bool));
31 return v;
32}
33
34char Reader::readChar()
35{
36 char v;
37 read(&v, sizeof(char));
38 return v;
39}
40
41unsigned char Reader::readUChar()
42{
43 unsigned char v;
44 read(&v, sizeof(unsigned char));
45 return v;
46}
47
48short Reader::readShort()
49{
50 short v;
51 read(&v, sizeof(short));
52#if defined(__ENDIAN_L__)
53 reverse(&v, sizeof(short));
54#endif
55 return v;
56}
57
58unsigned short Reader::readUShort()
59{
60 unsigned short v;
61 read(&v, sizeof(unsigned short));
62#if defined(__ENDIAN_L__)
63 reverse(&v, sizeof(unsigned short));
64#endif
65 return v;
66}
67
68int Reader::readInt()
69{
70 int v;
71 read(&v, sizeof(int));
72#if defined(__ENDIAN_L__)
73 reverse(&v, sizeof(int));
74#endif
75 return v;
76}
77
78unsigned int Reader::readUInt()
79{
80 unsigned int v;
81 read(&v, sizeof(unsigned int));
82#if defined(__ENDIAN_L__)
83 reverse(&v, sizeof(unsigned int));
84#endif
85 return v;
86}
87
88long long Reader::readLong()
89{
90 long long v;
91 read(&v, sizeof(long long));
92#if defined(__ENDIAN_L__)
93 reverse(&v, sizeof(long long));
94#endif
95 return v;
96}
97
98unsigned long long Reader::readULong()
99{
100 unsigned long long v;
101 read(&v, sizeof(unsigned long long));
102#if defined(__ENDIAN_L__)
103 reverse(&v, sizeof(unsigned long long));
104#endif
105 return v;
106}
107
108float Reader::readFloat()
109{
110 float v;
111 read(&v, sizeof(float));
112#if defined(__ENDIAN_L__)
113 reverse(&v, sizeof(float));
114#endif
115 return v;
116}
117
118double Reader::readDouble()
119{
120 double v;
121 read(&v, sizeof(double));
122#if defined(__ENDIAN_L__)
123 reverse(&v, sizeof(double));
124#endif
125 return v;
126}
127
128const std::string Reader::readString()
129{
130 int count = readInt();
131 char str[1025];
132 std::string v;
133 while (count > 0) {
134 int c = (count > 1024) ? 1024 : count;
135 read(str, c);
136 v.append(str, 0, c);
137 count -= c;
138 }
139 return v;
140}
141
142void Reader::readObject(Serializable& v)
143{
144 v.readObject(*this);
145}
Abstract base class for different kinds of events.