qxLib
unique_ref.h
Go to the documentation of this file.
1 /**
2 
3  @file unique_ref.h
4  @author Khrapov
5  @date 25.05.2025
6  @copyright © Nick Khrapov, 2025. All right reserved.
7 
8 **/
9 #pragma once
10 
12 
13 namespace qx
14 {
15 
16 /**
17  @brief A version of std::unique_ptr that cannot have an invalid value (nullptr) (compile time guaranteed).
18  @details Can only be constructed with make_unique_ref or make_unique_ref_with_deleter.
19  Cannot be constructed from / assigned from / reset from a unique_ptr.
20  @tparam T - type to store
21  @tparam deleter_t - optional custom deleter
22 **/
23 template<class T, class deleter_t = std::default_delete<T>>
25 
26 /**
27  @brief Make a unique ref (same as std::make_unique but for qx::unique_ref)
28  @tparam T - type to store
29  @tparam constructor_args_t - T constructor types
30  @param args - T constructor values
31  @retval - made unique_ref
32 **/
33 template<class T, class... constructor_args_t>
34 unique_ref<T> make_unique_ref(constructor_args_t&&... args);
35 
36 /**
37  @brief Make a unique ref with a custom deleter
38  @tparam T - type to store
39  @tparam deleter_t - custom constructor type
40  @tparam constructor_args_t - T constructor types
41  @param deleter - custom constructor
42  @param args - T constructor values
43  @retval - made unique_ref
44 **/
45 template<class T, class deleter_t, class... constructor_args_t>
46 unique_ref<T, deleter_t> make_unique_ref_with_deleter(deleter_t deleter, constructor_args_t&&... args);
47 
48 namespace details
49 {
50 
51 template<class T, class... args_t>
52 class smart_ptr_ref_adapter<std::unique_ptr, T, args_t...>
53  : public overload_functions_smart_ptr_ref_adapter<std::unique_ptr, T, args_t...>
54 {
55  struct private_token
56  {
57  };
58 
59  template<class _T, class... _constructor_args_t>
60  friend unique_ref<_T> qx::make_unique_ref(_constructor_args_t&&... args);
61 
62  template<class _T, class _deleter_t, class... _constructor_args_t>
64  _deleter_t deleter,
65  _constructor_args_t&&... args);
66 
67 public:
68  using super = overload_functions_smart_ptr_ref_adapter<std::unique_ptr, T, args_t...>;
69  using original_pointer_type = typename super::original_pointer_type;
70  using element_type = typename super::element_type;
71  using pointer = typename super::pointer;
72  using reference = typename super::reference;
73 
74  smart_ptr_ref_adapter(smart_ptr_ref_adapter&&) noexcept = default;
75  smart_ptr_ref_adapter& operator=(smart_ptr_ref_adapter&&) noexcept = default;
76 
77  void reset(smart_ptr_ref_adapter other) noexcept;
78 
79  // private constructor, use qx::make_unique_ref or qx::make_unique_ref_with_deleter instead
80  template<class... constructor_args_t>
81  smart_ptr_ref_adapter(private_token, constructor_args_t&&... args);
82 
83 private:
84  using super::release;
85 };
86 
87 } // namespace details
88 
89 } // namespace qx
90 
unique_ref< T, deleter_t > make_unique_ref_with_deleter(deleter_t deleter, constructor_args_t &&... args)
Make a unique ref with a custom deleter.
Definition: unique_ref.inl:22
unique_ref< T > make_unique_ref(constructor_args_t &&... args)
Make a unique ref (same as std::make_unique but for qx::unique_ref)
Definition: unique_ref.inl:14