Skip to content
This repository was archived by the owner on Jul 8, 2022. It is now read-only.

Commit b3bad4d

Browse files
bourtembIngvord
authored andcommitted
Fix DeviceAttribute constructor for short and enum data types (#392) (#393)
Close #392
1 parent f76b2cd commit b3bad4d

File tree

3 files changed

+136
-5
lines changed

3 files changed

+136
-5
lines changed

include/tango/client/devapi_attr.tpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,12 @@ void DeviceAttribute::base_val(T _val)
7777
d_state_filled = false;
7878
exceptions_flags.set(failed_flag);
7979
exceptions_flags.set(isempty_flag);
80+
// This constructor is supposed to be used only for enum types since all
81+
// the constructors for the other standard Tango types have a specialization defined
8082
ShortSeq = new(DevVarShortArray);
8183
ShortSeq->length(1);
8284
ShortSeq[0] = static_cast<short>(_val);
85+
data_type = DEV_ENUM;
8386
}
8487

8588
//-----------------------------------------------------------------------------------------------------------------
@@ -111,6 +114,9 @@ void DeviceAttribute::base_vect(vector<T> &_val)
111114
d_state_filled = false;
112115
exceptions_flags.set(failed_flag);
113116
exceptions_flags.set(isempty_flag);
117+
// This is supposed to be used only for enum types since all
118+
// the constructors for the other standard Tango types have a specialization defined
119+
data_type = DEV_ENUM;
114120
ShortSeq = new(DevVarShortArray);
115121
ShortSeq->length(_val.size());
116122
for (size_t loop = 0;loop < _val.size();loop++)
@@ -149,6 +155,9 @@ void DeviceAttribute::base_vect_size(vector<T> &_val)
149155
exceptions_flags.set(isempty_flag);
150156
ShortSeq = new(DevVarShortArray);
151157
ShortSeq->length(_val.size());
158+
// This is supposed to be used only for enum types since all
159+
// the constructors for the other standard Tango types have a specialization defined
160+
data_type = DEV_ENUM;
152161
for (size_t loop = 0;loop < _val.size();loop++)
153162
ShortSeq[loop] = static_cast<short>(_val[loop]);
154163
}
@@ -354,6 +363,7 @@ void DeviceAttribute::operator << (T datum)
354363
w_dim_y = 0;
355364
quality = Tango::ATTR_VALID;
356365
data_format = Tango::FMT_UNKNOWN;
366+
data_type = DEV_ENUM;
357367

358368
DevVarShortArray *short_vararr = new(DevVarShortArray);
359369
short_vararr->length(1);
@@ -375,6 +385,7 @@ void DeviceAttribute::operator << (vector<T> &_datum)
375385
w_dim_y = 0;
376386
quality = Tango::ATTR_VALID;
377387
data_format = Tango::FMT_UNKNOWN;
388+
data_type = DEV_ENUM;
378389

379390
if (ShortSeq.operator->() == NULL)
380391
{
@@ -397,7 +408,7 @@ void DeviceAttribute::insert(vector<T> &_datum,int _x,int _y)
397408
}
398409

399410
template <typename T>
400-
bool DeviceAttribute::template_type_check(T &_datum)
411+
bool DeviceAttribute::template_type_check(T &TANGO_UNUSED(_datum))
401412
{
402413
#ifdef HAS_UNDERLYING
403414
bool short_enum = is_same<short,typename underlying_type<T>::type>::value;

src/client/devapi_attr.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ DeviceAttribute::DeviceAttribute(string &new_name, short datum):ext(new DeviceAt
563563
ShortSeq = new(DevVarShortArray);
564564
ShortSeq->length(1);
565565
ShortSeq[0] = datum;
566+
data_type = DEV_SHORT;
566567
}
567568

568569
DeviceAttribute::DeviceAttribute(const char *new_name, short datum):ext(new DeviceAttributeExt)
@@ -580,6 +581,7 @@ DeviceAttribute::DeviceAttribute(const char *new_name, short datum):ext(new Devi
580581
ShortSeq = new(DevVarShortArray);
581582
ShortSeq->length(1);
582583
ShortSeq[0] = datum;
584+
data_type = DEV_SHORT;
583585
}
584586

585587
//-----------------------------------------------------------------------------
@@ -1116,6 +1118,7 @@ DeviceAttribute::DeviceAttribute(string& new_name, vector<short> &datum):ext(new
11161118
exceptions_flags.set(isempty_flag);
11171119
ShortSeq = new(DevVarShortArray);
11181120
ShortSeq.inout() << datum;
1121+
data_type = DEV_SHORT;
11191122
}
11201123

11211124
DeviceAttribute::DeviceAttribute(const char *new_name, vector<short> &datum):ext(new DeviceAttributeExt)
@@ -1132,6 +1135,7 @@ DeviceAttribute::DeviceAttribute(const char *new_name, vector<short> &datum):ext
11321135
exceptions_flags.set(isempty_flag);
11331136
ShortSeq = new(DevVarShortArray);
11341137
ShortSeq.inout() << datum;
1138+
data_type = DEV_SHORT;
11351139
}
11361140

11371141
DeviceAttribute::DeviceAttribute(string& new_name, vector<short> &datum,int x,int y):ext(new DeviceAttributeExt)
@@ -1148,6 +1152,7 @@ DeviceAttribute::DeviceAttribute(string& new_name, vector<short> &datum,int x,in
11481152
exceptions_flags.set(isempty_flag);
11491153
ShortSeq = new(DevVarShortArray);
11501154
ShortSeq.inout() << datum;
1155+
data_type = DEV_SHORT;
11511156
}
11521157

11531158
DeviceAttribute::DeviceAttribute(const char *new_name, vector<short> &datum,int x,int y):ext(new DeviceAttributeExt)
@@ -1164,6 +1169,7 @@ DeviceAttribute::DeviceAttribute(const char *new_name, vector<short> &datum,int
11641169
exceptions_flags.set(isempty_flag);
11651170
ShortSeq = new(DevVarShortArray);
11661171
ShortSeq.inout() << datum;
1172+
data_type = DEV_SHORT;
11671173
}
11681174

11691175
//-----------------------------------------------------------------------------
@@ -2063,9 +2069,18 @@ bool DeviceAttribute::is_empty()
20632069
int DeviceAttribute::get_type()
20642070
{
20652071
int da_type;
2066-
2067-
if (is_empty() == true)
2068-
return -1;
2072+
bool da_is_empty;
2073+
2074+
// reset is_empty exception flag to avoid throwing an exception
2075+
// during is_empty() call
2076+
bitset<DeviceAttribute::numFlags> bs = exceptions();
2077+
reset_exceptions(DeviceAttribute::isempty_flag);
2078+
da_is_empty = is_empty();
2079+
// restore is_empty exception flag
2080+
exceptions(bs);
2081+
2082+
if (da_is_empty)
2083+
return DATA_TYPE_UNKNOWN;
20692084
else
20702085
{
20712086
if (LongSeq.operator->() != NULL)
@@ -2097,7 +2112,7 @@ int DeviceAttribute::get_type()
20972112
else if ((StateSeq.operator->() != NULL) || (d_state_filled == true))
20982113
da_type = Tango::DEV_STATE;
20992114
else
2100-
da_type = -1;
2115+
da_type = DATA_TYPE_UNKNOWN;
21012116
}
21022117

21032118
return da_type;
@@ -2164,6 +2179,8 @@ void DeviceAttribute::operator << (short datum)
21642179
w_dim_y = 0;
21652180
quality = Tango::ATTR_VALID;
21662181
data_format = Tango::FMT_UNKNOWN;
2182+
if(data_type != DEV_ENUM)
2183+
{ data_type = DEV_SHORT;}
21672184

21682185
DevVarShortArray *short_vararr = new(DevVarShortArray);
21692186
short_vararr->length(1);
@@ -3012,6 +3029,9 @@ void DeviceAttribute::operator << (vector<short> &datum)
30123029
quality = Tango::ATTR_VALID;
30133030
data_format = Tango::FMT_UNKNOWN;
30143031

3032+
if(data_type != DEV_ENUM)
3033+
{ data_type = DEV_SHORT;}
3034+
30153035
if (ShortSeq.operator->() == NULL)
30163036
{
30173037
DevVarShortArray *short_vararr = new(DevVarShortArray);

test/cpp_test_suite/new_tests/cxx_cmd_types.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,106 @@ class CmdTypesTestSuite: public CxxTest::TestSuite
11911191
#endif
11921192
}
11931193

1194+
// Test DeviceAttribute get_type method after constructor call
1195+
void test_DeviceAttribute_get_type_after_constructor_call(void)
1196+
{
1197+
string attr_name = "MyAttrName";
1198+
short my_short = 42;
1199+
DeviceAttribute da_short("MyAttributeName", my_short);
1200+
TS_ASSERT(da_short.get_type() == DEV_SHORT);
1201+
DeviceAttribute da_short2(attr_name, my_short);
1202+
TS_ASSERT(da_short2.get_type() == DEV_SHORT);
1203+
1204+
enum Color
1205+
{
1206+
red, green, blue
1207+
};
1208+
Color color = red;
1209+
DeviceAttribute da_enum("MyColorEnumAttr", color);
1210+
TS_ASSERT(da_enum.get_type() == DEV_ENUM);
1211+
DeviceAttribute da_enum2(attr_name, color);
1212+
TS_ASSERT(da_enum2.get_type() == DEV_ENUM);
1213+
1214+
vector<short> my_short_vector;
1215+
for (size_t i = 0; i < 10; i++)
1216+
{
1217+
my_short_vector.push_back(i);
1218+
my_short_vector.push_back(-i);
1219+
}
1220+
DeviceAttribute da_short_vec("MyShortSpectrumAttr", my_short_vector);
1221+
TS_ASSERT(da_short_vec.get_type() == DEV_SHORT);
1222+
DeviceAttribute da_short_vec2(attr_name, my_short_vector);
1223+
TS_ASSERT(da_short_vec2.get_type() == DEV_SHORT);
1224+
1225+
DeviceAttribute da_short_vec3("MyShortSpectrumAttr", my_short_vector, 4, 5);
1226+
TS_ASSERT(da_short_vec3.get_type() == DEV_SHORT);
1227+
DeviceAttribute da_short_vec4(attr_name, my_short_vector, 10, 2);
1228+
TS_ASSERT(da_short_vec4.get_type() == DEV_SHORT);
1229+
1230+
vector <Color> my_enum_vector;
1231+
my_enum_vector.push_back(red);
1232+
my_enum_vector.push_back(blue);
1233+
my_enum_vector.push_back(red);
1234+
my_enum_vector.push_back(green);
1235+
1236+
DeviceAttribute da_enum_vec("MyEnumSpectrumAttr", my_enum_vector);
1237+
TS_ASSERT(da_enum_vec.get_type() == DEV_ENUM);
1238+
DeviceAttribute da_enum_vec2(attr_name, my_enum_vector);
1239+
TS_ASSERT(da_enum_vec2.get_type() == DEV_ENUM);
1240+
1241+
DeviceAttribute da_enum_vec3("MyEnumSpectrumAttr", my_enum_vector, 2, 2);
1242+
TS_ASSERT(da_enum_vec3.get_type() == DEV_ENUM);
1243+
DeviceAttribute da_enum_vec4(attr_name, my_enum_vector, 4, 1);
1244+
TS_ASSERT(da_enum_vec4.get_type() == DEV_ENUM);
1245+
}
1246+
1247+
// Test DeviceAttribute get_type method after short or enum insertion
1248+
void test_DeviceAttribute_get_type_after_short_or_enum_insertion(void)
1249+
{
1250+
enum Color
1251+
{
1252+
red, green, blue
1253+
};
1254+
1255+
DeviceAttribute da;
1256+
TS_ASSERT(da.get_type() == DATA_TYPE_UNKNOWN);
1257+
short my_short = 42;
1258+
da << my_short;
1259+
TS_ASSERT(da.get_type() == DEV_SHORT);
1260+
1261+
Color color = blue;
1262+
da << color;
1263+
TS_ASSERT(da.get_type() == DEV_ENUM);
1264+
1265+
da << my_short;
1266+
TS_ASSERT(da.get_type() == DEV_ENUM);
1267+
1268+
vector<short> my_short_vector;
1269+
for (size_t i = 0; i < 10; i++)
1270+
{
1271+
my_short_vector.push_back(i);
1272+
my_short_vector.push_back(-i);
1273+
}
1274+
1275+
da << my_short_vector;
1276+
// If the device attribute data type was previously set to DEV_ENUM
1277+
// and we insert a short, we can still consider it as an enum
1278+
TS_ASSERT(da.get_type() == DEV_ENUM);
1279+
1280+
DeviceAttribute da2;
1281+
da2 << my_short_vector;
1282+
TS_ASSERT(da2.get_type() == DEV_SHORT);
1283+
1284+
vector <Color> my_enum_vector;
1285+
my_enum_vector.push_back(red);
1286+
my_enum_vector.push_back(blue);
1287+
my_enum_vector.push_back(red);
1288+
my_enum_vector.push_back(green);
1289+
1290+
da2 << my_enum_vector;
1291+
TS_ASSERT(da2.get_type() == DEV_ENUM);
1292+
}
1293+
11941294
};
11951295
#undef cout
11961296
#endif // CmdTypesTestSuite_h

0 commit comments

Comments
 (0)