Belle II Software development
NSMVar.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/nsm/NSMVar.h"
9
10#include <string.h>
11#include <stdlib.h>
12
13#include <daq/slc/base/Reader.h>
14#include <daq/slc/base/Writer.h>
15
16using namespace Belle2;
17
18NSMVar::NSMVar(const std::string& name, const std::vector<int>& value)
19 : m_value(NULL)
20{
21 int* v = (int*)malloc(value.size() * sizeof(int));
22 for (size_t i = 0; i < value.size(); i++)
23 v[i] = value[i];
24 m_name = name;
25 m_type = INT;
26 m_len = value.size();
27 m_value = v;
28 m_id = 0;
29 m_date = Date().get();
30}
31
32NSMVar::NSMVar(const std::string& name, const std::vector<float>& value)
33 : m_value(NULL)
34{
35 float* v = (float*)malloc(value.size() * sizeof(float));
36 for (size_t i = 0; i < value.size(); i++)
37 v[i] = value[i];
38 m_name = name;
39 m_type = INT;
40 m_len = value.size();
41 m_value = v;
42 m_id = 0;
43 m_date = Date().get();
44}
45
46const NSMVar& NSMVar::operator=(const std::vector<int>& val)
47{
48 if (m_value) free(m_value);
49 int* v = (int*)malloc(val.size() * sizeof(int));
50 for (size_t i = 0; i < val.size(); i++)
51 v[i] = val[i];
52 m_value = v;
53 m_type = INT;
54 m_len = val.size();
55 m_date = Date().get();
56 return *this;
57}
58
59const NSMVar& NSMVar::operator=(const std::vector<float>& val)
60{
61 if (m_value) free(m_value);
62 float* v = (float*)malloc(val.size() * sizeof(float));
63 for (size_t i = 0; i < val.size(); i++)
64 v[i] = val[i];
65 m_value = v;
66 m_type = FLOAT;
67 m_len = val.size();
68 m_date = Date().get();
69 return *this;
70}
71
72int NSMVar::size() const
73{
74 int s = 1;
75 switch (m_type) {
76 case NONE: return 0;
77 case INT: s = 4; break;
78 case FLOAT: s = 4; break;
79 case TEXT: return m_len + 1;
80 default: break;
81 }
82 return (m_len > 0) ? s * m_len : s;
83}
84
85const char* NSMVar::getTypeLabel() const
86{
87 switch (m_type) {
88 case INT:
89 if (m_len > 0) return "inta";
90 else return "int";
91 case FLOAT:
92 if (m_len > 0) return "floata";
93 else return "float";
94 case TEXT: return "text";
95 default: break;
96 }
97 return "";
98}
99
100void NSMVar::copy(const std::string& name,
101 Type type, int len, const void* value,
102 int id, int date)
103{
104 if (m_value) free(m_value);
105 m_value = NULL;
106 m_name = name;
107 m_type = type;
108 m_len = len;
109 m_id = id;
110 m_date = (date > 0) ? date : Date().get();
111 int s = size();
112 if (s > 0) {
113 m_value = malloc(s);
114 memcpy(m_value, value, s);
115 } else {
116 m_type = NONE;
117 m_len = 0;
118 m_value = NULL;
119 }
120}
121
122NSMVar::~NSMVar()
123{
124 if (m_value) free(m_value);
125}
126
127void NSMVar::readObject(Reader& reader)
128{
129 m_node = reader.readString();
130 m_name = reader.readString();
131 m_type = (Type)reader.readInt();
132 m_len = reader.readInt();
133 m_id = reader.readInt();
134 m_date = reader.readInt();
135 int len = (m_len > 0) ? m_len : 1;
136 int s = size();
137 if (s > 0) {
138 if (m_value) free(m_value);
139 m_value = malloc(s);
140 memset(m_value, 0, s);
141 switch (m_type) {
142 case INT: {
143 int* v = (int*)m_value;
144 for (int i = 0; i < len; i++) {
145 v[i] = reader.readInt();
146 }
147 } break;
148 case FLOAT: {
149 float* v = (float*)m_value;
150 for (int i = 0; i < len; i++) {
151 v[i] = reader.readFloat();
152 }
153 } break;
154 case TEXT: {
155 char* v = (char*)m_value;
156 // Use m_len condition instead of len due to empty string problem
157 for (int i = 0; i < m_len; i++) {
158 v[i] = reader.readChar();
159 }
160 } break;
161 default: return;
162 }
163 }
164}
165
166void NSMVar::writeObject(Writer& writer) const
167{
168 writer.writeString(m_node);
169 writer.writeString(m_name);
170 writer.writeInt((int)m_type);
171 writer.writeInt(m_len);
172 writer.writeInt(m_id);
173 writer.writeInt(m_date);
174 int len = (m_len > 0) ? m_len : 1;
175 switch (m_type) {
176 case INT: {
177 int* v = (int*)m_value;
178 for (int i = 0; i < len; i++) {
179 writer.writeInt(v[i]);
180 }
181 } break;
182 case FLOAT: {
183 float* v = (float*)m_value;
184 for (int i = 0; i < len; i++) {
185 writer.writeFloat(v[i]);
186 }
187 } break;
188 case TEXT: {
189 char* v = (char*)m_value;
190 writer.write(v, len);
191 } break;
192 default: return;
193 }
194}
195
196int NSMVar::getInt() const
197{
198 return (m_type == INT&& m_len == 0) ? *(int*)m_value : 0;
199}
200
201float NSMVar::getFloat() const
202{
203 return (m_type == FLOAT && m_len == 0) ? *(float*)m_value : 0;
204}
205
206const char* NSMVar::getText() const
207{
208 return (m_type == TEXT && m_len > 0 && m_value != NULL) ? (const char*)m_value : "";
209}
210
211int NSMVar::getInt(int i) const
212{
213 return (m_type == INT&& i < m_len) ? ((int*)m_value)[i] : 0;
214}
215
216float NSMVar::getFloat(int i) const
217{
218 return (m_type == FLOAT && i < m_len) ? ((float*)m_value)[i] : 0;
219}
220
221const NSMVar& NSMVar::operator>>(std::vector<int>& val) const
222{
223 int* pv = (int*)get();
224 val = std::vector<int>();
225 for (int i = 0; i < getLength(); i++) {
226 val.push_back(pv[i]);
227 }
228 return *this;
229}
230
231const NSMVar& NSMVar::operator>>(std::vector<float>& val) const
232{
233 float* pv = (float*)get();
234 val = std::vector<float>();
235 for (int i = 0; i < getLength(); i++) {
236 val.push_back(pv[i]);
237 }
238 return *this;
239}
Abstract base class for different kinds of events.