Skip to content

Commit 7a2d89c

Browse files
committed
Added de-reference operators to observer_ptr and added tests
1 parent 4418f25 commit 7a2d89c

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

include/oup/observable_unique_ptr.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,25 @@ class observer_ptr : private std::weak_ptr<T> {
363363
return std::weak_ptr<T>::lock().get();
364364
}
365365

366+
/// Get a reference to the pointed object (undefined behavior if deleted).
367+
/** \return A reference to the pointed object
368+
* \note Using this function if expired() is 'true' will leave to undefined behavior.
369+
*/
370+
T& operator*() const noexcept {
371+
return *std::weak_ptr<T>::lock().get();
372+
}
373+
374+
/// Get a non-owning raw pointer to the pointed object, or nullptr if deleted.
375+
/** \return 'nullptr' if expired() is 'true', or the pointed object otherwise
376+
* \note Contrary to std::weak_ptr::lock(), this does not extend the lifetime
377+
* of the pointed object. Therefore, when calling this function, you must
378+
* make sure that the owning observable_unique_ptr will not be reset until
379+
* you are done using the raw pointer.
380+
*/
381+
T* operator->() const noexcept {
382+
return std::weak_ptr<T>::lock().get();
383+
}
384+
366385
/// Swap the content of this pointer with that of another pointer.
367386
/** \param other The other pointer to swap with
368387
*/

tests/runtime_tests.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,12 @@ TEST_CASE("owner swap two instances with deleter", "[owner_utility]") {
523523
REQUIRE(instances_deleter == 0);
524524
}
525525

526+
TEST_CASE("owner dereference", "[owner_utility]") {
527+
test_ptr ptr(new test_object);
528+
REQUIRE(ptr->state_ == 1337);
529+
REQUIRE((*ptr).state_ == 1337);
530+
}
531+
526532
TEST_CASE("make observable", "[make_observable_unique]") {
527533
{
528534
test_ptr ptr = oup::make_observable_unique<test_object>();
@@ -743,6 +749,13 @@ TEST_CASE("observer swap two different instances", "[observer_utility]") {
743749
REQUIRE(instances == 0);
744750
}
745751

752+
TEST_CASE("observer dereference", "[observer_utility]") {
753+
test_ptr ptr_owner(new test_object);
754+
test_optr ptr(ptr_owner);
755+
REQUIRE(ptr->state_ == 1337);
756+
REQUIRE((*ptr).state_ == 1337);
757+
}
758+
746759
TEST_CASE("observer comparison valid ptr vs nullptr", "[observer_comparison]") {
747760
test_ptr ptr_owner(new test_object);
748761
test_optr ptr(ptr_owner);

tests/tests_common.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ int instances_thrower = 0;
88
int instances_deleter = 0;
99

1010
struct test_object {
11+
int state_ = 1337;
12+
1113
test_object() noexcept { ++instances; }
1214
virtual ~test_object() noexcept { --instances; }
1315

0 commit comments

Comments
 (0)