Belle II Software  release-08-01-10
Writer.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/Writer.h"
9 
10 #include "daq/slc/base/Serializable.h"
11 
12 #include <unistd.h>
13 
14 #define __ENDIAN_L__
15 
16 using namespace Belle2;
17 
18 void Writer::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 
27 void Writer::writeBool(bool v)
28 {
29  write(&v, sizeof(bool));
30 }
31 
32 void Writer::writeChar(char v)
33 {
34  write(&v, sizeof(char));
35 }
36 
37 void Writer::writeUChar(unsigned char v)
38 {
39  write(&v, sizeof(unsigned char));
40 }
41 
42 void Writer::writeShort(short v)
43 {
44 #if defined(__ENDIAN_L__)
45  reverse(&v, sizeof(short));
46 #endif
47  write(&v, sizeof(short));
48 }
49 
50 void Writer::writeUShort(unsigned short v)
51 {
52 #if defined(__ENDIAN_L__)
53  reverse(&v, sizeof(unsigned short));
54 #endif
55  write(&v, sizeof(unsigned short));
56 }
57 
58 void Writer::writeInt(int v)
59 {
60 #if defined(__ENDIAN_L__)
61  reverse(&v, sizeof(int));
62 #endif
63  write(&v, sizeof(int));
64 }
65 
66 void Writer::writeUInt(unsigned int v)
67 {
68 #if defined(__ENDIAN_L__)
69  reverse(&v, sizeof(unsigned int));
70 #endif
71  write(&v, sizeof(unsigned int));
72 }
73 
74 void Writer::writeLong(long long v)
75 {
76 #if defined(__ENDIAN_L__)
77  reverse(&v, sizeof(long long));
78 #endif
79  write(&v, sizeof(long long));
80 }
81 
82 void Writer::writeULong(unsigned long long v)
83 {
84 #if defined(__ENDIAN_L__)
85  reverse(&v, sizeof(unsigned long long));
86 #endif
87  write(&v, sizeof(unsigned long long));
88 }
89 
90 void Writer::writeFloat(float v)
91 {
92 #if defined(__ENDIAN_L__)
93  reverse(&v, sizeof(float));
94 #endif
95  write(&v, sizeof(float));
96 }
97 
98 void Writer::writeDouble(double v)
99 {
100 #if defined(__ENDIAN_L__)
101  reverse(&v, sizeof(double));
102 #endif
103  write(&v, sizeof(double));
104 }
105 
106 void Writer::writeString(const std::string& v)
107 {
108  writeInt((int)v.size());
109  write(v.data(), v.size());
110 }
111 
112 void Writer::writeObject(const Serializable& v)
113 {
114  return v.writeObject(*this);
115 }
Abstract base class for different kinds of events.