blob: 230ae9a6cb001f85d45d082f31a7d385c7bf30c0 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
// This source code is released into the public domain.
export module nihil.posix:argv;
import nihil.std;
namespace nihil {
/*
* argv: stores a null-terminated array of nul-terminated C strings.
* argv::data() is suitable for passing to ::execv().
*/
export struct argv
{
using value_type = char *;
using iterator = value_type const *;
using const_iterator = value_type const *;
/*
* Create a new argv from a range.
*
* Delegate to the default constructor to ensure the pointers in
* m_args are deleted if we throw when allocating.
*/
argv(std::from_range_t, std::ranges::range auto &&args)
: argv()
{
for (auto &&arg : args) {
auto str = std::string_view(arg);
// Create a nul-terminated C string.
auto ptr = std::make_unique<char[]>(str.size() + 1); // NOLINT
std::ranges::copy(str, ptr.get());
ptr[str.size()] = '\0';
// Ensure we won't throw when emplacing the pointer.
m_args.reserve(m_args.size() + 1);
m_args.emplace_back(ptr.release());
}
m_args.push_back(nullptr);
}
/*
* Create an argv from an initializer list.
*/
template <typename T>
argv(std::initializer_list<T> const &args)
: argv(std::from_range, args)
{
}
template <typename T>
argv(std::initializer_list<T> &&args)
: argv(std::from_range, std::move(args))
{
}
// Destructor.
~argv()
{
for (auto *arg : m_args)
delete[] arg; // NOLINT
}
// Movable.
argv(argv &&) noexcept = default;
auto operator=(argv &&other) noexcept -> argv & = default;
// Not copyable. TODO: for completeness, it probably should be.
argv(argv const &) = delete;
auto operator=(argv const &other) -> argv & = delete;
// Access the stored arguments.
[[nodiscard]] auto data(this argv const &self) -> value_type const *
{
return self.m_args.data();
}
[[nodiscard]] auto size(this argv const &self) -> std::size_t
{
return self.m_args.size();
}
// Access an element by index. Throws std::out_of_range if the index is out of range.
[[nodiscard]] auto operator[](this argv const &self, std::size_t index) -> value_type
{
return self.m_args.at(index);
}
// Range access
[[nodiscard]] auto begin(this argv const &self) -> const_iterator
{
return self.data();
}
[[nodiscard]] auto end(this argv const &self) -> const_iterator
{
return self.data() + self.size(); // NOLINT
}
private:
// Private since default-constructing an argv isn't useful.
argv() = default;
// The argument pointers, including the null terminator. This can't be a
// vector<unique_ptr> because we need an array of char pointers to pass to exec.
std::vector<char *> m_args;
};
} // namespace nihil
|