From f90b418460135fe52036c8764957c3eb7e336cac Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Tue, 13 Jun 2017 01:34:31 +0300 Subject: [PATCH] Add enable_shared_from_this.adoc --- doc/smart_ptr/enable_shared_from_this.adoc | 136 ++++++++++++++++++++- 1 file changed, 135 insertions(+), 1 deletion(-) diff --git a/doc/smart_ptr/enable_shared_from_this.adoc b/doc/smart_ptr/enable_shared_from_this.adoc index 428fc30..62c16d0 100644 --- a/doc/smart_ptr/enable_shared_from_this.adoc +++ b/doc/smart_ptr/enable_shared_from_this.adoc @@ -1,5 +1,5 @@ //// -Copyright 2017 Peter Dimov +Copyright 2002, 2003, 2015, 2017 Peter Dimov Distributed under the Boost Software License, Version 1.0. @@ -13,3 +13,137 @@ http://www.boost.org/LICENSE_1_0.txt :toc-title: :idprefix: enable_shared_from_this_ +## 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` defines two member functions called `shared_from_this` +that return a `shared_ptr` and `shared_ptr`, depending on constness, to +`this`. It also defines two member functions called `weak_from_this` that return a +corresponding `weak_ptr`. + +## Example + +``` +#include +#include +#include + +class Y: public boost::enable_shared_from_this +{ +public: + + boost::shared_ptr f() + { + return shared_from_this(); + } +}; + +int main() +{ + boost::shared_ptr p(new Y); + boost::shared_ptr 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 ``. + +``` +namespace boost +{ + +template class enable_shared_from_this +{ +private: + + // exposition only + weak_ptr weak_this_; + +protected: + + 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; + +public: + + shared_ptr shared_from_this(); + shared_ptr shared_from_this() const; + + weak_ptr weak_from_this() noexcept; + weak_ptr weak_from_this() const noexcept; +} + +} // namespace boost +``` + +## 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. + +``` +template shared_ptr enable_shared_from_this::shared_from_this(); +``` +``` +template shared_ptr enable_shared_from_this::shared_from_this() const; +``` +[none] +* {blank} ++ +Returns:: `shared_ptr(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 +{ +}; + +int main() +{ + boost::shared_ptr p(new Y); +} +``` +the construction of `p` will automatically initialize `p->weak_this_` to `p`. +==== + +``` +template weak_ptr enable_shared_from_this::weak_from_this() noexcept; +``` +``` +template weak_ptr enable_shared_from_this::weak_from_this() const noexcept; +``` +[none] +* {blank} ++ +Returns:: `weak_this_`.