Merge pull request #87 from ros2/refactor_smart_ptr_macros
refactored the smart ptr class macros
This commit is contained in:
commit
adfee27b59
20 changed files with 74 additions and 35 deletions
|
@ -1,4 +1,4 @@
|
||||||
// Copyright 2014 Open Source Robotics Foundation, Inc.
|
// Copyright 2015 Open Source Robotics Foundation, Inc.
|
||||||
//
|
//
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
// you may not use this file except in compliance with the License.
|
// you may not use this file except in compliance with the License.
|
||||||
|
@ -27,11 +27,11 @@ namespace executor
|
||||||
|
|
||||||
struct AnyExecutable
|
struct AnyExecutable
|
||||||
{
|
{
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(AnyExecutable);
|
RCLCPP_SMART_PTR_DEFINITIONS(AnyExecutable);
|
||||||
AnyExecutable()
|
AnyExecutable()
|
||||||
: subscription(0), timer(0), callback_group(0), node(0)
|
: subscription(0), timer(0), callback_group(0), node(0)
|
||||||
{}
|
{}
|
||||||
// Either the subscription or the timer will be set, but not both
|
// Only one of the following pointers will be set.
|
||||||
rclcpp::subscription::SubscriptionBase::SharedPtr subscription;
|
rclcpp::subscription::SubscriptionBase::SharedPtr subscription;
|
||||||
rclcpp::timer::TimerBase::SharedPtr timer;
|
rclcpp::timer::TimerBase::SharedPtr timer;
|
||||||
rclcpp::service::ServiceBase::SharedPtr service;
|
rclcpp::service::ServiceBase::SharedPtr service;
|
||||||
|
|
|
@ -52,7 +52,7 @@ class CallbackGroup
|
||||||
friend class rclcpp::executor::Executor;
|
friend class rclcpp::executor::Executor;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(CallbackGroup);
|
RCLCPP_SMART_PTR_DEFINITIONS(CallbackGroup);
|
||||||
|
|
||||||
CallbackGroup(CallbackGroupType group_type)
|
CallbackGroup(CallbackGroupType group_type)
|
||||||
: type_(group_type), can_be_taken_from_(true)
|
: type_(group_type), can_be_taken_from_(true)
|
||||||
|
|
|
@ -45,7 +45,7 @@ class ClientBase
|
||||||
friend class rclcpp::executor::Executor;
|
friend class rclcpp::executor::Executor;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(ClientBase);
|
RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(ClientBase);
|
||||||
|
|
||||||
ClientBase(
|
ClientBase(
|
||||||
std::shared_ptr<rmw_node_t> node_handle,
|
std::shared_ptr<rmw_node_t> node_handle,
|
||||||
|
@ -99,7 +99,7 @@ public:
|
||||||
|
|
||||||
typedef std::function<void (SharedFuture)> CallbackType;
|
typedef std::function<void (SharedFuture)> CallbackType;
|
||||||
|
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(Client);
|
RCLCPP_SMART_PTR_DEFINITIONS(Client);
|
||||||
|
|
||||||
Client(
|
Client(
|
||||||
std::shared_ptr<rmw_node_t> node_handle,
|
std::shared_ptr<rmw_node_t> node_handle,
|
||||||
|
|
|
@ -28,7 +28,7 @@ namespace context
|
||||||
class Context
|
class Context
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(Context);
|
RCLCPP_SMART_PTR_DEFINITIONS(Context);
|
||||||
|
|
||||||
Context() {}
|
Context() {}
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,7 @@ namespace default_context
|
||||||
class DefaultContext : public rclcpp::context::Context
|
class DefaultContext : public rclcpp::context::Context
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(DefaultContext);
|
RCLCPP_SMART_PTR_DEFINITIONS(DefaultContext);
|
||||||
|
|
||||||
DefaultContext() {}
|
DefaultContext() {}
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ class Executor
|
||||||
friend class memory_strategy::MemoryStrategy;
|
friend class memory_strategy::MemoryStrategy;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(Executor);
|
RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(Executor);
|
||||||
|
|
||||||
explicit Executor(memory_strategy::MemoryStrategy::SharedPtr ms =
|
explicit Executor(memory_strategy::MemoryStrategy::SharedPtr ms =
|
||||||
memory_strategy::create_default_strategy())
|
memory_strategy::create_default_strategy())
|
||||||
|
|
|
@ -38,7 +38,7 @@ namespace multi_threaded_executor
|
||||||
class MultiThreadedExecutor : public executor::Executor
|
class MultiThreadedExecutor : public executor::Executor
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(MultiThreadedExecutor);
|
RCLCPP_SMART_PTR_DEFINITIONS(MultiThreadedExecutor);
|
||||||
|
|
||||||
MultiThreadedExecutor(memory_strategy::MemoryStrategy::SharedPtr ms =
|
MultiThreadedExecutor(memory_strategy::MemoryStrategy::SharedPtr ms =
|
||||||
memory_strategy::create_default_strategy())
|
memory_strategy::create_default_strategy())
|
||||||
|
|
|
@ -39,7 +39,7 @@ namespace single_threaded_executor
|
||||||
class SingleThreadedExecutor : public executor::Executor
|
class SingleThreadedExecutor : public executor::Executor
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(SingleThreadedExecutor);
|
RCLCPP_SMART_PTR_DEFINITIONS(SingleThreadedExecutor);
|
||||||
|
|
||||||
SingleThreadedExecutor(memory_strategy::MemoryStrategy::SharedPtr ms =
|
SingleThreadedExecutor(memory_strategy::MemoryStrategy::SharedPtr ms =
|
||||||
memory_strategy::create_default_strategy())
|
memory_strategy::create_default_strategy())
|
||||||
|
|
|
@ -23,14 +23,33 @@
|
||||||
Class(const Class &) = delete; \
|
Class(const Class &) = delete; \
|
||||||
Class & operator=(const Class &) = delete;
|
Class & operator=(const Class &) = delete;
|
||||||
|
|
||||||
/* Defines a make_shared static function on the class using perfect forwarding.
|
/* Defines aliases and static functions for using the Class with smart pointers.
|
||||||
*
|
*
|
||||||
* Use in the public section of the class.
|
* Use in the public section of the class.
|
||||||
* Make sure to include <memory> in the header when using this.
|
* Make sure to include <memory> in the header when using this.
|
||||||
*/
|
*/
|
||||||
#define RCLCPP_MAKE_SHARED_DEFINITIONS(Class) \
|
#define RCLCPP_SMART_PTR_DEFINITIONS(Class) \
|
||||||
typedef std::shared_ptr<Class> SharedPtr; \
|
RCLCPP_SHARED_PTR_DEFINITIONS(Class) \
|
||||||
\
|
RCLCPP_WEAK_PTR_DEFINITIONS(Class) \
|
||||||
|
RCLCPP_UNIQUE_PTR_DEFINITIONS(Class)
|
||||||
|
|
||||||
|
/* Defines aliases and static functions for using the Class with smart pointers.
|
||||||
|
*
|
||||||
|
* Same as RCLCPP_SMART_PTR_DEFINITIONS expect it excludes the static
|
||||||
|
* Class::make_unique() method definition which does not work on classes which
|
||||||
|
* are not CopyConstructable.
|
||||||
|
*
|
||||||
|
* Use in the public section of the class.
|
||||||
|
* Make sure to include <memory> in the header when using this.
|
||||||
|
*/
|
||||||
|
#define RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(Class) \
|
||||||
|
RCLCPP_SHARED_PTR_DEFINITIONS(Class) \
|
||||||
|
RCLCPP_WEAK_PTR_DEFINITIONS(Class) \
|
||||||
|
__RCLCPP_UNIQUE_PTR_ALIAS(Class)
|
||||||
|
|
||||||
|
#define __RCLCPP_SHARED_PTR_ALIAS(Class) using SharedPtr = std::shared_ptr<Class>;
|
||||||
|
|
||||||
|
#define __RCLCPP_MAKE_SHARED_DEFINITION(Class) \
|
||||||
template<typename ... Args> \
|
template<typename ... Args> \
|
||||||
static std::shared_ptr<Class> \
|
static std::shared_ptr<Class> \
|
||||||
make_shared(Args && ... args) \
|
make_shared(Args && ... args) \
|
||||||
|
@ -38,6 +57,30 @@
|
||||||
return std::make_shared<Class>(std::forward<Args>(args) ...); \
|
return std::make_shared<Class>(std::forward<Args>(args) ...); \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Defines aliases and static functions for using the Class with shared_ptrs.
|
||||||
|
#define RCLCPP_SHARED_PTR_DEFINITIONS(Class) \
|
||||||
|
__RCLCPP_SHARED_PTR_ALIAS(Class) \
|
||||||
|
__RCLCPP_MAKE_SHARED_DEFINITION(Class)
|
||||||
|
|
||||||
|
#define __RCLCPP_WEAK_PTR_ALIAS(Class) using WeakPtr = std::weak_ptr<Class>;
|
||||||
|
|
||||||
|
/// Defines aliases and static functions for using the Class with weak_ptrs.
|
||||||
|
#define RCLCPP_WEAK_PTR_DEFINITIONS(Class) __RCLCPP_WEAK_PTR_ALIAS(Class)
|
||||||
|
|
||||||
|
#define __RCLCPP_UNIQUE_PTR_ALIAS(Class) using UniquePtr = std::unique_ptr<Class>;
|
||||||
|
|
||||||
|
#define __RCLCPP_MAKE_UNIQUE_DEFINITION(Class) \
|
||||||
|
template<typename ... Args> \
|
||||||
|
static std::unique_ptr<Class> \
|
||||||
|
make_unique(Args && ... args) \
|
||||||
|
{ \
|
||||||
|
return std::unique_ptr<Class>(new Class(std::forward<Args>(args) ...)); \
|
||||||
|
}
|
||||||
|
/// Defines aliases and static functions for using the Class with unique_ptrs.
|
||||||
|
#define RCLCPP_UNIQUE_PTR_DEFINITIONS(Class) \
|
||||||
|
__RCLCPP_UNIQUE_PTR_ALIAS(Class) \
|
||||||
|
__RCLCPP_MAKE_UNIQUE_DEFINITION(Class)
|
||||||
|
|
||||||
#define RCLCPP_INFO(Args) std::cout << Args << std::endl;
|
#define RCLCPP_INFO(Args) std::cout << Args << std::endl;
|
||||||
|
|
||||||
#endif /* RCLCPP_RCLCPP_MACROS_HPP_ */
|
#endif /* RCLCPP_RCLCPP_MACROS_HPP_ */
|
||||||
|
|
|
@ -36,7 +36,7 @@ class MemoryStrategy
|
||||||
friend class executor::Executor;
|
friend class executor::Executor;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(MemoryStrategy);
|
RCLCPP_SMART_PTR_DEFINITIONS(MemoryStrategy);
|
||||||
virtual void ** borrow_handles(HandleType type, size_t number_of_handles)
|
virtual void ** borrow_handles(HandleType type, size_t number_of_handles)
|
||||||
{
|
{
|
||||||
(void)type;
|
(void)type;
|
||||||
|
|
|
@ -29,7 +29,7 @@ class MessageMemoryStrategy
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(MessageMemoryStrategy);
|
RCLCPP_SMART_PTR_DEFINITIONS(MessageMemoryStrategy);
|
||||||
|
|
||||||
static SharedPtr create_default()
|
static SharedPtr create_default()
|
||||||
{
|
{
|
||||||
|
|
|
@ -100,7 +100,7 @@ class Node
|
||||||
friend class rclcpp::executor::Executor;
|
friend class rclcpp::executor::Executor;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(Node);
|
RCLCPP_SMART_PTR_DEFINITIONS(Node);
|
||||||
|
|
||||||
/* Create a node based on the node name. */
|
/* Create a node based on the node name. */
|
||||||
Node(const std::string & node_name);
|
Node(const std::string & node_name);
|
||||||
|
|
|
@ -44,7 +44,7 @@ class AsyncParametersClient
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(AsyncParametersClient);
|
RCLCPP_SMART_PTR_DEFINITIONS(AsyncParametersClient);
|
||||||
|
|
||||||
AsyncParametersClient(const rclcpp::node::Node::SharedPtr & node,
|
AsyncParametersClient(const rclcpp::node::Node::SharedPtr & node,
|
||||||
const std::string & remote_node_name = "")
|
const std::string & remote_node_name = "")
|
||||||
|
@ -252,7 +252,7 @@ private:
|
||||||
class SyncParametersClient
|
class SyncParametersClient
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(SyncParametersClient);
|
RCLCPP_SMART_PTR_DEFINITIONS(SyncParametersClient);
|
||||||
|
|
||||||
SyncParametersClient(
|
SyncParametersClient(
|
||||||
rclcpp::node::Node::SharedPtr & node)
|
rclcpp::node::Node::SharedPtr & node)
|
||||||
|
|
|
@ -41,7 +41,7 @@ class ParameterService
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(ParameterService);
|
RCLCPP_SMART_PTR_DEFINITIONS(ParameterService);
|
||||||
|
|
||||||
ParameterService(const rclcpp::node::Node::SharedPtr & node)
|
ParameterService(const rclcpp::node::Node::SharedPtr & node)
|
||||||
: node_(node)
|
: node_(node)
|
||||||
|
|
|
@ -39,7 +39,7 @@ namespace publisher
|
||||||
class Publisher
|
class Publisher
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(Publisher);
|
RCLCPP_SMART_PTR_DEFINITIONS(Publisher);
|
||||||
|
|
||||||
Publisher(std::shared_ptr<rmw_node_t> node_handle, rmw_publisher_t * publisher_handle)
|
Publisher(std::shared_ptr<rmw_node_t> node_handle, rmw_publisher_t * publisher_handle)
|
||||||
: node_handle_(node_handle), publisher_handle_(publisher_handle)
|
: node_handle_(node_handle), publisher_handle_(publisher_handle)
|
||||||
|
|
|
@ -30,7 +30,7 @@ namespace rate
|
||||||
class RateBase
|
class RateBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(RateBase);
|
RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(RateBase);
|
||||||
|
|
||||||
virtual bool sleep() = 0;
|
virtual bool sleep() = 0;
|
||||||
virtual bool is_steady() = 0;
|
virtual bool is_steady() = 0;
|
||||||
|
@ -45,7 +45,7 @@ template<class Clock = std::chrono::high_resolution_clock>
|
||||||
class GenericRate : public RateBase
|
class GenericRate : public RateBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(GenericRate);
|
RCLCPP_SMART_PTR_DEFINITIONS(GenericRate);
|
||||||
|
|
||||||
GenericRate(double rate)
|
GenericRate(double rate)
|
||||||
: GenericRate<Clock>(
|
: GenericRate<Clock>(
|
||||||
|
|
|
@ -44,7 +44,7 @@ class ServiceBase
|
||||||
friend class rclcpp::executor::Executor;
|
friend class rclcpp::executor::Executor;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(ServiceBase);
|
RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(ServiceBase);
|
||||||
|
|
||||||
ServiceBase(
|
ServiceBase(
|
||||||
std::shared_ptr<rmw_node_t> node_handle,
|
std::shared_ptr<rmw_node_t> node_handle,
|
||||||
|
@ -105,7 +105,7 @@ public:
|
||||||
const std::shared_ptr<rmw_request_id_t> &,
|
const std::shared_ptr<rmw_request_id_t> &,
|
||||||
const std::shared_ptr<typename ServiceT::Request> &,
|
const std::shared_ptr<typename ServiceT::Request> &,
|
||||||
std::shared_ptr<typename ServiceT::Response> &)> CallbackWithHeaderType;
|
std::shared_ptr<typename ServiceT::Response> &)> CallbackWithHeaderType;
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(Service);
|
RCLCPP_SMART_PTR_DEFINITIONS(Service);
|
||||||
|
|
||||||
Service(
|
Service(
|
||||||
std::shared_ptr<rmw_node_t> node_handle,
|
std::shared_ptr<rmw_node_t> node_handle,
|
||||||
|
|
|
@ -32,7 +32,7 @@ class MessagePoolMemoryStrategy
|
||||||
: public message_memory_strategy::MessageMemoryStrategy<MessageT>
|
: public message_memory_strategy::MessageMemoryStrategy<MessageT>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(MessagePoolMemoryStrategy);
|
RCLCPP_SMART_PTR_DEFINITIONS(MessagePoolMemoryStrategy);
|
||||||
MessagePoolMemoryStrategy()
|
MessagePoolMemoryStrategy()
|
||||||
: next_array_index_(0)
|
: next_array_index_(0)
|
||||||
{
|
{
|
||||||
|
|
|
@ -44,8 +44,7 @@ class SubscriptionBase
|
||||||
friend class rclcpp::executor::Executor;
|
friend class rclcpp::executor::Executor;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
typedef std::weak_ptr<SubscriptionBase> WeakPtr;
|
RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(SubscriptionBase);
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(SubscriptionBase);
|
|
||||||
|
|
||||||
SubscriptionBase(
|
SubscriptionBase(
|
||||||
std::shared_ptr<rmw_node_t> node_handle,
|
std::shared_ptr<rmw_node_t> node_handle,
|
||||||
|
@ -98,8 +97,7 @@ class Subscription : public SubscriptionBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
typedef std::function<void (const std::shared_ptr<MessageT> &)> CallbackType;
|
typedef std::function<void (const std::shared_ptr<MessageT> &)> CallbackType;
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(Subscription);
|
RCLCPP_SMART_PTR_DEFINITIONS(Subscription);
|
||||||
typedef std::weak_ptr<Subscription> WeakPtr;
|
|
||||||
|
|
||||||
Subscription(
|
Subscription(
|
||||||
std::shared_ptr<rmw_node_t> node_handle,
|
std::shared_ptr<rmw_node_t> node_handle,
|
||||||
|
|
|
@ -47,8 +47,7 @@ class TimerBase
|
||||||
friend class rclcpp::executor::Executor;
|
friend class rclcpp::executor::Executor;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(TimerBase);
|
RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(TimerBase);
|
||||||
typedef std::weak_ptr<TimerBase> WeakPtr;
|
|
||||||
|
|
||||||
TimerBase(std::chrono::nanoseconds period, CallbackType callback)
|
TimerBase(std::chrono::nanoseconds period, CallbackType callback)
|
||||||
: period_(period),
|
: period_(period),
|
||||||
|
@ -89,8 +88,7 @@ class GenericTimer : public TimerBase
|
||||||
friend class rclcpp::executor::Executor;
|
friend class rclcpp::executor::Executor;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
RCLCPP_MAKE_SHARED_DEFINITIONS(GenericTimer);
|
RCLCPP_SMART_PTR_DEFINITIONS(GenericTimer);
|
||||||
typedef std::weak_ptr<GenericTimer> WeakPtr;
|
|
||||||
|
|
||||||
GenericTimer(std::chrono::nanoseconds period, CallbackType callback)
|
GenericTimer(std::chrono::nanoseconds period, CallbackType callback)
|
||||||
: TimerBase(period, callback), loop_rate_(period)
|
: TimerBase(period, callback), loop_rate_(period)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue