Skip to content

Commit 71eccb5

Browse files
committed
use switch statements on value type code in pvxs converter
1 parent 4793ace commit 71eccb5

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

ADApp/ntndArrayConverterSrc/ntndArrayConverter_pvxs.cpp

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,19 @@ NTNDArrayInfo_t NTNDArrayConverter::getInfo (void)
4747
int bpe;
4848

4949
if (info.codec.empty()) {
50-
// TODO would be nicer as a switch statement
51-
std::string fieldName = m_value["value"].nameOf(m_value["value"]["->"]);
52-
std::string typeName(fieldName.substr(0, fieldName.find("Value")));
53-
if (typeName == "byte") {dt = NDDataType_t::NDInt8; bpe = sizeof(int8_t);}
54-
else if (typeName == "ubyte") {dt = NDDataType_t::NDUInt8; bpe = sizeof(uint8_t);}
55-
else if (typeName == "short") {dt = NDDataType_t::NDInt16; bpe = sizeof(int16_t);}
56-
else if (typeName == "ushort") {dt = NDDataType_t::NDUInt16; bpe = sizeof(uint16_t);}
57-
else if (typeName == "int") {dt = NDDataType_t::NDInt32; bpe = sizeof(int32_t);}
58-
else if (typeName == "uint") {dt = NDDataType_t::NDUInt32; bpe = sizeof(uint32_t);}
59-
else if (typeName == "long") {dt = NDDataType_t::NDInt64; bpe = sizeof(int64_t);}
60-
else if (typeName == "ulong") {dt = NDDataType_t::NDUInt64; bpe = sizeof(uint64_t);}
61-
else if (typeName == "float") {dt = NDDataType_t::NDFloat32; bpe = sizeof(float_t);}
62-
else if (typeName == "double") {dt = NDDataType_t::NDFloat64; bpe = sizeof(double_t);}
63-
else throw std::runtime_error("invalid value data type");
50+
switch (m_value["value->"].type().code) {
51+
case pvxs::TypeCode::Int8A: {dt = NDDataType_t::NDInt8; bpe = sizeof(int8_t); break;}
52+
case pvxs::TypeCode::UInt8A: {dt = NDDataType_t::NDUInt8; bpe = sizeof(uint8_t); break;}
53+
case pvxs::TypeCode::Int16A: {dt = NDDataType_t::NDInt16; bpe = sizeof(int16_t); break;}
54+
case pvxs::TypeCode::UInt16A: {dt = NDDataType_t::NDUInt16; bpe = sizeof(uint16_t); break;}
55+
case pvxs::TypeCode::Int32A: {dt = NDDataType_t::NDInt32; bpe = sizeof(int32_t); break;}
56+
case pvxs::TypeCode::UInt32A: {dt = NDDataType_t::NDUInt32; bpe = sizeof(uint32_t); break;}
57+
case pvxs::TypeCode::Int64A: {dt = NDDataType_t::NDInt64; bpe = sizeof(int64_t); break;}
58+
case pvxs::TypeCode::UInt64A: {dt = NDDataType_t::NDUInt64; bpe = sizeof(uint64_t); break;}
59+
case pvxs::TypeCode::Float32A: {dt = NDDataType_t::NDFloat32; bpe = sizeof(float_t); break;}
60+
case pvxs::TypeCode::Float64A: {dt = NDDataType_t::NDFloat64; bpe = sizeof(double_t); break;}
61+
default: throw std::runtime_error("invalid value data type");
62+
}
6463
// TODO get datatype
6564
} else {
6665
throw std::runtime_error("Have not implemeted parsing from known codec type yet");
@@ -173,19 +172,19 @@ void NTNDArrayConverter::toValue (NDArray *dest, std::string fieldName)
173172

174173
void NTNDArrayConverter::toValue (NDArray *dest)
175174
{
176-
std::string fieldName = m_value["value"].nameOf(m_value["value"]["->"]);
177-
std::string typeName(fieldName.substr(0, fieldName.find("Value")));
178-
if (typeName == "byte") {toValue<int8_t>(dest, std::string("value->byteValue"));}
179-
else if (typeName == "ubyte") {toValue<uint8_t>(dest, std::string("value->ubyteValue"));}
180-
else if (typeName == "short") {toValue<int16_t>(dest, std::string("value->shortValue"));}
181-
else if (typeName == "ushort") {toValue<uint16_t>(dest, std::string("value->ushortValue"));}
182-
else if (typeName == "int") {toValue<int32_t>(dest, std::string("value->intValue"));}
183-
else if (typeName == "uint") {toValue<uint32_t>(dest, std::string("value->uintValue"));}
184-
else if (typeName == "long") {toValue<int64_t>(dest, std::string("value->longValue"));}
185-
else if (typeName == "ulong") {toValue<uint64_t>(dest, std::string("value->ulongValue"));}
186-
else if (typeName == "float") {toValue<float_t>(dest, std::string("value->floatValue"));}
187-
else if (typeName == "double") {toValue<double_t>(dest, std::string("value->doubleValue"));}
188-
else throw std::runtime_error("invalid value data type");
175+
switch (m_value["value->"].type().code) {
176+
case pvxs::TypeCode::Int8A: {toValue<int8_t>(dest, std::string("value->byteValue")); break;}
177+
case pvxs::TypeCode::UInt8A: {toValue<uint8_t>(dest, std::string("value->ubyteValue")); break;}
178+
case pvxs::TypeCode::Int16A: {toValue<int16_t>(dest, std::string("value->shortValue")); break;}
179+
case pvxs::TypeCode::UInt16A: {toValue<uint16_t>(dest, std::string("value->ushortValue")); break;}
180+
case pvxs::TypeCode::Int32A: {toValue<int32_t>(dest, std::string("value->intValue")); break;}
181+
case pvxs::TypeCode::UInt32A: {toValue<uint32_t>(dest, std::string("value->uintValue")); break;}
182+
case pvxs::TypeCode::Int64A: {toValue<int64_t>(dest, std::string("value->longValue")); break;}
183+
case pvxs::TypeCode::UInt64A: {toValue<uint64_t>(dest, std::string("value->ulongValue")); break;}
184+
case pvxs::TypeCode::Float32A: {toValue<float_t>(dest, std::string("value->floatValue")); break;}
185+
case pvxs::TypeCode::Float64A: {toValue<double_t>(dest, std::string("value->doubleValue")); break;}
186+
default: throw std::runtime_error("invalid value data type");
187+
}
189188
}
190189

191190
void NTNDArrayConverter::toDimensions (NDArray *dest)

0 commit comments

Comments
 (0)