File tree Expand file tree Collapse file tree 4 files changed +29
-4
lines changed
Expand file tree Collapse file tree 4 files changed +29
-4
lines changed Original file line number Diff line number Diff 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
3940protected:
4041 ~cursor_mixin () = default ;
Original file line number Diff line number Diff line change @@ -32,6 +32,11 @@ class cursor_base;
3232template <typename DerivT>
3333struct 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
8291template <typename NodeT>
Original file line number Diff line number Diff line change @@ -28,6 +28,11 @@ class cursor_base;
2828template <typename DerivT>
2929struct 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
8190template <typename NodeT>
Original file line number Diff line number Diff line change @@ -37,30 +37,36 @@ using namespace lager;
3737TEST_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
4648TEST_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
5660TEST_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
You can’t perform that action at this time.
0 commit comments