Skip to content

Commit f092926

Browse files
juanjo4936juanlofer-eprosima
authored andcommitted
Add modules support to idl_serialize (#5691)
* Refs 22893: First implementation of module resolution for IDLs Signed-off-by: Juanjo Garcia <[email protected]> * Refs 22893: Corrected some errors in IDL Signed-off-by: Juanjo Garcia <[email protected]> * Refs 22893: Corrected typo Signed-off-by: Juanjo Garcia <[email protected]> * Refs 22893: Complete IDL module resolution Signed-off-by: Juanjo Garcia <[email protected]> * Refs 22893: Corrected some issues Signed-off-by: Juanjo Garcia <[email protected]> * Refs 22893: Expanded funtionality to accept all types Signed-off-by: Juanjo Garcia <[email protected]> * Refs 22893: Uncrustufy Signed-off-by: Juanjo Garcia <[email protected]> * Refs 22893: Solved failing CI Signed-off-by: Juanjo Garcia <[email protected]> * Refs 22893: added test to test the new feature Signed-off-by: Juanjo Garcia <[email protected]> * Refs 22893: Refatored the test for the new feature Signed-off-by: Juanjo Garcia <[email protected]> * Refs 22893: Uncrustify Signed-off-by: Juanjo Garcia <[email protected]> * Refs 22893: corrected small typo Signed-off-by: Juanjo Garcia <[email protected]> * Refs 22893: corrected extensibility issue Signed-off-by: Juanjo Garcia <[email protected]> * Refs 22893: applied most of reviewer suggestions Signed-off-by: Juanjo Garcia <[email protected]> * Refs 22893: added explicit appendable extensibility (like the rest) Signed-off-by: Juanjo Garcia <[email protected]> * Refs 22893: uncrustify Signed-off-by: Juanjo Garcia <[email protected]> * Refs 22893: corrected typo Signed-off-by: Juanjo Garcia <[email protected]> * Refs 22893: refatoring of tabulation function Signed-off-by: Juanjo Garcia <[email protected]> * Refs 22893: corrected typo Signed-off-by: Juanjo Garcia <[email protected]> * Refs 22893: last suggestions Signed-off-by: Juan Lopez Fernandez <[email protected]> --------- Signed-off-by: Juanjo Garcia <[email protected]> Signed-off-by: Juan Lopez Fernandez <[email protected]> Co-authored-by: Juan Lopez Fernandez <[email protected]> (cherry picked from commit a020c1d)
1 parent 7d39c0a commit f092926

38 files changed

+3670
-32
lines changed

src/cpp/fastdds/xtypes/serializers/idl/dynamic_type_idl.cpp

+135-30
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ namespace dds {
3434

3535
using namespace eprosima::utilities::collections;
3636

37-
constexpr auto TYPE_OPENING = "\n{\n";
37+
constexpr auto TYPE_OPENING = "{\n";
3838
constexpr auto TYPE_CLOSURE = "};\n";
3939
constexpr auto TAB_SEPARATOR = " ";
40+
constexpr auto MODULE_SEPARATOR = "::";
4041

4142
//////////////////////////
4243
// DYNAMIC TYPE TO TREE //
@@ -667,6 +668,10 @@ ReturnCode_t alias_to_idl(
667668
return ret;
668669
}
669670

671+
// Open modules definition (if any) and get type name
672+
std::string type_name = node.info.type_kind_name;
673+
unsigned int n_modules = open_modules_definition(type_name, idl);
674+
670675
idl << "typedef ";
671676

672677
// Find the base type of the alias
@@ -678,7 +683,10 @@ ReturnCode_t alias_to_idl(
678683
return ret;
679684
}
680685

681-
idl << " " << node.info.type_kind_name << ";\n";
686+
idl << " " << type_name << ";\n";
687+
688+
// Close modules definition (if any)
689+
close_modules_definition(n_modules, idl);
682690

683691
return ret;
684692
}
@@ -706,6 +714,10 @@ ReturnCode_t bitmask_to_idl(
706714
return RETCODE_BAD_PARAMETER;
707715
}
708716

717+
// Open modules definition (if any) and get type name
718+
std::string type_name = node.info.type_kind_name;
719+
unsigned int n_modules = open_modules_definition(type_name, idl);
720+
709721
// Annotation with the bitmask size
710722
static constexpr std::uint32_t DEFAULT_BITMASK_SIZE = 32;
711723
const auto bitmask_size = bounds[0];
@@ -715,7 +727,9 @@ ReturnCode_t bitmask_to_idl(
715727
idl << "@bit_bound(" << std::to_string(bitmask_size) << ")\n";
716728
}
717729

718-
idl << "bitmask " << node.info.type_kind_name << TYPE_OPENING;
730+
idl << tabulate_n(n_modules) << "bitmask " << type_name << "\n";
731+
732+
idl << tabulate_n(n_modules) << TYPE_OPENING;
719733

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

@@ -732,7 +746,7 @@ ReturnCode_t bitmask_to_idl(
732746
return ret;
733747
}
734748

735-
idl << TAB_SEPARATOR;
749+
idl << TAB_SEPARATOR << tabulate_n(n_modules);
736750

737751
// Annotation with the position
738752
const auto id = member->get_id();
@@ -756,8 +770,11 @@ ReturnCode_t bitmask_to_idl(
756770
pos = id + 1;
757771
}
758772

759-
// Close definition
760-
idl << TYPE_CLOSURE;
773+
// Close type definition
774+
idl << tabulate_n(n_modules) << TYPE_CLOSURE;
775+
776+
// Close modules definition (if any)
777+
close_modules_definition(n_modules, idl);
761778

762779
return ret;
763780
}
@@ -770,7 +787,13 @@ ReturnCode_t bitset_to_idl(
770787

771788
ReturnCode_t ret = RETCODE_OK;
772789

773-
idl << "bitset " << node.info.type_kind_name << TYPE_OPENING;
790+
// Open modules definition (if any) and get type name
791+
std::string type_name = node.info.type_kind_name;
792+
unsigned int n_modules = open_modules_definition(type_name, idl);
793+
794+
idl << "bitset " << type_name << "\n";
795+
796+
idl << tabulate_n(n_modules) << TYPE_OPENING;
774797

775798
// Find the bits that each bitfield occupies
776799
BoundSeq bounds;
@@ -814,10 +837,10 @@ ReturnCode_t bitset_to_idl(
814837
const auto gap = id - bits_set;
815838
bits_set += gap;
816839

817-
idl << TAB_SEPARATOR << "bitfield<" << std::to_string(gap) << ">;\n";
840+
idl << tabulate_n(n_modules) << TAB_SEPARATOR << "bitfield<" << std::to_string(gap) << ">;\n";
818841
}
819842

820-
idl << TAB_SEPARATOR << "bitfield<" << std::to_string(bounds[index]);
843+
idl << tabulate_n(n_modules) << TAB_SEPARATOR << "bitfield<" << std::to_string(bounds[index]);
821844

822845
MemberDescriptor::_ref_type member_descriptor {traits<MemberDescriptor>::make_shared()};
823846
ret = member->get_descriptor(member_descriptor);
@@ -852,8 +875,11 @@ ReturnCode_t bitset_to_idl(
852875
bits_set += bounds[index];
853876
}
854877

855-
// Close definition
856-
idl << TYPE_CLOSURE;
878+
// Close type definition
879+
idl << tabulate_n(n_modules) << TYPE_CLOSURE;
880+
881+
// Close modules definition (if any)
882+
close_modules_definition(n_modules, idl);
857883

858884
return ret;
859885
}
@@ -866,7 +892,13 @@ ReturnCode_t enum_to_idl(
866892

867893
ReturnCode_t ret = RETCODE_OK;
868894

869-
idl << "enum " << node.info.type_kind_name << TYPE_OPENING << TAB_SEPARATOR;
895+
// Open modules definition (if any) and get type name
896+
std::string type_name = node.info.type_kind_name;
897+
unsigned int n_modules = open_modules_definition(type_name, idl);
898+
899+
idl << "enum " << type_name << "\n";
900+
901+
idl << tabulate_n(n_modules) << TYPE_OPENING << TAB_SEPARATOR;
870902

871903
for (std::uint32_t index = 0; index < node.info.dynamic_type->get_member_count(); index++)
872904
{
@@ -879,16 +911,23 @@ ReturnCode_t enum_to_idl(
879911
return ret;
880912
}
881913

882-
if (0 != index)
914+
idl << tabulate_n(n_modules) << member->get_name().to_string();
915+
916+
if (node.info.dynamic_type->get_member_count() - 1 != index)
883917
{
884918
idl << ",\n" << TAB_SEPARATOR;
885919
}
886-
887-
idl << member->get_name().to_string();
920+
else
921+
{
922+
idl << "\n";
923+
}
888924
}
889925

890-
// Close definition
891-
idl << "\n" << TYPE_CLOSURE;
926+
// Close type definition
927+
idl << tabulate_n(n_modules) << TYPE_CLOSURE;
928+
929+
// Close modules definition (if any)
930+
close_modules_definition(n_modules, idl);
892931

893932
return ret;
894933
}
@@ -911,6 +950,10 @@ ReturnCode_t struct_to_idl(
911950
return ret;
912951
}
913952

953+
// Open modules definition (if any) and get type name
954+
std::string type_name = node.info.type_kind_name;
955+
unsigned int n_modules = open_modules_definition(type_name, idl);
956+
914957
switch (type_descriptor->extensibility_kind())
915958
{
916959
case ExtensibilityKind::FINAL:
@@ -925,7 +968,7 @@ ReturnCode_t struct_to_idl(
925968
}
926969
case ExtensibilityKind::APPENDABLE:
927970
{
928-
// Appendable is the default extensibility kind
971+
idl << "@extensibility(APPENDABLE)\n";
929972
break;
930973
}
931974
default:
@@ -935,8 +978,8 @@ ReturnCode_t struct_to_idl(
935978
}
936979
}
937980

938-
// Add types name
939-
idl << "struct " << node.info.type_kind_name;
981+
// Add type name
982+
idl << tabulate_n(n_modules) << "struct " << type_name;
940983

941984
const auto base_type = type_descriptor->base_type();
942985

@@ -955,11 +998,13 @@ ReturnCode_t struct_to_idl(
955998
}
956999
}
9571000

958-
idl << TYPE_OPENING;
1001+
idl << "\n" << tabulate_n(n_modules) << TYPE_OPENING;
9591002

9601003
// Add struct attributes
9611004
for (auto const& child : node.branches())
9621005
{
1006+
idl << tabulate_n(n_modules);
1007+
9631008
if (child.info.is_base)
9641009
{
9651010
continue;
@@ -970,8 +1015,11 @@ ReturnCode_t struct_to_idl(
9701015
idl << ";\n";
9711016
}
9721017

973-
// Close definition
974-
idl << TYPE_CLOSURE;
1018+
// Close type definition
1019+
idl << tabulate_n(n_modules) << TYPE_CLOSURE;
1020+
1021+
// Close modules definition (if any)
1022+
close_modules_definition(n_modules, idl);
9751023

9761024
return ret;
9771025
}
@@ -993,7 +1041,11 @@ ReturnCode_t union_to_idl(
9931041
return ret;
9941042
}
9951043

996-
idl << "union " << node.info.type_kind_name << " switch (";
1044+
// Open modules definition (if any) and get type name
1045+
std::string type_name = node.info.type_kind_name;
1046+
unsigned int n_modules = open_modules_definition(type_name, idl);
1047+
1048+
idl << "union " << type_name << " switch (";
9971049

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

@@ -1002,7 +1054,7 @@ ReturnCode_t union_to_idl(
10021054
return ret;
10031055
}
10041056

1005-
idl << ")" << TYPE_OPENING;
1057+
idl << ")\n" << tabulate_n(n_modules) << TYPE_OPENING;
10061058

10071059
for (std::uint32_t index = 1; index < node.info.dynamic_type->get_member_count(); index++)
10081060
{
@@ -1022,15 +1074,15 @@ ReturnCode_t union_to_idl(
10221074

10231075
for (const auto& label : labels)
10241076
{
1025-
idl << TAB_SEPARATOR << "case " << std::to_string(label) << ":\n";
1077+
idl << tabulate_n(n_modules) << TAB_SEPARATOR << "case " << std::to_string(label) << ":\n";
10261078
}
10271079

10281080
if (member_descriptor->is_default_label())
10291081
{
1030-
idl << TAB_SEPARATOR << "default:\n";
1082+
idl << tabulate_n(n_modules) << TAB_SEPARATOR << "default:\n";
10311083
}
10321084

1033-
idl << TAB_SEPARATOR << TAB_SEPARATOR;
1085+
idl << TAB_SEPARATOR << TAB_SEPARATOR << tabulate_n(n_modules);
10341086

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

@@ -1042,8 +1094,11 @@ ReturnCode_t union_to_idl(
10421094
idl << " " << member->get_name().to_string() << ";\n";
10431095
}
10441096

1045-
// Close definition
1046-
idl << TYPE_CLOSURE;
1097+
// Close type definition
1098+
idl << tabulate_n(n_modules) << TYPE_CLOSURE;
1099+
1100+
// Close modules definition (if any)
1101+
close_modules_definition(n_modules, idl);
10471102

10481103
return ret;
10491104
}
@@ -1082,6 +1137,56 @@ ReturnCode_t node_to_idl(
10821137
return RETCODE_OK;
10831138
}
10841139

1140+
unsigned int open_modules_definition(
1141+
std::string& type_name,
1142+
std::ostream& idl) noexcept
1143+
{
1144+
unsigned int n_modules = 0;
1145+
1146+
while (type_name.find(MODULE_SEPARATOR) != std::string::npos)
1147+
{
1148+
size_t pos_start = 0;
1149+
size_t pos_end = type_name.find(MODULE_SEPARATOR);
1150+
1151+
std::string module_name = type_name.substr(0, pos_end);
1152+
type_name.erase(pos_start, pos_end - pos_start + std::strlen(MODULE_SEPARATOR));
1153+
1154+
idl << tabulate_n(n_modules) << "module " << module_name << "\n";
1155+
1156+
idl << tabulate_n(n_modules) << TYPE_OPENING;
1157+
1158+
n_modules++;
1159+
}
1160+
1161+
idl << tabulate_n(n_modules);
1162+
1163+
return n_modules;
1164+
}
1165+
1166+
void close_modules_definition(
1167+
unsigned int& n_modules,
1168+
std::ostream& idl) noexcept
1169+
{
1170+
while (n_modules > 0)
1171+
{
1172+
idl << tabulate_n(--n_modules) << TYPE_CLOSURE;
1173+
}
1174+
}
1175+
1176+
std::string tabulate_n(
1177+
const unsigned int& n_tabs) noexcept
1178+
{
1179+
std::string tabs;
1180+
1181+
tabs.reserve(std::strlen(TAB_SEPARATOR) * n_tabs);
1182+
for (unsigned int i = 0; i < n_tabs; i++)
1183+
{
1184+
tabs += TAB_SEPARATOR;
1185+
}
1186+
1187+
return tabs;
1188+
}
1189+
10851190
///////////////////////
10861191
// AUXILIARY METHODS //
10871192
///////////////////////

src/cpp/fastdds/xtypes/serializers/idl/dynamic_type_idl.hpp

+32
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,38 @@ ReturnCode_t node_to_idl(
219219
const utilities::collections::TreeNode<TreeNodeType>& node,
220220
std::ostream& idl) noexcept;
221221

222+
/**
223+
* @brief Resolves and inserts the opening modules definition of a type (if any), and removes them from \c type_name .
224+
*
225+
* @param type_name The full name of the type, including modules (if defined).
226+
* @param idl The idl representation of the tree.
227+
*
228+
* @return The number of modules resolved.
229+
*/
230+
unsigned int open_modules_definition(
231+
std::string& type_name,
232+
std::ostream& idl) noexcept;
233+
234+
/**
235+
* @brief Closes the modules definition for the processed type.
236+
*
237+
* @param n_modules The number of modules that need to be closed.
238+
* @param idl The idl representation of the tree.
239+
*/
240+
void close_modules_definition(
241+
unsigned int& n_modules,
242+
std::ostream& idl) noexcept;
243+
244+
/**
245+
* @brief Returns a string with \c n_tabs concatenated tabs.
246+
*
247+
* @param n_tabs The number of tabs to return.
248+
*
249+
* @return A string with \c n_tabs concatenated tabs.
250+
*/
251+
std::string tabulate_n(
252+
const unsigned int& n_tabs) noexcept;
253+
222254
///////////////////////
223255
// AUXILIARY METHODS //
224256
///////////////////////

test/unittest/dds/xtypes/serializers/idl/DynTypeIDLTests.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ class DynTypeIDLTests : public ::testing::TestWithParam<std::string>
4545
{
4646
// Find TypeObjects for the type
4747
xtypes::TypeObjectPair type_objs;
48-
ASSERT_EQ(DomainParticipantFactory::get_instance()->type_object_registry().get_type_objects(
49-
type_name, type_objs),
48+
ASSERT_EQ(DomainParticipantFactory::get_instance()->type_object_registry().get_type_objects(type_name,
49+
type_objs),
5050
fastdds::dds::RETCODE_OK);
5151

5252
// Create DynamicType from TypeObject

test/unittest/dds/xtypes/serializers/idl/types/alias_struct/alias_struct.idl

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ typedef boolean MyBoolean;
88

99
typedef MyBoolean MyRecursiveBoolean;
1010

11+
@extensibility(APPENDABLE)
1112
struct AliasStruct
1213
{
1314
MyLong my_long;

0 commit comments

Comments
 (0)