Skip to content

Commit 9f149e3

Browse files
Document how to iterate over declared types processed with IDL Parser (#1105)
Signed-off-by: Carlosespicur <carlosespicur@proton.me>
1 parent f1f0c6f commit 9f149e3

File tree

3 files changed

+74
-9
lines changed

3 files changed

+74
-9
lines changed

code/DDSCodeTester.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2117,7 +2117,7 @@ void dds_topic_examples()
21172117
}
21182118

21192119
{
2120-
//DDS_DYNAMIC_TYPES_IDL_PARSING
2120+
//DDS_DYNAMIC_TYPES_IDL_PARSING_CREATE_TYPE
21212121
// Create a DomainParticipant in the desired domain
21222122
DomainParticipant* participant =
21232123
DomainParticipantFactory::get_instance()->create_participant(0, PARTICIPANT_QOS_DEFAULT);
@@ -2154,6 +2154,36 @@ void dds_topic_examples()
21542154
}
21552155
//!--
21562156
}
2157+
2158+
{
2159+
//DDS_DYNAMIC_TYPES_IDL_PARSING_ITERATE_OVER_TYPES
2160+
// Optional: Set the preprocessor to be executed before parsing the IDL
2161+
DynamicTypeBuilderFactory::get_instance()->set_preprocessor("<optional_path_to_your_preprocessor_executable>");
2162+
2163+
// Load the IDL file
2164+
std::string idl_file = "<path_to_idl>.idl";
2165+
std::vector<std::string> include_paths;
2166+
include_paths.push_back("<path/to/folder/containing/included/idl/files>");
2167+
2168+
// Vector to store the names of the parsed types
2169+
std::vector<std::string> parsed_types;
2170+
2171+
// Define the function callback to be executed for each aggregated type parsed in the IDL file
2172+
auto callback = [&parsed_types](traits<DynamicTypeBuilder>::ref_type builder)
2173+
{
2174+
if (builder)
2175+
{
2176+
parsed_types.emplace_back(builder->get_name().to_string());
2177+
}
2178+
2179+
return true; // continue parsing
2180+
};
2181+
2182+
// Store the names of the parsed types in the vector
2183+
DynamicTypeBuilderFactory::get_instance()->for_each_type_w_uri(idl_file, include_paths, callback);
2184+
2185+
//!--
2186+
}
21572187
}
21582188

21592189
void dds_content_filtered_topic_examples()

docs/03-exports/aliases-api.include

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,7 @@
10801080
.. |DynamicTypeBuilderFactory::create_type_w_uri| replace:: :cpp:func:`DynamicTypeBuilderFactory::create_type_w_uri <eprosima::fastdds::dds::DynamicTypeBuilderFactory::create_type_w_uri>`
10811081
.. |DynamicTypeBuilderFactory::create_type| replace:: :cpp:func:`DynamicTypeBuilderFactory::create_type <eprosima::fastdds::dds::DynamicTypeBuilderFactory::create_type>`
10821082
.. |DynamicTypeBuilderFactory::create_wstring_type| replace:: :cpp:func:`DynamicTypeBuilderFactory::create_wstring_type <eprosima::fastdds::dds::DynamicTypeBuilderFactory::create_wstring_type>`
1083+
.. |DynamicTypeBuilderFactory::for_each_type_w_uri| replace:: :cpp:func:`DynamicTypeBuilderFactory::for_each_type_w_uri <eprosima::fastdds::dds::DynamicTypeBuilderFactory::for_each_type_w_uri>`
10831084
.. |DynamicTypeBuilderFactory::get_primitive_type| replace:: :cpp:func:`DynamicTypeBuilderFactory::get_primitive_type <eprosima::fastdds::dds::DynamicTypeBuilderFactory::get_primitive_type>`
10841085
.. |DynamicTypeBuilderFactory::set_preprocessor| replace:: :cpp:func:`DynamicTypeBuilderFactory::set_preprocessor <eprosima::fastdds::dds::DynamicTypeBuilderFactory::set_preprocessor>`
10851086

docs/fastdds/xtypes/idl_parsing.rst

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@ Dynamic Types IDL Parsing
99

1010
*Fast DDS* supports the implementation of |DynamicTypes| for parsing IDL files at runtime to generate dynamic
1111
data types.
12-
The |DynamicTypeBuilderFactory::create_type_w_uri| API allows users to provide a URI pointing to an IDL file.
13-
Fast DDS will process the file and return the corresponding DynamicTypeBuilder, which can then be used to create a
14-
DynamicType.
1512

1613
This feature enables applications to dynamically load type definitions from IDL files instead of relying solely on
1714
pre-generated types or XML profiles.
1815
This enhances flexibility, as new data types can be introduced without modifying and recompiling the application.
1916

20-
Type definition
21-
^^^^^^^^^^^^^^^
17+
Supported Types
18+
---------------
19+
2220
Below, the types supported by *eProsima Fast DDS* IDL parsing are presented.
2321
For further information about the supported |DynamicTypes|, please, refer to :ref:`xtypes_supportedtypes`.
2422

@@ -44,8 +42,8 @@ The following types are currently not supported by the IDL parsing feature:
4442
* Inheritance
4543
* Member ID
4644

47-
Example
48-
^^^^^^^
45+
Create a Dynamic Type from a IDL file
46+
-------------------------------------
4947

5048
*Fast DDS* application can use dynamic types generated in runtime just by loading the corresponding IDL files into the
5149
|DynamicTypeBuilderFactory-api| using |DynamicTypeBuilderFactory::create_type_w_uri|.
@@ -61,10 +59,46 @@ data.
6159

6260
The preprocessor can be manually selected using |DynamicTypeBuilderFactory::set_preprocessor|
6361

62+
Example
63+
^^^^^^^
64+
6465
The following snippet shows the previously explained steps:
6566

6667
.. literalinclude:: /../code/DDSCodeTester.cpp
6768
:language: c++
6869
:dedent: 8
69-
:start-after: //DDS_DYNAMIC_TYPES_IDL_PARSING
70+
:start-after: //DDS_DYNAMIC_TYPES_IDL_PARSING_CREATE_TYPE
71+
:end-before: //!--
72+
73+
74+
Iterate over the parsed IDL types
75+
---------------------------------
76+
77+
*Fast DDS* application supports a simple way to iterate over each aggregated type declared and parsed in the IDL file,
78+
*i.e*:
79+
80+
* :ref:`xtypes_supportedtypes_union`
81+
* :ref:`xtypes_supportedtypes_enumeration`
82+
* :ref:`xtypes_supportedtypes_structure`
83+
* :ref:`xtypes_supportedtypes_alias`
84+
85+
using |DynamicTypeBuilderFactory::for_each_type_w_uri| method.
86+
User can customize the runtime behavior by providing a lambda expression, which will be triggered
87+
when a new aggregated type is parsed.
88+
89+
The callback provided by the user receives the |DynamicTypeBuilder-api| instance related to the parsed type
90+
and returns a boolean value, indicating whether the iteration should continue or not.
91+
In case of returning false for a given type,
92+
the parsing process will stop and no further types will be processed.
93+
94+
Example
95+
^^^^^^^
96+
97+
The following snippet shows how to iterate over the aggregated types declared in the IDL file,
98+
storing their names in a vector:
99+
100+
.. literalinclude:: /../code/DDSCodeTester.cpp
101+
:language: c++
102+
:dedent: 8
103+
:start-after: //DDS_DYNAMIC_TYPES_IDL_PARSING_ITERATE_OVER_TYPES
70104
:end-before: //!--

0 commit comments

Comments
 (0)