Files
boost_smart_ptr/doc/smart_ptr/enable_shared_from_this.adoc

149 lines
3.3 KiB
Plaintext
Raw Normal View History

////
2017-06-13 01:34:31 +03:00
Copyright 2002, 2003, 2015, 2017 Peter Dimov
Distributed under the Boost Software License, Version 1.0.
See accompanying file LICENSE_1_0.txt or copy at
http://www.boost.org/LICENSE_1_0.txt
////
[#enable_shared_from_this]
# enable_shared_from_this
:toc:
:toc-title:
2017-06-12 03:12:03 +03:00
:idprefix: enable_shared_from_this_
2017-06-13 01:34:31 +03:00
## Description
The class template `enable_shared_from_this` is used as a base class that allows
a `shared_ptr` or a `weak_ptr` to the current object to be obtained from within a
member function.
`enable_shared_from_this<T>` defines two member functions called `shared_from_this`
that return a `shared_ptr<T>` and `shared_ptr<T const>`, depending on constness, to
`this`. It also defines two member functions called `weak_from_this` that return a
corresponding `weak_ptr`.
## Example
```
#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <cassert>
class Y: public boost::enable_shared_from_this<Y>
{
public:
boost::shared_ptr<Y> f()
{
return shared_from_this();
}
};
int main()
{
boost::shared_ptr<Y> p(new Y);
boost::shared_ptr<Y> q = p->f();
assert(p == q);
assert(!(p < q || q < p)); // p and q must share ownership
}
```
## Synopsis
`enable_shared_from_this` is defined in `<boost/smart_ptr/enable_shared_from_this.hpp>`.
```
2017-06-15 21:23:06 +03:00
namespace boost {
2017-06-13 01:34:31 +03:00
2017-06-15 21:23:06 +03:00
template<class T> class enable_shared_from_this {
private:
2017-06-13 01:34:31 +03:00
// exposition only
weak_ptr<T> weak_this_;
2017-06-15 21:23:06 +03:00
protected:
2017-06-13 01:34:31 +03:00
enable_shared_from_this() = default;
~enable_shared_from_this() = default;
enable_shared_from_this(const enable_shared_from_this&) noexcept;
enable_shared_from_this& operator=(const enable_shared_from_this&) noexcept;
2017-06-15 21:23:06 +03:00
public:
2017-06-13 01:34:31 +03:00
shared_ptr<T> shared_from_this();
shared_ptr<T const> shared_from_this() const;
weak_ptr<T> weak_from_this() noexcept;
weak_ptr<T const> weak_from_this() const noexcept;
2017-06-15 21:23:06 +03:00
}
2017-06-13 01:34:31 +03:00
}
```
## Members
```
enable_shared_from_this(enable_shared_from_this const &) noexcept;
```
[none]
* {blank}
+
Effects:: Default-constructs `weak_this_`.
NOTE: `weak_this_` is _not_ copied from the argument.
```
enable_shared_from_this& operator=(enable_shared_from_this const &) noexcept;
```
[none]
* {blank}
+
Returns:: `*this`.
NOTE: `weak_this_` is unchanged.
```
2017-06-15 21:34:53 +03:00
template<class T> shared_ptr<T> shared_from_this();
2017-06-13 01:34:31 +03:00
```
```
2017-06-15 21:34:53 +03:00
template<class T> shared_ptr<T const> shared_from_this() const;
2017-06-13 01:34:31 +03:00
```
[none]
* {blank}
+
Returns:: `shared_ptr<T>(weak_this_)`.
NOTE: These members throw `bad_weak_ptr` when `*this` is not owned by a `shared_ptr`.
[NOTE]
====
`weak_this_` is initialized by `shared_ptr` to a copy of itself when it's constructed by a pointer to `*this`.
For example, in the following code:
```
class Y: public boost::enable_shared_from_this<Y> {};
2017-06-13 01:34:31 +03:00
int main()
{
boost::shared_ptr<Y> p(new Y);
}
```
the construction of `p` will automatically initialize `p\->weak_this_` to `p`.
2017-06-13 01:34:31 +03:00
====
```
2017-06-15 21:34:53 +03:00
template<class T> weak_ptr<T> weak_from_this() noexcept;
2017-06-13 01:34:31 +03:00
```
```
2017-06-15 21:34:53 +03:00
template<class T> weak_ptr<T const> weak_from_this() const noexcept;
2017-06-13 01:34:31 +03:00
```
[none]
* {blank}
+
Returns:: `weak_this_`.
2019-04-24 05:16:03 +03:00
NOTE: Unlike `shared_from_this()`, `weak_from_this()` is valid in a destructor
and returns a `weak_ptr` that is `expired()` but still shares ownership
with other `weak_ptr` instances (if any) that refer to the object.