From c1c5e2a0c8ce270938b5706fa7513a29e54bda5b Mon Sep 17 00:00:00 2001 From: Son Dinh Date: Tue, 22 Oct 2024 17:38:58 -0500 Subject: [PATCH] Read nested members --- src/dds_data.cpp | 74 +++++++++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/src/dds_data.cpp b/src/dds_data.cpp index 0b4a1d7..16eb7e5 100644 --- a/src/dds_data.cpp +++ b/src/dds_data.cpp @@ -213,45 +213,56 @@ QVariant CommonData::readDynamicMember(const QString& topicName, const QString& memberName, unsigned int index) { + const char* error = "NULL"; QVariant value; QMutexLocker locker(&m_dynamicSamplesMutex); if (!m_dynamicSamples.contains(topicName)) { - value = "NULL"; - return value; + return error; } const QList& sampleList = m_dynamicSamples[topicName]; - if ((int)index >= sampleList.count()) { - value = "NULL"; - return value; + if (static_cast(index) >= sampleList.count()) { + return error; } DDS::DynamicData_var sample = sampleList.at(index); DDS::DynamicType_var topic_type = sample->type(); + OpenDDS::XTypes::MemberPath member_path; + if (member_path.resolve_string_path(topic_type, memberName.toStdString().c_str()) != DDS::RETCODE_OK) { + return error; + } + + // The direct parent dynamic data of this member + DDS::DynamicData_var parent_data; + + // The Id of this member within the direct parent type + DDS::MemberId id; + + if (member_path.get_member_from_data(sample, parent_data, id) != DDS::RETCODE_OK) { + return error; + } + + DDS::DynamicType_var parent_type = parent_data->type(); DDS::DynamicTypeMember_var dtm; - DDS::ReturnCode_t rc = topic_type->get_member_by_name(dtm, memberName.toStdString().c_str()); - if (rc != DDS::RETCODE_OK) { - value = "NULL"; - return value; + if (parent_type->get_member(dtm, id) != DDS::RETCODE_OK) { + return error; } DDS::MemberDescriptor_var md; - rc = dtm->get_descriptor(md); - if (rc != DDS::RETCODE_OK) { - value = "NULL"; - return value; + if (dtm->get_descriptor(md) != DDS::RETCODE_OK) { + return error; } const DDS::TypeKind member_tk = md->type()->get_kind(); - const DDS::MemberId id = md->id(); - bool failed = true; + DDS::ReturnCode_t rc = DDS::RETCODE_OK; + switch (member_tk) { case OpenDDS::XTypes::TK_BOOLEAN: { CORBA::Boolean tmp; - rc = sample->get_boolean_value(tmp, id); + rc = parent_data->get_boolean_value(tmp, id); if (rc == DDS::RETCODE_OK) { value = tmp; failed = false; @@ -261,7 +272,7 @@ QVariant CommonData::readDynamicMember(const QString& topicName, case OpenDDS::XTypes::TK_BYTE: { CORBA::Octet tmp; - rc = sample->get_byte_value(tmp, id); + rc = parent_data->get_byte_value(tmp, id); if (rc == DDS::RETCODE_OK) { value = tmp; failed = false; @@ -271,7 +282,7 @@ QVariant CommonData::readDynamicMember(const QString& topicName, case OpenDDS::XTypes::TK_INT16: { CORBA::Short tmp; - rc = sample->get_int16_value(tmp, id); + rc = parent_data->get_int16_value(tmp, id); if (rc == DDS::RETCODE_OK) { value = tmp; failed = false; @@ -281,7 +292,7 @@ QVariant CommonData::readDynamicMember(const QString& topicName, case OpenDDS::XTypes::TK_UINT16: { CORBA::UShort tmp; - rc = sample->get_uint16_value(tmp, id); + rc = parent_data->get_uint16_value(tmp, id); if (rc == DDS::RETCODE_OK) { value = tmp; failed = false; @@ -291,7 +302,7 @@ QVariant CommonData::readDynamicMember(const QString& topicName, case OpenDDS::XTypes::TK_INT32: { CORBA::Long tmp; - rc = sample->get_int32_value(tmp, id); + rc = parent_data->get_int32_value(tmp, id); if (rc == DDS::RETCODE_OK) { value = tmp; failed = false; @@ -300,8 +311,8 @@ QVariant CommonData::readDynamicMember(const QString& topicName, } case OpenDDS::XTypes::TK_UINT32: { - CORBA::UShort tmp; - rc = sample->get_uint16_value(tmp, id); + CORBA::ULong tmp; + rc = parent_data->get_uint32_value(tmp, id); if (rc == DDS::RETCODE_OK) { value = tmp; failed = false; @@ -311,7 +322,7 @@ QVariant CommonData::readDynamicMember(const QString& topicName, case OpenDDS::XTypes::TK_INT64: { CORBA::LongLong tmp; - rc = sample->get_int64_value(tmp, id); + rc = parent_data->get_int64_value(tmp, id); if (rc == DDS::RETCODE_OK) { value = static_cast(tmp); failed = false; @@ -321,7 +332,7 @@ QVariant CommonData::readDynamicMember(const QString& topicName, case OpenDDS::XTypes::TK_UINT64: { CORBA::ULongLong tmp; - rc = sample->get_uint64_value(tmp, id); + rc = parent_data->get_uint64_value(tmp, id); if (rc == DDS::RETCODE_OK) { value = static_cast(tmp); failed = false; @@ -331,7 +342,7 @@ QVariant CommonData::readDynamicMember(const QString& topicName, case OpenDDS::XTypes::TK_FLOAT32: { CORBA::Float tmp; - rc = sample->get_float32_value(tmp, id); + rc = parent_data->get_float32_value(tmp, id); if (rc == DDS::RETCODE_OK) { value = tmp; failed = false; @@ -341,7 +352,7 @@ QVariant CommonData::readDynamicMember(const QString& topicName, case OpenDDS::XTypes::TK_FLOAT64: { CORBA::Double tmp; - rc = sample->get_float64_value(tmp, id); + rc = parent_data->get_float64_value(tmp, id); if (rc == DDS::RETCODE_OK) { value = tmp; failed = false; @@ -351,7 +362,7 @@ QVariant CommonData::readDynamicMember(const QString& topicName, case OpenDDS::XTypes::TK_CHAR8: { CORBA::Char tmp; - rc = sample->get_char8_value(tmp, id); + rc = parent_data->get_char8_value(tmp, id); if (rc == DDS::RETCODE_OK) { value = tmp; failed = false; @@ -361,7 +372,7 @@ QVariant CommonData::readDynamicMember(const QString& topicName, case OpenDDS::XTypes::TK_CHAR16: { CORBA::WChar tmp; - rc = sample->get_char16_value(tmp, id); + rc = parent_data->get_char16_value(tmp, id); if (rc == DDS::RETCODE_OK) { value = tmp; failed = false; @@ -371,7 +382,7 @@ QVariant CommonData::readDynamicMember(const QString& topicName, case OpenDDS::XTypes::TK_STRING8: { CORBA::String_var tmp; - rc = sample->get_string_value(tmp, id); + rc = parent_data->get_string_value(tmp, id); if (rc == DDS::RETCODE_OK) { value = tmp.in(); failed = false; @@ -381,7 +392,7 @@ QVariant CommonData::readDynamicMember(const QString& topicName, case OpenDDS::XTypes::TK_ENUM: { CORBA::Long tmp; - rc = sample->get_int32_value(tmp, id); + rc = parent_data->get_int32_value(tmp, id); if (rc == DDS::RETCODE_OK) { DDS::String8_var name; rc = OpenDDS::XTypes::get_enumerator_name(name, tmp, md->type()); @@ -397,7 +408,7 @@ QVariant CommonData::readDynamicMember(const QString& topicName, } if (failed) { - value = "NULL"; + value = error; } return value; } @@ -411,7 +422,6 @@ QVariant CommonData::readValue(const QString& topicName, if (value.toString() != "NULL") { return value; } - return readDynamicMember(topicName, memberName, index); }