You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+19-20Lines changed: 19 additions & 20 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,16 +4,16 @@
4
4
5
5
## Introduction
6
6
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.
8
8
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.
10
10
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`.
12
12
13
13
14
14
## Usage
15
15
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:
17
17
- just include this repository as a submodule in your own git repository and use CMake `add_subdirectory`,
18
18
- use CMake `FetchContent`,
19
19
- 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
28
28
#include<cassert>
29
29
30
30
intmain() {
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;
33
33
34
34
{
35
35
// 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");
37
37
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;
40
40
41
-
// Weak pointer is valid
42
-
assert(!wptr.expired());
41
+
// Observer pointer is valid
42
+
assert(!obs_ptr.expired());
43
43
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;
48
47
49
48
// The unique pointer cannot be copied
50
-
auto tmp_copied = ptr; // error!
49
+
auto tmp_copied = owner_ptr; // error!
51
50
52
51
// ... but it can be moved
53
-
auto tmp_moved = std::move(ptr); // OK
52
+
auto tmp_moved = std::move(owner_ptr); // OK
54
53
}
55
54
56
55
// The unique pointer has gone out of scope, the object is deleted,
0 commit comments