Skip to content

Commit

Permalink
Fix complex member printing for DynamicDataHelper ((#2965)
Browse files Browse the repository at this point in the history
* Fix complex member printing for DynamicDataHelper (#2957)

* DynamicDataHelper::print can print Sequences as well as Arrays
Signed-off-by: Will Stott <willstott101@gmail.com>

* Fix complex member printing

Signed-off-by: methylDragon <methylDragon@gmail.com>

* Clean up prints

Signed-off-by: methylDragon <methylDragon@gmail.com>

* Fix FastDDS windows CI error

Signed-off-by: methylDragon <methylDragon@gmail.com>

Signed-off-by: methylDragon <methylDragon@gmail.com>
Co-authored-by: Will Stott <willstott101@gmail.com>
(cherry picked from commit 78473a0)

# Conflicts:
#	src/cpp/dynamic-types/DynamicDataHelper.cpp

* Fix conflict

Co-authored-by: methylDragon <methylDragon@gmail.com>

* Fixing linters

Signed-off-by: Miguel Company <MiguelCompany@eprosima.com>

Signed-off-by: Miguel Company <MiguelCompany@eprosima.com>
Co-authored-by: methylDragon <methylDragon@gmail.com>
Co-authored-by: Miguel Company <miguelcompany@eprosima.com>
  • Loading branch information
3 people authored Sep 30, 2022
1 parent 9f6dece commit 03fdb06
Showing 1 changed file with 71 additions and 33 deletions.
104 changes: 71 additions & 33 deletions src/cpp/dynamic-types/DynamicDataHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void DynamicDataHelper::print_basic_element(
MemberId id,
TypeKind kind)
{
switch(kind)
switch (kind)
{
case TK_NONE:
{
Expand Down Expand Up @@ -252,33 +252,71 @@ void DynamicDataHelper::aux_index_position(
void DynamicDataHelper::print_basic_collection(
DynamicData* data)
{
const std::vector<uint32_t>& bounds = data->type_->descriptor_->bound_;
if (data->type_->get_kind() == TK_SEQUENCE)
{
auto count = data->get_item_count();
std::cout << "[";
for (uint32_t i = 0; i < count; ++i)
{
print_basic_element(data, i, data->type_->get_element_type()->get_kind());
std::cout << (i == count - 1 ? "]" : ", ");
}
if (count == 0)
{
std::cout << "]";
}
}
else
{
const std::vector<uint32_t>& bounds = data->type_->descriptor_->bound_;

std::vector<std::vector<uint32_t>> positions;
fill_array_positions(bounds, positions);
std::vector<std::vector<uint32_t>> positions;
fill_array_positions(bounds, positions);

std::cout << "[";
for (size_t i = 0; i < positions.size(); ++i)
{
print_basic_element(data, data->get_array_index(positions[i]), data->type_->get_element_type()->get_kind());
std::cout << (i == positions.size() - 1 ? "]" : ", ");
std::cout << "[";
for (size_t i = 0; i < positions.size(); ++i)
{
print_basic_element(data, data->get_array_index(positions[i]), data->type_->get_element_type()->get_kind());
std::cout << (i == positions.size() - 1 ? "]" : ", ");
}
}
std::cout << std::endl;
}

void DynamicDataHelper::print_complex_collection(
DynamicData* data,
const std::string& tabs)
{
const std::vector<uint32_t>& bounds = data->type_->descriptor_->bound_;
std::cout << std::endl;
if (data->type_->get_kind() == TK_SEQUENCE)
{
auto count = data->get_item_count();

std::vector<std::vector<uint32_t>> positions;
fill_array_positions(bounds, positions);
for (uint32_t i = 0; i < count; ++i)
{
std::cout << tabs << "[" << i << "] = ";
print_complex_element(data, i, tabs);
std::cout << std::endl;
}

for (size_t i = 0; i < positions.size(); ++i)
if (count == 0)
{
std::cout << "[]";
}
}
else
{
std::cout << tabs << "[" << i << "] = ";
print_complex_element(data, data->get_array_index(positions[i]), tabs);
std::cout << std::endl;
const std::vector<uint32_t>& bounds = data->type_->descriptor_->bound_;

std::vector<std::vector<uint32_t>> positions;
fill_array_positions(bounds, positions);

for (size_t i = 0; i < positions.size(); ++i)
{
std::cout << tabs << "[" << i << "] = ";
print_complex_element(data, data->get_array_index(positions[i]), tabs);
std::cout << std::endl;
}
}
}

Expand All @@ -287,61 +325,58 @@ void DynamicDataHelper::print_complex_element(
MemberId id,
const std::string& tabs)
{
const TypeDescriptor* desc = data->type_->get_type_descriptor();
switch(desc->get_kind())
DynamicData* st_data = data->loan_value(id);
const TypeDescriptor* desc = st_data->type_->get_type_descriptor();
switch (desc->get_kind())
{
case TK_STRUCTURE:
case TK_BITSET:
{
DynamicData* st_data = data->loan_value(id);
std::cout << "<struct/bitset>" << std::endl;
std::map<types::MemberId, types::DynamicTypeMember*> members;
data->type_->get_all_members(members);
st_data->type_->get_all_members(members);
for (auto it : members)
{
print_member(st_data, it.second, tabs + "\t");
}
data->return_loaned_value(st_data);
break;
}
case TK_UNION:
{
DynamicData* st_data = data->loan_value(id);
std::cout << "<union>" << std::endl;
DynamicTypeMember member;
data->type_->get_member(member, data->union_id_);
st_data->type_->get_member(member, st_data->union_id_);
print_member(st_data, &member, tabs + "\t");
break;
}
case TK_SEQUENCE:
case TK_ARRAY:
{
DynamicData* st_data = data->loan_value(id);
print_collection(st_data, tabs + "\t");
data->return_loaned_value(st_data);
break;
}
case TK_MAP:
{
DynamicData* st_data = data->loan_value(id);
std::cout << "<map>" << std::endl;
std::map<types::MemberId, types::DynamicTypeMember*> members;
data->type_->get_all_members(members);
size_t size = data->get_item_count();
st_data->type_->get_all_members(members);
size_t size = st_data->get_item_count();
for (size_t i = 0; i < size; ++i)
{
size_t index = i * 2;
MemberId member_id = data->get_member_id_at_index(static_cast<uint32_t>(index));
MemberId member_id = st_data->get_member_id_at_index(static_cast<uint32_t>(index));
std::cout << "Key: ";
print_member(st_data, members[member_id], tabs + "\t");
member_id = data->get_member_id_at_index(static_cast<uint32_t>(index + 1));
std::cout << "Value: ";
print_member(st_data, members[member_id], tabs + "\t");
}
data->return_loaned_value(st_data);
break;
}
default:
break;
}
std::cout << std::endl;
data->return_loaned_value(st_data);
}

void DynamicDataHelper::print_member(
Expand All @@ -351,7 +386,7 @@ void DynamicDataHelper::print_member(
{
std::cout << tabs << type->get_name() << ": ";
const MemberDescriptor* desc = type->get_descriptor();
switch(desc->get_kind())
switch (desc->get_kind())
{
case TK_NONE:
case TK_BOOLEAN:
Expand All @@ -373,12 +408,14 @@ void DynamicDataHelper::print_member(
case TK_BITMASK:
{
print_basic_element(data, type->get_id(), desc->get_kind());
std::cout << std::endl;
break;
}
case TK_STRUCTURE:
case TK_BITSET:
{
DynamicData* st_data = data->loan_value(type->get_id());
std::cout << "<struct/bitset>" << std::endl;
std::map<types::MemberId, types::DynamicTypeMember*> members;
desc->get_type()->get_all_members(members);
for (auto it : members)
Expand All @@ -390,6 +427,7 @@ void DynamicDataHelper::print_member(
}
case TK_UNION:
{
std::cout << "<union>" << std::endl;
DynamicData* st_data = data->loan_value(type->get_id());
DynamicTypeMember member;
desc->get_type()->get_member(member, data->union_id_);
Expand All @@ -406,6 +444,7 @@ void DynamicDataHelper::print_member(
}
case TK_MAP:
{
std::cout << "<map>" << std::endl;
DynamicData* st_data = data->loan_value(type->get_id());
std::map<types::MemberId, types::DynamicTypeMember*> members;
desc->get_type()->get_all_members(members);
Expand All @@ -426,5 +465,4 @@ void DynamicDataHelper::print_member(
default:
break;
}
std::cout << std::endl;
}

0 comments on commit 03fdb06

Please sign in to comment.