Nix 2.26.3
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
7namespace nix {
8
13template<typename T>
14class ref
15{
16private:
17
18 std::shared_ptr<T> p;
19
20public:
21 explicit ref(const std::shared_ptr<T> & p)
22 : p(p)
23 {
24 if (!p)
25 throw std::invalid_argument("null pointer cast to ref");
26 }
27
28 explicit ref(T * p)
29 : p(p)
30 {
31 if (!p)
32 throw std::invalid_argument("null pointer cast to ref");
33 }
34
35 T* operator ->() const
36 {
37 return &*p;
38 }
39
40 T& operator *() const
41 {
42 return *p;
43 }
44
45 operator std::shared_ptr<T> () const
46 {
47 return p;
48 }
49
50 std::shared_ptr<T> get_ptr() const
51 {
52 return p;
53 }
54
55 template<typename T2>
56 ref<T2> cast() const
57 {
58 return ref<T2>(std::dynamic_pointer_cast<T2>(p));
59 }
60
61 template<typename T2>
62 std::shared_ptr<T2> dynamic_pointer_cast() const
63 {
64 return std::dynamic_pointer_cast<T2>(p);
65 }
66
67 template<typename T2>
68 operator ref<T2> () const
69 {
70 return ref<T2>((std::shared_ptr<T2>) p);
71 }
72
73 bool operator == (const ref<T> & other) const
74 {
75 return p == other.p;
76 }
77
78 bool operator != (const ref<T> & other) const
79 {
80 return p != other.p;
81 }
82
83 auto operator <=> (const ref<T> & other) const
84 {
85 return p <=> other.p;
86 }
87
88private:
89
90 template<typename T2, typename... Args>
91 friend ref<T2>
92 make_ref(Args&&... args);
93
94};
95
96template<typename T, typename... Args>
97inline ref<T>
98make_ref(Args&&... args)
99{
100 auto p = std::make_shared<T>(std::forward<Args>(args)...);
101 return ref<T>(p);
102}
103
104}
Definition args.hh:28
Definition ref.hh:15
std::shared_ptr< T > p
Definition lexer.l:1269
std::vector< Expr * > args
Definition lexer.l:6126