diff --git a/README.md b/README.md index ec2d4bf46..9e214a748 100644 --- a/README.md +++ b/README.md @@ -244,6 +244,40 @@ TEST_CASE("Interface overload-sets are directly supported.") +
+Watching object-instances + +``mimicpp::Watched`` helper can report destruction and relocations of object-instances. + +```cpp +#include + +namespace expect = mimicpp::expect; +namespace then = mimicpp::then; + +TEST_CASE("LifetimeWatcher and RelocationWatcher can trace object instances.") +{ + mimicpp::Watched< + mimicpp::Mock, + mimicpp::LifetimeWatcher, + mimicpp::RelocationWatcher> watched{}; + + SCOPED_EXP watched.expect_destruct(); + int relocationCounter{}; + SCOPED_EXP watched.expect_relocate() + and then::invoke([&] { ++relocationCounter; }) + and expect::at_least(1); + + std::optional wrapped{std::move(watched)}; // satisfies one relocate-expectation + std::optional other{std::move(wrapped)}; // satisfies a second relocate-expectation + wrapped.reset(); // won't require a destruct-expectation, as moved-from objects are considered dead + other.reset(); // fulfills the destruct-expectation + REQUIRE(2 == relocationCounter); // let's see, how often the instance has been relocated +} +``` + +
+ ### Other Choices #### Always Stay Within The Language Definition diff --git a/examples/Watcher.cpp b/examples/Watcher.cpp index 5da8abb4b..488fd92a0 100644 --- a/examples/Watcher.cpp +++ b/examples/Watcher.cpp @@ -221,10 +221,10 @@ TEST_CASE( and expect::at_least(1); std::optional wrapped{std::move(watched)}; // satisfies one relocate-expectation - std::optional other{std::move(wrapped)}; // satisfies a second relocate-expectation - wrapped.reset(); // won't require a destruct-expectation, as moved-from objects are considered dead - other.reset(); // fulfills the destruct-expectation - REQUIRE(2 == relocationCounter); // let's see, how often the instance has been relocated + std::optional other{std::move(wrapped)}; // satisfies a second relocate-expectation + wrapped.reset(); // won't require a destruct-expectation, as moved-from objects are considered dead + other.reset(); // fulfills the destruct-expectation + REQUIRE(2 == relocationCounter); // let's see, how often the instance has been relocated //! [watched lifetime relocation] } STOP_WARNING_SUPPRESSION