.. _program_listing_file_include_rclcpp_allocator_allocator_deleter.hpp: Program Listing for File allocator_deleter.hpp ============================================== |exhale_lsh| :ref:`Return to documentation for file ` (``include/rclcpp/allocator/allocator_deleter.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp // 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. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef RCLCPP__ALLOCATOR__ALLOCATOR_DELETER_HPP_ #define RCLCPP__ALLOCATOR__ALLOCATOR_DELETER_HPP_ #include #include namespace rclcpp { namespace allocator { template class AllocatorDeleter { template using AllocRebind = typename std::allocator_traits::template rebind_alloc; public: AllocatorDeleter() : allocator_(nullptr) { } explicit AllocatorDeleter(Allocator * a) : allocator_(a) { } template explicit AllocatorDeleter(const AllocatorDeleter & a) { allocator_ = a.get_allocator(); } template void operator()(T * ptr) { std::allocator_traits>::destroy(*allocator_, ptr); std::allocator_traits>::deallocate(*allocator_, ptr, 1); ptr = nullptr; } Allocator * get_allocator() const { return allocator_; } void set_allocator(Allocator * alloc) { allocator_ = alloc; } private: Allocator * allocator_; }; template void set_allocator_for_deleter(D * deleter, Alloc * alloc) { (void) alloc; (void) deleter; throw std::runtime_error("Reached unexpected template specialization"); } template void set_allocator_for_deleter(std::default_delete * deleter, std::allocator * alloc) { (void) deleter; (void) alloc; } template void set_allocator_for_deleter(AllocatorDeleter * deleter, Alloc * alloc) { if (!deleter || !alloc) { throw std::invalid_argument("Argument was NULL to set_allocator_for_deleter"); } deleter->set_allocator(alloc); } template using Deleter = typename std::conditional< std::is_same::template rebind_alloc, std::allocator>::value, std::default_delete, AllocatorDeleter >::type; } // namespace allocator } // namespace rclcpp #endif // RCLCPP__ALLOCATOR__ALLOCATOR_DELETER_HPP_