Line |
Branch |
Exec |
Source |
1 |
|
|
/***************************************************************************** |
2 |
|
|
* |
3 |
|
|
* $Id$ |
4 |
|
|
* |
5 |
|
|
* Copyright 2010 Richard Hacker (lerichi at gmx dot net) |
6 |
|
|
* |
7 |
|
|
* This file is part of the pdserv library. |
8 |
|
|
* |
9 |
|
|
* The pdserv library is free software: you can redistribute it and/or modify |
10 |
|
|
* it under the terms of the GNU Lesser General Public License as published |
11 |
|
|
* by the Free Software Foundation, either version 3 of the License, or (at |
12 |
|
|
* your option) any later version. |
13 |
|
|
* |
14 |
|
|
* The pdserv library is distributed in the hope that it will be useful, but |
15 |
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
16 |
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
17 |
|
|
* License for more details. |
18 |
|
|
* |
19 |
|
|
* You should have received a copy of the GNU Lesser General Public License |
20 |
|
|
* along with the pdserv library. If not, see <http://www.gnu.org/licenses/>. |
21 |
|
|
* |
22 |
|
|
*****************************************************************************/ |
23 |
|
|
|
24 |
|
|
#include "Variable.h" |
25 |
|
|
#include "XmlElement.h" |
26 |
|
|
|
27 |
|
|
#include <sstream> |
28 |
|
|
|
29 |
|
|
using namespace MsrProto; |
30 |
|
|
|
31 |
|
|
///////////////////////////////////////////////////////////////////////////// |
32 |
|
✗ |
Variable::Variable (const PdServ::Variable* v, size_t index, |
33 |
|
|
const PdServ::DataType& dtype, const PdServ::DataType::DimType& dim, |
34 |
|
✗ |
size_t offset, Variable* parent): |
35 |
|
|
variable(v), index(index), |
36 |
|
|
dtype(dtype), dim(dim), offset(offset), |
37 |
|
✗ |
memSize(dtype.size * dim.nelem), |
38 |
|
✗ |
hidden(false) |
39 |
|
|
{ |
40 |
|
✗ |
children = 0; |
41 |
|
✗ |
if (parent) { |
42 |
|
✗ |
hidden = parent->hidden; |
43 |
|
✗ |
parent->addChild(this); |
44 |
|
|
} |
45 |
|
|
} |
46 |
|
|
|
47 |
|
|
///////////////////////////////////////////////////////////////////////////// |
48 |
|
✗ |
Variable::~Variable() |
49 |
|
|
{ |
50 |
|
✗ |
if (children) { |
51 |
|
✗ |
for (List::const_iterator it = children->begin(); |
52 |
|
✗ |
it != children->end(); ++it) |
53 |
|
✗ |
delete *it; |
54 |
|
|
|
55 |
|
✗ |
delete children; |
56 |
|
|
} |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
///////////////////////////////////////////////////////////////////////////// |
60 |
|
✗ |
const Variable::List* Variable::getChildren() const |
61 |
|
|
{ |
62 |
|
✗ |
return children; |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
///////////////////////////////////////////////////////////////////////////// |
66 |
|
✗ |
void Variable::addChild(const Variable* child) |
67 |
|
|
{ |
68 |
|
✗ |
if (!children) |
69 |
|
✗ |
children = new List; |
70 |
|
|
|
71 |
|
✗ |
children->push_back(child); |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
///////////////////////////////////////////////////////////////////////////// |
75 |
|
✗ |
void Variable::setDataType(XmlElement &element, const PdServ::DataType& dtype, |
76 |
|
|
const PdServ::DataType::DimType& dim) const |
77 |
|
|
{ |
78 |
|
|
// datasize= |
79 |
|
✗ |
XmlElement::Attribute(element, "datasize") << dtype.size; |
80 |
|
|
|
81 |
|
|
// typ= |
82 |
|
|
const char *dtstr; |
83 |
|
✗ |
switch (dtype.primary()) { |
84 |
|
✗ |
case PdServ::DataType::boolean_T : dtstr = "TCHAR"; break; |
85 |
|
✗ |
case PdServ::DataType:: uint8_T : dtstr = "TUCHAR"; break; |
86 |
|
✗ |
case PdServ::DataType:: int8_T : dtstr = "TCHAR"; break; |
87 |
|
✗ |
case PdServ::DataType:: uint16_T : dtstr = "TUSHORT"; break; |
88 |
|
✗ |
case PdServ::DataType:: int16_T : dtstr = "TSHORT"; break; |
89 |
|
✗ |
case PdServ::DataType:: uint32_T : dtstr = "TUINT"; break; |
90 |
|
✗ |
case PdServ::DataType:: int32_T : dtstr = "TINT"; break; |
91 |
|
✗ |
case PdServ::DataType:: uint64_T : dtstr = "TULINT"; break; |
92 |
|
✗ |
case PdServ::DataType:: int64_T : dtstr = "TLINT"; break; |
93 |
|
✗ |
case PdServ::DataType:: double_T : dtstr = "TDBL"; break; |
94 |
|
✗ |
case PdServ::DataType:: single_T : dtstr = "TFLT"; break; |
95 |
|
✗ |
default : dtstr = "COMPOUND"; break; |
96 |
|
|
} |
97 |
|
✗ |
if (!dim.isScalar()) |
98 |
|
✗ |
XmlElement::Attribute(element, "typ") |
99 |
|
✗ |
<< dtstr |
100 |
|
✗ |
<< (dim.isVector() ? "_LIST" : "_MATRIX"); |
101 |
|
|
else |
102 |
|
✗ |
XmlElement::Attribute(element, "typ") << dtstr; |
103 |
|
|
|
104 |
|
|
// For vectors: |
105 |
|
|
// anz= |
106 |
|
|
// cnum= |
107 |
|
|
// rnum= |
108 |
|
|
// orientation= |
109 |
|
✗ |
if (!dim.isScalar()) { |
110 |
|
✗ |
XmlElement::Attribute(element, "anz") << dim.nelem; |
111 |
|
|
const char *orientation; |
112 |
|
✗ |
size_t cnum, rnum; |
113 |
|
|
|
114 |
|
|
// Transmit either a vector or a matrix |
115 |
|
✗ |
if (dim.isVector()) { |
116 |
|
✗ |
cnum = dim.nelem; |
117 |
|
✗ |
rnum = 1; |
118 |
|
✗ |
orientation = "VECTOR"; |
119 |
|
|
} |
120 |
|
|
else { |
121 |
|
✗ |
cnum = dim.back(); |
122 |
|
✗ |
rnum = dim.nelem / cnum; |
123 |
|
✗ |
orientation = "MATRIX_ROW_MAJOR"; |
124 |
|
|
} |
125 |
|
|
|
126 |
|
✗ |
XmlElement::Attribute(element, "cnum") << cnum; |
127 |
|
✗ |
XmlElement::Attribute(element, "rnum") << rnum; |
128 |
|
✗ |
XmlElement::Attribute(element, "orientation") << orientation; |
129 |
|
|
} |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
///////////////////////////////////////////////////////////////////////////// |
133 |
|
✗ |
void Variable::addCompoundFields(XmlElement &element, |
134 |
|
|
const PdServ::DataType& dtype) const |
135 |
|
|
{ |
136 |
|
✗ |
const PdServ::DataType::FieldList& fieldList = dtype.getFieldList(); |
137 |
|
|
|
138 |
|
✗ |
for (PdServ::DataType::FieldList::const_iterator it = fieldList.begin(); |
139 |
|
✗ |
it != fieldList.end(); ++it) { |
140 |
|
✗ |
XmlElement field(element.createChild("field")); |
141 |
|
✗ |
XmlElement::Attribute(field, "name").setEscaped((*it)->name.c_str()); |
142 |
|
✗ |
XmlElement::Attribute(field, "offset") << (*it)->offset; |
143 |
|
|
|
144 |
|
✗ |
setDataType(element, (*it)->type, (*it)->dim); |
145 |
|
|
|
146 |
|
✗ |
addCompoundFields(field, (*it)->type); |
147 |
|
|
} |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
///////////////////////////////////////////////////////////////////////////// |
151 |
|
✗ |
void Variable::setAttributes(XmlElement &element, bool shortReply) const |
152 |
|
|
{ |
153 |
|
|
// name= |
154 |
|
|
// value= |
155 |
|
|
// index= |
156 |
|
✗ |
XmlElement::Attribute(element, "index") << index; |
157 |
|
✗ |
XmlElement::Attribute(element, "name").setEscaped(path().c_str()); |
158 |
|
|
|
159 |
|
✗ |
if (shortReply) |
160 |
|
✗ |
return; |
161 |
|
|
|
162 |
|
|
// alias= |
163 |
|
|
// comment= |
164 |
|
✗ |
if (!variable->alias.empty()) |
165 |
|
✗ |
XmlElement::Attribute(element, "alias") << variable->alias; |
166 |
|
✗ |
if (!variable->comment.empty()) |
167 |
|
✗ |
XmlElement::Attribute(element, "comment") << variable->comment; |
168 |
|
|
|
169 |
|
✗ |
setDataType(element, dtype, dim); |
170 |
|
|
|
171 |
|
|
// text= |
172 |
|
|
// FIXME: variable->comment is used twice! 2012-12-14 fp |
173 |
|
✗ |
if (!variable->comment.empty()) |
174 |
|
✗ |
XmlElement::Attribute(element, "text") << variable->comment; |
175 |
|
|
|
176 |
|
|
// hide= |
177 |
|
|
// unhide= |
178 |
|
|
} |
179 |
|
|
|