Nix  2.93.0-dev
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(T & map,
73 const typename T::key_type & key,
74 const typename T::mapped_type & defaultValue)
75{
76 auto i = map.find(key);
77 if (i == map.end()) return defaultValue;
78 return i->second;
79}
80
84template <class T>
85std::optional<typename T::value_type> remove_begin(T & c)
86{
87 auto i = c.begin();
88 if (i == c.end()) return {};
89 auto v = std::move(*i);
90 c.erase(i);
91 return v;
92}
93
94
98template <class T>
99std::optional<typename T::value_type> pop(T & c)
100{
101 if (c.empty()) return {};
102 auto v = std::move(c.front());
103 c.pop();
104 return v;
105}
106
107
112template<typename T>
114{
115 T & counter;
116 long delta;
117 MaintainCount(T & counter, long delta = 1) : counter(counter), delta(delta) { counter += delta; }
118 ~MaintainCount() { counter -= delta; }
119};
120
121
130template <typename T,
131 typename TIter = decltype(std::begin(std::declval<T>())),
132 typename = decltype(std::end(std::declval<T>()))>
133constexpr auto enumerate(T && iterable)
134{
135 struct iterator
136 {
137 size_t i;
138 TIter iter;
139 constexpr bool operator != (const iterator & other) const { return iter != other.iter; }
140 constexpr void operator ++ () { ++i; ++iter; }
141 constexpr auto operator * () { return std::tie(i, *iter); }
142 };
143
144 struct iterable_wrapper
145 {
146 T iterable;
147 constexpr auto begin() { return iterator{ 0, std::begin(iterable) }; }
148 constexpr auto end() { return iterator{ 0, std::end(iterable) }; }
149 };
150
151 return iterable_wrapper{ std::forward<T>(iterable) };
152}
153
154
158template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
159template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
160
166struct NeverAsync {};
167
176
177}
Definition types.hh:40
Definition types.hh:114
Definition types.hh:166
Definition types.hh:158
std::optional< typename T::value_type > remove_begin(T &c)
Definition types.hh:85
std::optional< typename T::value_type > pop(T &c)
Definition types.hh:99
constexpr auto enumerate(T &&iterable)
Definition types.hh:133
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:175