jss::object_ptr<T>
This is an implementation of a class similar
to std::experimental::observer_ptr
from the Library Fundamentals TS v2, but with various improvements
suggested in WG21 email discussions of the feature.
std::experimental::observer_ptr
Differences to The most obvious change is the name: observer_ptr
is a bad name, because it conjures up the idea
of the Observer pattern, but it doesn't really "observe" anything. I believe object_ptr
is better:
it is a pointer to an object, so doesn't have any array-related functionality such as pointer
arithmetic, but it doesn't tell you anything about ownership.
The most important change to functionality is that it has implicit conversions from raw
pointers, std::shared_ptr<T>
and std::unique_ptr<T>
. This facilitates the use of
jss::object_ptr<T>
as a drop-in replacement for T*
in function parameters. There is nothing you
can do with a jss::object_ptr<T>
that you can't do with a T*
, and in fact there is considerably
less that you can do. The same applies with std::shared_ptr<T>
and std::unique_ptr<T>
: you are
reducing functionality, so this is safe, and reducing typing for safe operations is a good thing.
The other change is the removal of the release()
member function. An object_ptr
doesn't own
anything, so it can't release ownership. To clear it, call reset()
; if you want the wrapped
pointer, call get()
first.
Examples
#include "object_ptr.hpp"
#include <iostream>
void foo(jss::object_ptr<int> p) {
if(p) {
std::cout << *p;
} else {
std::cout << "(null)";
}
std::cout << "\n";
}
int main() {
foo(nullptr);
int x= 42;
foo(&x);
auto sp= std::make_shared<int>(123);
foo(sp);
auto up= std::make_unique<int>(456);
foo(up);
}
The most common expected use case is as a function parameter, as in the example above. It is similar
to std::string_view
from C++17 and std::span
from C++20, in that it provides a view on data
managed elsewhere, and we only care about accessing that data, not how it is managed. It is
therefore ideally suited to places where you know the lifetime of the pointee exceeds the lifetime
of the object_ptr
. Function arguments typically provide this guarantee, but it may also be used as
a class member where the pointee outlives the new class object, such as where the wrapper class
object is created and destroyed within a scope, and the pointee outlives that scope.
License
This code is released under the Boost Software License:
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization obtaining a copy of the software and accompanying documentation covered by this license (the "Software") to use, reproduce, display, distribute, execute, and transmit the Software, and to prepare derivative works of the Software, and to permit third-parties to whom the Software is furnished to do so, all subject to the following:
The copyright notices in the Software and this entire statement, including the above license grant, this restriction and the following disclaimer, must be included in all copies of the Software, in whole or in part, and all derivative works of the Software, unless such copies or derivative works are solely in the form of machine-executable object code generated by a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.