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
As with `std::shared_ptr`/`std::weak_ptr`, if you need to obtain an observer pointer to an object when you only have `this` (i.e., from a member function), you can inherit from `oup::enable_observer_from_this<T>` to gain access to the `observer_from_this()` member function. This function will return a valid observer pointer as long as the object is owned by a unique or sealed pointer, and will return `nullptr` in all other cases. Contrary to `std::enable_shared_from_this<T>`, this feature naturally supports multiple inheritance.
87
+
88
+
## enable_observer_from_this
89
+
90
+
As with `std::shared_ptr`/`std::weak_ptr`, if you need to obtain an observer pointer to an object when you only have `this` (i.e., from a member function), you can inherit from `oup::enable_observer_from_this_unique<T>` or `oup::enable_observer_from_this_sealed<T>` (depending on the type of the owner pointer) to gain access to the `observer_from_this()` member function. Contrary to `std::enable_shared_from_this<T>`, this function is able to return a valid observer pointer at all times, even if the object is being constructed or is not owned by a unique or sealed pointer. Also contrary to `std::enable_shared_from_this<T>`, this feature naturally supports multiple inheritance.
91
+
92
+
To achieve this, the price to pay is that `oup::enable_observer_from_this_unique<T>` uses virtual inheritance, while `oup::enable_observer_from_this_sealed<T>` requires `T`'s constructor to take a control block as input (thereby preventing `T` from being default-constructible, copiable, or movable).
93
+
94
+
If these trade-offs are not appropriate for your use cases, they can be fine-tuned using the generic classes `basic_observable_pointer<T,Deleter,Policy>`, `basic_observer_ptr<T,Policy>`, and `basic_enable_observer_from_this<T,Policy>`. Please see the documentation for more information on policies.
87
95
88
96
89
97
## Limitations
@@ -152,6 +160,8 @@ Detail of the benchmarks:
152
160
- Create observer copy: construct a new observer pointer from another observer pointer.
153
161
- Dereference observer: get a reference to the underlying object from an observer pointer.
0 commit comments