Nix 2.93.3
Lix: A modern, delicious implementation of the Nix 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 <optional>
6#include <set>
7#include <string>
8#include <string_view>
9#include <map>
10#include <vector>
11#include <span>
12#include <stdint.h> // IWYU pragma: keep (this is used literally everywhere)
13
14namespace nix {
15
16typedef std::list<std::string> Strings;
17typedef std::set<std::string> StringSet;
18typedef std::map<std::string, std::string> StringMap;
19typedef std::map<std::string, std::string> StringPairs;
20
21// TODO this should be a std::byte span, but too much of the
22// current codebase predates std::byte and uses char instead
23using Bytes = std::span<const char>;
24
28typedef std::string Path;
29typedef std::string_view PathView;
30typedef std::list<Path> Paths;
31typedef std::set<Path> PathSet;
32
33typedef std::vector<std::pair<std::string, std::string>> Headers;
34
39template<typename T>
40struct Explicit {
41 T t;
42
43 bool operator ==(const Explicit<T> & other) const
44 {
45 return t == other.t;
46 }
47};
48
52template <class T>
53const typename T::mapped_type * get(const T & map, const typename T::key_type & key)
54{
55 auto i = map.find(key);
56 if (i == map.end()) return nullptr;
57 return &i->second;
58}
59
60template <class T>
61typename T::mapped_type * get(T & map, const typename T::key_type & key)
62{
63 auto i = map.find(key);
64 if (i == map.end()) return nullptr;
65 return &i->second;
66}
67
71template<class T>
72const typename T::mapped_type & getOr(
73 T & map [[clang::lifetimebound]],
74 const typename T::key_type & key,
75 const typename T::mapped_type & defaultValue [[clang::lifetimebound]]
76)
77{
78 auto i = map.find(key);
79 if (i == map.end()) {
80 /* FIXME(Raito): `[[clang::lifetimebound]]` has no effect on `defaultValue` warning. */
81 // NOLINTNEXTLINE(bugprone-return-const-ref-from-parameter)
82 return defaultValue;
83 }
84 return i->second;
85}
86
90template <class T>
91std::optional<typename T::value_type> remove_begin(T & c)
92{
93 auto i = c.begin();
94 if (i == c.end()) return {};
95 auto v = std::move(*i);
96 c.erase(i);
97 return v;
98}
99
100
104template <class T>
105std::optional<typename T::value_type> pop(T & c)
106{
107 if (c.empty()) return {};
108 auto v = std::move(c.front());
109 c.pop();
110 return v;
111}
112
113
118template<typename T>
119struct MaintainCount
120{
121 T & counter;
122 long delta;
123 MaintainCount(T & counter, long delta = 1) : counter(counter), delta(delta) { counter += delta; }
124 ~MaintainCount() { counter -= delta; }
125};
126
127
136template <typename T,
137 typename TIter = decltype(std::begin(std::declval<T>())),
138 typename = decltype(std::end(std::declval<T>()))>
139constexpr auto enumerate(T && iterable)
140{
141 struct iterator
142 {
143 size_t i;
144 TIter iter;
145 constexpr bool operator != (const iterator & other) const { return iter != other.iter; }
146 constexpr void operator ++ () { ++i; ++iter; }
147 constexpr auto operator * () { return std::tie(i, *iter); }
148 };
149
150 struct iterable_wrapper
151 {
152 T iterable;
153 constexpr auto begin() { return iterator{ 0, std::begin(iterable) }; }
154 constexpr auto end() { return iterator{ 0, std::end(iterable) }; }
155 };
156
157 return iterable_wrapper{ std::forward<T>(iterable) };
158}
159
160
164template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
165template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
166
172struct NeverAsync {};
173
182
183}
Definition types.hh:40
Definition types.hh:172
Definition types.hh:164
std::optional< typename T::value_type > remove_begin(T &c)
Definition types.hh:91
std::optional< typename T::value_type > pop(T &c)
Definition types.hh:105
constexpr auto enumerate(T &&iterable)
Definition types.hh:139
std::string Path
Definition types.hh:28
const T::mapped_type & getOr(T &map, const typename T::key_type &key, const typename T::mapped_type &defaultValue)
Definition types.hh:72
constexpr NeverAsync always_progresses
Definition types.hh:181