Skip to content

Commit 2f16f84

Browse files
committed
Merge branch 'separate-include-source-dir' into new-tuple
2 parents 0795121 + caf7226 commit 2f16f84

8 files changed

Lines changed: 323 additions & 78 deletions

File tree

ARLib.natvis

Lines changed: 87 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -169,34 +169,65 @@
169169
</Expand>
170170
</Type>
171171
<Type Name="ARLib::v2::Variant&lt;*&gt;">
172-
<DisplayString Condition="m_current_type==0">{*($T1*)m_storage.m_memory}</DisplayString>
173-
<DisplayString Condition="m_current_type==1" Optional="true">{*($T2*)m_storage.m_memory}</DisplayString>
174-
<DisplayString Condition="m_current_type==2" Optional="true">{*($T3*)m_storage.m_memory}</DisplayString>
175-
<DisplayString Condition="m_current_type==3" Optional="true">{*($T4*)m_storage.m_memory}</DisplayString>
176-
<DisplayString Condition="m_current_type==4" Optional="true">{*($T5*)m_storage.m_memory}</DisplayString>
177-
<DisplayString Condition="m_current_type==5" Optional="true">{*($T6*)m_storage.m_memory}</DisplayString>
178-
<DisplayString Condition="m_current_type==6" Optional="true">{*($T7*)m_storage.m_memory}</DisplayString>
179-
<Expand>
180-
<Item Name="current_type">m_current_type</Item>
181-
<Item Name="value0" Condition="m_current_type==0">*($T1*)m_storage.m_memory</Item>
182-
<Item Name="value1" Condition="m_current_type==1" Optional="true">*($T2*)m_storage.m_memory</Item>
183-
<Item Name="value2" Condition="m_current_type==2" Optional="true">*($T3*)m_storage.m_memory</Item>
184-
<Item Name="value3" Condition="m_current_type==3" Optional="true">*($T4*)m_storage.m_memory</Item>
185-
<Item Name="value4" Condition="m_current_type==4" Optional="true">*($T5*)m_storage.m_memory</Item>
186-
<Item Name="value5" Condition="m_current_type==5" Optional="true">*($T6*)m_storage.m_memory</Item>
187-
<Item Name="value6" Condition="m_current_type==6" Optional="true">*($T7*)m_storage.m_memory</Item>
188-
</Expand>
189-
</Type>
190-
<Type Name="ARLib::Optional&lt;*&gt;">
172+
<DisplayString Condition="m_current_type==0">{*($T1*)m_storage.m_memory,na}</DisplayString>
173+
<DisplayString Condition="m_current_type==1" Optional="true">{*($T2*)m_storage.m_memory,na}</DisplayString>
174+
<DisplayString Condition="m_current_type==2" Optional="true">{*($T3*)m_storage.m_memory,na}</DisplayString>
175+
<DisplayString Condition="m_current_type==3" Optional="true">{*($T4*)m_storage.m_memory,na}</DisplayString>
176+
<DisplayString Condition="m_current_type==4" Optional="true">{*($T5*)m_storage.m_memory,na}</DisplayString>
177+
<DisplayString Condition="m_current_type==5" Optional="true">{*($T6*)m_storage.m_memory,na}</DisplayString>
178+
<DisplayString Condition="m_current_type==6" Optional="true">{*($T7*)m_storage.m_memory,na}</DisplayString>
179+
<DisplayString Condition="m_current_type==7" Optional="true">{*($T8*)m_storage.m_memory,na}</DisplayString>
180+
<DisplayString Condition="m_current_type==8" Optional="true">{*($T9*)m_storage.m_memory,na}</DisplayString>
181+
<DisplayString Condition="m_current_type==9" Optional="true">{*($T10*)m_storage.m_memory,na}</DisplayString>
182+
<DisplayString Condition="m_current_type==10" Optional="true">{*($T11*)m_storage.m_memory,na}</DisplayString>
183+
<DisplayString Condition="m_current_type==11" Optional="true">{*($T12*)m_storage.m_memory,na}</DisplayString>
184+
<DisplayString Condition="m_current_type==12" Optional="true">{*($T13*)m_storage.m_memory,na}</DisplayString>
185+
<Expand>
186+
<Item Name="[index]">(size_t)m_current_type</Item>
187+
<Item Name="[active type]" Condition="m_current_type==0">"$T1"</Item>
188+
<Item Name="[active type]" Condition="m_current_type==1" Optional="true">"$T2"</Item>
189+
<Item Name="[active type]" Condition="m_current_type==2" Optional="true">"$T3"</Item>
190+
<Item Name="[active type]" Condition="m_current_type==3" Optional="true">"$T4"</Item>
191+
<Item Name="[active type]" Condition="m_current_type==4" Optional="true">"$T5"</Item>
192+
<Item Name="[active type]" Condition="m_current_type==5" Optional="true">"$T6"</Item>
193+
<Item Name="[active type]" Condition="m_current_type==6" Optional="true">"$T7"</Item>
194+
<Item Name="[active type]" Condition="m_current_type==7" Optional="true">"$T8"</Item>
195+
<Item Name="[active type]" Condition="m_current_type==8" Optional="true">"$T9"</Item>
196+
<Item Name="[active type]" Condition="m_current_type==9" Optional="true">"$T10"</Item>
197+
<Item Name="[active type]" Condition="m_current_type==10" Optional="true">"$T11"</Item>
198+
<Item Name="[active type]" Condition="m_current_type==11" Optional="true">"$T12"</Item>
199+
<Item Name="[active type]" Condition="m_current_type==12" Optional="true">"$T13"</Item>
200+
<Item Name="[active value]" Condition="m_current_type==0">*($T1*)m_storage.m_memory</Item>
201+
<Item Name="[active value]" Condition="m_current_type==1" Optional="true">*($T2*)m_storage.m_memory</Item>
202+
<Item Name="[active value]" Condition="m_current_type==2" Optional="true">*($T3*)m_storage.m_memory</Item>
203+
<Item Name="[active value]" Condition="m_current_type==3" Optional="true">*($T4*)m_storage.m_memory</Item>
204+
<Item Name="[active value]" Condition="m_current_type==4" Optional="true">*($T5*)m_storage.m_memory</Item>
205+
<Item Name="[active value]" Condition="m_current_type==5" Optional="true">*($T6*)m_storage.m_memory</Item>
206+
<Item Name="[active value]" Condition="m_current_type==6" Optional="true">*($T7*)m_storage.m_memory</Item>
207+
<Item Name="[active value]" Condition="m_current_type==7" Optional="true">*($T8*)m_storage.m_memory</Item>
208+
<Item Name="[active value]" Condition="m_current_type==8" Optional="true">*($T9*)m_storage.m_memory</Item>
209+
<Item Name="[active value]" Condition="m_current_type==9" Optional="true">*($T10*)m_storage.m_memory</Item>
210+
<Item Name="[active value]" Condition="m_current_type==10" Optional="true">*($T11*)m_storage.m_memory</Item>
211+
<Item Name="[active value]" Condition="m_current_type==11" Optional="true">*($T12*)m_storage.m_memory</Item>
212+
<Item Name="[active value]" Condition="m_current_type==12" Optional="true">*($T13*)m_storage.m_memory</Item>
213+
</Expand>
214+
</Type>
215+
<Type Name="ARLib::OptionalStorage&lt;*&gt;">
191216
<Intrinsic Name="exists" Expression="(bool)m_exists"/>
192-
<Intrinsic Name="getValue" Expression="*m_object"/>
217+
<Intrinsic Name="getValue" Expression="*($T1*)m_memory"/>
193218
<DisplayString Condition="exists()">{{ {getValue()} }}</DisplayString>
194219
<DisplayString Condition="!exists()">{{ empty }}</DisplayString>
195220
<Expand>
196221
<Item Condition="exists()" Name="[value]" ExcludeView="simple">getValue()</Item>
197222
<Item Name="[exists]" ExcludeView="simple">exists()</Item>
198223
</Expand>
199224
</Type>
225+
<Type Name="ARLib::Optional&lt;*&gt;">
226+
<DisplayString>{m_object}</DisplayString>
227+
<Expand>
228+
<Item Name="[storage]" ExcludeView="simple">m_object</Item>
229+
</Expand>
230+
</Type>
200231
<Type Name="ARLib::UniquePtr&lt;*&gt;">
201232
<Intrinsic Name="getValue" Expression="*m_storage"/>
202233
<Intrinsic Name="exists" Expression="m_storage != nullptr"/>
@@ -758,4 +789,40 @@
758789
<Item Name="[should_dealloc]" ExcludeView="simple">m_should_dealloc</Item>
759790
</Expand>
760791
</Type>
792+
<Type Name="ARLib::AtomicStorage&lt;*&gt;">
793+
<DisplayString>{{ storage = {m_storage} }}</DisplayString>
794+
<Expand>
795+
<Item Name="locked" Optional="true">m_lock != 0</Item>
796+
<Item Name="storage">m_storage</Item>
797+
<Item Name="lock" Optional="true">m_lock</Item>
798+
</Expand>
799+
</Type>
800+
<Type Name="ARLib::PointerAtomicBase&lt;*&gt;">
801+
<Intrinsic Name="AsPtr" Expression="($T1*)m_storage.m_storage"/>
802+
<DisplayString Condition="AsPtr() != nullptr">{{ value={ AsPtr() }}</DisplayString>
803+
<DisplayString Condition="AsPtr() == nullptr">{{ value=null }}</DisplayString>
804+
<Expand>
805+
<Item Name="storage">m_storage</Item>
806+
</Expand>
807+
</Type>
808+
<Type Name="ARLib::IntegralAtomicBase&lt;*&gt;">
809+
<Intrinsic Name="AsValue" Expression="m_storage.m_storage"/>
810+
<DisplayString>{{ value={AsValue()} }}</DisplayString>
811+
<Expand>
812+
<Item Name="storage">m_storage</Item>
813+
</Expand>
814+
</Type>
815+
<Type Name="ARLib::AtomicBase&lt;*&gt;">
816+
<DisplayString>{m_storage}</DisplayString>
817+
<Expand>
818+
<Item Name="storage">m_storage</Item>
819+
</Expand>
820+
</Type>
821+
<Type Name="ARLib::Atomic&lt;*&gt;">
822+
<Intrinsic Name="AsBase" Expression="*(ARLib::Atomic&lt;$T1&gt;::Base*)this"/>
823+
<DisplayString>{{ {AsBase()} }}</DisplayString>
824+
<Expand>
825+
<Item Name="storage">m_storage</Item>
826+
</Expand>
827+
</Type>
761828
</AutoVisualizer>

Tests/Runner.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,4 +1702,50 @@ TEST(ARLibTests, StreamingTest) {
17021702
StringStream str{ test_string };
17031703
Vector<StringView> expected{ "hello world"_sv, "this is a test"_sv, "another line"_sv };
17041704
for (const auto& [i, line] : enumerate(str.lines())) { EXPECT_EQ(line, expected[i]); }
1705+
}
1706+
TEST(ARLibTests, ArgParserDefaultValueTest) {
1707+
const char* argv[] = { "test" };
1708+
const int argc = 1;
1709+
ArgParser parser{ argc, argv };
1710+
Path p{};
1711+
parser.add_option({ "-p", "--path" }, "PATH", "Path to file", p, "testfile.txt"_p);
1712+
parser.parse().must();
1713+
EXPECT_EQ(p, "testfile.txt"_p);
1714+
1715+
ArgParser parser2{ argc, argv };
1716+
Path p2{};
1717+
parser2.add_option({ "-p", "--path" }, "PATH", "Path to file", p2);
1718+
auto res = parser2.parse();
1719+
EXPECT_TRUE(res.is_error());
1720+
res.ignore_error();
1721+
}
1722+
TEST(ARLibTests, PrimeGeneratorTest) {
1723+
constexpr size_t primes[] = {
1724+
2, 2, 3, 5, 5, 7, 7, 11, 11, 11, 11, 13, 13, 17, 17, 17, 17, 19, 19, 23, 23, 23,
1725+
23, 29, 29, 29, 29, 29, 29, 31, 31, 37, 37, 37, 37, 37, 37, 41, 41, 41, 41, 43, 43, 47,
1726+
47, 47, 47, 53, 53, 53, 53, 53, 53, 59, 59, 59, 59, 59, 59, 61, 61, 67, 67, 67, 67, 67,
1727+
67, 71, 71, 71, 71, 73, 73, 79, 79, 79, 79, 79, 79, 83, 83, 83, 83, 89, 89, 89, 89, 89,
1728+
89, 97, 97, 97, 97, 97, 97, 97, 97, 101, 101, 101, 101, 103, 103, 107, 107, 107, 107, 109, 109, 113,
1729+
113, 113, 113, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 127, 131, 131, 131, 131, 137,
1730+
137, 137, 137, 137, 137, 139, 139, 149, 149, 149, 149, 149, 149, 149, 149, 149, 149, 151, 151, 157, 157, 157,
1731+
157, 157, 157, 163, 163, 163, 163, 163, 163, 167, 167, 167, 167, 173, 173, 173, 173, 173, 173, 179, 179, 179,
1732+
179, 179, 179, 181, 181, 191, 191, 191, 191, 191, 191, 191, 191, 191, 191, 193, 193, 197, 197, 197, 197, 199,
1733+
199, 211, 211, 211, 211, 211, 211, 211, 211, 211, 211, 211, 211, 223, 223, 223, 223, 223, 223, 223, 223, 223,
1734+
223, 223, 223, 227, 227, 227, 227, 229, 229, 233, 233, 233, 233, 239, 239, 239, 239, 239, 239, 241, 241, 251,
1735+
251, 251, 251, 251, 251, 251, 251, 251, 251, 257, 257, 257, 257, 257, 257, 263, 263, 263, 263, 263, 263, 269,
1736+
269, 269, 269, 269, 269, 271, 271, 277, 277, 277, 277, 277, 277, 281, 281, 281, 281, 283, 283, 293, 293, 293,
1737+
293, 293, 293, 293, 293, 293, 293, 307, 307, 307, 307, 307, 307, 307, 307, 307, 307, 307, 307, 307, 307, 311,
1738+
311, 311, 311, 313, 313, 317, 317, 317, 317, 331, 331, 331, 331, 331, 331, 331, 331, 331, 331, 331, 331, 331,
1739+
331, 337, 337, 337, 337, 337, 337, 347, 347, 347, 347, 347, 347, 347, 347, 347, 347, 349, 349, 353, 353, 353,
1740+
353, 359, 359, 359, 359, 359, 359, 367, 367, 367, 367, 367, 367, 367, 367, 373, 373, 373, 373, 373, 373, 379,
1741+
379, 379, 379, 379, 379, 383, 383, 383, 383, 389, 389, 389, 389, 389, 389, 397, 397, 397, 397, 397, 397, 397,
1742+
397, 401, 401, 401, 401, 409, 409, 409, 409, 409, 409, 409, 409, 419, 419, 419, 419, 419, 419, 419, 419, 419,
1743+
419, 421, 421, 431, 431, 431, 431, 431, 431, 431, 431, 431, 431, 433, 433, 439, 439, 439, 439, 439, 439, 443,
1744+
443, 443, 443, 449, 449, 449, 449, 449, 449, 457, 457, 457, 457, 457, 457, 457, 457, 461, 461, 461, 461, 463,
1745+
463, 467, 467, 467, 467, 479, 479, 479, 479, 479, 479, 479, 479, 479, 479, 479, 479, 487, 487, 487, 487, 487,
1746+
487, 487, 487, 491, 491, 491, 491, 499, 499, 499, 499, 499, 499, 499, 499, 503
1747+
};
1748+
for (size_t i = 0; i < sizeof_array(primes); ++i) {
1749+
EXPECT_EQ(prime_generator(i), primes[i]);
1750+
}
17051751
}

include/ArgParser.hpp

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ArgParser {
3131
public:
3232
enum class OptionLength {
3333
Short = 0x01,
34-
Long = 0x02,
34+
Long = 0x02,
3535
};
3636
class OptionName {
3737
Optional<StringView> m_long_name;
@@ -65,11 +65,15 @@ class ArgParser {
6565
StringView description;
6666
StringView value_name;
6767

68-
Variant<
68+
using OptionVariant = Variant<
6969
NoValueTag, BoolRef, StringRef, IntRef, UintRef, RealRef, StringVecRef, IntVecRef, UintVecRef, RealVecRef,
70-
PathRef>
71-
value;
70+
PathRef>;
71+
using OptionValueVariant = Variant<
72+
String, int, unsigned int, double, Vector<String>, Vector<int>, Vector<unsigned int>, Vector<double>, Path>;
73+
74+
OptionVariant value;
7275
bool found;
76+
Optional<OptionValueVariant> default_value;
7377
constexpr static inline size_t npos = static_cast<size_t>(-1);
7478
constexpr static inline Type map_t_to_type(const OptionType auto& val) {
7579
using T = RemoveCvRefT<decltype(val)>;
@@ -99,7 +103,12 @@ class ArgParser {
99103
}
100104
Option() : type{ Type::NoValue }, description{}, value_name{}, value(NoValueTag{}), found{ false } {}
101105
Option(StringView desc, StringView name, OptionType auto&& val) :
102-
type{ map_t_to_type(val) }, description{ desc }, value_name{ name }, found{ false } {
106+
type{ map_t_to_type(val) }, description{ desc }, value_name{ name }, found{ false }, default_value{} {
107+
value = move(val);
108+
}
109+
Option(StringView desc, StringView name, OptionType auto&& val, Optional<OptionValueVariant>&& default_value) :
110+
type{ map_t_to_type(val) }, description{ desc }, value_name{ name }, found{ false },
111+
default_value{ move(default_value) } {
103112
value = move(val);
104113
}
105114
bool requires_value() const;
@@ -135,6 +144,7 @@ class ArgParser {
135144
return true;
136145
}
137146
bool has_default() const;
147+
bool has_default_value() const;
138148
};
139149
using OptT = Pair<OptionName, Option>;
140150
Vector<OptT> m_options{};
@@ -164,19 +174,43 @@ class ArgParser {
164174
void add_usage_string(StringView usage_string);
165175
ParseResult parse();
166176
bool help_requested() const;
167-
ArgParser& add_option(OptionName opt_name, StringView value_name, StringView description, String& value_ref);
168-
ArgParser& add_option(OptionName opt_name, StringView value_name, StringView description, Path& value_ref);
169-
ArgParser& add_option(OptionName opt_name, StringView value_name, StringView description, int& value_ref);
170-
ArgParser& add_option(OptionName opt_name, StringView value_name, StringView description, unsigned int& value_ref);
171-
ArgParser& add_option(OptionName opt_name, StringView value_name, StringView description, double& value_ref);
172-
ArgParser& add_option(OptionName opt_name, StringView value_name, StringView description, Vector<int>& value_ref);
173-
ArgParser&
174-
add_option(OptionName opt_name, StringView value_name, StringView description, Vector<unsigned int>& value_ref);
175-
ArgParser&
176-
add_option(OptionName opt_name, StringView value_name, StringView description, Vector<String>& value_ref);
177+
ArgParser& add_option(
178+
OptionName opt_name, StringView value_name, StringView description, String& value_ref,
179+
Optional<String> default_value = {}
180+
);
181+
ArgParser& add_option(
182+
OptionName opt_name, StringView value_name, StringView description, Path& value_ref,
183+
Optional<Path> default_value = {}
184+
);
185+
ArgParser& add_option(
186+
OptionName opt_name, StringView value_name, StringView description, int& value_ref, Optional<int> default_value = {}
187+
);
188+
ArgParser& add_option(
189+
OptionName opt_name, StringView value_name, StringView description, unsigned int& value_ref,
190+
Optional<unsigned int> default_value = {}
191+
);
192+
ArgParser& add_option(
193+
OptionName opt_name, StringView value_name, StringView description, double& value_ref,
194+
Optional<double> default_value = {}
195+
);
196+
ArgParser& add_option(
197+
OptionName opt_name, StringView value_name, StringView description, Vector<int>& value_ref,
198+
Optional<Vector<int>> default_value = {}
199+
);
200+
ArgParser& add_option(
201+
OptionName opt_name, StringView value_name, StringView description, Vector<unsigned int>& value_ref,
202+
Optional<Vector<unsigned int>> default_value = {}
203+
);
204+
ArgParser& add_option(
205+
OptionName opt_name, StringView value_name, StringView description, Vector<String>& value_ref,
206+
Optional<Vector<String>> default_value = {}
207+
);
208+
ArgParser& add_option(
209+
OptionName opt_name, StringView value_name, StringView description, Vector<double>& value_ref,
210+
Optional<Vector<double>> default_value = {}
211+
);
177212
ArgParser&
178-
add_option(OptionName opt_name, StringView value_name, StringView description, Vector<double>& value_ref);
179-
ArgParser& add_option(OptionName opt_name, StringView description, bool& value_ref);
213+
add_option(OptionName opt_name, StringView description, bool& value_ref, Optional<bool> default_value = {});
180214
ArgParser& add_option(OptionName opt_name, StringView description, NoValueTag);
181215
void print_help() const;
182216
const String& help_string() const;

include/EventLoop.hpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,33 @@ class EventLoop {
99
Vector<Function<void()>> m_callbacks;
1010
Mutex m_callback_loc;
1111
Thread m_thread{};
12-
bool m_running{ false };
12+
Atomic<bool> m_running{ false };
13+
Atomic<bool> m_sleeping{ false };
14+
ConditionVariable m_condition_var;
15+
ConditionVariable m_sleep_condition;
1316

1417
static void loop_function(EventLoop* loop);
1518

1619
public:
17-
EventLoop() = default;
18-
20+
enum class JoinType {
21+
WaitUntilFinished,
22+
ForceStop,
23+
};
24+
EventLoop() = default;
25+
EventLoop(const EventLoop&) = delete;
26+
EventLoop(EventLoop&& other) {
27+
other.m_thread.swap(m_thread);
28+
m_callbacks = move(other.m_callbacks);
29+
m_running = other.m_running.load();
30+
m_sleeping = other.m_sleeping.load();
31+
m_condition_var = move(other.m_condition_var);
32+
m_sleep_condition = move(other.m_sleep_condition);
33+
};
1934
void start();
20-
void stop() { m_running = false; }
35+
void stop() {
36+
m_condition_var.notify_one();
37+
m_running = false;
38+
}
2139
bool running() { return m_running; }
2240
template <typename Functor, typename... Args>
2341
requires CallableWith<Functor, Args...>
@@ -29,9 +47,15 @@ class EventLoop {
2947
ScopedLock lock{ m_callback_loc };
3048
m_callbacks.append(move(lam));
3149
if (!m_running) start();
50+
m_condition_var.notify_one();
3251
}
3352
}
34-
int join() {
53+
int join(JoinType join_type = JoinType::WaitUntilFinished) {
54+
if (join_type == JoinType::WaitUntilFinished) {
55+
UniqueLock lock{ m_callback_loc };
56+
m_sleep_condition.wait(lock, [&]() { return m_sleeping.load(); });
57+
}
58+
stop();
3559
m_thread.join();
3660
return 0;
3761
}

0 commit comments

Comments
 (0)