Skip to content

Commit

Permalink
use rcutls macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Karsten1987 committed May 29, 2018
1 parent 52d1868 commit e766f5e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 32 deletions.
8 changes: 0 additions & 8 deletions rmw_fastrtps_cpp/include/rmw_fastrtps_cpp/macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,4 @@
} \
};

// check the input pointer for null, sets the error msg accordingly and
// return RMW_RET_ERROR if null
#define RETURN_ERROR_ON_NULL(POINTER) \
if (POINTER == nullptr) { \
RMW_SET_ERROR_MSG("#POINTER pointer is null"); \
return RMW_RET_ERROR; \
} \

#endif // RMW_FASTRTPS_CPP__MACROS_HPP_
20 changes: 14 additions & 6 deletions rmw_fastrtps_cpp/src/rmw_publish.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,20 @@ extern "C"
rmw_ret_t
rmw_publish(const rmw_publisher_t * publisher, const void * ros_message)
{
RETURN_ERROR_ON_NULL(publisher);
RETURN_ERROR_ON_NULL(ros_message);
auto error_allocator = rcutils_get_default_allocator();
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
publisher, "publisher pointer is null", return RMW_RET_ERROR, error_allocator);
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
ros_message, "ros_message pointer is null", return RMW_RET_ERROR, error_allocator);

if (publisher->implementation_identifier != eprosima_fastrtps_identifier) {
RMW_SET_ERROR_MSG("publisher handle not from this implementation");
return RMW_RET_ERROR;
}

auto info = static_cast<CustomPublisherInfo *>(publisher->data);
RETURN_ERROR_ON_NULL(info);
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
info, "publisher info pointer is null", return RMW_RET_ERROR, error_allocator);

eprosima::fastcdr::FastBuffer buffer;
eprosima::fastcdr::Cdr ser(buffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN,
Expand All @@ -62,16 +66,20 @@ rmw_publish(const rmw_publisher_t * publisher, const void * ros_message)
rmw_ret_t
rmw_publish_raw(const rmw_publisher_t * publisher, const rmw_message_raw_t * raw_message)
{
RETURN_ERROR_ON_NULL(publisher);
RETURN_ERROR_ON_NULL(raw_message);
auto error_allocator = rcutils_get_default_allocator();
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
publisher, "publisher pointer is null", return RMW_RET_ERROR, error_allocator);
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
raw_message, "raw_message pointer is null", return RMW_RET_ERROR, error_allocator);

if (publisher->implementation_identifier != eprosima_fastrtps_identifier) {
RMW_SET_ERROR_MSG("publisher handle not from this implementation");
return RMW_RET_ERROR;
}

auto info = static_cast<CustomPublisherInfo *>(publisher->data);
RETURN_ERROR_ON_NULL(info);
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
info, "publisher info pointer is null", return RMW_RET_ERROR, error_allocator);

eprosima::fastcdr::FastBuffer buffer(raw_message->buffer, raw_message->buffer_length);
eprosima::fastcdr::Cdr ser(
Expand Down
56 changes: 38 additions & 18 deletions rmw_fastrtps_cpp/src/rmw_take.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ _take(
}

CustomSubscriberInfo * info = static_cast<CustomSubscriberInfo *>(subscription->data);
RETURN_ERROR_ON_NULL(info);
auto error_msg_allocator = rcutils_get_default_allocator();
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
info, "custom subscriber info is null", return RMW_RET_ERROR, error_msg_allocator);

eprosima::fastcdr::FastBuffer buffer;
eprosima::fastrtps::SampleInfo_t sinfo;
Expand All @@ -85,9 +87,13 @@ _take(
rmw_ret_t
rmw_take(const rmw_subscription_t * subscription, void * ros_message, bool * taken)
{
RETURN_ERROR_ON_NULL(subscription);
RETURN_ERROR_ON_NULL(ros_message);
RETURN_ERROR_ON_NULL(taken);
auto error_msg_allocator = rcutils_get_default_allocator();
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
subscription, "subscription pointer is null", return RMW_RET_ERROR, error_msg_allocator);
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
ros_message, "ros_message pointer is null", return RMW_RET_ERROR, error_msg_allocator);
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
taken, "boolean flag for taken is null", return RMW_RET_ERROR, error_msg_allocator);

return _take(subscription, ros_message, taken, nullptr);
}
Expand All @@ -99,10 +105,15 @@ rmw_take_with_info(
bool * taken,
rmw_message_info_t * message_info)
{
RETURN_ERROR_ON_NULL(subscription);
RETURN_ERROR_ON_NULL(ros_message);
RETURN_ERROR_ON_NULL(taken);
RETURN_ERROR_ON_NULL(message_info);
auto error_msg_allocator = rcutils_get_default_allocator();
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
subscription, "subscription pointer is null", return RMW_RET_ERROR, error_msg_allocator);
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
ros_message, "ros_message pointer is null", return RMW_RET_ERROR, error_msg_allocator);
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
taken, "boolean flag for taken is null", return RMW_RET_ERROR, error_msg_allocator);
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
message_info, "message info pointer is null", return RMW_RET_ERROR, error_msg_allocator);

return _take(subscription, ros_message, taken, message_info);
}
Expand All @@ -122,7 +133,9 @@ _take_raw(
}

CustomSubscriberInfo * info = static_cast<CustomSubscriberInfo *>(subscription->data);
RETURN_ERROR_ON_NULL(info);
auto error_msg_allocator = rcutils_get_default_allocator();
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
info, "custom subscriber info is null", return RMW_RET_ERROR, error_msg_allocator);

eprosima::fastcdr::FastBuffer buffer;
eprosima::fastrtps::SampleInfo_t sinfo;
Expand All @@ -137,8 +150,6 @@ _take_raw(
if (ret != RMW_RET_OK) {
return ret; // Error message already set
}
// TODO(karsten1987): Put that in RCUTILS_DEBUG_LOG()
fprintf(stderr, "had to resize to %u\n", buffer_size);
}
raw_message->buffer_length = buffer_size;
memcpy(raw_message->buffer, buffer.getBuffer(), raw_message->buffer_length);
Expand All @@ -159,9 +170,13 @@ rmw_take_raw(
rmw_message_raw_t * raw_message,
bool * taken)
{
RETURN_ERROR_ON_NULL(subscription);
RETURN_ERROR_ON_NULL(raw_message);
RETURN_ERROR_ON_NULL(taken);
auto error_msg_allocator = rcutils_get_default_allocator();
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
subscription, "subscription pointer is null", return RMW_RET_ERROR, error_msg_allocator);
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
raw_message, "ros_message pointer is null", return RMW_RET_ERROR, error_msg_allocator);
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
taken, "boolean flag for taken is null", return RMW_RET_ERROR, error_msg_allocator);

return _take_raw(subscription, raw_message, taken, nullptr);
}
Expand All @@ -173,10 +188,15 @@ rmw_take_raw_with_info(
bool * taken,
rmw_message_info_t * message_info)
{
RETURN_ERROR_ON_NULL(subscription);
RETURN_ERROR_ON_NULL(raw_message);
RETURN_ERROR_ON_NULL(taken);
RETURN_ERROR_ON_NULL(message_info);
auto error_msg_allocator = rcutils_get_default_allocator();
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
subscription, "subscription pointer is null", return RMW_RET_ERROR, error_msg_allocator);
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
raw_message, "ros_message pointer is null", return RMW_RET_ERROR, error_msg_allocator);
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
taken, "boolean flag for taken is null", return RMW_RET_ERROR, error_msg_allocator);
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
message_info, "message info pointer is null", return RMW_RET_ERROR, error_msg_allocator);

return _take_raw(subscription, raw_message, taken, message_info);
}
Expand Down

0 comments on commit e766f5e

Please sign in to comment.