2 * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
8 #ifndef LTTNG_MAKE_UNIQUE_WRAPPER_H
9 #define LTTNG_MAKE_UNIQUE_WRAPPER_H
11 #include <common/macros.hpp>
18 * make_unique_wrapper is intended to facilitate the use of std::unique_ptr
19 * to wrap C-style APIs that don't provide RAII resource management facilities.
24 * struct my_c_struct {
28 * struct my_c_struct *create_my_c_struct(void);
29 * void destroy_my_c_struct(struct my_c_struct *value);
31 * // Creating a unique_ptr to my_c_struct.
32 * auto safe_c_struct =
33 * lttng::make_unique_wrapper<my_c_struct, destroy_my_c_struct>(
34 * create_my_c_struct());
36 * Note that this facility is intended for use in the scope of a function.
37 * If you need to return this unique_ptr instance, you should consider writting
38 * a proper, idiomatic, wrapper.
43 template <typename WrappedType, void (*DeleterFunction)(WrappedType *)>
44 struct create_unique_class {
46 void operator()(WrappedType *instance) const
48 DeleterFunction(instance);
52 std::unique_ptr<WrappedType, deleter> operator()(WrappedType *instance) const
54 return std::unique_ptr<WrappedType, deleter>(instance);
57 } /* namespace details */
60 * 'free' is a utility function for use with make_unique_wrapper. It makes it easier to
61 * wrap raw pointers that have to be deleted with `free`. Using libc's 'free' as
62 * a make_unique_wrapper template argument will result in an error as 'WrappedType *' will
63 * not match free's 'void *' argument.
71 template <typename WrappedType, void (*DeleterFunc)(WrappedType *)>
72 std::unique_ptr<WrappedType, typename details::create_unique_class<WrappedType, DeleterFunc>::deleter>
73 make_unique_wrapper(WrappedType *instance)
75 const details::create_unique_class<WrappedType, DeleterFunc> unique_deleter;
77 return unique_deleter(instance);
80 } /* namespace lttng */
82 #endif /* LTTNG_MAKE_UNIQUE_WRAPPER_H */