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 #include <concepts>
14 
15 namespace qx
16 {
17 
18 /**
19  @brief A version of std::unique_ptr that cannot have an invalid value (nullptr) (compile time guaranteed).
20  @details Can only be constructed with make_unique_ref or make_unique_ref_with_deleter.
21  Cannot be constructed from / assigned from / reset from a unique_ptr.
22  @tparam T - type to store
23  @tparam deleter_t - optional custom deleter
24 **/
25 template<class T, class deleter_t = std::default_delete<T>>
27 
28 /**
29  @brief Make a unique ref (same as std::make_unique but for qx::unique_ref)
30  @tparam T - type to store
31  @tparam constructor_args_t - T constructor types
32  @param args - T constructor values
33  @retval - made unique_ref
34 **/
35 template<class T, class... constructor_args_t>
36 unique_ref<T> make_unique_ref(constructor_args_t&&... args);
37 
38 /**
39  @brief Make a unique ref with a custom deleter
40  @tparam T - type to store
41  @tparam deleter_t - custom constructor type
42  @tparam constructor_args_t - T constructor types
43  @param deleter - custom constructor
44  @param args - T constructor values
45  @retval - made unique_ref
46 **/
47 template<class T, class deleter_t, class... constructor_args_t>
48 unique_ref<T, deleter_t> make_unique_ref_with_deleter(deleter_t deleter, constructor_args_t&&... args);
49 
50 namespace details
51 {
52 
53 template<class T, class... args_t>
54 class smart_ptr_ref_adapter<std::unique_ptr, T, args_t...>
55  : public overload_functions_smart_ptr_ref_adapter<std::unique_ptr, T, args_t...>
56 {
57  struct private_token
58  {
59  };
60 
61  template<class _T, class... _constructor_args_t>
62  friend unique_ref<_T> qx::make_unique_ref(_constructor_args_t&&... args);
63 
64  template<class _T, class _deleter_t, class... _constructor_args_t>
66  _deleter_t deleter,
67  _constructor_args_t&&... args);
68 
69 public:
70  using super = overload_functions_smart_ptr_ref_adapter<std::unique_ptr, T, args_t...>;
71  using original_pointer_type = typename super::original_pointer_type;
72  using element_type = typename super::element_type;
73  using pointer = typename super::pointer;
74  using reference = typename super::reference;
75 
76  smart_ptr_ref_adapter(smart_ptr_ref_adapter&&) noexcept = default;
77  smart_ptr_ref_adapter& operator=(smart_ptr_ref_adapter&&) noexcept = default;
78 
79  template<class U, class other_deleter_t>
80  requires std::constructible_from<std::unique_ptr<T, args_t...>, std::unique_ptr<U, other_deleter_t>&&>
82 
83  void reset(smart_ptr_ref_adapter other) noexcept;
84 
85  // private constructor, use qx::make_unique_ref or qx::make_unique_ref_with_deleter instead
86  template<class... constructor_args_t>
87  smart_ptr_ref_adapter(private_token, constructor_args_t&&... args);
88 
89 private:
90  using super::release;
91 };
92 
93 } // namespace details
94 
95 } // namespace qx
96 
97 #include <qx/memory/unique_ref.inl>
requires(same_variadic_args_v< args_t... >) const expr auto coalesce(args_t &&... args)
Coalesce function, C# a ?? b analogue.
Definition: coalesce.inl:57
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