Nix
2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Toggle main menu visibility
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
9
#include "
nix/util/demangle.hh
"
10
11
namespace
nix {
12
17
class
bad_ref_cast :
public
std::bad_cast
18
{
19
std::string msg;
20
21
public
:
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).
49
template
<
typename
From,
typename
To>
50
concept
RefImplicitlyUpcastableTo
= std::is_convertible_v<From *, To *>;
51
56
template
<
typename
T>
57
class
ref
58
{
59
private
:
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
69
public
:
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
153
private
:
154
155
template
<
typename
T2,
typename
...
Args
>
156
friend
ref<T2>
make_ref(
Args
&&... args);
157
};
158
159
template
<
typename
T,
typename
... Args>
160
inline
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
166
struct
RefDeepComparator
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
nix::Args
Definition
args.hh:29
nix::bad_ref_cast
Definition
ref.hh:18
nix::ref
Definition
ref.hh:58
nix::RefImplicitlyUpcastableTo
Definition
ref.hh:50
demangle.hh
nix::demangle
std::string demangle(const char *name)
Definition
demangle.hh:14
nix::RefDeepComparator
Definition
ref.hh:167
libutil
include
nix
util
ref.hh
Generated by
1.18.0