Skip to content

Commit eac4201

Browse files
authored
Expose type hash on typesupports (rep2011) (#729)
* Expose type hash on typesupports * Change TYPE_VERSION_HASH to TYPE_HASH, fix variable inlining * Undo the serviceMembers idea * Re-fix hash definition indentation now that not using defines * Tweak the separator to match rcl capabilities Signed-off-by: Emerson Knapp <[email protected]>
1 parent 10d0883 commit eac4201

File tree

19 files changed

+70
-53
lines changed

19 files changed

+70
-53
lines changed

rosidl_generator_c/resource/idl__struct.h.em

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
@{
1414
from rosidl_generator_c import idl_structure_type_to_c_typename
1515
from rosidl_generator_c import type_hash_to_c_definition
16+
from rosidl_generator_type_description import TYPE_HASH_VAR
1617
from rosidl_pycommon import convert_camel_case_to_lower_case_underscore
1718
include_parts = [package_name] + list(interface_path.parents[0].parts) + [
1819
'detail', convert_camel_case_to_lower_case_underscore(interface_path.stem)]
@@ -60,10 +61,7 @@ from rosidl_parser.definition import Service
6061
}@
6162
@[for service in content.get_elements_of_type(Service)]@
6263

63-
@{ hash_var = idl_structure_type_to_c_typename(service.namespaced_type) + '__TYPE_VERSION_HASH' }@
64-
// Note: this define is for MSVC, where the static const var can't be used in downstream aggregate initializers
65-
#define @(hash_var)__INIT @(type_hash_to_c_definition(type_hash['service'], line_final_backslash=True))
66-
static const rosidl_type_hash_t @(hash_var) = @(hash_var)__INIT;
64+
static const rosidl_type_hash_t @(idl_structure_type_to_c_typename(service.namespaced_type))__@(TYPE_HASH_VAR) = @(type_hash_to_c_definition(type_hash['service']));
6765

6866
@{
6967
TEMPLATE(
@@ -99,9 +97,7 @@ from rosidl_parser.definition import Action
9997
}@
10098
@[for action in content.get_elements_of_type(Action)]@
10199

102-
@{ hash_var = idl_structure_type_to_c_typename(action.namespaced_type) + '__TYPE_VERSION_HASH' }@
103-
#define @(hash_var)__INIT @(type_hash_to_c_definition(type_hash['action'], line_final_backslash=True))
104-
static const rosidl_type_hash_t @(hash_var) = @(hash_var)__INIT;
100+
static const rosidl_type_hash_t @(idl_structure_type_to_c_typename(action.namespaced_type))__@(TYPE_HASH_VAR) = @(type_hash_to_c_definition(type_hash['action']));
105101

106102
@{
107103
TEMPLATE(
@@ -127,10 +123,7 @@ TEMPLATE(
127123
type_hash=type_hash['feedback'])
128124
}@
129125

130-
@{ hash_var = idl_structure_type_to_c_typename(action.send_goal_service.namespaced_type) + '__TYPE_VERSION_HASH' }@
131-
// Note: this define is for MSVC, where the static const var can't be used in downstream aggregate initializers
132-
#define @(hash_var)__INIT @(type_hash_to_c_definition(type_hash['send_goal_service']['service'], line_final_backslash=True))
133-
static const rosidl_type_hash_t @(hash_var) = @(hash_var)__INIT;
126+
static const rosidl_type_hash_t @(idl_structure_type_to_c_typename(action.send_goal_service.namespaced_type))__@(TYPE_HASH_VAR) = @(type_hash_to_c_definition(type_hash['send_goal_service']['service']));
134127

135128
@{
136129
TEMPLATE(
@@ -156,10 +149,7 @@ TEMPLATE(
156149
type_hash=type_hash['send_goal_service']['event_message'])
157150
}@
158151

159-
@{ hash_var = idl_structure_type_to_c_typename(action.get_result_service.namespaced_type) + '__TYPE_VERSION_HASH' }@
160-
// Note: this define is for MSVC, where the static const var can't be used in downstream aggregate initializers
161-
#define @(hash_var)__INIT @(type_hash_to_c_definition(type_hash['get_result_service']['service'], line_final_backslash=True))
162-
static const rosidl_type_hash_t @(hash_var) = @(hash_var)__INIT;
152+
static const rosidl_type_hash_t @(idl_structure_type_to_c_typename(action.get_result_service.namespaced_type))__@(TYPE_HASH_VAR) = @(type_hash_to_c_definition(type_hash['get_result_service']['service']));
163153

164154
@{
165155
TEMPLATE(

rosidl_generator_c/resource/msg__struct.h.em

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ from rosidl_generator_c import idl_structure_type_to_c_typename
2323
from rosidl_generator_c import interface_path_to_string
2424
from rosidl_generator_c import type_hash_to_c_definition
2525
from rosidl_generator_c import value_to_c
26+
from rosidl_generator_type_description import TYPE_HASH_VAR
2627
}@
2728
@#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2829
@# Collect necessary include directives for all members
@@ -63,11 +64,8 @@ for member in message.structure.members:
6364
@#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
6465

6566
@#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
66-
// Type Version Hash for interface
67-
@{ hash_var = idl_structure_type_to_c_typename(message.structure.namespaced_type) + '__TYPE_VERSION_HASH' }@
68-
// Note: this define is for MSVC, where the static const var can't be used in downstream aggregate initializers
69-
#define @(hash_var)__INIT @(type_hash_to_c_definition(type_hash['message'], line_final_backslash=True))
70-
static const rosidl_type_hash_t @(hash_var) = @(hash_var)__INIT;
67+
// Type Hash for interface
68+
static const rosidl_type_hash_t @(idl_structure_type_to_c_typename(message.structure.namespaced_type))__@(TYPE_HASH_VAR) = @(type_hash_to_c_definition(type_hash['message']));
7169

7270
// Constants defined in the message
7371
@[for constant in message.constants]@

rosidl_generator_c/resource/srv__type_support.c.em

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
@# Included from rosidl_generator_c/resource/srv__type_support.c.em
22
@{
3-
from rosidl_pycommon import convert_camel_case_to_lower_case_underscore
3+
from rosidl_parser.definition import SERVICE_EVENT_MESSAGE_SUFFIX
44
from rosidl_parser.definition import SERVICE_REQUEST_MESSAGE_SUFFIX
55
from rosidl_parser.definition import SERVICE_RESPONSE_MESSAGE_SUFFIX
6-
from rosidl_parser.definition import SERVICE_EVENT_MESSAGE_SUFFIX
6+
from rosidl_pycommon import convert_camel_case_to_lower_case_underscore
77
event_type = '__'.join([package_name, *interface_path.parents[0].parts, service.namespaced_type.name]) + SERVICE_EVENT_MESSAGE_SUFFIX
88
request_type = '__'.join([package_name, *interface_path.parents[0].parts, service.namespaced_type.name]) + SERVICE_REQUEST_MESSAGE_SUFFIX
99
response_type = '__'.join([package_name, *interface_path.parents[0].parts, service.namespaced_type.name]) + SERVICE_RESPONSE_MESSAGE_SUFFIX

rosidl_generator_c/rosidl_generator_c/__init__.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,25 +223,20 @@ def escape_wstring(s):
223223
return escape_string(s)
224224

225225

226-
def type_hash_to_c_definition(hash_string, *, line_final_backslash=False):
226+
def type_hash_to_c_definition(hash_string, *, indent=2):
227227
"""Generate empy for rosidl_type_hash_t instance with 8 bytes per line for readability."""
228228
bytes_per_row = 8
229229
rows = 4
230230
assert bytes_per_row * rows == RIHS01_HASH_VALUE_SIZE, 'This function is outdated.'
231-
indent = 4 # Uncrustify prefers this indentation
232231
version, value = parse_rihs_string(hash_string)
233232
assert version == 1, 'This function only knows how to generate RIHS01 definitions.'
234233

235234
result = f'{{{version}, {{'
236-
if line_final_backslash:
237-
result += ' \\'
238235
result += '\n'
239236
for row in range(rows):
240237
result += ' ' * (indent + 1)
241238
for i in range(row * bytes_per_row, (row + 1) * bytes_per_row):
242239
result += f' 0x{value[i * 2]}{value[i * 2 + 1]},'
243-
if line_final_backslash:
244-
result += ' \\'
245240
result += '\n'
246241
result += ' ' * indent
247242
result += '}}'

rosidl_generator_cpp/cmake/rosidl_generator_cpp_generate_interfaces.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ add_dependencies(
106106

107107
set(_target_suffix "__rosidl_generator_cpp")
108108
add_library(${rosidl_generate_interfaces_TARGET}${_target_suffix} INTERFACE)
109+
target_compile_features(${rosidl_generate_interfaces_TARGET}${_target_suffix} INTERFACE cxx_std_17)
109110
add_library(${PROJECT_NAME}::${rosidl_generate_interfaces_TARGET}${_target_suffix} ALIAS
110111
${rosidl_generate_interfaces_TARGET}${_target_suffix})
111112
target_include_directories(${rosidl_generate_interfaces_TARGET}${_target_suffix}

rosidl_generator_cpp/resource/action__struct.hpp.em

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@# Included from rosidl_generator_cpp/resource/idl__struct.hpp.em
22
@{
33
from rosidl_generator_c import type_hash_to_c_definition
4+
from rosidl_generator_type_description import TYPE_HASH_VAR
45
from rosidl_parser.definition import ACTION_FEEDBACK_MESSAGE_SUFFIX
56
from rosidl_parser.definition import ACTION_FEEDBACK_SUFFIX
67
from rosidl_parser.definition import ACTION_GOAL_SERVICE_SUFFIX
@@ -79,7 +80,7 @@ namespace @(ns)
7980
@[end for]@
8081
struct @(action.namespaced_type.name)
8182
{
82-
static constexpr const rosidl_type_hash_t TYPE_VERSION_HASH = @(type_hash_to_c_definition(type_hash['action']));
83+
static constexpr const rosidl_type_hash_t @(TYPE_HASH_VAR) = @(type_hash_to_c_definition(type_hash['action'], indent=4));
8384

8485
/// The goal message defined in the action definition.
8586
using Goal = @(action_name)@(ACTION_GOAL_SUFFIX);

rosidl_generator_cpp/resource/msg__struct.hpp.em

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ from rosidl_generator_cpp import escape_string
66
from rosidl_generator_cpp import escape_wstring
77
from rosidl_generator_cpp import msg_type_to_cpp
88
from rosidl_generator_cpp import MSG_TYPE_TO_CPP
9+
from rosidl_generator_type_description import TYPE_HASH_VAR
910
from rosidl_parser.definition import AbstractNestedType
1011
from rosidl_parser.definition import AbstractString
1112
from rosidl_parser.definition import AbstractWString
@@ -102,8 +103,7 @@ struct @(message.structure.namespaced_type.name)_
102103
{
103104
using Type = @(message.structure.namespaced_type.name)_<ContainerAllocator>;
104105

105-
// Type Version Hash for interface
106-
constexpr static const rosidl_type_hash_t TYPE_VERSION_HASH = @(type_hash_to_c_definition(type_hash['message']));
106+
constexpr static const rosidl_type_hash_t @(TYPE_HASH_VAR) = @(type_hash_to_c_definition(type_hash['message'], indent=4));
107107

108108
@{
109109
# The creation of the constructors for messages is a bit complicated. The goal
@@ -362,7 +362,7 @@ using @(message.structure.namespaced_type.name) =
362362
@(message_typename)_<std::allocator<void>>;
363363

364364
template<class ContainerAllocator>
365-
constexpr const rosidl_type_hash_t @(message.structure.namespaced_type.name)_<ContainerAllocator>::TYPE_VERSION_HASH;
365+
constexpr const rosidl_type_hash_t @(message.structure.namespaced_type.name)_<ContainerAllocator>::@(TYPE_HASH_VAR);
366366

367367
// constant definitions
368368
@[for c in message.constants]@

rosidl_generator_cpp/resource/srv__struct.hpp.em

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
@# Included from rosidl_generator_cpp/resource/idl__struct.hpp.em
22
@{
33
from rosidl_generator_c import type_hash_to_c_definition
4+
from rosidl_generator_type_description import TYPE_HASH_VAR
5+
from rosidl_parser.definition import SERVICE_EVENT_MESSAGE_SUFFIX
6+
from rosidl_parser.definition import SERVICE_REQUEST_MESSAGE_SUFFIX
7+
from rosidl_parser.definition import SERVICE_RESPONSE_MESSAGE_SUFFIX
48
}@
59
@{
610
TEMPLATE(
@@ -37,11 +41,11 @@ struct @(service.namespaced_type.name)
3741
@{
3842
service_typename = '::'.join(service.namespaced_type.namespaced_name())
3943
}@
40-
static constexpr const rosidl_type_hash_t TYPE_VERSION_HASH = @(type_hash_to_c_definition(type_hash['service']));
44+
static constexpr const rosidl_type_hash_t @(TYPE_HASH_VAR) = @(type_hash_to_c_definition(type_hash['service'], indent=4));
4145

42-
using Request = @(service_typename)_Request;
43-
using Response = @(service_typename)_Response;
44-
using Event = @(service_typename)_Event;
46+
using Request = @(service_typename)@(SERVICE_REQUEST_MESSAGE_SUFFIX);
47+
using Response = @(service_typename)@(SERVICE_RESPONSE_MESSAGE_SUFFIX);
48+
using Event = @(service_typename)@(SERVICE_EVENT_MESSAGE_SUFFIX);
4549
};
4650
@
4751
@[for ns in reversed(service.namespaced_type.namespaces)]@

rosidl_generator_type_description/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This generator serializes ROS 2 interface descriptions (message, service, action) to a common format and uses SHA256 to hash that representation into a unique hash for each type.
44

5-
The SHA256 hashes generated by this package must match those generated by `rcl_calculate_type_version_hash`. The `.json` files generated must, therefore, match the result of `rcl_type_description_to_hashable_json`.
5+
The SHA256 hashes generated by this package must match those generated by `rcl_calculate_type_hash`. The `.json` files generated must, therefore, match the result of `rcl_type_description_to_hashable_json`.
66

77
## Generated files
88

rosidl_generator_type_description/rosidl_generator_type_description/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
RIHS01_HASH_VALUE_SIZE = 32
3232
RIHS01_PATTERN = re.compile(r'RIHS([0-9a-f]{2})_([0-9a-f]{64})')
3333

34+
# Used by code generators to create type hash variable names
35+
TYPE_HASH_VAR = 'TYPE_HASH'
36+
3437

3538
def generate_type_hash(generator_arguments_file: str) -> List[str]:
3639
with open(generator_arguments_file, 'r') as f:
@@ -461,7 +464,8 @@ def _calculate_hash_tree(self) -> dict:
461464
check_circular=True,
462465
allow_nan=False,
463466
indent=None,
464-
separators=(',', ': '),
467+
# note: libyaml in C doesn't allow for tweaking these separators, this is its builtin
468+
separators=(', ', ': '),
465469
sort_keys=False
466470
)
467471
sha = hashlib.sha256()

0 commit comments

Comments
 (0)