blob: 696b9f375bb47eb23fbd54a24f0865fc4c1fa45f [file] [log] [blame]
brians343bc112013-02-10 01:53:46 +00001#include <memory>
2
3namespace aos {
4
5namespace {
6
7template<typename T, void(*function)(T *)>
8void 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).
14template<typename T>
15void 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).
21template<typename T, void(*function)(T *) = free_type>
22class 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