-
Notifications
You must be signed in to change notification settings - Fork 417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[POC] Apply default GSG-based ROS2 format #872
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,51 +31,40 @@ template<typename ServiceT> | |
class AnyServiceCallback | ||
{ | ||
private: | ||
using SharedPtrCallback = std::function< | ||
void ( | ||
const std::shared_ptr<typename ServiceT::Request>, | ||
std::shared_ptr<typename ServiceT::Response> | ||
)>; | ||
using SharedPtrWithRequestHeaderCallback = std::function< | ||
void ( | ||
const std::shared_ptr<rmw_request_id_t>, | ||
const std::shared_ptr<typename ServiceT::Request>, | ||
std::shared_ptr<typename ServiceT::Response> | ||
)>; | ||
using SharedPtrCallback = std::function<void( | ||
const std::shared_ptr<typename ServiceT::Request>, | ||
std::shared_ptr<typename ServiceT::Response>)>; | ||
using SharedPtrWithRequestHeaderCallback = std::function<void( | ||
const std::shared_ptr<rmw_request_id_t>, | ||
const std::shared_ptr<typename ServiceT::Request>, | ||
std::shared_ptr<typename ServiceT::Response>)>; | ||
|
||
SharedPtrCallback shared_ptr_callback_; | ||
SharedPtrWithRequestHeaderCallback shared_ptr_with_request_header_callback_; | ||
|
||
public: | ||
AnyServiceCallback() | ||
: shared_ptr_callback_(nullptr), shared_ptr_with_request_header_callback_(nullptr) | ||
{} | ||
{ | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another case where both are perfectly valid ways to write the code (while I see minor value in one over the other, neither are unreadable), but since the developer has some freedom here they can save some vertical space in a case where using more doesn't help. However, If they think it does help for some reason (maybe it makes it more consistent with other functions in the same file that have single statement bodies, or maybe it will reduce the diff in the future), then them having the flexibility to make that choice (and defend it if needed, to a reviewer who questions it) is very important in my opinion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
IMHO this is key to this entire discussion. It doesn't seem to me like Now, that may not be ideal nor appropriate for ROS2. If it's not, then maybe |
||
|
||
AnyServiceCallback(const AnyServiceCallback &) = default; | ||
|
||
template< | ||
typename CallbackT, | ||
typename std::enable_if< | ||
rclcpp::function_traits::same_arguments< | ||
CallbackT, | ||
SharedPtrCallback | ||
>::value | ||
>::type * = nullptr | ||
> | ||
rclcpp::function_traits::same_arguments<CallbackT, SharedPtrCallback>::value>::type * = | ||
nullptr> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a case where I think the clang-format way is less readable (my personal opinion obviously), and that the previous way was actually a lot better, even though it takes up more vertical space. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree on this one too. |
||
void set(CallbackT callback) | ||
{ | ||
shared_ptr_callback_ = callback; | ||
} | ||
|
||
template< | ||
typename CallbackT, | ||
typename std::enable_if< | ||
rclcpp::function_traits::same_arguments< | ||
CallbackT, | ||
SharedPtrWithRequestHeaderCallback | ||
>::value | ||
>::type * = nullptr | ||
> | ||
typename std::enable_if<rclcpp::function_traits::same_arguments< | ||
CallbackT, | ||
SharedPtrWithRequestHeaderCallback>::value>::type * = nullptr> | ||
void set(CallbackT callback) | ||
{ | ||
shared_ptr_with_request_header_callback_ = callback; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,15 +39,15 @@ class AnySubscriptionCallback | |
using ConstMessageSharedPtr = std::shared_ptr<const MessageT>; | ||
using MessageUniquePtr = std::unique_ptr<MessageT, MessageDeleter>; | ||
|
||
using SharedPtrCallback = std::function<void (const std::shared_ptr<MessageT>)>; | ||
using SharedPtrCallback = std::function<void(const std::shared_ptr<MessageT>)>; | ||
using SharedPtrWithInfoCallback = | ||
std::function<void (const std::shared_ptr<MessageT>, const rmw_message_info_t &)>; | ||
using ConstSharedPtrCallback = std::function<void (const std::shared_ptr<const MessageT>)>; | ||
std::function<void(const std::shared_ptr<MessageT>, const rmw_message_info_t &)>; | ||
using ConstSharedPtrCallback = std::function<void(const std::shared_ptr<const MessageT>)>; | ||
using ConstSharedPtrWithInfoCallback = | ||
std::function<void (const std::shared_ptr<const MessageT>, const rmw_message_info_t &)>; | ||
using UniquePtrCallback = std::function<void (MessageUniquePtr)>; | ||
std::function<void(const std::shared_ptr<const MessageT>, const rmw_message_info_t &)>; | ||
using UniquePtrCallback = std::function<void(MessageUniquePtr)>; | ||
using UniquePtrWithInfoCallback = | ||
std::function<void (MessageUniquePtr, const rmw_message_info_t &)>; | ||
std::function<void(MessageUniquePtr, const rmw_message_info_t &)>; | ||
|
||
SharedPtrCallback shared_ptr_callback_; | ||
SharedPtrWithInfoCallback shared_ptr_with_info_callback_; | ||
|
@@ -58,9 +58,12 @@ class AnySubscriptionCallback | |
|
||
public: | ||
explicit AnySubscriptionCallback(std::shared_ptr<Alloc> allocator) | ||
: shared_ptr_callback_(nullptr), shared_ptr_with_info_callback_(nullptr), | ||
const_shared_ptr_callback_(nullptr), const_shared_ptr_with_info_callback_(nullptr), | ||
unique_ptr_callback_(nullptr), unique_ptr_with_info_callback_(nullptr) | ||
: shared_ptr_callback_(nullptr), | ||
shared_ptr_with_info_callback_(nullptr), | ||
const_shared_ptr_callback_(nullptr), | ||
const_shared_ptr_with_info_callback_(nullptr), | ||
unique_ptr_callback_(nullptr), | ||
unique_ptr_with_info_callback_(nullptr) | ||
{ | ||
message_allocator_ = std::make_shared<MessageAlloc>(*allocator.get()); | ||
allocator::set_allocator_for_deleter(&message_deleter_, message_allocator_.get()); | ||
|
@@ -71,12 +74,8 @@ class AnySubscriptionCallback | |
template< | ||
typename CallbackT, | ||
typename std::enable_if< | ||
rclcpp::function_traits::same_arguments< | ||
CallbackT, | ||
SharedPtrCallback | ||
>::value | ||
>::type * = nullptr | ||
> | ||
rclcpp::function_traits::same_arguments<CallbackT, SharedPtrCallback>::value>::type * = | ||
nullptr> | ||
void set(CallbackT callback) | ||
{ | ||
shared_ptr_callback_ = callback; | ||
|
@@ -85,12 +84,8 @@ class AnySubscriptionCallback | |
template< | ||
typename CallbackT, | ||
typename std::enable_if< | ||
rclcpp::function_traits::same_arguments< | ||
CallbackT, | ||
SharedPtrWithInfoCallback | ||
>::value | ||
>::type * = nullptr | ||
> | ||
rclcpp::function_traits::same_arguments<CallbackT, SharedPtrWithInfoCallback>::value>:: | ||
type * = nullptr> | ||
void set(CallbackT callback) | ||
{ | ||
shared_ptr_with_info_callback_ = callback; | ||
|
@@ -99,12 +94,8 @@ class AnySubscriptionCallback | |
template< | ||
typename CallbackT, | ||
typename std::enable_if< | ||
rclcpp::function_traits::same_arguments< | ||
CallbackT, | ||
ConstSharedPtrCallback | ||
>::value | ||
>::type * = nullptr | ||
> | ||
rclcpp::function_traits::same_arguments<CallbackT, ConstSharedPtrCallback>::value>::type * = | ||
nullptr> | ||
void set(CallbackT callback) | ||
{ | ||
const_shared_ptr_callback_ = callback; | ||
|
@@ -113,12 +104,8 @@ class AnySubscriptionCallback | |
template< | ||
typename CallbackT, | ||
typename std::enable_if< | ||
rclcpp::function_traits::same_arguments< | ||
CallbackT, | ||
ConstSharedPtrWithInfoCallback | ||
>::value | ||
>::type * = nullptr | ||
> | ||
rclcpp::function_traits::same_arguments<CallbackT, ConstSharedPtrWithInfoCallback>::value>:: | ||
type * = nullptr> | ||
void set(CallbackT callback) | ||
{ | ||
const_shared_ptr_with_info_callback_ = callback; | ||
|
@@ -127,12 +114,8 @@ class AnySubscriptionCallback | |
template< | ||
typename CallbackT, | ||
typename std::enable_if< | ||
rclcpp::function_traits::same_arguments< | ||
CallbackT, | ||
UniquePtrCallback | ||
>::value | ||
>::type * = nullptr | ||
> | ||
rclcpp::function_traits::same_arguments<CallbackT, UniquePtrCallback>::value>::type * = | ||
nullptr> | ||
void set(CallbackT callback) | ||
{ | ||
unique_ptr_callback_ = callback; | ||
|
@@ -141,19 +124,14 @@ class AnySubscriptionCallback | |
template< | ||
typename CallbackT, | ||
typename std::enable_if< | ||
rclcpp::function_traits::same_arguments< | ||
CallbackT, | ||
UniquePtrWithInfoCallback | ||
>::value | ||
>::type * = nullptr | ||
> | ||
rclcpp::function_traits::same_arguments<CallbackT, UniquePtrWithInfoCallback>::value>:: | ||
type * = nullptr> | ||
void set(CallbackT callback) | ||
{ | ||
unique_ptr_with_info_callback_ = callback; | ||
} | ||
|
||
void dispatch( | ||
std::shared_ptr<MessageT> message, const rmw_message_info_t & message_info) | ||
void dispatch(std::shared_ptr<MessageT> message, const rmw_message_info_t & message_info) | ||
{ | ||
if (shared_ptr_callback_) { | ||
shared_ptr_callback_(message); | ||
|
@@ -185,20 +163,18 @@ class AnySubscriptionCallback | |
const_shared_ptr_with_info_callback_(message, message_info); | ||
} else { | ||
if ( | ||
unique_ptr_callback_ || unique_ptr_with_info_callback_ || | ||
shared_ptr_callback_ || shared_ptr_with_info_callback_) | ||
{ | ||
unique_ptr_callback_ || unique_ptr_with_info_callback_ || shared_ptr_callback_ || | ||
shared_ptr_with_info_callback_) { | ||
throw std::runtime_error( | ||
"unexpected dispatch_intra_process const shared " | ||
"message call with no const shared_ptr callback"); | ||
"unexpected dispatch_intra_process const shared " | ||
"message call with no const shared_ptr callback"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a case where clang-format is better, but I think there's an on-going issue about addressing this in uncrustify. However, it's a case where I just don't care. It's a bit unsightly, but it doesn't harm readability significantly, in my opinion. These are the kind of linter shortcomings that are annoying but not consequential in my opinion. |
||
} else { | ||
throw std::runtime_error("unexpected message without any callback set"); | ||
} | ||
} | ||
} | ||
|
||
void dispatch_intra_process( | ||
MessageUniquePtr message, const rmw_message_info_t & message_info) | ||
void dispatch_intra_process(MessageUniquePtr message, const rmw_message_info_t & message_info) | ||
{ | ||
if (shared_ptr_callback_) { | ||
typename std::shared_ptr<MessageT> shared_message = std::move(message); | ||
|
@@ -212,8 +188,8 @@ class AnySubscriptionCallback | |
unique_ptr_with_info_callback_(std::move(message), message_info); | ||
} else if (const_shared_ptr_callback_ || const_shared_ptr_with_info_callback_) { | ||
throw std::runtime_error( | ||
"unexpected dispatch_intra_process unique message call" | ||
" with const shared_ptr callback"); | ||
"unexpected dispatch_intra_process unique message call" | ||
" with const shared_ptr callback"); | ||
} else { | ||
throw std::runtime_error("unexpected message without any callback set"); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an example of something that, while still readable after applying clang-format, is not better in my opinion. I think the original author probably picked the style so it was most readable to them (within some basic formatting rules like max line length and wrapping style).
If a pr came in with the new style I wouldn't ask them to change it (unless it was particularly tricky to read in some case), but I do think that letting the developer have some autonomy to decide how it looks best is super important. That's my main concern with
clang-format
, as it doesn't have much flexibility for cases like this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I can confirm
clang-format
won't give a developer much freedom of choice.