Skip to content

Commit 5eacb7a

Browse files
committed
Updated readme after rename of observer_ptr and lock
1 parent e039fba commit 5eacb7a

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

README.md

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44

55
## Introduction
66

7-
This is a small header-only library, providing a unique-ownership smart pointer that can be observed with weak pointers. It is a mixture of `std::unique_ptr` and `std::shared_ptr`: it borrows the unique-ownership semantic of `std::unique_ptr` (movable, non-copiable), but allows creating `std::weak_ptr` instances to monitor the lifetime of the pointed object.
7+
This is a small header-only library, providing a unique-ownership smart pointer `observable_unique_ptr` that can be observed with non-owning pointers `observer_ptr`. It is a mixture of `std::unique_ptr` and `std::shared_ptr`: it borrows the unique-ownership semantic of `std::unique_ptr` (movable, non-copiable), but allows creating observer pointers (like `std::weak_ptr` for `std::shared_ptr`) to monitor the lifetime of the pointed object.
88

9-
This is useful for cases where the shared-ownership of `std::shared_ptr` is not desirable, e.g., when lifetime must be carefully controlled and not be allowed to extend, yet non-owning "weak" references to the object may exist after the object has been deleted.
9+
This is useful for cases where the shared-ownership of `std::shared_ptr` is not desirable, e.g., when lifetime must be carefully controlled and not be allowed to extend, yet non-owning/weak/observer references to the object may exist after the object has been deleted.
1010

11-
Note: Because of the unique ownership model, weak pointers locking cannot extend the lifetime of the pointed object, hence `observable_unique_ptr` provides less thread-safety compared to `std::shared_ptr`. This is also true of `std::unique_ptr`, and is a fundamental limitation of unique ownership. Do not use this library if the lifetime of your objects is not well understood.
11+
Note: Because of the unique ownership model, observer pointers cannot extend the lifetime of the pointed object, hence `observable_unique_ptr`/`observer_ptr` provides less thread-safety compared to `std::shared_ptr`/`std::weak_ptr`. This is also true of `std::unique_ptr`, and is a fundamental limitation of unique ownership. If this is an issue, you will need either to add your own explicit locking logic, or use `std::shared_ptr`/`std::weak_ptr`.
1212

1313

1414
## Usage
1515

16-
This is a header-only library. You have multiple ways to set it up:
16+
This is a header-only library requiring a C++17-compliant compiler. You have multiple ways to set it up:
1717
- just include this repository as a submodule in your own git repository and use CMake `add_subdirectory`,
1818
- use CMake `FetchContent`,
1919
- download the header and include it in your own sources.
@@ -28,35 +28,34 @@ From there, include the single header `<oup/observable_unique_ptr.hpp>`, and dir
2828
#include <cassert>
2929

3030
int main() {
31-
// Weak pointer that will outlive the object
32-
oup::weak_ptr<std::string> wptr;
31+
// Non-owning pointer that will outlive the object
32+
oup::observer_ptr<std::string> obs_ptr;
3333

3434
{
3535
// Unique pointer that owns the object
36-
auto ptr = oup::make_observable_unique<std::string>("hello");
36+
auto owner_ptr = oup::make_observable_unique<std::string>("hello");
3737

38-
// Make the weak pointer observe the object
39-
wptr = ptr;
38+
// Make the observer pointer point to the object
39+
obs_ptr = owner_ptr;
4040

41-
// Weak pointer is valid
42-
assert(!wptr.expired());
41+
// Observer pointer is valid
42+
assert(!obs_ptr.expired());
4343

44-
// It can be locked to get a (non-owning!) pointer to the object
45-
std::string* s = wptr.lock();
46-
assert(s != nullptr);
47-
std::cout << *s << std::endl;
44+
// It can be used like a regular raw pointer
45+
assert(obs_ptr != nullptr);
46+
std::cout << *obs_ptr << std::endl;
4847

4948
// The unique pointer cannot be copied
50-
auto tmp_copied = ptr; // error!
49+
auto tmp_copied = owner_ptr; // error!
5150

5251
// ... but it can be moved
53-
auto tmp_moved = std::move(ptr); // OK
52+
auto tmp_moved = std::move(owner_ptr); // OK
5453
}
5554

5655
// The unique pointer has gone out of scope, the object is deleted,
57-
// the weak pointer is now null.
58-
assert(wptr.expired());
59-
assert(wptr.lock() == nullptr);
56+
// the observer pointer is now null.
57+
assert(obs_ptr.expired());
58+
assert(obs_ptr == nullptr);
6059

6160
return 0;
6261
}

0 commit comments

Comments
 (0)