Skip to content

Commit 66eb056

Browse files
Janosch MachowinskiJanosch Machowinski
authored andcommitted
feat: Provide a way to suppress the deprecation warning
1 parent 59f6957 commit 66eb056

6 files changed

Lines changed: 92 additions & 22 deletions

File tree

rclcpp/include/rclcpp/allocator/allocator_common.hpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,39 @@ namespace rclcpp
2727
namespace allocator
2828
{
2929

30+
template<typename T>
31+
using clean_t = std::remove_cv_t<std::remove_reference_t<T>>;
32+
33+
// Primary template: false
34+
template<typename, typename = std::void_t<>>
35+
struct has_get_rcl_allocator : std::false_type {};
36+
37+
// Specialization: true if expression is valid
38+
template<typename T>
39+
struct has_get_rcl_allocator<T,
40+
std::void_t<
41+
decltype(std::declval<clean_t<T> &>().get_rcl_allocator())
42+
>
43+
>
44+
: std::bool_constant<
45+
std::is_same_v<
46+
decltype(std::declval<clean_t<T> &>().get_rcl_allocator()),
47+
rcl_allocator_t
48+
>
49+
>
50+
{};
51+
52+
// Helper variable template
53+
template<typename T>
54+
inline constexpr bool has_get_rcl_allocator_v =
55+
has_get_rcl_allocator<T>::value;
56+
3057
template<typename T, typename Alloc>
3158
using AllocRebind = typename std::allocator_traits<Alloc>::template rebind_traits<T>;
3259

3360
template<typename Alloc>
61+
[[deprecated("Conversion of C++ allocators to C style is not valid, as the size on deallocate"
62+
"can not be determined. This will be remove in future versions of ros.")]]
3463
void * retyped_allocate(size_t size, void * untyped_allocator)
3564
{
3665
auto typed_allocator = static_cast<Alloc *>(untyped_allocator);
@@ -41,6 +70,8 @@ void * retyped_allocate(size_t size, void * untyped_allocator)
4170
}
4271

4372
template<typename Alloc>
73+
[[deprecated("Conversion of C++ allocators to C style is not valid, as the size on deallocate"
74+
"can not be determined. This will be remove in future versions of ros.")]]
4475
void * retyped_zero_allocate(size_t number_of_elem, size_t size_of_elem, void * untyped_allocator)
4576
{
4677
auto typed_allocator = static_cast<Alloc *>(untyped_allocator);
@@ -57,6 +88,8 @@ void * retyped_zero_allocate(size_t number_of_elem, size_t size_of_elem, void *
5788
}
5889

5990
template<typename T, typename Alloc>
91+
[[deprecated("Conversion of C++ allocators to C style is not valid, as the size on deallocate"
92+
"can not be determined. This will be remove in future versions of ros.")]]
6093
void retyped_deallocate(void * untyped_pointer, void * untyped_allocator)
6194
{
6295
auto typed_allocator = static_cast<Alloc *>(untyped_allocator);
@@ -68,6 +101,8 @@ void retyped_deallocate(void * untyped_pointer, void * untyped_allocator)
68101
}
69102

70103
template<typename T, typename Alloc>
104+
[[deprecated("Conversion of C++ allocators to C style is not valid, as the size on deallocate"
105+
"can not be determined. This will be remove in future versions of ros.")]]
71106
void * retyped_reallocate(void * untyped_pointer, size_t size, void * untyped_allocator)
72107
{
73108
auto typed_allocator = static_cast<Alloc *>(untyped_allocator);
@@ -86,7 +121,8 @@ template<
86121
typename Alloc,
87122
typename std::enable_if<!std::is_same<Alloc, std::allocator<void>>::value>::type * = nullptr>
88123
[[deprecated("Conversion of C++ allocators to C style is not valid, as the size on deallocate"
89-
"can not be determined. This will be remove in future versions of ros.")]]
124+
"can not be determined. This will be remove in future versions of ros. To suppress this warning"
125+
"define the method 'rcl_allocator_t get_rcl_allocator()' on your allocator")]]
90126
rcl_allocator_t get_rcl_allocator(Alloc & allocator)
91127
{
92128
rcl_allocator_t rcl_allocator = rcl_get_default_allocator();

rclcpp/include/rclcpp/message_memory_strategy.hpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,12 @@ class MessageMemoryStrategy
6565
if constexpr (std::is_same_v<Alloc, std::allocator<void>>) {
6666
rcutils_allocator_ = rcl_get_default_allocator();
6767
} else {
68-
rcutils_allocator_ = allocator::get_rcl_allocator<char,
69-
BufferAlloc>(*buffer_allocator_.get());
68+
if constexpr (rclcpp::allocator::has_get_rcl_allocator_v<Alloc>) {
69+
rcutils_allocator_ = message_allocator_->get_rcl_allocator();
70+
} else {
71+
rcutils_allocator_ = allocator::get_rcl_allocator<char,
72+
BufferAlloc>(*buffer_allocator_.get());
73+
}
7074
}
7175
}
7276

@@ -78,8 +82,12 @@ class MessageMemoryStrategy
7882
if constexpr (std::is_same_v<Alloc, std::allocator<void>>) {
7983
rcutils_allocator_ = rcl_get_default_allocator();
8084
} else {
81-
rcutils_allocator_ = allocator::get_rcl_allocator<char,
82-
BufferAlloc>(*buffer_allocator_.get());
85+
if constexpr (rclcpp::allocator::has_get_rcl_allocator_v<Alloc>) {
86+
rcutils_allocator_ = allocator->get_rcl_allocator();
87+
} else {
88+
rcutils_allocator_ = allocator::get_rcl_allocator<char,
89+
BufferAlloc>(*buffer_allocator_.get());
90+
}
8391
}
8492
}
8593

rclcpp/include/rclcpp/publisher_options.hpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,18 @@ struct PublisherOptionsWithAllocator : public PublisherOptionsBase
125125
{
126126
if constexpr (std::is_same_v<Allocator, std::allocator<void>>) {
127127
return rcl_get_default_allocator();
128+
} else {
129+
if constexpr (rclcpp::allocator::has_get_rcl_allocator_v<Allocator>) {
130+
return get_allocator()->get_rcl_allocator();
131+
} else {
132+
if (!plain_allocator_storage_) {
133+
plain_allocator_storage_ =
134+
std::make_shared<PlainAllocator>(*this->get_allocator());
135+
}
136+
137+
return rclcpp::allocator::get_rcl_allocator<char>(*plain_allocator_storage_);
138+
}
128139
}
129-
130-
if (!plain_allocator_storage_) {
131-
plain_allocator_storage_ =
132-
std::make_shared<PlainAllocator>(*this->get_allocator());
133-
}
134-
return rclcpp::allocator::get_rcl_allocator<char>(*plain_allocator_storage_);
135140
}
136141

137142
// This is a temporal workaround, to make sure that get_allocator()
@@ -143,13 +148,6 @@ struct PublisherOptionsWithAllocator : public PublisherOptionsBase
143148
mutable std::shared_ptr<PlainAllocator> plain_allocator_storage_;
144149
};
145150

146-
template<>
147-
inline rcl_allocator_t
148-
PublisherOptionsWithAllocator<std::allocator<void>>::get_rcl_allocator() const
149-
{
150-
return rcl_get_default_allocator();
151-
}
152-
153151
using PublisherOptions = PublisherOptionsWithAllocator<std::allocator<void>>;
154152

155153
} // namespace rclcpp

rclcpp/include/rclcpp/subscription_options.hpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,16 @@ struct SubscriptionOptionsWithAllocator : public SubscriptionOptionsBase
170170
if constexpr (std::is_same_v<Allocator, std::allocator<void>>) {
171171
return rcl_get_default_allocator();
172172
} else {
173-
if (!plain_allocator_storage_) {
174-
plain_allocator_storage_ =
175-
std::make_shared<PlainAllocator>(*this->get_allocator());
173+
if constexpr (rclcpp::allocator::has_get_rcl_allocator_v<Allocator>) {
174+
return get_allocator()->get_rcl_allocator();
175+
} else {
176+
if (!plain_allocator_storage_) {
177+
plain_allocator_storage_ =
178+
std::make_shared<PlainAllocator>(*this->get_allocator());
179+
}
180+
181+
return rclcpp::allocator::get_rcl_allocator<char>(*plain_allocator_storage_);
176182
}
177-
return rclcpp::allocator::get_rcl_allocator<char>(*plain_allocator_storage_);
178183
}
179184
}
180185

rclcpp/test/rclcpp/allocator/test_allocator_common.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
#include <memory>
1818

1919
#include "rclcpp/allocator/allocator_common.hpp"
20+
#include "rcpputils/compile_warnings.hpp"
2021

2122
TEST(TestAllocatorCommon, retyped_allocate) {
2223
std::allocator<int> allocator;
2324
void * untyped_allocator = &allocator;
25+
RCPPUTILS_DEPRECATION_WARNING_OFF_START
2426
void * allocated_mem =
2527
rclcpp::allocator::retyped_allocate<std::allocator<char>>(1u, untyped_allocator);
2628
// The more natural check here is ASSERT_NE(nullptr, ptr), but clang static
@@ -49,9 +51,12 @@ TEST(TestAllocatorCommon, retyped_allocate) {
4951
reallocated_mem, untyped_allocator);
5052
};
5153
EXPECT_NO_THROW(code2());
54+
55+
RCPPUTILS_DEPRECATION_WARNING_OFF_STOP
5256
}
5357

5458
TEST(TestAllocatorCommon, retyped_zero_allocate_basic) {
59+
RCPPUTILS_DEPRECATION_WARNING_OFF_START
5560
std::allocator<int> allocator;
5661
void * untyped_allocator = &allocator;
5762
void * allocated_mem =
@@ -63,9 +68,11 @@ TEST(TestAllocatorCommon, retyped_zero_allocate_basic) {
6368
allocated_mem, untyped_allocator);
6469
};
6570
EXPECT_NO_THROW(code());
71+
RCPPUTILS_DEPRECATION_WARNING_OFF_STOP
6672
}
6773

6874
TEST(TestAllocatorCommon, retyped_zero_allocate) {
75+
RCPPUTILS_DEPRECATION_WARNING_OFF_START
6976
std::allocator<int> allocator;
7077
void * untyped_allocator = &allocator;
7178
void * allocated_mem =
@@ -96,19 +103,24 @@ TEST(TestAllocatorCommon, retyped_zero_allocate) {
96103
reallocated_mem, untyped_allocator);
97104
};
98105
EXPECT_NO_THROW(code2());
106+
RCPPUTILS_DEPRECATION_WARNING_OFF_STOP
99107
}
100108

101109
TEST(TestAllocatorCommon, get_rcl_allocator) {
110+
RCPPUTILS_DEPRECATION_WARNING_OFF_START
102111
std::allocator<int> allocator;
103112
auto rcl_allocator = rclcpp::allocator::get_rcl_allocator<int>(allocator);
104113
EXPECT_NE(nullptr, rcl_allocator.allocate);
105114
EXPECT_NE(nullptr, rcl_allocator.deallocate);
106115
EXPECT_NE(nullptr, rcl_allocator.reallocate);
107116
EXPECT_NE(nullptr, rcl_allocator.zero_allocate);
108117
// Not testing state as that may or may not be null depending on platform
118+
RCPPUTILS_DEPRECATION_WARNING_OFF_STOP
109119
}
110120

111121
TEST(TestAllocatorCommon, get_void_rcl_allocator) {
122+
RCPPUTILS_DEPRECATION_WARNING_OFF_START
123+
112124
std::allocator<void> allocator;
113125
auto rcl_allocator =
114126
rclcpp::allocator::get_rcl_allocator<void, std::allocator<void>>(allocator);
@@ -117,4 +129,5 @@ TEST(TestAllocatorCommon, get_void_rcl_allocator) {
117129
EXPECT_NE(nullptr, rcl_allocator.reallocate);
118130
EXPECT_NE(nullptr, rcl_allocator.zero_allocate);
119131
// Not testing state as that may or may not be null depending on platform
132+
RCPPUTILS_DEPRECATION_WARNING_OFF_STOP
120133
}

rclcpp/test/rclcpp/test_intra_process_manager_with_allocators.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ struct MyAllocator
7575
{
7676
typedef MyAllocator<U> other;
7777
};
78+
79+
rcl_allocator_t get_rcl_allocator()
80+
{
81+
return rcl_get_default_allocator();
82+
}
7883
};
7984

8085
// Explicit specialization for void
@@ -102,6 +107,11 @@ struct MyAllocator<void>
102107
{
103108
typedef MyAllocator<U> other;
104109
};
110+
111+
rcl_allocator_t get_rcl_allocator()
112+
{
113+
return rcl_get_default_allocator();
114+
}
105115
};
106116

107117
template<typename T, typename U>

0 commit comments

Comments
 (0)