Belle II Software development
NSMVar Class Reference
Inheritance diagram for NSMVar:
Serializable

Public Types

enum  Type {
  NONE = 0 ,
  INT ,
  FLOAT ,
  TEXT
}
 

Public Member Functions

 NSMVar (const std::string &name, Type type, int len, const void *value)
 
 NSMVar (const std::string &name, const std::string &value)
 
 NSMVar (const std::string &name, int value)
 
 NSMVar (const std::string &name, float value)
 
 NSMVar (const std::string &name, int len, const int *value)
 
 NSMVar (const std::string &name, int len, const float *value)
 
 NSMVar (const std::string &name, const std::vector< int > &value)
 
 NSMVar (const std::string &name, const std::vector< float > &value)
 
 NSMVar (const std::string &name)
 
 NSMVar (const NSMVar &var)
 
const NSMVar & operator= (const NSMVar &var)
 
const NSMVar & operator= (int val)
 
const NSMVar & operator= (float val)
 
const NSMVar & operator= (const std::string &val)
 
const NSMVar & operator= (const std::vector< int > &val)
 
const NSMVar & operator= (const std::vector< float > &val)
 
const NSMVar & operator>> (int &val) const
 
const NSMVar & operator>> (float &val) const
 
const NSMVar & operator>> (std::string &val) const
 
const NSMVar & operator>> (std::vector< int > &val) const
 
const NSMVar & operator>> (std::vector< float > &val) const
 
void setNode (const std::string &node)
 
void setName (const std::string &name)
 
const void * get () const
 
void * get ()
 
int size () const
 
const std::string & getNode () const
 
const std::string & getName () const
 
Type getType () const
 
const char * getTypeLabel () const
 
int getLength () const
 
int getInt () const
 
float getFloat () const
 
const char * getText () const
 
int getInt (int i) const
 
float getFloat (int i) const
 
int getId () const
 
void setId (int id)
 
void setDate (int date)
 
void setDate (const Date &date)
 
int getDate () const
 
void readObject (Reader &) override
 
void writeObject (Writer &) const override
 
void copy (const std::string &name, Type type, int len, const void *value, int id=0, int date=0)
 

Static Public Attributes

static const NSMVar NOVALUE
 

Private Attributes

void * m_value
 
std::string m_node
 
std::string m_name
 
Type m_type
 
int m_len
 
int m_id = 0
 
int m_date = 0
 

Detailed Description

Definition at line 23 of file NSMVar.h.

Member Enumeration Documentation

◆ Type

enum Type

Definition at line 26 of file NSMVar.h.

26 {
27 NONE = 0,
28 INT,
29 FLOAT,
30 TEXT,
31 };

Constructor & Destructor Documentation

◆ NSMVar() [1/11]

NSMVar ( )
inline

Definition at line 37 of file NSMVar.h.

37 : m_value(NULL), m_name(), m_type(NONE), m_len(0)
38 {
39 setDate(Date());
40 }

◆ NSMVar() [2/11]

NSMVar ( const std::string & name,
Type type,
int len,
const void * value )
inline

Definition at line 41 of file NSMVar.h.

42 : m_value(NULL)
43 {
44 copy(name, type, len, value);
45 }

◆ NSMVar() [3/11]

NSMVar ( const std::string & name,
const std::string & value )
inline

Definition at line 46 of file NSMVar.h.

47 : m_value(NULL)
48 {
49 copy(name, TEXT, value.size(), value.c_str());
50 }

◆ NSMVar() [4/11]

NSMVar ( const std::string & name,
int value )
inline

Definition at line 51 of file NSMVar.h.

52 : m_value(NULL)
53 {
54 copy(name, INT, 0, &value);
55 }

◆ NSMVar() [5/11]

NSMVar ( const std::string & name,
float value )
inline

Definition at line 56 of file NSMVar.h.

57 : m_value(NULL)
58 {
59 copy(name, FLOAT, 0, &value);
60 }

◆ NSMVar() [6/11]

NSMVar ( const std::string & name,
int len,
const int * value )
inline

Definition at line 61 of file NSMVar.h.

62 : m_value(NULL)
63 {
64 copy(name, INT, len, value);
65 }

◆ NSMVar() [7/11]

NSMVar ( const std::string & name,
int len,
const float * value )
inline

Definition at line 66 of file NSMVar.h.

67 : m_value(NULL)
68 {
69 copy(name, FLOAT, len, value);
70 }

◆ NSMVar() [8/11]

NSMVar ( const std::string & name,
const std::vector< int > & value )

Definition at line 18 of file NSMVar.cc.

19 : m_value(NULL)
20{
21 int* v = static_cast<int*>(malloc(value.size() * sizeof(int)));
22 if (v == NULL) return;
23 for (size_t i = 0; i < value.size(); i++)
24 v[i] = value[i];
25 m_name = name;
26 m_type = INT;
27 m_len = value.size();
28 m_value = v;
29 m_id = 0;
30 m_date = Date().get();
31}

◆ NSMVar() [9/11]

NSMVar ( const std::string & name,
const std::vector< float > & value )

Definition at line 33 of file NSMVar.cc.

34 : m_value(NULL)
35{
36 float* v = static_cast<float*>(malloc(value.size() * sizeof(float)));
37 if (v == NULL) return;
38 for (size_t i = 0; i < value.size(); i++)
39 v[i] = value[i];
40 m_name = name;
41 m_type = INT;
42 m_len = value.size();
43 m_value = v;
44 m_id = 0;
45 m_date = Date().get();
46}

◆ NSMVar() [10/11]

NSMVar ( const std::string & name)
inlineexplicit

Definition at line 73 of file NSMVar.h.

73: m_value(NULL), m_name(name), m_type(NONE), m_len(0) {}

◆ NSMVar() [11/11]

NSMVar ( const NSMVar & var)
inline

Definition at line 74 of file NSMVar.h.

74: m_value(NULL) { *this = var; }

◆ ~NSMVar()

~NSMVar ( )
override

Definition at line 126 of file NSMVar.cc.

127{
128 if (m_value) free(m_value);
129}

Member Function Documentation

◆ copy()

void copy ( const std::string & name,
Type type,
int len,
const void * value,
int id = 0,
int date = 0 )

Definition at line 104 of file NSMVar.cc.

107{
108 if (m_value) free(m_value);
109 m_value = NULL;
110 m_name = name;
111 m_type = type;
112 m_len = len;
113 m_id = id;
114 m_date = (date > 0) ? date : Date().get();
115 int s = size();
116 if (s > 0) {
117 m_value = malloc(s);
118 if (m_value != NULL) memcpy(m_value, value, s);
119 } else {
120 m_type = NONE;
121 m_len = 0;
122 m_value = NULL;
123 }
124}

◆ get() [1/2]

void * get ( )
inline

Definition at line 116 of file NSMVar.h.

116{ return m_value; }

◆ get() [2/2]

const void * get ( ) const
inline

Definition at line 115 of file NSMVar.h.

115{ return m_value; }

◆ getDate()

int getDate ( ) const
inline

Definition at line 132 of file NSMVar.h.

132{ return m_date; }

◆ getFloat() [1/2]

float getFloat ( ) const

Definition at line 206 of file NSMVar.cc.

207{
208 return (m_type == FLOAT && m_len == 0) ? *static_cast<float*>(m_value) : 0;
209}

◆ getFloat() [2/2]

float getFloat ( int i) const

Definition at line 221 of file NSMVar.cc.

222{
223 return (m_type == FLOAT && i < m_len) ? (static_cast<float*>(m_value))[i] : 0;
224}

◆ getId()

int getId ( ) const
inline

Definition at line 128 of file NSMVar.h.

128{ return m_id; }

◆ getInt() [1/2]

int getInt ( ) const

Definition at line 201 of file NSMVar.cc.

202{
203 return (m_type == INT&& m_len == 0) ? *static_cast<int*>(m_value) : 0;
204}

◆ getInt() [2/2]

int getInt ( int i) const

Definition at line 216 of file NSMVar.cc.

217{
218 return (m_type == INT&& i < m_len) ? (static_cast<int*>(m_value))[i] : 0;
219}

◆ getLength()

int getLength ( ) const
inline

Definition at line 122 of file NSMVar.h.

122{ return m_len; }

◆ getName()

const std::string & getName ( ) const
inline

Definition at line 119 of file NSMVar.h.

119{ return m_name; }

◆ getNode()

const std::string & getNode ( ) const
inline

Definition at line 118 of file NSMVar.h.

118{ return m_node; }

◆ getText()

const char * getText ( ) const

Definition at line 211 of file NSMVar.cc.

212{
213 return (m_type == TEXT && m_len > 0 && m_value != NULL) ? static_cast<const char*>(m_value) : "";
214}

◆ getType()

Type getType ( ) const
inline

Definition at line 120 of file NSMVar.h.

120{ return m_type; }

◆ getTypeLabel()

const char * getTypeLabel ( ) const

Definition at line 89 of file NSMVar.cc.

90{
91 switch (m_type) {
92 case INT:
93 if (m_len > 0) return "inta";
94 else return "int";
95 case FLOAT:
96 if (m_len > 0) return "floata";
97 else return "float";
98 case TEXT: return "text";
99 default: break;
100 }
101 return "";
102}

◆ operator=() [1/6]

const NSMVar & operator= ( const NSMVar & var)
inline

Definition at line 78 of file NSMVar.h.

79 {
80 copy(var.m_name, var.m_type, var.m_len, var.m_value, var.m_id, var.m_date);
81 m_node = var.m_node;
82 m_date = var.m_date;
83 return *this;
84 }

◆ operator=() [2/6]

const NSMVar & operator= ( const std::string & val)
inline

Definition at line 95 of file NSMVar.h.

96 {
97 if (val.size() > 0) {
98 copy(m_name, TEXT, val.size(), val.c_str(), m_id);
99 } else {
100 copy(m_name, TEXT, 1, "", m_id);
101 }
102 return *this;
103 }

◆ operator=() [3/6]

const NSMVar & operator= ( const std::vector< float > & val)

Definition at line 62 of file NSMVar.cc.

63{
64 if (m_value) free(m_value);
65 float* v = static_cast<float*>(malloc(val.size() * sizeof(float)));
66 if (v == NULL) return *this;
67 for (size_t i = 0; i < val.size(); i++)
68 v[i] = val[i];
69 m_value = v;
70 m_type = FLOAT;
71 m_len = val.size();
72 m_date = Date().get();
73 return *this;
74}

◆ operator=() [4/6]

const NSMVar & operator= ( const std::vector< int > & val)

Definition at line 48 of file NSMVar.cc.

49{
50 if (m_value) free(m_value);
51 int* v = static_cast<int*>(malloc(val.size() * sizeof(int)));
52 if (v == NULL) return *this;
53 for (size_t i = 0; i < val.size(); i++)
54 v[i] = val[i];
55 m_value = v;
56 m_type = INT;
57 m_len = val.size();
58 m_date = Date().get();
59 return *this;
60}

◆ operator=() [5/6]

const NSMVar & operator= ( float val)
inline

Definition at line 90 of file NSMVar.h.

91 {
92 copy(m_name, FLOAT, 0, &val, m_id);
93 return *this;
94 }

◆ operator=() [6/6]

const NSMVar & operator= ( int val)
inline

Definition at line 85 of file NSMVar.h.

86 {
87 copy(m_name, INT, 0, &val, m_id);
88 return *this;
89 }

◆ operator>>() [1/5]

const NSMVar & operator>> ( float & val) const
inline

Definition at line 107 of file NSMVar.h.

107{ val = getFloat(); return *this; }

◆ operator>>() [2/5]

const NSMVar & operator>> ( int & val) const
inline

Definition at line 106 of file NSMVar.h.

106{ val = getInt(); return *this; }

◆ operator>>() [3/5]

const NSMVar & operator>> ( std::string & val) const
inline

Definition at line 108 of file NSMVar.h.

108{ val = getText(); return *this; }

◆ operator>>() [4/5]

const NSMVar & operator>> ( std::vector< float > & val) const

Definition at line 236 of file NSMVar.cc.

237{
238 float* pv = static_cast<float*>(const_cast<void*>(get()));
239 val = std::vector<float>();
240 for (int i = 0; i < getLength(); i++) {
241 val.push_back(pv[i]);
242 }
243 return *this;
244}

◆ operator>>() [5/5]

const NSMVar & operator>> ( std::vector< int > & val) const

Definition at line 226 of file NSMVar.cc.

227{
228 int* pv = static_cast<int*>(const_cast<void*>(get()));
229 val = std::vector<int>();
230 for (int i = 0; i < getLength(); i++) {
231 val.push_back(pv[i]);
232 }
233 return *this;
234}

◆ readObject()

void readObject ( Reader & reader)
overridevirtual

Implements Serializable.

Definition at line 131 of file NSMVar.cc.

132{
133 m_node = reader.readString();
134 m_name = reader.readString();
135 m_type = (Type)reader.readInt();
136 m_len = reader.readInt();
137 m_id = reader.readInt();
138 m_date = reader.readInt();
139 int len = (m_len > 0) ? m_len : 1;
140 int s = size();
141 if (s > 0) {
142 if (m_value) free(m_value);
143 m_value = malloc(s);
144 if (m_value == NULL) return;
145 memset(m_value, 0, s);
146 switch (m_type) {
147 case INT: {
148 int* v = static_cast<int*>(m_value);
149 for (int i = 0; i < len; i++) {
150 v[i] = reader.readInt();
151 }
152 } break;
153 case FLOAT: {
154 float* v = static_cast<float*>(m_value);
155 for (int i = 0; i < len; i++) {
156 v[i] = reader.readFloat();
157 }
158 } break;
159 case TEXT: {
160 char* v = static_cast<char*>(m_value);
161 // Use m_len condition instead of len due to empty string problem
162 for (int i = 0; i < m_len; i++) {
163 v[i] = reader.readChar();
164 }
165 } break;
166 default: return;
167 }
168 }
169}

◆ setDate() [1/2]

void setDate ( const Date & date)
inline

Definition at line 131 of file NSMVar.h.

131{ m_date = date.get(); }

◆ setDate() [2/2]

void setDate ( int date)
inline

Definition at line 130 of file NSMVar.h.

130{ m_date = date; }

◆ setId()

void setId ( int id)
inline

Definition at line 129 of file NSMVar.h.

129{ m_id = id; }

◆ setName()

void setName ( const std::string & name)
inline

Definition at line 114 of file NSMVar.h.

114{ m_name = name; }

◆ setNode()

void setNode ( const std::string & node)
inline

Definition at line 113 of file NSMVar.h.

113{ m_node = node; }

◆ size()

int size ( ) const

Definition at line 76 of file NSMVar.cc.

77{
78 int s = 1;
79 switch (m_type) {
80 case NONE: return 0;
81 case INT: s = 4; break;
82 case FLOAT: s = 4; break;
83 case TEXT: return m_len + 1;
84 default: break;
85 }
86 return (m_len > 0) ? s * m_len : s;
87}

◆ writeObject()

void writeObject ( Writer & writer) const
overridevirtual

Implements Serializable.

Definition at line 171 of file NSMVar.cc.

172{
173 writer.writeString(m_node);
174 writer.writeString(m_name);
175 writer.writeInt((int)m_type);
176 writer.writeInt(m_len);
177 writer.writeInt(m_id);
178 writer.writeInt(m_date);
179 int len = (m_len > 0) ? m_len : 1;
180 switch (m_type) {
181 case INT: {
182 const int* v = static_cast<int*>(m_value);
183 for (int i = 0; i < len; i++) {
184 writer.writeInt(v[i]);
185 }
186 } break;
187 case FLOAT: {
188 const float* v = static_cast<float*>(m_value);
189 for (int i = 0; i < len; i++) {
190 writer.writeFloat(v[i]);
191 }
192 } break;
193 case TEXT: {
194 const char* v = static_cast<char*>(m_value);
195 writer.write(v, len);
196 } break;
197 default: return;
198 }
199}

Member Data Documentation

◆ m_date

int m_date = 0
private

Definition at line 149 of file NSMVar.h.

◆ m_id

int m_id = 0
private

Definition at line 148 of file NSMVar.h.

◆ m_len

int m_len
private

Definition at line 147 of file NSMVar.h.

◆ m_name

std::string m_name
private

Definition at line 145 of file NSMVar.h.

◆ m_node

std::string m_node
private

Definition at line 144 of file NSMVar.h.

◆ m_type

Type m_type
private

Definition at line 146 of file NSMVar.h.

◆ m_value

void* m_value
private

Definition at line 143 of file NSMVar.h.

◆ NOVALUE

const NSMVar NOVALUE
static

Definition at line 34 of file NSMVar.h.


The documentation for this class was generated from the following files: