8#include "daq/slc/nsm/NSMVar.h"
13#include <daq/slc/base/Reader.h>
14#include <daq/slc/base/Writer.h>
18NSMVar::NSMVar(
const std::string& name,
const std::vector<int>& value)
21 int* v = (
int*)malloc(value.size() *
sizeof(int));
22 for (
size_t i = 0; i < value.size(); i++)
29 m_date =
Date().get();
32NSMVar::NSMVar(
const std::string& name,
const std::vector<float>& value)
35 float* v = (
float*)malloc(value.size() *
sizeof(float));
36 for (
size_t i = 0; i < value.size(); i++)
43 m_date =
Date().get();
46const NSMVar& NSMVar::operator=(
const std::vector<int>& val)
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++)
55 m_date =
Date().get();
59const NSMVar& NSMVar::operator=(
const std::vector<float>& val)
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++)
68 m_date =
Date().get();
72int NSMVar::size()
const
77 case INT: s = 4;
break;
78 case FLOAT: s = 4;
break;
79 case TEXT:
return m_len + 1;
82 return (m_len > 0) ? s * m_len : s;
85const char* NSMVar::getTypeLabel()
const
89 if (m_len > 0)
return "inta";
92 if (m_len > 0)
return "floata";
94 case TEXT:
return "text";
100void NSMVar::copy(
const std::string& name,
101 Type type,
int len,
const void* value,
104 if (m_value) free(m_value);
110 m_date = (date > 0) ? date :
Date().get();
114 memcpy(m_value, value, s);
124 if (m_value) free(m_value);
127void NSMVar::readObject(
Reader& reader)
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;
138 if (m_value) free(m_value);
140 memset(m_value, 0, s);
143 int* v = (
int*)m_value;
144 for (
int i = 0; i < len; i++) {
145 v[i] = reader.readInt();
149 float* v = (
float*)m_value;
150 for (
int i = 0; i < len; i++) {
151 v[i] = reader.readFloat();
155 char* v = (
char*)m_value;
157 for (
int i = 0; i < m_len; i++) {
158 v[i] = reader.readChar();
166void NSMVar::writeObject(
Writer& writer)
const
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;
177 int* v = (
int*)m_value;
178 for (
int i = 0; i < len; i++) {
179 writer.writeInt(v[i]);
183 float* v = (
float*)m_value;
184 for (
int i = 0; i < len; i++) {
185 writer.writeFloat(v[i]);
189 char* v = (
char*)m_value;
190 writer.write(v, len);
196int NSMVar::getInt()
const
198 return (m_type == INT&& m_len == 0) ? *(
int*)m_value : 0;
201float NSMVar::getFloat()
const
203 return (m_type == FLOAT && m_len == 0) ? *(
float*)m_value : 0;
206const char* NSMVar::getText()
const
208 return (m_type == TEXT && m_len > 0 && m_value != NULL) ? (
const char*)m_value :
"";
211int NSMVar::getInt(
int i)
const
213 return (m_type == INT&& i < m_len) ? ((
int*)m_value)[i] : 0;
216float NSMVar::getFloat(
int i)
const
218 return (m_type == FLOAT && i < m_len) ? ((
float*)m_value)[i] : 0;
221const NSMVar& NSMVar::operator>>(std::vector<int>& val)
const
223 int* pv = (
int*)get();
224 val = std::vector<int>();
225 for (
int i = 0; i < getLength(); i++) {
226 val.push_back(pv[i]);
231const NSMVar& NSMVar::operator>>(std::vector<float>& val)
const
233 float* pv = (
float*)get();
234 val = std::vector<float>();
235 for (
int i = 0; i < getLength(); i++) {
236 val.push_back(pv[i]);
Abstract base class for different kinds of events.