Skip to content

Commit

Permalink
Feature: topic keys (#3)
Browse files Browse the repository at this point in the history
* Refs #20156: Add is_key_ field to identify member as key

Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>

* Refs #20156: Add has_any_member_with_annotation() method for a struct

Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>

* Refs #20310: Define v2 ABI identifier

Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>

* Refs #20310: Update Message introspection with a new static key_members_array in the parent messagemembers

Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>

* Refs #20310: Review suggestions

Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>

* Refs #20310: NITs

Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>

---------

Signed-off-by: Mario Dominguez <mariodominguez@eprosima.com>
  • Loading branch information
Mario-DL authored Mar 6, 2024
1 parent 995917e commit 28a73d3
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 4 deletions.
9 changes: 9 additions & 0 deletions rosidl_parser/rosidl_parser/definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,15 @@ def __init__(self, namespaced_type: NamespacedType, members=None):
self.namespaced_type = namespaced_type
self.members = members or []

def has_any_member_with_annotation(self, name: str):
"""
Returns whether any member has a particular annotation.
:param name: the annotation name
"""
has_any = [member.name for member in self.members if member.has_annotation(name)]
return bool(has_any)


class Include:
"""An include statement."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ extern "C"
ROSIDL_TYPESUPPORT_INTROSPECTION_C_PUBLIC
extern const char * rosidl_typesupport_introspection_c__identifier;

/// String identifying the typesupport introspection implementation in use.
ROSIDL_TYPESUPPORT_INTROSPECTION_C_PUBLIC
extern const char * rosidl_typesupport_introspection_c__identifier_v2;

#ifdef __cplusplus
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ typedef struct rosidl_typesupport_introspection_c__MessageMembers_s
void (* init_function)(void *, enum rosidl_runtime_c__message_initialization);
/// The function used to clean up the interface's in-memory representation
void (* fini_function)(void *);
/// A pointer to the array that indicates whether each field is annotated as @key
const bool * key_members_array_;
} rosidl_typesupport_introspection_c__MessageMembers;

#endif // ROSIDL_TYPESUPPORT_INTROSPECTION_C__MESSAGE_INTROSPECTION_H_
22 changes: 20 additions & 2 deletions rosidl_typesupport_introspection_c/resource/msg__type_support.c.em
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,23 @@ bool @(function_prefix)__resize_function__@(message.structure.namespaced_type.na
@[ end if]@
@[ end if]@
@[end for]@

static bool @(function_prefix)__@(message.structure.namespaced_type.name)_key_members_array[@(len(message.structure.members))] = {
@{
for index, member in enumerate(message.structure.members):
if member.has_annotation('key'):
if index < len(message.structure.members) - 1:
print(' true,')
else:
print(' true')
else:
if index < len(message.structure.members) - 1:
print(' false,')
else:
print(' false')
}@
};

static rosidl_typesupport_introspection_c__MessageMember @(function_prefix)__@(message.structure.namespaced_type.name)_message_member_array[@(len(message.structure.members))] = {
@{
for index, member in enumerate(message.structure.members):
Expand Down Expand Up @@ -278,7 +295,8 @@ static const rosidl_typesupport_introspection_c__MessageMembers @(function_prefi
sizeof(@('__'.join([package_name] + list(interface_path.parents[0].parts) + [message.structure.namespaced_type.name]))),
@(function_prefix)__@(message.structure.namespaced_type.name)_message_member_array, // message members
@(function_prefix)__@(message.structure.namespaced_type.name)_init_function, // function to initialize message memory (memory has to be allocated)
@(function_prefix)__@(message.structure.namespaced_type.name)_fini_function // function to terminate message instance (will not free memory)
@(function_prefix)__@(message.structure.namespaced_type.name)_fini_function, // function to terminate message instance (will not free memory)
@(function_prefix)__@(message.structure.namespaced_type.name)_key_members_array // mapping to each field to know whether it is keyed or not
};

// this is not const since it must be initialized on first access
Expand Down Expand Up @@ -308,7 +326,7 @@ if isinstance(type_, AbstractNestedType):
@[end for]@
if (!@(function_prefix)__@(message.structure.namespaced_type.name)_message_type_support_handle.typesupport_identifier) {
@(function_prefix)__@(message.structure.namespaced_type.name)_message_type_support_handle.typesupport_identifier =
rosidl_typesupport_introspection_c__identifier;
rosidl_typesupport_introspection_c__identifier_v2;
}
return &@(function_prefix)__@(message.structure.namespaced_type.name)_message_type_support_handle;
}
Expand Down
1 change: 1 addition & 0 deletions rosidl_typesupport_introspection_c/src/identifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
#include "rosidl_typesupport_introspection_c/identifier.h"

const char * rosidl_typesupport_introspection_c__identifier = "rosidl_typesupport_introspection_c";
const char * rosidl_typesupport_introspection_c__identifier_v2 = "rosidl_typesupport_introspection_c_v2";
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ namespace rosidl_typesupport_introspection_cpp
ROSIDL_TYPESUPPORT_INTROSPECTION_CPP_IMPORT
extern const char * typesupport_identifier;

/// String identifying the typesupport introspection implementation in use.
ROSIDL_TYPESUPPORT_INTROSPECTION_CPP_IMPORT
extern const char * typesupport_identifier_v2;

} // namespace rosidl_typesupport_introspection_cpp

#endif // ROSIDL_TYPESUPPORT_INTROSPECTION_CPP__IDENTIFIER_HPP_
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ typedef struct ROSIDL_TYPESUPPORT_INTROSPECTION_CPP_PUBLIC MessageMembers_s
void (* init_function)(void *, rosidl_runtime_cpp::MessageInitialization);
/// The function used to clean up the interface's in-memory representation
void (* fini_function)(void *);
/// A pointer to the array that indicates whether each field is annotated as @key
const bool * key_members_array_;
} MessageMembers;

} // namespace rosidl_typesupport_introspection_cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,23 @@ void resize_function__@(message.structure.namespaced_type.name)__@(member.name)(
@[ end if]@
@[ end if]@
@[end for]@

static const bool @(message.structure.namespaced_type.name)_key_members_array[@(len(message.structure.members))] = {
@{
for index, member in enumerate(message.structure.members):
if member.has_annotation('key'):
if index < len(message.structure.members) - 1:
print(' true,')
else:
print(' true')
else:
if index < len(message.structure.members) - 1:
print(' false,')
else:
print(' false')
}@
};

static const ::rosidl_typesupport_introspection_cpp::MessageMember @(message.structure.namespaced_type.name)_message_member_array[@(len(message.structure.members))] = {
@{
for index, member in enumerate(message.structure.members):
Expand Down Expand Up @@ -244,11 +261,12 @@ static const ::rosidl_typesupport_introspection_cpp::MessageMembers @(message.st
sizeof(@('::'.join([package_name] + list(interface_path.parents[0].parts) + [message.structure.namespaced_type.name]))),
@(message.structure.namespaced_type.name)_message_member_array, // message members
@(message.structure.namespaced_type.name)_init_function, // function to initialize message memory (memory has to be allocated)
@(message.structure.namespaced_type.name)_fini_function // function to terminate message instance (will not free memory)
@(message.structure.namespaced_type.name)_fini_function, // function to terminate message instance (will not free memory)
@(message.structure.namespaced_type.name)_key_members_array // mapping to each field to know whether it is keyed or not
};

static const rosidl_message_type_support_t @(message.structure.namespaced_type.name)_message_type_support_handle = {
::rosidl_typesupport_introspection_cpp::typesupport_identifier,
::rosidl_typesupport_introspection_cpp::typesupport_identifier_v2,
&@(message.structure.namespaced_type.name)_message_members,
get_message_typesupport_handle_function,
&@(idl_structure_type_to_c_typename(message.structure.namespaced_type))__@(GET_HASH_FUNC),
Expand Down
4 changes: 4 additions & 0 deletions rosidl_typesupport_introspection_cpp/src/identifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ namespace rosidl_typesupport_introspection_cpp
ROSIDL_TYPESUPPORT_INTROSPECTION_CPP_EXPORT
const char * typesupport_identifier = "rosidl_typesupport_introspection_cpp";


ROSIDL_TYPESUPPORT_INTROSPECTION_CPP_EXPORT
const char * typesupport_identifier_v2 = "rosidl_typesupport_introspection_cpp_v2";

} // namespace rosidl_typesupport_introspection_cpp

0 comments on commit 28a73d3

Please sign in to comment.