| Directory: | ./ |
|---|---|
| File: | pdserv/src/msrproto/Variable.cpp |
| Date: | 2025-11-02 04:09:49 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 49 | 67 | 73.1% |
| Branches: | 45 | 102 | 44.1% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /***************************************************************************** | ||
| 2 | * | ||
| 3 | * Copyright 2010 Richard Hacker (lerichi at gmx dot net) | ||
| 4 | * | ||
| 5 | * This file is part of the pdserv library. | ||
| 6 | * | ||
| 7 | * The pdserv library is free software: you can redistribute it and/or modify | ||
| 8 | * it under the terms of the GNU Lesser General Public License as published | ||
| 9 | * by the Free Software Foundation, either version 3 of the License, or (at | ||
| 10 | * your option) any later version. | ||
| 11 | * | ||
| 12 | * The pdserv library is distributed in the hope that it will be useful, but | ||
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
| 14 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | ||
| 15 | * License for more details. | ||
| 16 | * | ||
| 17 | * You should have received a copy of the GNU Lesser General Public License | ||
| 18 | * along with the pdserv library. If not, see <http://www.gnu.org/licenses/>. | ||
| 19 | * | ||
| 20 | ****************************************************************************/ | ||
| 21 | |||
| 22 | #include "Variable.h" | ||
| 23 | #include "XmlElement.h" | ||
| 24 | |||
| 25 | using namespace MsrProto; | ||
| 26 | |||
| 27 | ///////////////////////////////////////////////////////////////////////////// | ||
| 28 | 5236 | Variable::Variable (const PdServ::Variable* v, size_t index, | |
| 29 | const PdServ::DataType& dtype, const PdServ::DataType::DimType& dim, | ||
| 30 | 5236 | size_t offset): | |
| 31 | variable(v), index(index), | ||
| 32 | dtype(dtype), dim(dim), offset(offset), | ||
| 33 | 10472 | memSize(dtype.size * dim.nelem), | |
| 34 |
2/4✓ Branch 5 taken 5236 times.
✗ Branch 6 not taken.
✓ Branch 21 taken 5236 times.
✗ Branch 22 not taken.
|
15708 | hidden(false) |
| 35 | { | ||
| 36 | 5236 | } | |
| 37 | |||
| 38 | ///////////////////////////////////////////////////////////////////////////// | ||
| 39 | 270 | void Variable::setDataType(XmlElement &element, const PdServ::DataType& dtype, | |
| 40 | const PdServ::DataType::DimType& dim) const | ||
| 41 | { | ||
| 42 | // datasize= | ||
| 43 |
1/2✓ Branch 5 taken 270 times.
✗ Branch 6 not taken.
|
270 | XmlElement::Attribute(element, "datasize") << dtype.size; |
| 44 | |||
| 45 | // typ= | ||
| 46 | const char *dtstr; | ||
| 47 |
4/12✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 33 times.
✓ Branch 11 taken 22 times.
✓ Branch 12 taken 113 times.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 102 times.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
|
270 | switch (dtype.primary()) { |
| 48 | ✗ | case PdServ::DataType::boolean_T : dtstr = "TCHAR"; break; | |
| 49 | ✗ | case PdServ::DataType:: uint8_T : dtstr = "TUCHAR"; break; | |
| 50 | ✗ | case PdServ::DataType:: int8_T : dtstr = "TCHAR"; break; | |
| 51 | ✗ | case PdServ::DataType:: uint16_T : dtstr = "TUSHORT"; break; | |
| 52 | 33 | case PdServ::DataType:: int16_T : dtstr = "TSHORT"; break; | |
| 53 | 22 | case PdServ::DataType:: uint32_T : dtstr = "TUINT"; break; | |
| 54 | 113 | case PdServ::DataType:: int32_T : dtstr = "TINT"; break; | |
| 55 | ✗ | case PdServ::DataType:: uint64_T : dtstr = "TULINT"; break; | |
| 56 | ✗ | case PdServ::DataType:: int64_T : dtstr = "TLINT"; break; | |
| 57 | 102 | case PdServ::DataType:: double_T : dtstr = "TDBL"; break; | |
| 58 | ✗ | case PdServ::DataType:: single_T : dtstr = "TFLT"; break; | |
| 59 | ✗ | default : dtstr = "COMPOUND"; break; | |
| 60 | } | ||
| 61 |
2/2✓ Branch 2 taken 33 times.
✓ Branch 3 taken 237 times.
|
270 | if (!dim.isScalar()) |
| 62 | 66 | XmlElement::Attribute(element, "typ") | |
| 63 |
1/2✓ Branch 3 taken 33 times.
✗ Branch 4 not taken.
|
66 | << dtstr |
| 64 |
3/4✓ Branch 3 taken 19 times.
✓ Branch 4 taken 14 times.
✓ Branch 6 taken 33 times.
✗ Branch 7 not taken.
|
33 | << (dim.isVector() ? "_LIST" : "_MATRIX"); |
| 65 | else | ||
| 66 |
1/2✓ Branch 4 taken 237 times.
✗ Branch 5 not taken.
|
237 | XmlElement::Attribute(element, "typ") << dtstr; |
| 67 | |||
| 68 | // For vectors: | ||
| 69 | // anz= | ||
| 70 | // cnum= | ||
| 71 | // rnum= | ||
| 72 | // orientation= | ||
| 73 |
2/2✓ Branch 2 taken 33 times.
✓ Branch 3 taken 237 times.
|
270 | if (!dim.isScalar()) { |
| 74 |
2/4✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 33 times.
✗ Branch 8 not taken.
|
33 | XmlElement::Attribute(element, "anz") << dim.nelem; |
| 75 | const char *orientation; | ||
| 76 | 33 | size_t cnum, rnum; | |
| 77 | |||
| 78 | // Transmit either a vector or a matrix | ||
| 79 |
2/2✓ Branch 2 taken 19 times.
✓ Branch 3 taken 14 times.
|
33 | if (dim.isVector()) { |
| 80 | 19 | cnum = dim.nelem; | |
| 81 | 19 | rnum = 1; | |
| 82 | 19 | orientation = "VECTOR"; | |
| 83 | } | ||
| 84 | else { | ||
| 85 | 14 | cnum = dim.back(); | |
| 86 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | rnum = dim.nelem / cnum; |
| 87 | 14 | orientation = "MATRIX_ROW_MAJOR"; | |
| 88 | } | ||
| 89 | |||
| 90 |
2/4✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 33 times.
✗ Branch 7 not taken.
|
33 | XmlElement::Attribute(element, "cnum") << cnum; |
| 91 |
2/4✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 33 times.
✗ Branch 7 not taken.
|
33 | XmlElement::Attribute(element, "rnum") << rnum; |
| 92 |
2/4✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 33 times.
✗ Branch 7 not taken.
|
33 | XmlElement::Attribute(element, "orientation") << orientation; |
| 93 | } | ||
| 94 | 270 | } | |
| 95 | |||
| 96 | ///////////////////////////////////////////////////////////////////////////// | ||
| 97 | 270 | void Variable::addCompoundFields(XmlElement &element, | |
| 98 | const PdServ::DataType& dtype) const | ||
| 99 | { | ||
| 100 | 270 | const PdServ::DataType::FieldList& fieldList = dtype.getFieldList(); | |
| 101 | |||
| 102 | 270 | for (PdServ::DataType::FieldList::const_iterator it = fieldList.begin(); | |
| 103 |
1/2✗ Branch 6 not taken.
✓ Branch 7 taken 270 times.
|
270 | it != fieldList.end(); ++it) { |
| 104 | ✗ | XmlElement field(element.createChild("field")); | |
| 105 | ✗ | XmlElement::Attribute(field, "name").setEscaped((*it)->name); | |
| 106 | ✗ | XmlElement::Attribute(field, "offset") << (*it)->offset; | |
| 107 | |||
| 108 | ✗ | setDataType(element, (*it)->type, (*it)->dim); | |
| 109 | |||
| 110 | ✗ | addCompoundFields(field, (*it)->type); | |
| 111 | } | ||
| 112 | 270 | } | |
| 113 | |||
| 114 | ///////////////////////////////////////////////////////////////////////////// | ||
| 115 | 198 | bool Variable::hasChildren(const Variable* v) const | |
| 116 | { | ||
| 117 |
1/2✓ Branch 6 taken 193 times.
✗ Branch 7 not taken.
|
193 | return (!v or v->variable != variable) |
| 118 |
3/4✓ Branch 0 taken 193 times.
✓ Branch 1 taken 5 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 193 times.
|
391 | or DirectoryNode::hasChildren(v); |
| 119 | } | ||
| 120 | |||
| 121 | ///////////////////////////////////////////////////////////////////////////// | ||
| 122 | 311 | void Variable::setAttributes( | |
| 123 | XmlElement &element, bool shortReply, bool derived) const | ||
| 124 | { | ||
| 125 | // index= | ||
| 126 |
1/2✓ Branch 5 taken 311 times.
✗ Branch 6 not taken.
|
311 | XmlElement::Attribute(element, "index") << index; |
| 127 | |||
| 128 | // name= | ||
| 129 | // DO NOT REMOVE: Testmanager requires name even with short reply! | ||
| 130 |
2/4✓ Branch 5 taken 311 times.
✗ Branch 6 not taken.
✓ Branch 9 taken 311 times.
✗ Branch 10 not taken.
|
311 | XmlElement::Attribute(element, "name").setEscaped(path()); |
| 131 | |||
| 132 |
2/2✓ Branch 0 taken 41 times.
✓ Branch 1 taken 270 times.
|
311 | if (shortReply) |
| 133 | 41 | return; | |
| 134 | |||
| 135 | // alias= | ||
| 136 | // comment= | ||
| 137 |
1/2✗ Branch 8 not taken.
✓ Branch 9 taken 270 times.
|
270 | if (!variable->alias.empty()) |
| 138 | ✗ | XmlElement::Attribute(element, "alias").setEscaped(variable->alias); | |
| 139 |
1/2✗ Branch 8 not taken.
✓ Branch 9 taken 270 times.
|
270 | if (!variable->comment.empty()) |
| 140 | ✗ | XmlElement::Attribute(element, "comment") | |
| 141 | ✗ | .setEscaped(variable->comment); | |
| 142 | |||
| 143 | 270 | setDataType(element, dtype, dim); | |
| 144 | |||
| 145 | // text= | ||
| 146 | // Historically, comments were transmitted with the "text=" attribute. | ||
| 147 | // It is only there for old TestManager backward compatability. | ||
| 148 | // This is deprecated for MSR clients and "comment=" should be used | ||
| 149 | // instead. | ||
| 150 |
1/2✗ Branch 8 not taken.
✓ Branch 9 taken 270 times.
|
270 | if (!variable->comment.empty()) |
| 151 | ✗ | XmlElement::Attribute(element, "text").setEscaped(variable->comment); | |
| 152 | |||
| 153 |
5/8✓ Branch 4 taken 33 times.
✓ Branch 5 taken 237 times.
✓ Branch 6 taken 33 times.
✗ Branch 7 not taken.
✗ Branch 14 not taken.
✓ Branch 15 taken 33 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 270 times.
|
270 | if (childCount() and (derived or hasChildren(this))) |
| 154 | ✗ | XmlElement::Attribute(element, "dir") << 1; | |
| 155 | |||
| 156 | // hide= | ||
| 157 | // unhide= | ||
| 158 | } | ||
| 159 |