Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
ref.hh
Go to the documentation of this file.
1#pragma once
3
4#include <memory>
5#include <stdexcept>
6#include <string>
7#include <typeinfo>
8
10
11namespace nix {
12
17class bad_ref_cast : public std::bad_cast
18{
19 std::string msg;
20
21public:
22 bad_ref_cast(std::string msg)
23 : msg(std::move(msg))
24 {
25 }
26
27 bad_ref_cast(bad_ref_cast &&) = default;
28 bad_ref_cast(const bad_ref_cast &) = default;
29 bad_ref_cast & operator=(bad_ref_cast &&) = default;
30 bad_ref_cast & operator=(const bad_ref_cast &) = default;
31 ~bad_ref_cast() override;
32
33 const char * what() const noexcept override
34 {
35 return msg.c_str();
36 }
37};
38
44// Design note: This named concept is technically redundant but provides a readable hint
45// in error messages. Alternative: static_assert can have custom messages, but doesn't
46// participate in SFINAE, so std::is_convertible_v<ref<Base>, ref<Derived>> would
47// incorrectly return true (the conversion would exist but fail at instantiation
48// rather than being excluded).
49template<typename From, typename To>
50concept RefImplicitlyUpcastableTo = std::is_convertible_v<From *, To *>;
51
56template<typename T>
57class ref
58{
59private:
60
61 std::shared_ptr<T> p;
62
63 void assertNonNull()
64 {
65 if (!p)
66 throw std::invalid_argument("null pointer cast to ref");
67 }
68
69public:
70
71 using element_type = T;
72
73 explicit ref(const std::shared_ptr<T> & p)
74 : p(p)
75 {
76 assertNonNull();
77 }
78
79 explicit ref(std::shared_ptr<T> && p)
80 : p(std::move(p))
81 {
82 assertNonNull();
83 }
84
85 explicit ref(T * p)
86 : p(p)
87 {
88 assertNonNull();
89 }
90
91 T * get() const
92 {
93 return p.get();
94 }
95
96 T * operator->() const
97 {
98 return get();
99 }
100
101 T & operator*() const
102 {
103 return *p;
104 }
105
106 std::shared_ptr<T> get_ptr() const &
107 {
108 return p;
109 }
110
111 std::shared_ptr<T> get_ptr() &&
112 {
113 return std::move(p);
114 }
115
119 operator std::shared_ptr<T>(this auto && self)
120 {
121 return std::forward<decltype(self)>(self).get_ptr();
122 }
123
124 template<typename T2>
125 ref<T2> cast() const
126 {
127 auto casted = std::dynamic_pointer_cast<T2>(p);
128 if (!casted)
129 throw bad_ref_cast(
130 "ref<" + demangle(typeid(T).name()) + "> cannot be cast to ref<" + demangle(typeid(T2).name()) + ">");
131 return ref<T2>(std::move(casted));
132 }
133
134 template<typename T2>
135 std::shared_ptr<T2> dynamic_pointer_cast() const
136 {
137 return std::dynamic_pointer_cast<T2>(p);
138 }
139
144 template<typename T2>
145 requires RefImplicitlyUpcastableTo<T, T2>
146 operator ref<T2>() const
147 {
148 return ref<T2>(p);
149 }
150
151 auto operator<=>(const ref<T> & other) const = default;
152
153private:
154
155 template<typename T2, typename... Args>
156 friend ref<T2> make_ref(Args &&... args);
157};
158
159template<typename T, typename... Args>
160inline ref<T> make_ref(Args &&... args)
161{
162 auto p = std::make_shared<T>(std::forward<Args>(args)...);
163 return ref<T>(std::move(p));
164}
165
167{
168 template<typename T>
169 bool operator()(const ref<T> & lhs, const ref<T> & rhs) const
170 {
171 return *lhs < *rhs;
172 }
173};
174
175} // namespace nix
Definition args.hh:29
Definition ref.hh:18
Definition ref.hh:58
Definition ref.hh:50
std::string demangle(const char *name)
Definition demangle.hh:14
Definition ref.hh:167