Belle II Software development
NSMData Class Reference
Inheritance diagram for NSMData:
AbstractDBObject Serializable

Classes

struct  NameValue
 

Public Types

typedef std::vector< NameValueNameValueList
 

Public Member Functions

 NSMData (const std::string &dataname, const std::string &format, int revision)
 
 NSMData (const NSMData &data)
 
const NSMDataoperator= (const NSMData &data)
 Operator =.
 
int getRevision () const
 
const std::string & getFormat () const
 
int getSize () const
 
void setRevision (int revision)
 
void setFormat (const std::string &format)
 
void setSize (int size)
 
bool isAvailable ()
 
void * open (NSMCommunicator &comm)
 
void * allocate (NSMCommunicator &comm, int interval=2)
 
void flush ()
 
void * get ()
 
const void * get () const
 
void * parse (const char *inc_dir=NULL, bool allocated=false)
 
int getNObjects (const std::string &name) const
 
const NSMDataList & getObjects (const std::string &name) const
 
NSMDataList & getObjects (const std::string &name)
 
const NSMDatagetObject (const std::string &name, int index=0) const
 
NSMDatagetObject (const std::string &name, int index=0)
 
void print (const std::string &name_in="") const
 
void printPV (const std::string &name_in="") const
 
const void * find (const std::string &name_in, DBField::Type &type, int &length) const
 
void search (NameValueList &map, const std::string &name="") const
 
void * getValue (const std::string &name)
 
const void * getValue (const std::string &name) const override
 
void setValue (const std::string &name, const void *value, int size) override
 
void addValue (const std::string &name, const void *value, DBField::Type type, int length=0) override
 
const std::string & getText (const std::string &) const override
 
void addText (const std::string &, const std::string &) override
 
void readObject (Reader &reader) override
 
void writeObject (Writer &writer) const override
 
const std::string & getPath () const
 
void setPath (const std::string &path)
 
int getId () const
 
void setId (int id)
 
const std::string & getName () const
 
void setName (const std::string &name)
 
int getIndex () const
 
void setIndex (int index)
 
DBField::NameList & getFieldNames ()
 
const DBField::NameList & getFieldNames () const
 
DBField::Property getProperty (const std::string &name) const
 
bool hasField (const std::string &name) const
 
bool hasValue (const std::string &name) const
 
bool hasText (const std::string &name) const
 
bool hasObject (const std::string &name) const
 
void add (const std::string &name, DBField::Property pro)
 
void setValue (const std::string &name, const std::string &value)
 
void setText (const std::string &name, const std::string &value)
 
bool getBool (const std::string &name, int index=0) const
 
char getChar (const std::string &name, int index=0) const
 
short getShort (const std::string &name, int index=0) const
 
int getInt (const std::string &name, int index=0) const
 
long long getLong (const std::string &name, int index=0) const
 
float getFloat (const std::string &name, int index=0) const
 
double getDouble (const std::string &name, int index=0) const
 
void addBool (const std::string &name, bool value)
 
void addChar (const std::string &name, char value)
 
void addShort (const std::string &name, short value)
 
void addInt (const std::string &name, int value)
 
void addLong (const std::string &name, long long value)
 
void addFloat (const std::string &name, float value)
 
void addDouble (const std::string &name, double value)
 
void setBool (const std::string &name, bool value, int index=0)
 
void setChar (const std::string &name, int value, int index=0)
 
void setShort (const std::string &name, int value, int index=0)
 
void setInt (const std::string &name, int value, int index=0)
 
void setLong (const std::string &name, long long value, int index=0)
 
void setFloat (const std::string &name, float value, int index=0)
 
void setDouble (const std::string &name, double value, int index=0)
 
const std::string getValueText (const std::string &name) const
 
virtual void setValueText (const std::string &name, const std::string &value)
 

Protected Member Functions

void reset () override
 

Private Types

typedef std::vector< NSMDataNSMDataList
 
typedef std::map< std::string, NSMDataList > NSMDataListMap
 

Private Member Functions

 NSMData (void *pdata, const NSMData &data)
 
 NSMData (void *pdata, const std::string &dataname, const std::string &format, int revision)
 
void set (void *pdata)
 
int initProperties ()
 
template<typename T >
getD (const std::string &name, int index=0) const
 

Private Attributes

bool m_allocated
 
void * m_pdata
 
int m_size
 
int m_offset
 
std::string m_format
 
int m_revision
 
NSMDataListMap m_data_v_m
 
std::string m_empty
 
NSMCommunicatorm_com
 
int m_tstamp
 
int m_index
 
std::string m_path
 
int m_id
 
std::string m_name
 
DBField::NameList m_name_v
 
DBField::PropertyList m_pro_m
 

Detailed Description

Definition at line 31 of file NSMData.h.

Member Typedef Documentation

◆ NameValueList

typedef std::vector<NameValue> NameValueList

Definition at line 43 of file NSMData.h.

◆ NSMDataList

typedef std::vector<NSMData> NSMDataList
private

Definition at line 33 of file NSMData.h.

◆ NSMDataListMap

typedef std::map<std::string, NSMDataList> NSMDataListMap
private

Definition at line 34 of file NSMData.h.

Constructor & Destructor Documentation

◆ NSMData() [1/3]

NSMData ( const std::string &  dataname,
const std::string &  format,
int  revision 
)

Definition at line 33 of file NSMData.cc.

35 : AbstractDBObject(), m_allocated(false),
36 m_pdata(NULL), m_size(0), m_offset(0)
37{
38 setName(dataname);
39 setFormat(format);
40 setRevision(revision);
41}

◆ NSMData() [2/3]

NSMData ( )

Definition at line 43 of file NSMData.cc.

44 : AbstractDBObject(), m_allocated(false),
45 m_pdata(NULL), m_size(0), m_offset(0)
46{
47 setRevision(-1);
48}

◆ NSMData() [3/3]

NSMData ( const NSMData data)

Definition at line 50 of file NSMData.cc.

51 : AbstractDBObject(data)
52{
53 m_allocated = data.m_allocated;
54 m_size = data.m_size;
55 setFormat(data.getFormat());
56 setRevision(data.getRevision());
57 setIndex(data.getIndex());
58 setName(data.getName());
59 m_pdata = NULL;
60 if (m_allocated) {
61 m_pdata = malloc(m_size);
62 memcpy(m_pdata, data.m_pdata, m_size);
63 } else {
64 if (data.m_pdata != NULL) m_pdata = data.m_pdata;
65 }
66 const DBField::NameList& name_v(data.getFieldNames());
67 for (DBField::NameList::const_iterator it = name_v.begin();
68 it != name_v.end(); ++it) {
69 const std::string& name(*it);
70 const DBField::Property& pro(data.getProperty(name));
71 add(name, pro);
72 if (pro.getType() == DBField::OBJECT) {
73 NSMDataList data_v(data.getObjects(name));
74 for (size_t i = 0; i < data_v.size(); i++) {
75 NSMData& cdata(data_v[i]);
76 cdata.m_pdata = (void*)((char*)m_pdata +
77 pro.getOffset() + cdata.m_size * i);
78 }
79 m_data_v_m.insert(NSMDataListMap::value_type(name, data_v));
80 }
81 }
82}

◆ ~NSMData()

~NSMData ( )
virtual

Definition at line 84 of file NSMData.cc.

85{
86 reset();
87 if (m_allocated && m_pdata != NULL) {
88 free(m_pdata);
89 m_pdata = NULL;
90 }
91}

Member Function Documentation

◆ add()

void add ( const std::string &  name,
DBField::Property  pro 
)
inherited

Definition at line 88 of file AbstractDBObject.cc.

89{
90 if (!hasField(name)) {
91 m_name_v.push_back(name);
92 m_pro_m.insert(DBField::PropertyList::value_type(name, pro));
93 }
94}

◆ addText()

void addText ( const std::string &  ,
const std::string &   
)
inlineoverridevirtual

Implements AbstractDBObject.

Definition at line 95 of file NSMData.h.

95{}

◆ addValue()

void addValue ( const std::string &  name,
const void *  value,
DBField::Type  type,
int  length = 0 
)
overridevirtual

Implements AbstractDBObject.

Definition at line 118 of file NSMData.cc.

120{
121 DBField::Property pro(type, length, m_offset);
122 if (length == 0) length = 1;
123 int size = pro.getTypeSize() * length;
124 if (size <= 0) return;
125 if (!hasField(name)) {
126 add(name, pro);
127 m_offset += size;
128 }
129 if (data != NULL) {
130 memcpy((char*)m_pdata + pro.getOffset(), data, size);
131 }
132}

◆ allocate()

void * allocate ( NSMCommunicator comm,
int  interval = 2 
)

Definition at line 164 of file NSMData.cc.

165{
166 if (m_pdata == NULL) {
167 b2nsm_context(com.getContext());
168 if (getenv("NSM2_INCDIR") == NULL) {
169 ConfigFile file("slowcontrol");
170 setenv("NSM2_INCDIR", file.get("nsm.data.incpath").c_str(), 0);
171 }
172 if ((m_pdata = b2nsm_allocmem(getName().c_str(), getFormat().c_str(),
173 getRevision(), interval)) == NULL) {
174 throw (NSMHandlerException("Failed to allocate data memory %s",
175 nsmlib_strerror(com.getContext())));
176 }
177 parse();
178 memset(m_pdata, 0, m_size);
179 m_com = &com;
180 m_tstamp = 0;
181 /*
182 com.getCallback().add(new NSMVHandlerText("nsmdata.name", true, false, getName()));
183 com.getCallback().add(new NSMVHandlerText("nsmdata.format", true, false, getFormat()));
184 com.getCallback().add(new NSMVHandlerInt("nsmdata.revision", true, false, getRevision()));
185 com.getCallback().add(new NSMVHandlerInt("nsmdata.tstamp", true, false, m_tstamp));
186 */
187 }
188 return m_pdata;
189}

◆ find()

const void * find ( const std::string &  name_in,
DBField::Type &  type,
int &  length 
) const

Definition at line 518 of file NSMData.cc.

519{
520 size_t pos;
521 std::string name_out = name_in;
522 if ((pos = name_in.find(".")) != std::string::npos) {
523 StringList str = StringUtil::split(name_out, '.');
524 StringList sstr = StringUtil::split(str[0], '[');
525 const NSMDataList& data_v(getObjects(sstr[0]));
526 int index = 0;
527 if (sstr.size() > 1) {
528 index = atoi(sstr[1].c_str());
529 }
530 name_out = name_in.substr(pos + 1);
531 return data_v[index].find(name_out, type, length);
532 }
533 int index = 0;
534 if ((pos = name_out.find("[")) != std::string::npos) {
535 StringList str = StringUtil::split(name_out, '[');
536 name_out = str[0];
537 index = atoi(str[1].c_str());
538 }
539 if (!hasValue(name_out)) return NULL;
540 const DBField::Property& pro(getProperty(name_out));
541 length = pro.getLength();
542 type = pro.getType();
543 const void* buf = getValue(name_out);
544 switch (type) {
545 case DBField::CHAR: return (((const char*)buf) + index);
546 case DBField::SHORT: return (((const int16*)buf) + index);
547 case DBField::INT: return (((const int32*)buf) + index);
548 case DBField::LONG: return (((const int64*)buf) + index);
549 case DBField::FLOAT: return (((const float*)buf) + index);
550 case DBField::DOUBLE: return (((const double*)buf) + index);
551 default: return NULL;
552 }
553 return NULL;
554}

◆ flush()

void flush ( )

Definition at line 222 of file NSMData.cc.

223{
224#if NSM_PACKAGE_VERSION >= 1914
225 m_tstamp = Time().getSecond();
226 sleep(1);
227 m_com->getCallback().set("nsmdata.tstamp", m_tstamp);
228#endif
229}

◆ get() [1/2]

void * get ( )
inline

Definition at line 73 of file NSMData.h.

73{ return m_pdata; }

◆ get() [2/2]

const void * get ( ) const
inline

Definition at line 74 of file NSMData.h.

74{ return m_pdata; }

◆ getD()

T getD ( const std::string &  name,
int  index = 0 
) const
inlineprivateinherited

Definition at line 103 of file AbstractDBObject.h.

104 {
105 const void* value = getValue(name);
106 if (value == NULL/* || index >= getProperty(name).getLength()*/) {
107 throw (std::out_of_range(name + " not found"));
108 }
109 DBField::Type type(getProperty(name).getType());
110 if (type == DBField::INT) return (T)(((int*)value)[index]);
111 if (type == DBField::FLOAT) return (T)(((float*)value)[index]);
112 if (type == DBField::DOUBLE) return (T)(((double*)value)[index]);
113 if (type == DBField::SHORT) return (T)(((short*)value)[index]);
114 if (type == DBField::CHAR) return (T)(((char*)value)[index]);
115 if (type == DBField::LONG) return (T)(((long*)value)[index]);
116 return ((T*)value)[index];
117 }

◆ getFieldNames() [1/2]

DBField::NameList & getFieldNames ( )
inlineinherited

Definition at line 46 of file AbstractDBObject.h.

46{ return m_name_v; }

◆ getFieldNames() [2/2]

const DBField::NameList & getFieldNames ( ) const
inlineinherited

Definition at line 47 of file AbstractDBObject.h.

47{ return m_name_v; }

◆ getFormat()

const std::string & getFormat ( ) const
inline

Definition at line 64 of file NSMData.h.

64{ return m_format; }

◆ getId()

int getId ( ) const
inlineinherited

Definition at line 40 of file AbstractDBObject.h.

40{ return m_id; }

◆ getIndex()

int getIndex ( ) const
inlineinherited

Definition at line 44 of file AbstractDBObject.h.

44{ return m_index; }

◆ getName()

const std::string & getName ( ) const
inlineinherited

Definition at line 42 of file AbstractDBObject.h.

42{ return m_name; }

◆ getNObjects()

int getNObjects ( const std::string &  name) const

Definition at line 556 of file NSMData.cc.

557{
558 NSMDataListMap::const_iterator it = m_data_v_m.find(name);
559 if (it != m_data_v_m.end()) return it->second.size();
560 return 0;
561}

◆ getObject() [1/2]

NSMData & getObject ( const std::string &  name,
int  index = 0 
)

Definition at line 570 of file NSMData.cc.

571{
572 NSMDataListMap::iterator it = m_data_v_m.find(name);
573 if (it != m_data_v_m.end()) return it->second[index];
574 else throw (std::out_of_range(StringUtil::form("%s:%d", __FILE__, __LINE__)));
575}

◆ getObject() [2/2]

const NSMData & getObject ( const std::string &  name,
int  index = 0 
) const

Definition at line 563 of file NSMData.cc.

564{
565 NSMDataListMap::const_iterator it = m_data_v_m.find(name);
566 if (it != m_data_v_m.end()) return it->second[index];
567 else throw (std::out_of_range(StringUtil::form("%s:%d", __FILE__, __LINE__)));
568}

◆ getObjects() [1/2]

NSMData::NSMDataList & getObjects ( const std::string &  name)

Definition at line 584 of file NSMData.cc.

585{
586 NSMDataListMap::iterator it = m_data_v_m.find(name);
587 if (it != m_data_v_m.end()) return it->second;
588 else throw (std::out_of_range(StringUtil::form("%s:%d", __FILE__, __LINE__)));
589}

◆ getObjects() [2/2]

const NSMData::NSMDataList & getObjects ( const std::string &  name) const

Definition at line 577 of file NSMData.cc.

578{
579 NSMDataListMap::const_iterator it = m_data_v_m.find(name);
580 if (it != m_data_v_m.end()) return it->second;
581 else throw (std::out_of_range(StringUtil::form("%s:%d", __FILE__, __LINE__)));
582}

◆ getPath()

const std::string & getPath ( ) const
inlineinherited

Definition at line 38 of file AbstractDBObject.h.

38{ return m_path; }

◆ getProperty()

DBField::Property getProperty ( const std::string &  name) const
inherited

Definition at line 54 of file AbstractDBObject.cc.

55{
56 DBField::PropertyList::const_iterator it = m_pro_m.find(name);
57 if (it != m_pro_m.end()) return it->second;
58 return DBField::Property();
59}

◆ getRevision()

int getRevision ( ) const
inline

Definition at line 63 of file NSMData.h.

63{ return m_revision; }

◆ getSize()

int getSize ( ) const
inline

Definition at line 65 of file NSMData.h.

65{ return m_size; }

◆ getText()

const std::string & getText ( const std::string &  ) const
inlineoverridevirtual

Implements AbstractDBObject.

Definition at line 94 of file NSMData.h.

94{ return m_empty; }

◆ getValue() [1/2]

void * getValue ( const std::string &  name)

Definition at line 283 of file NSMData.cc.

284{
285 if (!hasValue(name)) return NULL;
286 char* data = (char*)get();
287 return (data + getProperty(name).getOffset());
288}

◆ getValue() [2/2]

const void * getValue ( const std::string &  name) const
overridevirtual

Implements AbstractDBObject.

Definition at line 290 of file NSMData.cc.

292{
293 if (!hasValue(name)) return NULL;
294 char* data = (char*)get();
295 return (data + getProperty(name).getOffset());
296}

◆ getValueText()

const std::string getValueText ( const std::string &  name) const
inherited

Definition at line 96 of file AbstractDBObject.cc.

98{
99 const static int double_precision = std::numeric_limits<double>::digits10 + 2;
100
101 if (hasField(name)) {
102 switch (getProperty(name).getType()) {
103 case DBField::BOOL: return getBool(name) ? "true" : "false";
104 case DBField::CHAR: return StringUtil::form("%d", (int)getChar(name));
105 case DBField::SHORT: return StringUtil::form("%d", (int)getShort(name));
106 case DBField::INT: return StringUtil::form("%d", (int)getInt(name));
107 case DBField::LONG: return StringUtil::form("%ld", getLong(name));
108 case DBField::FLOAT: return StringUtil::form("%f", getFloat(name));
109 case DBField::DOUBLE: return StringUtil::form("%.*e", double_precision, getDouble(name));
110 case DBField::TEXT: return getText(name);
111 default: break;
112 }
113 }
114 throw (std::out_of_range(name + " not found"));
115}

◆ hasField()

bool hasField ( const std::string &  name) const
inherited

Definition at line 61 of file AbstractDBObject.cc.

62{
63 DBField::PropertyList::const_iterator it = m_pro_m.find(name);
64 return (it != m_pro_m.end());
65}

◆ hasObject()

bool hasObject ( const std::string &  name) const
inherited

Definition at line 81 of file AbstractDBObject.cc.

82{
83 DBField::PropertyList::const_iterator it = m_pro_m.find(name);
84 return hasField(name) &&
85 it->second.getType() == DBField::OBJECT;
86}

◆ hasText()

bool hasText ( const std::string &  name) const
inherited

Definition at line 75 of file AbstractDBObject.cc.

76{
77 DBField::PropertyList::const_iterator it = m_pro_m.find(name);
78 return hasField(name) && it->second.getType() == DBField::TEXT;
79}

◆ hasValue()

bool hasValue ( const std::string &  name) const
inherited

Definition at line 67 of file AbstractDBObject.cc.

68{
69 DBField::PropertyList::const_iterator it = m_pro_m.find(name);
70 return hasField(name) &&
71 it->second.getType() != DBField::TEXT &&
72 it->second.getType() != DBField::OBJECT;
73}

◆ isAvailable()

bool isAvailable ( )
inline

Definition at line 69 of file NSMData.h.

69{ return (m_pdata != NULL); }

◆ open()

void * open ( NSMCommunicator comm)

Definition at line 145 of file NSMData.cc.

146{
147 if (m_pdata == NULL) {
148 b2nsm_context(com.getContext());
149 if (getenv("NSM2_INCDIR") == NULL) {
150 ConfigFile file("slowcontrol");
151 setenv("NSM2_INCDIR", file.get("nsm.data.incpath").c_str(), 0);
152 }
153 if ((m_pdata = b2nsm_openmem(getName().c_str(), getFormat().c_str(),
154 getRevision())) == NULL) {
155 throw (NSMHandlerException("Failed to open data memory %s (%s %s %d)",
156 nsmlib_strerror(com.getContext()), getName().c_str(), getFormat().c_str(),
157 getRevision()));
158 }
159 parse();
160 }
161 return m_pdata;
162}

◆ operator=()

const NSMData & operator= ( const NSMData data)

Operator =.

Definition at line 93 of file NSMData.cc.

94{
96 m_allocated = data.m_allocated;
97 m_pdata = data.m_pdata;
98 m_size = data.m_size;
99 m_offset = data.m_offset;
100 m_format = data.m_format;
101 m_revision = data.m_revision;
102 m_data_v_m = data.m_data_v_m;
103 m_empty = data.m_empty;
104 m_com = data.m_com;
105 m_tstamp = data.m_tstamp;
106 return *this;
107}
const AbstractDBObject & operator=(const AbstractDBObject &object)
Operator =.

◆ parse()

void * parse ( const char *  inc_dir = NULL,
bool  allocated = false 
)

Definition at line 191 of file NSMData.cc.

192{
193#if NSM_PACKAGE_VERSION >= 1914
194 if (getenv("NSM2_INCDIR") == NULL) {
195 ConfigFile file("slowcontrol");
196 setenv("NSM2_INCDIR", file.get("nsm.data.incpath").c_str(), 0);
197 }
198 NSMparse* ptr = NULL;
199 char fmtstr[256];
200 int revision = 0;
201 if ((ptr = (NSMparse*)nsmlib_parsefile(getFormat().c_str(), getRevision(),
202 incpath, fmtstr, &revision)) == NULL) {
203 throw (NSMHandlerException("Failed to parse header file (%s:%d) : %s",
204 getFormat().c_str(), getRevision(), nsmlib_parseerr(NULL)));
205 }
206 if (getRevision() != revision && revision > 0) {
207 setRevision(revision);
208 }
209 int length;
210 std::string name_in;
211 parse(ptr, length, name_in);
212 if (m_size > 0 && malloc_new) {
213 m_allocated = true;
214 return (m_pdata = malloc(m_size));
215 }
216#else
217 throw (NSMHandlerException("too old nsmlib (nsmparse) : version = %d", NSM_PACKAGE_VERSION));
218#endif
219 return NULL;
220}

◆ print()

void print ( const std::string &  name_in = "") const

Definition at line 384 of file NSMData.cc.

385{
386 NameValueList map;
387 search(map, name_in);
388 size_t length = 0;
389 for (NameValueList::iterator it = map.begin();
390 it != map.end(); ++it) {
391 if (it->name.size() > length) length = it->name.size();
392 }
393 for (NameValueList::iterator it = map.begin();
394 it != map.end(); ++it) {
395 printf(StringUtil::form("%%-%ds : %%s\n", length).c_str(),
396 it->name.c_str(), it->value.c_str());
397 }
398}

◆ printPV()

void printPV ( const std::string &  name_in = "") const

Definition at line 464 of file NSMData.cc.

465{
466 const DBField::NameList& name_v(getFieldNames());
467 for (DBField::NameList::const_iterator it = name_v.begin();
468 it != name_v.end(); ++it) {
469 const std::string& name(*it);
470 const DBField::Property& pro(getProperty(name));
471 size_t length = pro.getLength();
472 std::string name_out = name_in;
473 if (name_in.size() > 0) name_out += ":";
474 else {
475 name_out += (getName() + ":") + (getFormat() + ":");
476 }
477 name_out += name;
478 std::string pvtype;
479 switch (pro.getType()) {
480 case DBField::CHAR:
481 case DBField::SHORT:
482 case DBField::INT:
483 case DBField::LONG: pvtype = "longin"; break;
484 case DBField::FLOAT:
485 case DBField::DOUBLE: pvtype = "ai"; break;
486 default : break;
487 }
488 if (length > 0) {
489 if (pro.getType() == DBField::OBJECT) {
490 const NSMDataList& data_v(getObjects(name));
491 for (size_t i = 0; i < length; i++) {
492 data_v[i].printPV(StringUtil::form("%s[%d]", name_out.c_str(), i));
493 }
494 } else {
495 for (size_t i = 0; i < length; i++) {
496 printf("record(%s, \"B2_nsm:%s[%d]\")\n"
497 "{\n"
498 " field(SCAN, \".1 second\")\n"
499 " field(DTYP, \"nsm2_data_%s\")\n"
500 "}\n\n", pvtype.c_str(), name_out.c_str(), (int)i, pvtype.c_str());
501 }
502 }
503 } else {
504 if (pro.getType() == DBField::OBJECT) {
505 const NSMDataList& data_v(getObjects(name));
506 data_v[0].print(name_out);
507 } else {
508 printf("record(%s, \"B2_nsm2:%s\")\n"
509 "{\n"
510 " field(SCAN, \".1 second\")\n"
511 " field(DTYP, \"nsm2_data_%s\")\n"
512 "}\n\n", pvtype.c_str(), name_out.c_str(), pvtype.c_str());
513 }
514 }
515 }
516}

◆ readObject()

void readObject ( Reader reader)
overridevirtual

Implements Serializable.

Definition at line 298 of file NSMData.cc.

299{
300 setName(reader.readString());
301 setFormat(reader.readString());
302 setRevision(reader.readInt());
303 int size = reader.readInt();
304 if (m_size > 0 && m_size < size) {
305 m_allocated = true;
306 free(m_pdata);
307 m_pdata = malloc(size);
308 }
309 setSize(size);
310 int npars = reader.readInt();
311 for (int n = 0; n < npars; n++) {
312 std::string name = reader.readString();
313 DBField::Type type = (DBField::Type)reader.readInt();
314 size_t length = reader.readInt();
315 size_t offset = reader.readInt();
316 if (!hasValue(name)) add(name, DBField::Property(type, length, offset));
317 void* buf = getValue(name);
318 if (length == 0) length = 1;
319 if (type == DBField::OBJECT) {
320 NSMDataList data_v;
321 for (size_t i = 0; i < length; i++) {
322 NSMData data;
323 data.m_pdata = (char*)getValue(name) + length * i;
324 reader.readObject(data);
325 data_v.push_back(data);
326 }
327 m_data_v_m.insert(NSMDataListMap::value_type(name, data_v));
328 } else {
329 for (size_t i = 0; i < length; i++) {
330 switch (type) {
331 case DBField::CHAR: ((char*)buf)[i] = reader.readChar(); break;
332 case DBField::SHORT: ((int16*)buf)[i] = reader.readShort(); break;
333 case DBField::INT: ((int32*)buf)[i] = reader.readInt(); break;
334 case DBField::LONG: ((int64*)buf)[i] = reader.readLong(); break;
335 case DBField::FLOAT: ((float*)buf)[i] = reader.readFloat(); break;
336 case DBField::DOUBLE: ((double*)buf)[i] = reader.readDouble(); break;
337 default: break;
338 }
339 }
340 }
341 }
342}

◆ reset()

void reset ( )
overrideprotectedvirtual

Reimplemented from AbstractDBObject.

Definition at line 109 of file NSMData.cc.

110{
111 AbstractDBObject::reset();
112 if (m_allocated && m_pdata != NULL) {
113 free(m_pdata);
114 m_pdata = NULL;
115 }
116}

◆ search()

void search ( NSMData::NameValueList &  map,
const std::string &  name = "" 
) const

Definition at line 400 of file NSMData.cc.

402{
403 const DBField::NameList& name_v(getFieldNames());
404 for (DBField::NameList::const_iterator it = name_v.begin();
405 it != name_v.end(); ++it) {
406 const std::string& name(*it);
407 const DBField::Property& pro(getProperty(name));
408 size_t length = pro.getLength();
409 std::string name_out = name_in;
410 if (name_in.size() > 0) name_out += ".";
411 name_out += name;
412 if (length > 0) {
413 if (pro.getType() == DBField::OBJECT) {
414 const NSMDataList& data_v(getObjects(name));
415 for (size_t i = 0; i < length; i++) {
416 data_v[i].search(map, StringUtil::form("%s[%d]", name_out.c_str(), i));
417 }
418 } else {
419 const void* buf = getValue(name);
420 if (pro.getType() == DBField::CHAR) {
421 NameValue nv;
422 nv.name = name_out;
423 nv.value = (char*)buf;
424 map.push_back(nv);
425 } else {
426 const char* name_c = name_out.c_str();
427 for (int i = 0; i < (int)length; i++) {
428 std::string vname = StringUtil::form("%s[%d]", name_c, i);
429 std::string val;
430 switch (pro.getType()) {
431 case DBField::SHORT:
432 val = StringUtil::form("%d", ((int16*)buf)[i]); break;
433 case DBField::INT:
434 val = StringUtil::form("%d", ((int32*)buf)[i]); break;
435 case DBField::LONG:
436 val = StringUtil::form("%ld", ((int64*)buf)[i]); break;
437 case DBField::FLOAT:
438 val = StringUtil::form("%f", ((float*)buf)[i]); break;
439 case DBField::DOUBLE:
440 val = StringUtil::form("%d", ((double*)buf)[i]); break;
441 default : break;
442 }
443 NameValue nv;
444 nv.name = vname;
445 nv.value = val;
446 map.push_back(nv);
447 }
448 }
449 }
450 } else {
451 if (pro.getType() == DBField::OBJECT) {
452 const NSMDataList& data_v(getObjects(name));
453 data_v[0].search(map, name_out);
454 } else {
455 NameValue nv;
456 nv.name = name_out;
457 nv.value = getValueText(name);
458 map.push_back(nv);
459 }
460 }
461 }
462}

◆ set()

void set ( void *  pdata)
inlineprivate

Definition at line 105 of file NSMData.h.

105{ m_pdata = pdata; }

◆ setFormat()

void setFormat ( const std::string &  format)
inline

Definition at line 67 of file NSMData.h.

67{ m_format = format; }

◆ setId()

void setId ( int  id)
inlineinherited

Definition at line 41 of file AbstractDBObject.h.

41{ m_id = id; }

◆ setIndex()

void setIndex ( int  index)
inlineinherited

Definition at line 45 of file AbstractDBObject.h.

45{ m_index = index; }

◆ setName()

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

Definition at line 43 of file AbstractDBObject.h.

43{ m_name = name; }

◆ setPath()

void setPath ( const std::string &  path)
inlineinherited

Definition at line 39 of file AbstractDBObject.h.

39{ m_path = path; }

◆ setRevision()

void setRevision ( int  revision)
inline

Definition at line 66 of file NSMData.h.

66{ m_revision = revision; }

◆ setSize()

void setSize ( int  size)
inline

Definition at line 68 of file NSMData.h.

68{ m_size = size; }

◆ setText()

void setText ( const std::string &  name,
const std::string &  value 
)
inlineinherited

Definition at line 55 of file AbstractDBObject.h.

55{ addText(name, value); }

◆ setValue()

void setValue ( const std::string &  name,
const void *  value,
int  size 
)
overridevirtual

Implements AbstractDBObject.

Definition at line 134 of file NSMData.cc.

136{
137 const DBField::Property& pro(getProperty(name));
138 if (length == 0) length = 1;
139 int size = pro.getTypeSize() * length;
140 if (data != NULL && hasField(name) && size > 0) {
141 memcpy((char*)m_pdata + pro.getOffset(), data, size);
142 }
143}

◆ setValueText()

void setValueText ( const std::string &  name,
const std::string &  value 
)
virtualinherited

Definition at line 117 of file AbstractDBObject.cc.

119{
120 if (hasField(name)) {
121 switch (getProperty(name).getType()) {
122 case DBField::BOOL: setBool(name, value == "true" || value == "t"); break;
123 case DBField::CHAR: {
124 if (StringUtil::find(value, "0x"))
125 setChar(name, (char)strtol(value.c_str(), NULL, 0));
126 else
127 setChar(name, (char)atoi(value.c_str()));
128 } break;
129 case DBField::SHORT: {
130 if (StringUtil::find(value, "0x"))
131 setShort(name, (short)strtol(value.c_str(), NULL, 0));
132 else
133 setShort(name, (short)atoi(value.c_str()));
134 } break;
135 case DBField::INT: {
136 if (StringUtil::find(value, "0x"))
137 setInt(name, (int)strtol(value.c_str(), NULL, 0));
138 else
139 setInt(name, (int)atoi(value.c_str()));
140 } break;
141 case DBField::LONG: setLong(name, (long long)atoi(value.c_str())); break;
142 case DBField::FLOAT: setFloat(name, atof(value.c_str())); break;
143 case DBField::DOUBLE: setDouble(name, atof(value.c_str())); break;
144 case DBField::TEXT: setText(name, value); break;
145 default: break;
146 }
147 return;
148 }
149 throw (std::out_of_range(name + " not found"));
150}

◆ writeObject()

void writeObject ( Writer writer) const
overridevirtual

Implements Serializable.

Definition at line 344 of file NSMData.cc.

345{
346 writer.writeString(getName());
347 writer.writeString(getFormat());
348 writer.writeInt(getRevision());
349 writer.writeInt(getSize());
350 const DBField::NameList& name_v(getFieldNames());
351 writer.writeInt(name_v.size());
352 for (DBField::NameList::const_iterator it = name_v.begin();
353 it != name_v.end(); ++it) {
354 const std::string& name(*it);
355 writer.writeString(name);
356 const DBField::Property& pro(getProperty(name));
357 writer.writeInt(pro.getType());
358 writer.writeInt(pro.getLength());
359 writer.writeInt(pro.getOffset());
360 size_t length = pro.getLength();
361 const void* buf = getValue(name);
362 if (length == 0) length = 1;
363 if (pro.getType() == DBField::OBJECT) {
364 const NSMDataList& data_v(getObjects(name));
365 for (size_t i = 0; i < length; i++) {
366 writer.writeObject(data_v[i]);
367 }
368 } else {
369 for (size_t i = 0; i < length; i++) {
370 switch (pro.getType()) {
371 case DBField::CHAR: writer.writeChar(((char*)buf)[i]); break;
372 case DBField::SHORT: writer.writeShort(((int16*)buf)[i]); break;
373 case DBField::INT: writer.writeInt(((int64*)buf)[i]); break;
374 case DBField::LONG: writer.writeLong(((int32*)buf)[i]); break;
375 case DBField::FLOAT: writer.writeFloat(((float*)buf)[i]); break;
376 case DBField::DOUBLE: writer.writeDouble(((double*)buf)[i]); break;
377 default : break;
378 }
379 }
380 }
381 }
382}

Member Data Documentation

◆ m_allocated

bool m_allocated
private

Definition at line 113 of file NSMData.h.

◆ m_com

NSMCommunicator* m_com
private

Definition at line 121 of file NSMData.h.

◆ m_data_v_m

NSMDataListMap m_data_v_m
private

Definition at line 119 of file NSMData.h.

◆ m_empty

std::string m_empty
private

Definition at line 120 of file NSMData.h.

◆ m_format

std::string m_format
private

Definition at line 117 of file NSMData.h.

◆ m_id

int m_id
privateinherited

Definition at line 96 of file AbstractDBObject.h.

◆ m_index

int m_index
privateinherited

Definition at line 94 of file AbstractDBObject.h.

◆ m_name

std::string m_name
privateinherited

Definition at line 97 of file AbstractDBObject.h.

◆ m_name_v

DBField::NameList m_name_v
privateinherited

Definition at line 98 of file AbstractDBObject.h.

◆ m_offset

int m_offset
private

Definition at line 116 of file NSMData.h.

◆ m_path

std::string m_path
privateinherited

Definition at line 95 of file AbstractDBObject.h.

◆ m_pdata

void* m_pdata
private

Definition at line 114 of file NSMData.h.

◆ m_pro_m

DBField::PropertyList m_pro_m
privateinherited

Definition at line 99 of file AbstractDBObject.h.

◆ m_revision

int m_revision
private

Definition at line 118 of file NSMData.h.

◆ m_size

int m_size
private

Definition at line 115 of file NSMData.h.

◆ m_tstamp

int m_tstamp
private

Definition at line 122 of file NSMData.h.


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