Skip to content

Commit

Permalink
Merge pull request arximboldi#164 from jarzec/throw-on-uninitialized
Browse files Browse the repository at this point in the history
Throw on access to uninitialized reader/writer
  • Loading branch information
arximboldi authored Nov 18, 2022
2 parents 11b4326 + c897e99 commit 24887ac
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
4 changes: 2 additions & 2 deletions lager/detail/access.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class access
}

/*!
* Returns a pointer to th underlying node of an object, if
* Returns a pointer to the underlying node of an object, if
* exists.
*/
template <typename T>
Expand All @@ -49,7 +49,7 @@ class access
}

/*!
* Returns a a optional boost.signal to the specific watchers of
* Returns an optional boost.signal to the specific watchers of
* the underlying signal of an object.
*/
template <typename T>
Expand Down
5 changes: 4 additions & 1 deletion lager/reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ struct reader_mixin

auto node_() const
{
return detail::access::node(*static_cast<const DerivT*>(this));
if(auto node = detail::access::node(*static_cast<const DerivT*>(this))) {
return node;
}
throw std::runtime_error("Accessing uninitialized reader");
}
};

Expand Down
5 changes: 4 additions & 1 deletion lager/writer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ struct writer_mixin

auto node_() const
{
return detail::access::node(*static_cast<const DerivT*>(this));
if(auto node = detail::access::node(*static_cast<const DerivT*>(this))) {
return node;
}
throw std::runtime_error("Accessing uninitialized writer");
}
};

Expand Down
9 changes: 9 additions & 0 deletions test/cursor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,29 @@ using namespace lager;
TEST_CASE("in, construction and assignment from temporary")
{
reader<int> in1;
// Access to uninitialized reader should throw an exception
REQUIRE_THROWS(in1.get());
in1 = make_state(0);
reader<int> in2{make_state(0)};
}

TEST_CASE("out, construction_and_assignment_from_temporary")
{
writer<int> out1;
// Access to uninitialized writer should throw an exception
REQUIRE_THROWS(out1.set(42));
REQUIRE_THROWS(out1.update([](auto){ return 42;}));
out1 = make_state(0);
writer<int> out2{make_state(0)};
}

TEST_CASE("inout, construction_and_assignment_from_temporary")
{
cursor<int> inout1;
// Access to uninitialized cursor should throw an exception
REQUIRE_THROWS(inout1.get());
REQUIRE_THROWS(inout1.set(42));
REQUIRE_THROWS(inout1.update([](auto){ return 42;}));
inout1 = make_state(0);
cursor<int> inout2{make_state(0)};
}
Expand Down

0 comments on commit 24887ac

Please sign in to comment.