Austin Schuh | 1676fec | 2019-12-28 20:17:34 -0800 | [diff] [blame] | 1 | #ifndef AOS_UNIQUE_MALLOC_PTR_H_ |
| 2 | #define AOS_UNIQUE_MALLOC_PTR_H_ |
| 3 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 4 | #include <memory> |
| 5 | |
| 6 | namespace aos { |
| 7 | |
Austin Schuh | 1676fec | 2019-12-28 20:17:34 -0800 | [diff] [blame] | 8 | namespace internal { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 9 | |
brians | da7756f | 2013-02-27 04:19:33 +0000 | [diff] [blame] | 10 | // Written as a functor so that it doesn't have to get passed to |
| 11 | // std::unique_ptr's constructor as an argument. |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 12 | template<typename T, void(*function)(T *)> |
brians | da7756f | 2013-02-27 04:19:33 +0000 | [diff] [blame] | 13 | class const_wrap { |
| 14 | public: |
| 15 | void operator()(const T *ptr) { |
| 16 | function(const_cast<T *>(ptr)); |
| 17 | } |
| 18 | }; |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 19 | |
| 20 | // Wrapper function to deal with the differences between C and C++ (C++ doesn't |
| 21 | // automatically convert T* to void* like C). |
| 22 | template<typename T> |
| 23 | void free_type(T *ptr) { ::free(reinterpret_cast<void *>(ptr)); } |
| 24 | |
Austin Schuh | 1676fec | 2019-12-28 20:17:34 -0800 | [diff] [blame] | 25 | } // namespace internal |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 26 | |
| 27 | // A std::unique_ptr that should get freed with a C-style free function |
| 28 | // (free(2) by default). |
Austin Schuh | 1676fec | 2019-12-28 20:17:34 -0800 | [diff] [blame] | 29 | template <typename T, void (*function)(T *) = internal::free_type<T>> |
| 30 | class unique_c_ptr |
| 31 | : public std::unique_ptr<T, internal::const_wrap<T, function>> { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 32 | public: |
Austin Schuh | 1676fec | 2019-12-28 20:17:34 -0800 | [diff] [blame] | 33 | unique_c_ptr(T *value) |
| 34 | : std::unique_ptr<T, internal::const_wrap<T, function>>(value) {} |
brians | da7756f | 2013-02-27 04:19:33 +0000 | [diff] [blame] | 35 | |
| 36 | // perfect forwarding of these 2 to make unique_ptr work |
Austin Schuh | 1676fec | 2019-12-28 20:17:34 -0800 | [diff] [blame] | 37 | template <typename... Args> |
| 38 | unique_c_ptr(Args &&... args) |
| 39 | : std::unique_ptr<T, internal::const_wrap<T, function>>( |
| 40 | std::forward<Args>(args)...) {} |
brians | da7756f | 2013-02-27 04:19:33 +0000 | [diff] [blame] | 41 | template<typename... Args> |
| 42 | unique_c_ptr<T, function> &operator=(Args&&... args) { |
Austin Schuh | 1676fec | 2019-12-28 20:17:34 -0800 | [diff] [blame] | 43 | std::unique_ptr<T, internal::const_wrap<T, function>>::operator=( |
brians | da7756f | 2013-02-27 04:19:33 +0000 | [diff] [blame] | 44 | std::forward<Args>(args)...); |
| 45 | return *this; |
| 46 | } |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | } // namespace aos |
Austin Schuh | 1676fec | 2019-12-28 20:17:34 -0800 | [diff] [blame] | 50 | |
| 51 | #endif // AOS_UNIQUE_MALLOC_PTR_H_ |