Skip to content

Commit

Permalink
Use create instead of init
Browse files Browse the repository at this point in the history
Signed-off-by: methylDragon <methylDragon@gmail.com>
  • Loading branch information
methylDragon committed Apr 7, 2023
1 parent 29577da commit d871af0
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 53 deletions.
37 changes: 19 additions & 18 deletions rclcpp/include/rclcpp/dynamic_typesupport/dynamic_message_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,43 +34,44 @@ namespace dynamic_typesupport
class DynamicMessage;
class DynamicMessageTypeBuilder;

/// Utility wrapper class for rosidl_dynamic_typesupport_dynamic_type_t *
/// Utility wrapper class for `rosidl_dynamic_typesupport_dynamic_type_t *`
/**
* This class:
* - Manages the lifetime of the raw pointer.
* - Exposes getter methods to get the raw pointer and shared pointers
* - Exposes the underlying serialization support API
*
* Ownership:
* - This class borrows the rosidl_dynamic_typesupport_serialization_support_t stored in the passed
* DynamicSerializationSupport. So it cannot outlive the DynamicSerializationSupport.
* - The DynamicSerializationSupport's rosidl_dynamic_typesupport_serialization_support_t pointer
* must point to the same location in memory as the stored raw pointer!
* - This class borrows the `rosidl_dynamic_typesupport_serialization_support_t` stored in the
* passed `DynamicSerializationSupport`.
* So it cannot outlive the `DynamicSerializationSupport`.
* - The `DynamicSerializationSupport`'s `rosidl_dynamic_typesupport_serialization_support_t`
* pointer must point to the same location in memory as the stored raw pointer!
*
* Note: This class is meant to map to the lower level rosidl_dynamic_typesupport_dynamic_type_t,
* which can be constructed via DynamicMessageTypeBuilder, which maps to
* rosidl_dynamic_typesupport_dynamic_type_builder_t.
* This class is meant to map to the lower level `rosidl_dynamic_typesupport_dynamic_type_t`,
* which can be constructed via `DynamicMessageTypeBuilder`, which maps to
* `rosidl_dynamic_typesupport_dynamic_type_builder_t`.
*
* The usual method of obtaining a DynamicMessageType is through construction of
* rosidl_message_type_support_t via rcl_dynamic_message_type_support_handle_init(), then
* taking ownership of its contents. But DynamicMessageTypeBuilder can also be used to obtain
* DynamicMessageType by constructing it bottom-up instead, since it exposes the lower_level
* rosidl methods.
* The usual method of obtaining a `DynamicMessageType` is through construction of
* `rosidl_message_type_support_t` via `rcl_dynamic_message_type_support_handle_create()`, then
* taking ownership of its contents. But `DynamicMessageTypeBuilder` can also be used to obtain
* `DynamicMessageType` by constructing it bottom-up instead, since it exposes the lower_level
* rosidl methods.
*/
class DynamicMessageType : public std::enable_shared_from_this<DynamicMessageType>
{
public:
RCLCPP_SMART_PTR_ALIASES_ONLY(DynamicMessageType)

// CONSTRUCTION ==================================================================================
// Most constructors require a passed in DynamicSerializationSupport::SharedPtr, to extend the
// Most constructors require a passed in `DynamicSerializationSupport::SharedPtr`, to extend the
// lifetime of the serialization support (if the constructor cannot otherwise get it from args).
//
// In cases where a dynamic type pointer is passed, the serialization support composed by
// the type should be the exact same object managed by the DynamicSerializationSupport,
// the type should be the exact same object managed by the `DynamicSerializationSupport`,
// otherwise the lifetime management will not work properly.

/// Construct a new DynamicMessageType with the provided dynamic type builder
/// Construct a new `DynamicMessageType` with the provided dynamic type builder
RCLCPP_PUBLIC
explicit DynamicMessageType(std::shared_ptr<DynamicMessageTypeBuilder> dynamic_type_builder);

Expand Down Expand Up @@ -111,7 +112,7 @@ class DynamicMessageType : public std::enable_shared_from_this<DynamicMessageTyp
RCLCPP_PUBLIC
virtual ~DynamicMessageType();

/// Swaps the serialization support if serialization_support is populated
/// Swaps the serialization support if `serialization_support` is populated
RCLCPP_PUBLIC
void
init_from_description(
Expand Down Expand Up @@ -183,7 +184,7 @@ class DynamicMessageType : public std::enable_shared_from_this<DynamicMessageTyp
// It isn't actually used by the builder since the builder should compose its own support
//
// ... Though ideally it should be the exact same support as the one stored in the
// DynamicSerializationSupport
// `DynamicSerializationSupport`
DynamicSerializationSupport::SharedPtr serialization_support_;

std::shared_ptr<rosidl_dynamic_typesupport_dynamic_type_t> rosidl_dynamic_type_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace dynamic_typesupport
class DynamicMessage;
class DynamicMessageType;

/// Utility wrapper class for rosidl_dynamic_typesupport_dynamic_type_builder_t *
/// Utility wrapper class for `rosidl_dynamic_typesupport_dynamic_type_builder_t *`
/**
* This class:
* - Manages the lifetime of the raw pointer.
Expand All @@ -42,33 +42,34 @@ class DynamicMessageType;
*
* Ownership:
* - This class borrows the rosidl_dynamic_typesupport_serialization_support_t stored in the passed
* DynamicSerializationSupport. So it cannot outlive the DynamicSerializationSupport.
* - The DynamicSerializationSupport's rosidl_dynamic_typesupport_serialization_support_t pointer
* must point to the same location in memory as the stored raw pointer!
* `DynamicSerializationSupport`.
* So it cannot outlive the `DynamicSerializationSupport`.
* - The `DynamicSerializationSupport`'s `rosidl_dynamic_typesupport_serialization_support_t`
* pointer must point to the same location in memory as the stored raw pointer!
*
* Note: This class is meant to map to rosidl_dynamic_typesupport_dynamic_type_builder_t,
* facilitating the construction of dynamic types bottom-up in the C++ layer.
* This class is meant to map to rosidl_dynamic_typesupport_dynamic_type_builder_t, facilitating the
* construction of dynamic types bottom-up in the C++ layer.
*
* The usual method of obtaining a DynamicMessageType is through construction of
* rosidl_message_type_support_t via rcl_dynamic_message_type_support_handle_init(), then taking
* ownership of its contents. But DynamicMessageTypeBuilder can also be used to obtain
* DynamicMessageType by constructing it bottom-up instead, since it exposes the lower_level
* rosidl methods.
* The usual method of obtaining a `DynamicMessageType` is through construction of
* `rosidl_message_type_support_t` via `rcl_dynamic_message_type_support_handle_create()`, then
* taking ownership of its contents.
* But `DynamicMessageTypeBuilder` can also be used to obtain `DynamicMessageType` by constructing
* it bottom-up instead, since it exposes the lower_level rosidl methods.
*/
class DynamicMessageTypeBuilder : public std::enable_shared_from_this<DynamicMessageTypeBuilder>
{
public:
RCLCPP_SMART_PTR_ALIASES_ONLY(DynamicMessageTypeBuilder)

// CONSTRUCTION ==================================================================================
// All constructors require a passed in DynamicSerializationSupport::SharedPtr, to extend the
// All constructors require a passed in `DynamicSerializationSupport::SharedPtr`, to extend the
// lifetime of the serialization support.
//
// In cases where a dynamic type builder pointer is passed, the serialization support composed by
// the builder should be the exact same object managed by the DynamicSerializationSupport,
// the builder should be the exact same object managed by the `DynamicSerializationSupport`,
// otherwise the lifetime management will not work properly.

/// Construct a new DynamicMessageTypeBuilder with the provided serialization support
/// Construct a new `DynamicMessageTypeBuilder` with the provided serialization support
RCLCPP_PUBLIC
DynamicMessageTypeBuilder(
DynamicSerializationSupport::SharedPtr serialization_support,
Expand Down Expand Up @@ -383,7 +384,7 @@ class DynamicMessageTypeBuilder : public std::enable_shared_from_this<DynamicMes
// It isn't actually used by the builder since the builder should compose its own support
//
// ... Though ideally it should be the exact same support as the one stored in the
// DynamicSerializationSupport
// `DynamicSerializationSupport`
DynamicSerializationSupport::SharedPtr serialization_support_;

std::shared_ptr<rosidl_dynamic_typesupport_dynamic_type_builder_t> rosidl_dynamic_type_builder_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ namespace rclcpp
namespace dynamic_typesupport
{

/// Utility wrapper class for rosidl_message_type_support_t * containing managed
/// Utility wrapper class for `rosidl_message_type_support_t *` containing managed
/// instances of the typesupport handle impl.
/**
*
* NOTE: This class is the recommended way to obtain the dynamic message type
* support struct, instead of rcl_dynamic_message_type_support_handle_init,
* support struct, instead of `rcl_dynamic_message_type_support_handle_create()`,
* because this class will manage the lifetimes for you.
*
* Do NOT call rcl_dynamic_message_type_support_handle_destroy!!
Expand All @@ -52,7 +52,7 @@ namespace dynamic_typesupport
* serialization support API
*
* Ownership:
* - This class, similarly to the rosidl_dynamic_typesupport_serialization_support_t, must outlive
* - This class, similarly to the `rosidl_dynamic_typesupport_serialization_support_t`, must outlive
* all downstream usages of the serialization support.
*/
class DynamicMessageTypeSupport : public std::enable_shared_from_this<DynamicMessageTypeSupport>
Expand All @@ -63,15 +63,15 @@ class DynamicMessageTypeSupport : public std::enable_shared_from_this<DynamicMes
// CONSTRUCTION ==================================================================================
/// From description
/// Does NOT take ownership of the description (copies instead.)
/// Constructs type support top-down (calling rcl_dynamic_message_type_support_handle_init)
/// Constructs type support top-down (calling `rcl_dynamic_message_type_support_handle_create()`)
RCLCPP_PUBLIC
DynamicMessageTypeSupport(
const rosidl_runtime_c__type_description__TypeDescription & description,
const std::string & serialization_library_name = "");

/// From description, for provided serialization support
/// Does NOT take ownership of the description (copies instead.)
/// Constructs type support top-down (calling rmw_dynamic_message_type_support_handle_init)
/// Constructs type support top-down (calling `rmw_dynamic_message_type_support_handle_create()`)
RCLCPP_PUBLIC
DynamicMessageTypeSupport(
DynamicSerializationSupport::SharedPtr serialization_support,
Expand Down
8 changes: 4 additions & 4 deletions rclcpp/src/rclcpp/dynamic_typesupport/dynamic_message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ DynamicMessage::DynamicMessage(const DynamicMessageTypeBuilder::SharedPtr dynami
}

rosidl_dynamic_typesupport_dynamic_data_t * rosidl_dynamic_data = nullptr;
rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_init_from_dynamic_type_builder(
rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_create_from_dynamic_type_builder(
rosidl_dynamic_type_builder, &rosidl_dynamic_data);
if (ret != RCUTILS_RET_OK || !rosidl_dynamic_data) {
throw std::runtime_error("could not create new dynamic data object from dynamic type builder");
Expand Down Expand Up @@ -90,7 +90,7 @@ DynamicMessage::DynamicMessage(const DynamicMessageType::SharedPtr dynamic_type)
}

rosidl_dynamic_typesupport_dynamic_data_t * rosidl_dynamic_data = nullptr;
rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_init_from_dynamic_type(
rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_create_from_dynamic_type(
rosidl_dynamic_type, &rosidl_dynamic_data);
if (ret != RCUTILS_RET_OK || !rosidl_dynamic_data) {
throw std::runtime_error(
Expand Down Expand Up @@ -428,7 +428,7 @@ DynamicMessage
DynamicMessage::init_from_type(DynamicMessageType & type) const
{
rosidl_dynamic_typesupport_dynamic_data_t * rosidl_dynamic_data = nullptr;
rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_init_from_dynamic_type(
rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_create_from_dynamic_type(
type.get_rosidl_dynamic_type(), &rosidl_dynamic_data);
if (ret != RCUTILS_RET_OK || !rosidl_dynamic_data) {
throw std::runtime_error("could not create new dynamic data object from dynamic type");
Expand All @@ -441,7 +441,7 @@ DynamicMessage::SharedPtr
DynamicMessage::init_from_type_shared(DynamicMessageType & type) const
{
rosidl_dynamic_typesupport_dynamic_data_t * rosidl_dynamic_data = nullptr;
rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_init_from_dynamic_type(
rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_data_create_from_dynamic_type(
type.get_rosidl_dynamic_type(), &rosidl_dynamic_data);
if (ret != RCUTILS_RET_OK || !rosidl_dynamic_data) {
throw std::runtime_error("could not create new dynamic data object from dynamic type");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ DynamicMessageType::DynamicMessageType(DynamicMessageTypeBuilder::SharedPtr dyna
}

rosidl_dynamic_typesupport_dynamic_type_t * rosidl_dynamic_type = nullptr;
rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_type_init_from_dynamic_type_builder(
rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_type_create_from_dynamic_type_builder(
rosidl_dynamic_type_builder, &rosidl_dynamic_type);
if (ret != RCUTILS_RET_OK || !rosidl_dynamic_type) {
throw std::runtime_error("could not create new dynamic type object");
Expand Down Expand Up @@ -159,7 +159,7 @@ DynamicMessageType::init_from_description(
}

rosidl_dynamic_typesupport_dynamic_type_t * rosidl_dynamic_type = nullptr;
rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_type_init_from_description(
rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_type_create_from_description(
serialization_support_->get_rosidl_serialization_support(), &description, &rosidl_dynamic_type);
if (ret != RCUTILS_RET_OK || !rosidl_dynamic_type) {
throw std::runtime_error("could not create new dynamic type object");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ using rclcpp::dynamic_typesupport::DynamicSerializationSupport;
#endif


// CONSTRUCTION ==================================================================================
// CONSTRUCTION ====================================================================================
DynamicMessageTypeBuilder::DynamicMessageTypeBuilder(
DynamicSerializationSupport::SharedPtr serialization_support, const std::string & name)
: serialization_support_(serialization_support), rosidl_dynamic_type_builder_(nullptr)
Expand Down Expand Up @@ -153,7 +153,7 @@ DynamicMessageTypeBuilder::init_from_description(
}

rosidl_dynamic_typesupport_dynamic_type_builder_t * rosidl_dynamic_type_builder = nullptr;
rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_type_builder_init_from_description(
rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_type_builder_create_from_description(
serialization_support_->get_rosidl_serialization_support(), &description,
&rosidl_dynamic_type_builder);
if (ret != RCUTILS_RET_OK || !rosidl_dynamic_type_builder) {
Expand Down Expand Up @@ -183,7 +183,7 @@ DynamicMessageTypeBuilder::init_from_serialization_support_(


rosidl_dynamic_typesupport_dynamic_type_builder_t * rosidl_dynamic_type_builder = nullptr;
rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_type_builder_init(
rcutils_ret_t ret = rosidl_dynamic_typesupport_dynamic_type_builder_create(
serialization_support->get_rosidl_serialization_support(),
name.c_str(), name.size(),
&rosidl_dynamic_type_builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ DynamicMessageTypeSupport::DynamicMessageTypeSupport(
rcl_ret_t ret;

if (serialization_library_name.empty()) {
ret = rcl_dynamic_message_type_support_handle_init(nullptr, &description, &ts);
ret = rcl_dynamic_message_type_support_handle_create(nullptr, &description, &ts);
} else {
ret = rcl_dynamic_message_type_support_handle_init(
ret = rcl_dynamic_message_type_support_handle_create(
serialization_library_name.c_str(), &description, &ts);
}
if (ret != RCL_RET_OK) {
Expand Down Expand Up @@ -135,7 +135,7 @@ DynamicMessageTypeSupport::DynamicMessageTypeSupport(
throw std::runtime_error("failed to get type hash");
}

rmw_ret_t ret = rmw_dynamic_message_type_support_handle_init(
rmw_ret_t ret = rmw_dynamic_message_type_support_handle_create(
serialization_support->get_rosidl_serialization_support(),
rmw_feature_supported(RMW_MIDDLEWARE_SUPPORTS_TYPE_DISCOVERY),
type_hash.get(), // type_hash
Expand Down Expand Up @@ -280,8 +280,8 @@ DynamicMessageTypeSupport::manage_description_(
}


// NOTE(methylDragon): This looks like rmw_dynamic_message_type_support_handle_init, but instead
// just aggregates already initialized objects
// This looks like rmw_`dynamic_message_type_support_handle_create()`, but instead just aggregates
// already initialized objects
void
DynamicMessageTypeSupport::init_rosidl_message_type_support_(
DynamicSerializationSupport::SharedPtr serialization_support,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ using rclcpp::dynamic_typesupport::DynamicSerializationSupport;

// CONSTRUCTION ====================================================================================
DynamicSerializationSupport::DynamicSerializationSupport()
: DynamicSerializationSupport::DynamicSerializationSupport("") {};
: DynamicSerializationSupport::DynamicSerializationSupport("") {}

DynamicSerializationSupport::DynamicSerializationSupport(
const std::string & serialization_library_name)
Expand Down

0 comments on commit d871af0

Please sign in to comment.