From 32160febc12b7b3a4ca0cd718cbc5917d3b2763f Mon Sep 17 00:00:00 2001 From: William Woodall Date: Fri, 14 Aug 2015 10:32:55 -0700 Subject: [PATCH] refactored the smart ptr class macros --- rclcpp/include/rclcpp/any_executable.hpp | 6 +-- rclcpp/include/rclcpp/callback_group.hpp | 2 +- rclcpp/include/rclcpp/client.hpp | 4 +- rclcpp/include/rclcpp/context.hpp | 2 +- .../rclcpp/contexts/default_context.hpp | 2 +- rclcpp/include/rclcpp/executor.hpp | 2 +- .../executors/multi_threaded_executor.hpp | 2 +- .../executors/single_threaded_executor.hpp | 2 +- rclcpp/include/rclcpp/macros.hpp | 51 +++++++++++++++++-- rclcpp/include/rclcpp/memory_strategy.hpp | 2 +- .../rclcpp/message_memory_strategy.hpp | 2 +- rclcpp/include/rclcpp/node.hpp | 2 +- rclcpp/include/rclcpp/parameter_client.hpp | 4 +- rclcpp/include/rclcpp/parameter_service.hpp | 2 +- rclcpp/include/rclcpp/publisher.hpp | 2 +- rclcpp/include/rclcpp/rate.hpp | 4 +- rclcpp/include/rclcpp/service.hpp | 4 +- .../message_pool_memory_strategy.hpp | 2 +- rclcpp/include/rclcpp/subscription.hpp | 6 +-- rclcpp/include/rclcpp/timer.hpp | 6 +-- 20 files changed, 74 insertions(+), 35 deletions(-) diff --git a/rclcpp/include/rclcpp/any_executable.hpp b/rclcpp/include/rclcpp/any_executable.hpp index 001441e..3f698f1 100644 --- a/rclcpp/include/rclcpp/any_executable.hpp +++ b/rclcpp/include/rclcpp/any_executable.hpp @@ -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"); // you may not use this file except in compliance with the License. @@ -27,11 +27,11 @@ namespace executor struct AnyExecutable { - RCLCPP_MAKE_SHARED_DEFINITIONS(AnyExecutable); + RCLCPP_SMART_PTR_DEFINITIONS(AnyExecutable); AnyExecutable() : 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::timer::TimerBase::SharedPtr timer; rclcpp::service::ServiceBase::SharedPtr service; diff --git a/rclcpp/include/rclcpp/callback_group.hpp b/rclcpp/include/rclcpp/callback_group.hpp index 34207a0..d3834e8 100644 --- a/rclcpp/include/rclcpp/callback_group.hpp +++ b/rclcpp/include/rclcpp/callback_group.hpp @@ -52,7 +52,7 @@ class CallbackGroup friend class rclcpp::executor::Executor; public: - RCLCPP_MAKE_SHARED_DEFINITIONS(CallbackGroup); + RCLCPP_SMART_PTR_DEFINITIONS(CallbackGroup); CallbackGroup(CallbackGroupType group_type) : type_(group_type), can_be_taken_from_(true) diff --git a/rclcpp/include/rclcpp/client.hpp b/rclcpp/include/rclcpp/client.hpp index c41963f..024d0f2 100644 --- a/rclcpp/include/rclcpp/client.hpp +++ b/rclcpp/include/rclcpp/client.hpp @@ -45,7 +45,7 @@ class ClientBase friend class rclcpp::executor::Executor; public: - RCLCPP_MAKE_SHARED_DEFINITIONS(ClientBase); + RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(ClientBase); ClientBase( std::shared_ptr node_handle, @@ -99,7 +99,7 @@ public: typedef std::function CallbackType; - RCLCPP_MAKE_SHARED_DEFINITIONS(Client); + RCLCPP_SMART_PTR_DEFINITIONS(Client); Client( std::shared_ptr node_handle, diff --git a/rclcpp/include/rclcpp/context.hpp b/rclcpp/include/rclcpp/context.hpp index f259fa1..5528792 100644 --- a/rclcpp/include/rclcpp/context.hpp +++ b/rclcpp/include/rclcpp/context.hpp @@ -28,7 +28,7 @@ namespace context class Context { public: - RCLCPP_MAKE_SHARED_DEFINITIONS(Context); + RCLCPP_SMART_PTR_DEFINITIONS(Context); Context() {} diff --git a/rclcpp/include/rclcpp/contexts/default_context.hpp b/rclcpp/include/rclcpp/contexts/default_context.hpp index 0ccceb6..dc78f78 100644 --- a/rclcpp/include/rclcpp/contexts/default_context.hpp +++ b/rclcpp/include/rclcpp/contexts/default_context.hpp @@ -27,7 +27,7 @@ namespace default_context class DefaultContext : public rclcpp::context::Context { public: - RCLCPP_MAKE_SHARED_DEFINITIONS(DefaultContext); + RCLCPP_SMART_PTR_DEFINITIONS(DefaultContext); DefaultContext() {} diff --git a/rclcpp/include/rclcpp/executor.hpp b/rclcpp/include/rclcpp/executor.hpp index 11df4a0..e560018 100644 --- a/rclcpp/include/rclcpp/executor.hpp +++ b/rclcpp/include/rclcpp/executor.hpp @@ -40,7 +40,7 @@ class Executor friend class memory_strategy::MemoryStrategy; public: - RCLCPP_MAKE_SHARED_DEFINITIONS(Executor); + RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(Executor); explicit Executor(memory_strategy::MemoryStrategy::SharedPtr ms = memory_strategy::create_default_strategy()) diff --git a/rclcpp/include/rclcpp/executors/multi_threaded_executor.hpp b/rclcpp/include/rclcpp/executors/multi_threaded_executor.hpp index 68c6e29..a251560 100644 --- a/rclcpp/include/rclcpp/executors/multi_threaded_executor.hpp +++ b/rclcpp/include/rclcpp/executors/multi_threaded_executor.hpp @@ -38,7 +38,7 @@ namespace multi_threaded_executor class MultiThreadedExecutor : public executor::Executor { public: - RCLCPP_MAKE_SHARED_DEFINITIONS(MultiThreadedExecutor); + RCLCPP_SMART_PTR_DEFINITIONS(MultiThreadedExecutor); MultiThreadedExecutor(memory_strategy::MemoryStrategy::SharedPtr ms = memory_strategy::create_default_strategy()) diff --git a/rclcpp/include/rclcpp/executors/single_threaded_executor.hpp b/rclcpp/include/rclcpp/executors/single_threaded_executor.hpp index 2fa1e02..96c9fa7 100644 --- a/rclcpp/include/rclcpp/executors/single_threaded_executor.hpp +++ b/rclcpp/include/rclcpp/executors/single_threaded_executor.hpp @@ -39,7 +39,7 @@ namespace single_threaded_executor class SingleThreadedExecutor : public executor::Executor { public: - RCLCPP_MAKE_SHARED_DEFINITIONS(SingleThreadedExecutor); + RCLCPP_SMART_PTR_DEFINITIONS(SingleThreadedExecutor); SingleThreadedExecutor(memory_strategy::MemoryStrategy::SharedPtr ms = memory_strategy::create_default_strategy()) diff --git a/rclcpp/include/rclcpp/macros.hpp b/rclcpp/include/rclcpp/macros.hpp index b76e74f..4f7c8da 100644 --- a/rclcpp/include/rclcpp/macros.hpp +++ b/rclcpp/include/rclcpp/macros.hpp @@ -23,14 +23,33 @@ Class(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. * Make sure to include in the header when using this. */ -#define RCLCPP_MAKE_SHARED_DEFINITIONS(Class) \ - typedef std::shared_ptr SharedPtr; \ - \ +#define RCLCPP_SMART_PTR_DEFINITIONS(Class) \ + 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 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; + +#define __RCLCPP_MAKE_SHARED_DEFINITION(Class) \ template \ static std::shared_ptr \ make_shared(Args && ... args) \ @@ -38,6 +57,30 @@ return std::make_shared(std::forward(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; + +/// 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; + +#define __RCLCPP_MAKE_UNIQUE_DEFINITION(Class) \ + template \ + static std::unique_ptr \ + make_unique(Args && ... args) \ + { \ + return std::unique_ptr(new Class(std::forward(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; #endif /* RCLCPP_RCLCPP_MACROS_HPP_ */ diff --git a/rclcpp/include/rclcpp/memory_strategy.hpp b/rclcpp/include/rclcpp/memory_strategy.hpp index 8dc4944..3d7d5f5 100644 --- a/rclcpp/include/rclcpp/memory_strategy.hpp +++ b/rclcpp/include/rclcpp/memory_strategy.hpp @@ -36,7 +36,7 @@ class MemoryStrategy friend class executor::Executor; public: - RCLCPP_MAKE_SHARED_DEFINITIONS(MemoryStrategy); + RCLCPP_SMART_PTR_DEFINITIONS(MemoryStrategy); virtual void ** borrow_handles(HandleType type, size_t number_of_handles) { (void)type; diff --git a/rclcpp/include/rclcpp/message_memory_strategy.hpp b/rclcpp/include/rclcpp/message_memory_strategy.hpp index 58c9807..bc9a2f9 100644 --- a/rclcpp/include/rclcpp/message_memory_strategy.hpp +++ b/rclcpp/include/rclcpp/message_memory_strategy.hpp @@ -29,7 +29,7 @@ class MessageMemoryStrategy { public: - RCLCPP_MAKE_SHARED_DEFINITIONS(MessageMemoryStrategy); + RCLCPP_SMART_PTR_DEFINITIONS(MessageMemoryStrategy); static SharedPtr create_default() { diff --git a/rclcpp/include/rclcpp/node.hpp b/rclcpp/include/rclcpp/node.hpp index 710f02c..61a916d 100644 --- a/rclcpp/include/rclcpp/node.hpp +++ b/rclcpp/include/rclcpp/node.hpp @@ -100,7 +100,7 @@ class Node friend class rclcpp::executor::Executor; public: - RCLCPP_MAKE_SHARED_DEFINITIONS(Node); + RCLCPP_SMART_PTR_DEFINITIONS(Node); /* Create a node based on the node name. */ Node(const std::string & node_name); diff --git a/rclcpp/include/rclcpp/parameter_client.hpp b/rclcpp/include/rclcpp/parameter_client.hpp index eb5efd9..0971356 100644 --- a/rclcpp/include/rclcpp/parameter_client.hpp +++ b/rclcpp/include/rclcpp/parameter_client.hpp @@ -44,7 +44,7 @@ class AsyncParametersClient { public: - RCLCPP_MAKE_SHARED_DEFINITIONS(AsyncParametersClient); + RCLCPP_SMART_PTR_DEFINITIONS(AsyncParametersClient); AsyncParametersClient(const rclcpp::node::Node::SharedPtr & node, const std::string & remote_node_name = "") @@ -252,7 +252,7 @@ private: class SyncParametersClient { public: - RCLCPP_MAKE_SHARED_DEFINITIONS(SyncParametersClient); + RCLCPP_SMART_PTR_DEFINITIONS(SyncParametersClient); SyncParametersClient( rclcpp::node::Node::SharedPtr & node) diff --git a/rclcpp/include/rclcpp/parameter_service.hpp b/rclcpp/include/rclcpp/parameter_service.hpp index 9bae3ec..a6d2a60 100644 --- a/rclcpp/include/rclcpp/parameter_service.hpp +++ b/rclcpp/include/rclcpp/parameter_service.hpp @@ -41,7 +41,7 @@ class ParameterService { public: - RCLCPP_MAKE_SHARED_DEFINITIONS(ParameterService); + RCLCPP_SMART_PTR_DEFINITIONS(ParameterService); ParameterService(const rclcpp::node::Node::SharedPtr & node) : node_(node) diff --git a/rclcpp/include/rclcpp/publisher.hpp b/rclcpp/include/rclcpp/publisher.hpp index b31b2dc..3a8cc38 100644 --- a/rclcpp/include/rclcpp/publisher.hpp +++ b/rclcpp/include/rclcpp/publisher.hpp @@ -39,7 +39,7 @@ namespace publisher class Publisher { public: - RCLCPP_MAKE_SHARED_DEFINITIONS(Publisher); + RCLCPP_SMART_PTR_DEFINITIONS(Publisher); Publisher(std::shared_ptr node_handle, rmw_publisher_t * publisher_handle) : node_handle_(node_handle), publisher_handle_(publisher_handle) diff --git a/rclcpp/include/rclcpp/rate.hpp b/rclcpp/include/rclcpp/rate.hpp index da68ee9..e3a11cb 100644 --- a/rclcpp/include/rclcpp/rate.hpp +++ b/rclcpp/include/rclcpp/rate.hpp @@ -30,7 +30,7 @@ namespace rate class RateBase { public: - RCLCPP_MAKE_SHARED_DEFINITIONS(RateBase); + RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(RateBase); virtual bool sleep() = 0; virtual bool is_steady() = 0; @@ -45,7 +45,7 @@ template class GenericRate : public RateBase { public: - RCLCPP_MAKE_SHARED_DEFINITIONS(GenericRate); + RCLCPP_SMART_PTR_DEFINITIONS(GenericRate); GenericRate(double rate) : GenericRate( diff --git a/rclcpp/include/rclcpp/service.hpp b/rclcpp/include/rclcpp/service.hpp index 6c756c1..2b94c18 100644 --- a/rclcpp/include/rclcpp/service.hpp +++ b/rclcpp/include/rclcpp/service.hpp @@ -44,7 +44,7 @@ class ServiceBase friend class rclcpp::executor::Executor; public: - RCLCPP_MAKE_SHARED_DEFINITIONS(ServiceBase); + RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(ServiceBase); ServiceBase( std::shared_ptr node_handle, @@ -105,7 +105,7 @@ public: const std::shared_ptr &, const std::shared_ptr &, std::shared_ptr &)> CallbackWithHeaderType; - RCLCPP_MAKE_SHARED_DEFINITIONS(Service); + RCLCPP_SMART_PTR_DEFINITIONS(Service); Service( std::shared_ptr node_handle, diff --git a/rclcpp/include/rclcpp/strategies/message_pool_memory_strategy.hpp b/rclcpp/include/rclcpp/strategies/message_pool_memory_strategy.hpp index 0e437eb..098078a 100644 --- a/rclcpp/include/rclcpp/strategies/message_pool_memory_strategy.hpp +++ b/rclcpp/include/rclcpp/strategies/message_pool_memory_strategy.hpp @@ -32,7 +32,7 @@ class MessagePoolMemoryStrategy : public message_memory_strategy::MessageMemoryStrategy { public: - RCLCPP_MAKE_SHARED_DEFINITIONS(MessagePoolMemoryStrategy); + RCLCPP_SMART_PTR_DEFINITIONS(MessagePoolMemoryStrategy); MessagePoolMemoryStrategy() : next_array_index_(0) { diff --git a/rclcpp/include/rclcpp/subscription.hpp b/rclcpp/include/rclcpp/subscription.hpp index b2c659c..297ca3f 100644 --- a/rclcpp/include/rclcpp/subscription.hpp +++ b/rclcpp/include/rclcpp/subscription.hpp @@ -44,8 +44,7 @@ class SubscriptionBase friend class rclcpp::executor::Executor; public: - typedef std::weak_ptr WeakPtr; - RCLCPP_MAKE_SHARED_DEFINITIONS(SubscriptionBase); + RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(SubscriptionBase); SubscriptionBase( std::shared_ptr node_handle, @@ -98,8 +97,7 @@ class Subscription : public SubscriptionBase { public: typedef std::function &)> CallbackType; - RCLCPP_MAKE_SHARED_DEFINITIONS(Subscription); - typedef std::weak_ptr WeakPtr; + RCLCPP_SMART_PTR_DEFINITIONS(Subscription); Subscription( std::shared_ptr node_handle, diff --git a/rclcpp/include/rclcpp/timer.hpp b/rclcpp/include/rclcpp/timer.hpp index 52bc886..ed738d3 100644 --- a/rclcpp/include/rclcpp/timer.hpp +++ b/rclcpp/include/rclcpp/timer.hpp @@ -47,8 +47,7 @@ class TimerBase friend class rclcpp::executor::Executor; public: - RCLCPP_MAKE_SHARED_DEFINITIONS(TimerBase); - typedef std::weak_ptr WeakPtr; + RCLCPP_SMART_PTR_DEFINITIONS_NOT_COPYABLE(TimerBase); TimerBase(std::chrono::nanoseconds period, CallbackType callback) : period_(period), @@ -89,8 +88,7 @@ class GenericTimer : public TimerBase friend class rclcpp::executor::Executor; public: - RCLCPP_MAKE_SHARED_DEFINITIONS(GenericTimer); - typedef std::weak_ptr WeakPtr; + RCLCPP_SMART_PTR_DEFINITIONS(GenericTimer); GenericTimer(std::chrono::nanoseconds period, CallbackType callback) : TimerBase(period, callback), loop_rate_(period)