From 25cd7c222d9555af6042350df5e5b7e62eee1cfd Mon Sep 17 00:00:00 2001 From: Mario Dominguez Date: Tue, 19 Dec 2023 11:40:57 +0000 Subject: [PATCH 1/2] Refs #20156: Add is_key_ field to identify member as key Signed-off-by: Mario Dominguez --- .../rosidl_typesupport_introspection_c/message_introspection.h | 2 ++ .../resource/msg__type_support.c.em | 2 ++ .../message_introspection.hpp | 2 ++ .../resource/msg__type_support.cpp.em | 2 ++ 4 files changed, 8 insertions(+) diff --git a/rosidl_typesupport_introspection_c/include/rosidl_typesupport_introspection_c/message_introspection.h b/rosidl_typesupport_introspection_c/include/rosidl_typesupport_introspection_c/message_introspection.h index 0d16e5923..646c11751 100644 --- a/rosidl_typesupport_introspection_c/include/rosidl_typesupport_introspection_c/message_introspection.h +++ b/rosidl_typesupport_introspection_c/include/rosidl_typesupport_introspection_c/message_introspection.h @@ -39,6 +39,8 @@ typedef struct rosidl_typesupport_introspection_c__MessageMember_s /// If the type_id_ value is rosidl_typesupport_introspection_c__ROS_TYPE_MESSAGE, /// this points to an array describing the fields of the sub-interface. const rosidl_message_type_support_t * members_; + /// True if this field is a keyed field, false otherwise. + bool is_key_; /// True if this field is an array type, false if it is any other type. An /// array has the same value for / type_id_. bool is_array_; diff --git a/rosidl_typesupport_introspection_c/resource/msg__type_support.c.em b/rosidl_typesupport_introspection_c/resource/msg__type_support.c.em index de392983f..faaa784cc 100644 --- a/rosidl_typesupport_introspection_c/resource/msg__type_support.c.em +++ b/rosidl_typesupport_introspection_c/resource/msg__type_support.c.em @@ -238,6 +238,8 @@ for index, member in enumerate(message.structure.members): print(' 0, // upper bound of string') # const rosidl_message_type_support_t * members_ print(' NULL, // members of sub message (initialized later)') + # bool is_key_ + print(' %s, // is key' % ('true' if member.has_annotation('key') else 'false')) # bool is_array_ print(' %s, // is array' % ('true' if isinstance(member.type, AbstractNestedType) else 'false')) # size_t array_size_ diff --git a/rosidl_typesupport_introspection_cpp/include/rosidl_typesupport_introspection_cpp/message_introspection.hpp b/rosidl_typesupport_introspection_cpp/include/rosidl_typesupport_introspection_cpp/message_introspection.hpp index 6c01c2ce8..45befff26 100644 --- a/rosidl_typesupport_introspection_cpp/include/rosidl_typesupport_introspection_cpp/message_introspection.hpp +++ b/rosidl_typesupport_introspection_cpp/include/rosidl_typesupport_introspection_cpp/message_introspection.hpp @@ -41,6 +41,8 @@ typedef struct ROSIDL_TYPESUPPORT_INTROSPECTION_CPP_PUBLIC MessageMember_s /// If the type_id_ value is rosidl_typesupport_introspection_cpp::ROS_TYPE_MESSAGE /// this points to an array describing the fields of the sub-interface. const rosidl_message_type_support_t * members_; + /// True if this field is a keyed field, false otherwise. + bool is_key_; /// True if this field is an array, false if it is a unary type. An array has the same value for /// type_id_. bool is_array_; diff --git a/rosidl_typesupport_introspection_cpp/resource/msg__type_support.cpp.em b/rosidl_typesupport_introspection_cpp/resource/msg__type_support.cpp.em index 679907386..a68a67586 100644 --- a/rosidl_typesupport_introspection_cpp/resource/msg__type_support.cpp.em +++ b/rosidl_typesupport_introspection_cpp/resource/msg__type_support.cpp.em @@ -204,6 +204,8 @@ for index, member in enumerate(message.structure.members): print(' 0, // upper bound of string') # const rosidl_message_type_support_t * members_ print(' ::rosidl_typesupport_introspection_cpp::get_message_type_support_handle<%s>(), // members of sub message' % '::'.join(type_.namespaced_name())) + # bool is_key_ + print(' %s, // is key' % ('true' if member.has_annotation('key') else 'false')) # bool is_array_ print(' %s, // is array' % ('true' if isinstance(member.type, AbstractNestedType) else 'false')) # size_t array_size_ From a8b9e2dc8fec102f3546ee059f750dfa4457910f Mon Sep 17 00:00:00 2001 From: Mario Dominguez Date: Wed, 20 Dec 2023 12:28:09 +0000 Subject: [PATCH 2/2] Refs #20156: Add has_any_member_with_annotation() method for a struct Signed-off-by: Mario Dominguez --- rosidl_parser/rosidl_parser/definition.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/rosidl_parser/rosidl_parser/definition.py b/rosidl_parser/rosidl_parser/definition.py index 689b71384..07c9ef575 100644 --- a/rosidl_parser/rosidl_parser/definition.py +++ b/rosidl_parser/rosidl_parser/definition.py @@ -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."""