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 namespace qx
14 {
15 
16 /**
17  @brief A version of std::shared_ptr that cannot have an invalid value (nullptr) (compile time guaranteed).
18  @details Can only be constructed with make_shared_ref.
19  Cannot be constructed from / assigned from / reset from a shared_ptr.
20  shared_ptr can be extracted via to_shared_ptr().
21  @tparam T - type to store
22 **/
23 template<class T>
25 
26 /**
27  @brief Make a unique ref (same as std::make_shared but for qx::shared_ref)
28  @tparam T - type to store
29  @tparam constructor_args_t - T constructor types
30  @param args - T constructor values
31  @retval - made shared_ref
32 **/
33 template<class T, class... constructor_args_t>
34 shared_ref<T> make_shared_ref(constructor_args_t&&... args);
35 
36 namespace details
37 {
38 
39 template<class T, class... args_t>
40 class smart_ptr_ref_adapter<std::shared_ptr, T, args_t...>
41  : public overload_functions_smart_ptr_ref_adapter<std::shared_ptr, T, args_t...>
42 {
43  struct private_token
44  {
45  };
46 
47  template<class _T, class... _constructor_args_t>
48  friend shared_ref<_T> qx::make_shared_ref(_constructor_args_t&&... args);
49 
50 public:
51  using super = overload_functions_smart_ptr_ref_adapter<std::shared_ptr, T, args_t...>;
52  using original_pointer_type = typename super::original_pointer_type;
53  using element_type = typename super::element_type;
54  using pointer = typename super::pointer;
55  using reference = typename super::reference;
56 
57  smart_ptr_ref_adapter(smart_ptr_ref_adapter&&) noexcept = default;
58  smart_ptr_ref_adapter& operator=(smart_ptr_ref_adapter&&) noexcept = default;
59 
60  void reset(smart_ptr_ref_adapter other) noexcept;
61 
62  original_pointer_type to_shared_ptr();
63 
64  // private constructor, use qx::make_shared_ref instead
65  template<class... constructor_args_t>
66  smart_ptr_ref_adapter(private_token, constructor_args_t&&... args);
67 };
68 
69 } // namespace details
70 
71 } // namespace qx
72 
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