Skip to content

Commit

Permalink
fix bool pInvokes in generated messages
Browse files Browse the repository at this point in the history
This does not change the C# side of the generated messages to avoid even more complexity in the templates.
As bools in C# pInvokes get marshalled as 32 bit integer by default there is no strictly need to change the code for bool fields.
  • Loading branch information
hoffmann-stefan committed May 26, 2023
1 parent c7add72 commit 86b182c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
26 changes: 25 additions & 1 deletion rosidl_generator_dotnet/resource/msg.c.em
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ from rosidl_parser.definition import AbstractSequence
from rosidl_parser.definition import Array
from rosidl_parser.definition import BasicType
from rosidl_parser.definition import NamespacedType
from rosidl_parser.definition import BOOLEAN_TYPE

from rosidl_cmake import convert_camel_case_to_lower_case_underscore

Expand Down Expand Up @@ -72,7 +73,20 @@ bool @(msg_typename)__init_sequence_field_@(member.name)_message(void *message_h
}
@[ end if]@

@[ if isinstance(member.type.value_type, BasicType)]@
@[ if isinstance(member.type.value_type, BasicType) and member.type.value_type.typename == BOOLEAN_TYPE]@
@# Special handling for marshaling bool as int32_t
void @(msg_typename)__write_field_@(member.name)(void *message_handle, int32_t /* bool */ value)
{
bool * ros_message = (bool *)message_handle;
*ros_message = value != 0;
}

int32_t /* bool */ @(msg_typename)__read_field_@(member.name)(void *message_handle)
{
bool * ros_message = (bool *)message_handle;
return (*ros_message) ? 1 : 0;
}
@[ elif isinstance(member.type.value_type, BasicType)]@
void @(msg_typename)__write_field_@(member.name)(void *message_handle, @(msg_type_to_c(member.type.value_type)) value)
{
@(msg_type_to_c(member.type.value_type)) * ros_message = (@(msg_type_to_c(member.type.value_type)) *)message_handle;
Expand Down Expand Up @@ -100,6 +114,16 @@ void @(msg_typename)__write_field_@(member.name)(void *message_handle, @(msg_typ

@[ elif isinstance(member.type, AbstractWString)]@
// TODO: Unicode types are not supported
@[ elif isinstance(member.type, BasicType) and member.type.typename == BOOLEAN_TYPE]@
@# Special handling for marshaling bool as int32_t
int32_t /* bool */ @(msg_typename)__read_field_@(member.name)(void * message_handle) {
@(msg_typename) * ros_message = (@(msg_typename) *)message_handle;
return (ros_message->@(member.name)) ? 1 : 0;
}
void @(msg_typename)__write_field_@(member.name)(void * message_handle, int32_t /* bool */ value) {
@(msg_typename) * ros_message = (@(msg_typename) *)message_handle;
ros_message->@(member.name) = value != 0;
}
@[ elif isinstance(member.type, BasicType) or isinstance(member.type, AbstractString)]@
@(msg_type_to_c(member.type)) @(msg_typename)__read_field_@(member.name)(void * message_handle) {
@(msg_typename) * ros_message = (@(msg_typename) *)message_handle;
Expand Down
16 changes: 15 additions & 1 deletion rosidl_generator_dotnet/resource/msg.h.em
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ from rosidl_parser.definition import AbstractSequence
from rosidl_parser.definition import Array
from rosidl_parser.definition import BasicType
from rosidl_parser.definition import NamespacedType
from rosidl_parser.definition import BOOLEAN_TYPE
from rosidl_generator_dotnet import msg_type_to_c

type_name = message.structure.namespaced_type.name
Expand Down Expand Up @@ -62,7 +63,13 @@ int32_t @(msg_prefix)_CDECL @(msg_typename)__getsize_field_@(member.name)_messag
bool @(msg_prefix)_CDECL @(msg_typename)__init_sequence_field_@(member.name)_message(void *, int32_t);

@[ end if]@
@[ if isinstance(member.type.value_type, BasicType) or isinstance(member.type.value_type, AbstractString)]@
@[ if isinstance(member.type.value_type, BasicType) and member.type.value_type.typename == BOOLEAN_TYPE]@
@# Special handling for marshaling bool as int32_t
@(msg_prefix)_EXPORT
void @(msg_typename)__write_field_@(member.name)(void *, int32_t /* bool */);
@(msg_prefix)_EXPORT
int32_t /* bool */ @(msg_prefix)_CDECL @(msg_typename)__read_field_@(member.name)(void *);
@[ elif isinstance(member.type.value_type, BasicType) or isinstance(member.type.value_type, AbstractString)]@
@(msg_prefix)_EXPORT
void @(msg_typename)__write_field_@(member.name)(void *, @(msg_type_to_c(member.type.value_type)));
@(msg_prefix)_EXPORT
Expand All @@ -71,6 +78,13 @@ void @(msg_typename)__write_field_@(member.name)(void *, @(msg_type_to_c(member.

@[ elif isinstance(member.type, AbstractWString)]@
// TODO: Unicode types are not supported
@[ elif isinstance(member.type, BasicType) and member.type.typename == BOOLEAN_TYPE]@
@# Special handling for marshaling bool as int32_t
@(msg_prefix)_EXPORT
int32_t /* bool */ @(msg_prefix)_CDECL @(msg_typename)__read_field_@(member.name)(void *);

@(msg_prefix)_EXPORT
void @(msg_typename)__write_field_@(member.name)(void *, int32_t /* bool */);
@[ elif isinstance(member.type, BasicType) or isinstance(member.type, AbstractString)]@
@(msg_prefix)_EXPORT
@(msg_type_to_c(member.type)) @(msg_prefix)_CDECL @(msg_typename)__read_field_@(member.name)(void *);
Expand Down

0 comments on commit 86b182c

Please sign in to comment.