Skip to content

Commit

Permalink
Fixed string introspection
Browse files Browse the repository at this point in the history
  • Loading branch information
jbendes committed Nov 26, 2024
1 parent a20fda3 commit 3a8946c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 31 deletions.
18 changes: 9 additions & 9 deletions test/IntrospectionTest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,29 @@ class IntrospectionTest : public CxxTest::TestSuite
zcm::TypeDb typeDb("./build/tests/test/types/libtestzcmtypes.so");
TS_ASSERT(typeDb.good());

recursive_t msg;
recursive_t msg {};

msg.text = (char*)"";
msg.static_texts[0] = (char*)"";
msg.static_texts[1] = (char*)"";
msg.static_texts[0] = (char*)"00";
msg.static_texts[1] = (char*)"01";
msg.n_children = 1;
msg.children = (recursive_t*)calloc(1, sizeof(recursive_t));

msg.children[0].text = (char*)"";
msg.children[0].static_texts[0] = (char*)"";
msg.children[0].static_texts[1] = (char*)"";
msg.children[0].static_texts[0] = (char*)"10";
msg.children[0].static_texts[1] = (char*)"11";
msg.children[0].n_children = 1;
msg.children[0].children = (recursive_t*)calloc(1, sizeof(recursive_t));

msg.children[0].children[0].text = (char*)"";
msg.children[0].children[0].static_texts[0] = (char*)"";
msg.children[0].children[0].static_texts[1] = (char*)"";
msg.children[0].children[0].static_texts[0] = (char*)"20";
msg.children[0].children[0].static_texts[1] = (char*)"21";
msg.children[0].children[0].n_children = 1;
msg.children[0].children[0].children = (recursive_t*)calloc(1, sizeof(recursive_t));

msg.children[0].children[0].children[0].text = (char*)"";
msg.children[0].children[0].children[0].static_texts[0] = (char*)"";
msg.children[0].children[0].children[0].static_texts[1] = (char*)"";
msg.children[0].children[0].children[0].static_texts[0] = (char*)"30";
msg.children[0].children[0].children[0].static_texts[1] = (char*)"31";
msg.children[0].children[0].children[0].n_texts = 2;
msg.children[0].children[0].children[0].dynamic_texts = (char**)calloc(2, sizeof(char*));
msg.children[0].children[0].children[0].dynamic_texts[0] = (char*)"text1";
Expand Down
37 changes: 15 additions & 22 deletions tools/cpp/util/Introspection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ bool processType(const string& name,
ret = false;
}
} else {
const void *p = !f.dim_is_variable[0] ? f.data : *(const void **) f.data;
if (!processArray(nextName, f.type, f.typestr,
f.num_dim, f.dim_size, f.dim_is_variable,
f.data, separator, typeDb, cb, usr)) {
p, separator, typeDb, cb, usr)) {
cerr << "Cannot process type's array: " << nextName << endl;
ret = false;
}
Expand All @@ -73,12 +74,6 @@ bool processType(const string& name,
return ret;
}

bool endsWith(const std::string& str, const std::string& suffix)
{
if (suffix.size() > str.size()) return false;
return str.rfind(suffix) == (str.size() - suffix.size());
}

bool processArray(string name,
zcm_field_type_t type, const char* typestr,
size_t num_dims, int32_t *dim_size, int8_t *dim_is_variable,
Expand All @@ -89,7 +84,7 @@ bool processArray(string name,
assert(num_dims > 0 && "Cant process an array of 0 dimensions");

if (type == ZCM_FIELD_BYTE && num_dims == 1) {
if (processEncodedType(name, *(uint8_t**)data, dim_size[0], separator, typeDb, cb, usr))
if (processEncodedType(name, (uint8_t*)data, dim_size[0], separator, typeDb, cb, usr))
return true;
}

Expand All @@ -101,9 +96,10 @@ bool processArray(string name,
string nextName = name + to_string(i);

if (num_dims - 1 > 0) {
const void *p = !dim_is_variable[0] ? data : *(const void **) data;
if (!processArray(nextName, type, typestr,
num_dims - 1, dim_size + 1, dim_is_variable + 1,
data, separator, typeDb, cb, usr)) {
p, separator, typeDb, cb, usr)) {
cerr << "Cannot process array's array: " << nextName << endl;
ret = false;
}
Expand All @@ -112,60 +108,58 @@ bool processArray(string name,

switch (type) {
case ZCM_FIELD_INT8_T:
if (!processScalar(nextName, type, typestr, &(*((int8_t**)data))[i], separator, typeDb, cb, usr)) {
if (!processScalar(nextName, type, typestr, &((int8_t*)data)[i], separator, typeDb, cb, usr)) {
cerr << "Cannot process int8_t array element: " << nextName << endl;
ret = false;
}
break;
case ZCM_FIELD_INT16_T:
if (!processScalar(nextName, type, typestr, &(*((int16_t**)data))[i], separator, typeDb, cb, usr)) {
if (!processScalar(nextName, type, typestr, &((int16_t*)data)[i], separator, typeDb, cb, usr)) {
cerr << "Cannot process int16_t array element: " << nextName << endl;
ret = false;
}
break;
case ZCM_FIELD_INT32_T:
if (!processScalar(nextName, type, typestr, &(*((int32_t**)data))[i], separator, typeDb, cb, usr)) {
if (!processScalar(nextName, type, typestr, &((int32_t*)data)[i], separator, typeDb, cb, usr)) {
cerr << "Cannot process int32_t array element: " << nextName << endl;
ret = false;
}
break;
case ZCM_FIELD_INT64_T:
if (!processScalar(nextName, type, typestr, &(*((int64_t**)data))[i], separator, typeDb, cb, usr)) {
if (!processScalar(nextName, type, typestr, &((int64_t*)data)[i], separator, typeDb, cb, usr)) {
cerr << "Cannot process int64_t array element: " << nextName << endl;
ret = false;
}
break;
case ZCM_FIELD_BYTE:
// Not introspecting into raw byte fields if it doesn't encode a zcmtype
/*
if (!processScalar(nextName, type, typestr, &(*((uint8_t**)data))[i], separator, typeDb, cb, usr)) {
if (!processScalar(nextName, type, typestr, &((uint8_t*)data)[i], separator, typeDb, cb, usr)) {
cerr << "Cannot process byte array element: " << nextName << endl;
ret = false;
}
*/
break;
case ZCM_FIELD_FLOAT:
if (!processScalar(nextName, type, typestr, &(*((float**)data))[i], separator, typeDb, cb, usr)) {
if (!processScalar(nextName, type, typestr, &((float*)data)[i], separator, typeDb, cb, usr)) {
cerr << "Cannot process float array element: " << nextName << endl;
ret = false;
}
break;
case ZCM_FIELD_DOUBLE:
if (!processScalar(nextName, type, typestr, &(*((double**)data))[i], separator, typeDb, cb, usr)) {
if (!processScalar(nextName, type, typestr, &((double*)data)[i], separator, typeDb, cb, usr)) {
cerr << "Cannot process double array element: " << nextName << endl;
ret = false;
}
break;
case ZCM_FIELD_BOOLEAN:
if (!processScalar(nextName, type, typestr, &(*((bool**)data))[i],
separator, typeDb, cb, usr)) {
if (!processScalar(nextName, type, typestr, &((bool*)data)[i], separator, typeDb, cb, usr)) {
cerr << "Cannot process boolean array element: " << nextName << endl;
ret = false;
}
break;
case ZCM_FIELD_STRING:
if (!processScalar(nextName, type, typestr, &((*((const char***)data))[i]),
separator, typeDb, cb, usr)) {
if (!processScalar(nextName, type, typestr, &((const char**)data)[i], separator, typeDb, cb, usr)) {
cerr << "Cannot process string array element: " << nextName << endl;
ret = false;
}
Expand All @@ -179,8 +173,7 @@ bool processArray(string name,
continue;
}
if (!processType(nextName, *zcmtype->info,
((uint8_t*)(*(void**)data)) +
i * zcmtype->info->struct_size(),
((uint8_t*)data) + i * zcmtype->info->struct_size(),
separator, typeDb, cb, usr)) {
cerr << "Cannot process " << typestr
<< " array element: " << nextName << endl;
Expand Down

0 comments on commit 3a8946c

Please sign in to comment.