Repository navigation
Expand file tree
/
Copy pathcpp2b.cppm.tpl
More file actions
178 lines (147 loc) · 3.55 KB
/
Copy pathcpp2b.cppm.tpl
File metadata and controls
178 lines (147 loc) · 3.55 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
module;
#ifdef _MSC_VER
# include <Windows.h>
#else
# include <stdlib.h>
#endif
export module cpp2b;
import std;
import std.compat;
#ifdef _MSC_VER
extern char **_environ;
#else
extern "C" char **environ;
#endif
export namespace cpp2b {
enum class platform { linux, macos, windows };
enum class compiler_type { gcc, clang, msvc };
enum class compilation_type { debug, optimized, fast };
enum class build_type { local, development, release };
constexpr auto host_platform() -> platform {
#if defined(_WIN32)
return platform::windows;
#elif defined(__APPLE__)
return platform::macos;
#elif defined(__linux__)
return platform::linux;
#else
# error unknown platform
#endif
}
constexpr auto target_platform() -> platform {
return host_platform();
}
constexpr auto compiler() -> compiler_type {
return compiler_type::@CPP2B_COMPILER@;
}
constexpr auto build() -> build_type {
return build_type::local;
}
constexpr auto install_dir() -> const std::string_view {
if constexpr (build() == build_type::local) {
return R"_____cpp2b_____(@CPP2B_PROJECT_ROOT@)_____cpp2b_____";
}
}
} // namespace cpp2b
export namespace cpp2b::env {
inline auto set_var(const std::string& name, const std::string& value) -> void {
#if defined(_MSC_VER)
SetEnvironmentVariableA(name.c_str(), value.c_str());
#else
setenv(name.c_str(), value.c_str(), 1);
#endif
}
inline auto set_var(auto name, auto value) -> void {
return cpp2b::env::set_var(std::string{name}, std::string{value});
}
inline auto get_var(const std::string& name) -> std::optional<std::string> {
#if defined(_MSC_VER)
auto val = std::string{""};
val.resize(GetEnvironmentVariableA(name.c_str(), val.data(), 1));
if(!val.empty()) {
GetEnvironmentVariableA(name.c_str(), val.data(), val.size() + 1);
return val;
}
#else
auto val = std::getenv(name.c_str());
if(val != nullptr) {
return std::string{val};
}
#endif
return {};
}
inline auto get_var(auto name) -> std::optional<std::string> {
return cpp2b::env::get_var(std::string{name});
}
class vars {
public:
struct entry {
std::string_view name;
std::string_view value;
};
class iterator {
public:
using iterator_category = std::forward_iterator_tag;
using value_type = entry;
using difference_type = std::ptrdiff_t;
using pointer = const value_type*;
using reference = const value_type&;
iterator(char** env) : env_(env) {
if (env_ && *env_) {
update_entry();
}
}
reference operator*() const noexcept {
return current_entry_;
}
pointer operator->() const noexcept {
return ¤t_entry_;
}
iterator& operator++() noexcept {
if (env_ && *env_) {
++env_;
if (*env_) {
update_entry();
} else {
env_ = nullptr; // end of iteration
}
}
return *this;
}
iterator operator++(int) noexcept {
iterator temp = *this;
++(*this);
return temp;
}
friend bool operator==(const iterator& a, const iterator& b) noexcept {
return a.env_ == b.env_;
}
friend bool operator!=(const iterator& a, const iterator& b) noexcept {
return !(a == b);
}
private:
void update_entry() noexcept {
if (env_ && *env_) {
std::string_view env_str(*env_);
auto pos = env_str.find('=');
if (pos != std::string_view::npos) {
current_entry_.name = env_str.substr(0, pos);
current_entry_.value = env_str.substr(pos + 1);
}
}
}
char** env_ = nullptr;
value_type current_entry_;
};
iterator begin() const noexcept {
#ifdef _MSC_VER
return iterator(_environ);
#else
return iterator(environ);
#endif
}
iterator end() const noexcept {
return iterator(nullptr);
}
};
}