brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame^] | 1 | #include <memory> |
| 2 | |
| 3 | namespace aos { |
| 4 | |
| 5 | namespace { |
| 6 | |
| 7 | template<typename T, void(*function)(T *)> |
| 8 | void const_wrap(const T *ptr) { |
| 9 | function(const_cast<T *>(ptr)); |
| 10 | } |
| 11 | |
| 12 | // Wrapper function to deal with the differences between C and C++ (C++ doesn't |
| 13 | // automatically convert T* to void* like C). |
| 14 | template<typename T> |
| 15 | void free_type(T *ptr) { ::free(reinterpret_cast<void *>(ptr)); } |
| 16 | |
| 17 | } // namespace |
| 18 | |
| 19 | // A std::unique_ptr that should get freed with a C-style free function |
| 20 | // (free(2) by default). |
| 21 | template<typename T, void(*function)(T *) = free_type> |
| 22 | class unique_c_ptr : public std::unique_ptr<T, void(*)(const T *)> { |
| 23 | public: |
| 24 | unique_c_ptr(T *pointer) |
| 25 | : std::unique_ptr<T, void(*)(const T *)>( |
| 26 | pointer, const_wrap<T, function>) {} |
| 27 | }; |
| 28 | |
| 29 | } // namespace aos |