Skip to content

[22893] Add modules support to idl_serialize #5691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 166 additions & 13 deletions src/cpp/fastdds/xtypes/serializers/idl/dynamic_type_idl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ namespace dds {

using namespace eprosima::utilities::collections;

constexpr auto TYPE_OPENING = "\n{\n";
constexpr auto TYPE_OPENING = "{\n";
constexpr auto TYPE_CLOSURE = "};\n";
constexpr auto TAB_SEPARATOR = " ";
constexpr auto MODULE_SEPARATOR = "::";

//////////////////////////
// DYNAMIC TYPE TO TREE //
Expand Down Expand Up @@ -667,6 +668,10 @@ ReturnCode_t alias_to_idl(
return ret;
}

// Add types name and resolve module structure
std::string type_name = node.info.type_kind_name;
unsigned int n_modules = resolve_module_structure(type_name, idl);

idl << "typedef ";

// Find the base type of the alias
Expand All @@ -678,7 +683,16 @@ ReturnCode_t alias_to_idl(
return ret;
}

idl << " " << node.info.type_kind_name << ";\n";
idl << " " << type_name << ";\n";

while (n_modules > 0)
{
tabulate_n(n_modules - 1, idl);

idl << TYPE_CLOSURE;

n_modules--;
}

return ret;
}
Expand Down Expand Up @@ -706,6 +720,10 @@ ReturnCode_t bitmask_to_idl(
return RETCODE_BAD_PARAMETER;
}

// Add types name and resolve module structure
std::string type_name = node.info.type_kind_name;
unsigned int n_modules = resolve_module_structure(type_name, idl);

// Annotation with the bitmask size
static constexpr std::uint32_t DEFAULT_BITMASK_SIZE = 32;
const auto bitmask_size = bounds[0];
Expand All @@ -715,7 +733,13 @@ ReturnCode_t bitmask_to_idl(
idl << "@bit_bound(" << std::to_string(bitmask_size) << ")\n";
}

idl << "bitmask " << node.info.type_kind_name << TYPE_OPENING;
tabulate_n(n_modules, idl);

idl << "bitmask " << type_name << "\n";

tabulate_n(n_modules, idl);

idl << TYPE_OPENING;

const auto member_count = node.info.dynamic_type->get_member_count();

Expand All @@ -734,6 +758,8 @@ ReturnCode_t bitmask_to_idl(

idl << TAB_SEPARATOR;

tabulate_n(n_modules, idl);

// Annotation with the position
const auto id = member->get_id();

Expand All @@ -757,6 +783,9 @@ ReturnCode_t bitmask_to_idl(
}

// Close definition

ret = close_module_structure(n_modules, idl);

idl << TYPE_CLOSURE;

return ret;
Expand All @@ -770,7 +799,15 @@ ReturnCode_t bitset_to_idl(

ReturnCode_t ret = RETCODE_OK;

idl << "bitset " << node.info.type_kind_name << TYPE_OPENING;
// Add types name and resolve module structure
std::string type_name = node.info.type_kind_name;
unsigned int n_modules = resolve_module_structure(type_name, idl);

idl << "bitset " << type_name << "\n";

tabulate_n(n_modules, idl);

idl << TYPE_OPENING;

// Find the bits that each bitfield occupies
BoundSeq bounds;
Expand Down Expand Up @@ -814,9 +851,13 @@ ReturnCode_t bitset_to_idl(
const auto gap = id - bits_set;
bits_set += gap;

tabulate_n(n_modules, idl);

idl << TAB_SEPARATOR << "bitfield<" << std::to_string(gap) << ">;\n";
}

tabulate_n(n_modules, idl);

idl << TAB_SEPARATOR << "bitfield<" << std::to_string(bounds[index]);

MemberDescriptor::_ref_type member_descriptor {traits<MemberDescriptor>::make_shared()};
Expand Down Expand Up @@ -853,6 +894,9 @@ ReturnCode_t bitset_to_idl(
}

// Close definition

ret = close_module_structure(n_modules, idl);

idl << TYPE_CLOSURE;

return ret;
Expand All @@ -866,7 +910,15 @@ ReturnCode_t enum_to_idl(

ReturnCode_t ret = RETCODE_OK;

idl << "enum " << node.info.type_kind_name << TYPE_OPENING << TAB_SEPARATOR;
// Add types name and resolve module structure
std::string type_name = node.info.type_kind_name;
unsigned int n_modules = resolve_module_structure(type_name, idl);

idl << "enum " << type_name << "\n";

tabulate_n(n_modules, idl);

idl << TYPE_OPENING << TAB_SEPARATOR;

for (std::uint32_t index = 0; index < node.info.dynamic_type->get_member_count(); index++)
{
Expand All @@ -879,16 +931,25 @@ ReturnCode_t enum_to_idl(
return ret;
}

if (0 != index)
tabulate_n(n_modules, idl);

idl << member->get_name().to_string();

if (node.info.dynamic_type->get_member_count() - 1 != index)
{
idl << ",\n" << TAB_SEPARATOR;
}

idl << member->get_name().to_string();
else
{
idl << "\n";
}
}

// Close definition
idl << "\n" << TYPE_CLOSURE;

ret = close_module_structure(n_modules, idl);

idl << TYPE_CLOSURE;

return ret;
}
Expand All @@ -911,32 +972,42 @@ ReturnCode_t struct_to_idl(
return ret;
}

// Add types name and resolve module structure
std::string type_name = node.info.type_kind_name;
unsigned int n_modules = resolve_module_structure(type_name, idl);

switch (type_descriptor->extensibility_kind())
{
case ExtensibilityKind::FINAL:
{
idl << "@extensibility(FINAL)\n";

break;
}
case ExtensibilityKind::MUTABLE:
{
idl << "@extensibility(MUTABLE)\n";

break;
}
case ExtensibilityKind::APPENDABLE:
{
// Appendable is the default extensibility kind
idl << "@extensibility(APPENDABLE)\n";

break;
}
default:
{
EPROSIMA_LOG_ERROR(DYNAMIC_TYPE_IDL, "Extensibility kind not supported.");
return RETCODE_BAD_PARAMETER;
}

}

// Add types name
idl << "struct " << node.info.type_kind_name;
tabulate_n(n_modules, idl);

idl << "struct " << type_name;

const auto base_type = type_descriptor->base_type();

Expand All @@ -955,11 +1026,17 @@ ReturnCode_t struct_to_idl(
}
}

idl << "\n";

tabulate_n(n_modules, idl);

idl << TYPE_OPENING;

// Add struct attributes
for (auto const& child : node.branches())
{
tabulate_n(n_modules, idl);

if (child.info.is_base)
{
continue;
Expand All @@ -971,6 +1048,9 @@ ReturnCode_t struct_to_idl(
}

// Close definition

ret = close_module_structure(n_modules, idl);

idl << TYPE_CLOSURE;

return ret;
Expand All @@ -993,7 +1073,11 @@ ReturnCode_t union_to_idl(
return ret;
}

idl << "union " << node.info.type_kind_name << " switch (";
// Add types name and resolve module structure
std::string type_name = node.info.type_kind_name;
unsigned int n_modules = resolve_module_structure(type_name, idl);

idl << "union " << type_name << " switch (";

ret = type_kind_to_idl(type_descriptor->discriminator_type(), idl);

Expand All @@ -1002,7 +1086,11 @@ ReturnCode_t union_to_idl(
return ret;
}

idl << ")" << TYPE_OPENING;
idl << ")\n";

tabulate_n(n_modules, idl);

idl << TYPE_OPENING;

for (std::uint32_t index = 1; index < node.info.dynamic_type->get_member_count(); index++)
{
Expand All @@ -1022,16 +1110,22 @@ ReturnCode_t union_to_idl(

for (const auto& label : labels)
{
tabulate_n(n_modules, idl);

idl << TAB_SEPARATOR << "case " << std::to_string(label) << ":\n";
}

if (member_descriptor->is_default_label())
{
tabulate_n(n_modules, idl);

idl << TAB_SEPARATOR << "default:\n";
}

idl << TAB_SEPARATOR << TAB_SEPARATOR;

tabulate_n(n_modules, idl);

ret = type_kind_to_idl(member_descriptor->type(), idl);

if (RETCODE_OK != ret)
Expand All @@ -1043,6 +1137,9 @@ ReturnCode_t union_to_idl(
}

// Close definition

ret = close_module_structure(n_modules, idl);

idl << TYPE_CLOSURE;

return ret;
Expand Down Expand Up @@ -1082,6 +1179,62 @@ ReturnCode_t node_to_idl(
return RETCODE_OK;
}

unsigned int resolve_module_structure(
std::string& type_name,
std::ostream& idl) noexcept
{
unsigned int n_modules = 0;

while (type_name.find(MODULE_SEPARATOR) != std::string::npos)
{
size_t pos_start = 0;
size_t pos_end = type_name.find(MODULE_SEPARATOR);

std::string module_name = type_name.substr(0, pos_end);
type_name.erase(pos_start, pos_end - pos_start + std::strlen(MODULE_SEPARATOR));

tabulate_n(n_modules, idl);

idl << "module " << module_name << "\n";

tabulate_n(n_modules, idl);

idl << TYPE_OPENING;

n_modules++;
}

tabulate_n(n_modules, idl);

return n_modules;
}

ReturnCode_t close_module_structure(
unsigned int& n_modules,
std::ostream& idl) noexcept
{
while (n_modules > 0)
{
tabulate_n(n_modules, idl);

idl << TYPE_CLOSURE;

n_modules--;
}

return RETCODE_OK;
}

void tabulate_n(
const unsigned int& n_modules,
std::ostream& idl) noexcept
{
for (unsigned int i = 0; i < n_modules; i++)
{
idl << TAB_SEPARATOR;
}
}

///////////////////////
// AUXILIARY METHODS //
///////////////////////
Expand Down
Loading
Loading