Skip to content

Commit 8ea4a43

Browse files
authored
Merge pull request #234 from BenLeadbetter/feature/boolean-operators-for-cursors
Feature | Boolean operators for cursors
2 parents 038749d + 0366791 commit 8ea4a43

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

lager/cursor.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ struct cursor_mixin
3535
using writer_mixin<DerivT>::zoom;
3636
using writer_mixin<DerivT>::xform;
3737
using reader_mixin<DerivT>::xform;
38+
using reader_mixin<DerivT>::operator bool;
3839

3940
protected:
4041
~cursor_mixin() = default;

lager/reader.hpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class cursor_base;
3232
template <typename DerivT>
3333
struct reader_mixin
3434
{
35+
explicit operator bool() const
36+
{
37+
return maybe_node_() != nullptr;
38+
};
39+
3540
decltype(auto) get() const { return node_()->last(); }
3641
decltype(auto) operator*() const { return get(); }
3742
decltype(auto) operator->() const { return &get(); }
@@ -71,12 +76,16 @@ struct reader_mixin
7176

7277
auto node_() const
7378
{
74-
if (auto node =
75-
detail::access::node(*static_cast<const DerivT*>(this))) {
79+
if (auto node = maybe_node_()) {
7680
return node;
7781
}
7882
LAGER_THROW(std::runtime_error("Accessing uninitialized reader"));
7983
}
84+
85+
auto maybe_node_() const
86+
{
87+
return detail::access::node(*static_cast<const DerivT*>(this));
88+
}
8089
};
8190

8291
template <typename NodeT>

lager/writer.hpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ class cursor_base;
2828
template <typename DerivT>
2929
struct writer_mixin
3030
{
31+
explicit operator bool() const
32+
{
33+
return maybe_node_() != nullptr;
34+
};
35+
3136
template <typename T>
3237
void set(T&& value)
3338
{
@@ -70,12 +75,16 @@ struct writer_mixin
7075

7176
auto node_() const
7277
{
73-
if (auto node =
74-
detail::access::node(*static_cast<const DerivT*>(this))) {
78+
if (auto node = maybe_node_()) {
7579
return node;
7680
}
7781
LAGER_THROW(std::runtime_error("Accessing uninitialized writer"));
7882
}
83+
84+
auto maybe_node_() const
85+
{
86+
return detail::access::node(*static_cast<const DerivT*>(this));
87+
}
7988
};
8089

8190
template <typename NodeT>

test/cursor.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,36 @@ using namespace lager;
3737
TEST_CASE("in, construction and assignment from temporary")
3838
{
3939
reader<int> in1;
40+
CHECK(!in1);
4041
// Access to uninitialized reader should throw an exception
4142
REQUIRE_THROWS(in1.get());
4243
in1 = make_state(0);
44+
CHECK(in1);
4345
reader<int> in2{make_state(0)};
4446
}
4547

4648
TEST_CASE("out, construction_and_assignment_from_temporary")
4749
{
4850
writer<int> out1;
51+
CHECK(!out1);
4952
// Access to uninitialized writer should throw an exception
5053
REQUIRE_THROWS(out1.set(42));
5154
REQUIRE_THROWS(out1.update([](auto) { return 42; }));
5255
out1 = make_state(0);
56+
CHECK(out1);
5357
writer<int> out2{make_state(0)};
5458
}
5559

5660
TEST_CASE("inout, construction_and_assignment_from_temporary")
5761
{
5862
cursor<int> inout1;
63+
CHECK(!inout1);
5964
// Access to uninitialized cursor should throw an exception
6065
REQUIRE_THROWS(inout1.get());
6166
REQUIRE_THROWS(inout1.set(42));
6267
REQUIRE_THROWS(inout1.update([](auto) { return 42; }));
6368
inout1 = make_state(0);
69+
CHECK(inout1);
6470
cursor<int> inout2{make_state(0)};
6571
}
6672

0 commit comments

Comments
 (0)