qxLib
shared_ref.h
Go to the documentation of this file.
1 /**
2 
3  @file shared_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::shared_ptr that cannot have an invalid value (nullptr) (compile time guaranteed).
20  @details Can only be constructed with make_shared_ref.
21  Cannot be constructed from / assigned from / reset from a shared_ptr.
22  shared_ptr can be extracted via to_shared_ptr().
23  @tparam T - type to store
24 **/
25 template<class T>
27 
28 /**
29  @brief Make a unique ref (same as std::make_shared but for qx::shared_ref)
30  @tparam T - type to store
31  @tparam constructor_args_t - T constructor types
32  @param args - T constructor values
33  @retval - made shared_ref
34 **/
35 template<class T, class... constructor_args_t>
36 shared_ref<T> make_shared_ref(constructor_args_t&&... args);
37 
38 namespace details
39 {
40 
41 template<class T>
42 class smart_ptr_ref_adapter<std::shared_ptr, T> : public overload_functions_smart_ptr_ref_adapter<std::shared_ptr, T>
43 {
44  struct private_token
45  {
46  };
47 
48  template<class _T, class... _constructor_args_t>
49  friend shared_ref<_T> qx::make_shared_ref(_constructor_args_t&&... args);
50 
51 public:
53  using original_pointer_type = typename super::original_pointer_type;
54  using element_type = typename super::element_type;
55  using pointer = typename super::pointer;
56  using reference = typename super::reference;
57 
58  smart_ptr_ref_adapter(smart_ptr_ref_adapter&&) noexcept = default;
59  smart_ptr_ref_adapter& operator=(smart_ptr_ref_adapter&&) noexcept = default;
60  smart_ptr_ref_adapter(const smart_ptr_ref_adapter&) noexcept = default;
61  smart_ptr_ref_adapter& operator=(const smart_ptr_ref_adapter&) noexcept = default;
62 
63  template<class U>
64  requires std::constructible_from<std::shared_ptr<T>, std::shared_ptr<U>&&>
66 
67  void reset(smart_ptr_ref_adapter other) noexcept;
68 
69  original_pointer_type to_shared_ptr();
70 
71  // private constructor, use qx::make_shared_ref instead
72  template<class... constructor_args_t>
73  smart_ptr_ref_adapter(private_token, constructor_args_t&&... args);
74 };
75 
76 } // namespace details
77 
78 } // namespace qx
79 
80 #include <qx/memory/shared_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
shared_ref< T > make_shared_ref(constructor_args_t &&... args)
Make a unique ref (same as std::make_shared but for qx::shared_ref)
Definition: shared_ref.inl:14