Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
types.hh
Go to the documentation of this file.
1#pragma once
3
4#include <list>
5#include <set>
6#include <string>
7#include <map>
8#include <variant>
9#include <vector>
10
11namespace nix {
12
13typedef std::list<std::string> Strings;
14
26using StringMap = std::map<std::string, std::string, std::less<>>;
33
45using StringSet = std::set<std::string, std::less<>>;
46
47typedef std::vector<std::pair<std::string, std::string>> Headers;
48
52template<typename T>
53struct OnStartup
54{
55 OnStartup(T && t)
56 {
57 t();
58 }
59};
60
65template<typename T>
67{
68 T t;
69
70 auto operator<=>(const Explicit<T> & other) const = default;
71};
72
83class BackedStringView
84{
85private:
86 std::variant<std::string, std::string_view> data;
87
93 class Ptr
94 {
95 private:
96 std::string_view view;
97 public:
98 Ptr(std::string_view view)
99 : view(view)
100 {
101 }
102
103 const std::string_view * operator->() const
104 {
105 return &view;
106 }
107 };
108
109public:
110 BackedStringView(std::string && s)
111 : data(std::move(s))
112 {
113 }
114
115 BackedStringView(std::string_view sv)
116 : data(sv)
117 {
118 }
119
120 template<size_t N>
121 BackedStringView(const char (&lit)[N])
122 : data(std::string_view(lit))
123 {
124 }
125
126 BackedStringView(const BackedStringView &) = delete;
127 BackedStringView & operator=(const BackedStringView &) = delete;
128
133 BackedStringView(BackedStringView && other) = default;
134 BackedStringView & operator=(BackedStringView && other) = default;
135
136 bool isOwned() const
137 {
138 return std::holds_alternative<std::string>(data);
139 }
140
141 std::string toOwned() &&
142 {
143 return isOwned() ? std::move(std::get<std::string>(data)) : std::string(std::get<std::string_view>(data));
144 }
145
146 std::string_view operator*() const
147 {
148 return isOwned() ? std::get<std::string>(data) : std::get<std::string_view>(data);
149 }
150
151 Ptr operator->() const
152 {
153 return Ptr(**this);
154 }
155};
156
157} // namespace nix
BackedStringView(BackedStringView &&other)=default
Definition types.hh:67
std::map< std::string, std::string, std::less<> > StringMap
Definition types.hh:26
StringMap StringPairs
Definition types.hh:32
std::set< std::string, std::less<> > StringSet
Definition types.hh:45
const T * get(const std::optional< T > &opt)
Definition util.hh:286