cycle_ptr
Protected Member Functions | Friends | Related Functions | List of all members
cycle_ptr::cycle_base Class Reference

An optional base for classes which need to supply ownership to cycle_member_ptr. More...

#include <cycle_ptr.h>

Protected Member Functions

 cycle_base ()
 Default constructor acquires its control block from context. More...
 
 cycle_base ([[maybe_unused]] unowned_cycle_t unowned_tag)
 Specialized constructor that signifies *this will not be pointed at by a cycle_ptr. More...
 
 cycle_base (const cycle_base &) noexcept
 Copy constructor. More...
 
auto operator= (const cycle_base &) noexcept -> cycle_base &
 Copy assignment. More...
 
 ~cycle_base () noexcept=default
 Default destructor.
 
template<typename T >
auto shared_from_this (T *this_ptr) const -> cycle_gptr< T >
 Create a cycle_gptr (equivalent of std::shared_ptr) from this. More...
 

Friends

template<typename >
class cycle_member_ptr
 
template<typename >
class cycle_allocator
 

Related Functions

(Note that these are not member functions.)

template<typename T , typename Alloc , typename... Args>
auto allocate_cycle (Alloc alloc, Args &&... args) -> cycle_gptr< T >
 Allocate a new instance of T, using the specificied allocator. More...
 
template<typename T , typename... Args>
auto make_cycle (Args &&... args) -> cycle_gptr< T >
 Allocate a new instance of T, using the default allocator. More...
 

Detailed Description

An optional base for classes which need to supply ownership to cycle_member_ptr.

The cycle_base keeps track of the control block of the object participating in the cycle_ptr graph, as well as providing a shared from this utility.

You are not required to inherit from cycle_base, for the cycle_ptr graph to function correctly.

Constructor & Destructor Documentation

◆ cycle_base() [1/3]

cycle_ptr::cycle_base::cycle_base ( )
inlineprotected

Default constructor acquires its control block from context.

Uses publisher logic to look up the control block for its range. Those ranges are published by cycle_allocator, make_cycle, and allocate_cycle.

Exceptions
std::runtime_errorif no range was published.

◆ cycle_base() [2/3]

cycle_ptr::cycle_base::cycle_base ( [[maybe_unused] ] unowned_cycle_t  unowned_tag)
inlineprotected

Specialized constructor that signifies *this will not be pointed at by a cycle_ptr.

Exceptions
std::bad_allocIf there is not enough memory to create the required control block.

◆ cycle_base() [3/3]

cycle_ptr::cycle_base::cycle_base ( const cycle_base )
inlineprotectednoexcept

Copy constructor.

Provided so that you don't lose the default copy constructor semantics, but keep in mind that this constructor simply invokes the default constructor.

Note
A copy has a different, automatically deduced, control block.
Exceptions
std::runtime_errorif no range was published.

Member Function Documentation

◆ operator=()

auto cycle_ptr::cycle_base::operator= ( const cycle_base ) -> cycle_base&
inlineprotectednoexcept

Copy assignment.

A noop, provided so you don't lose default assignment in derived classes.

◆ shared_from_this()

template<typename T >
auto cycle_ptr::cycle_base::shared_from_this ( T *  this_ptr) const -> cycle_gptr<T>
inlineprotected

Create a cycle_gptr (equivalent of std::shared_ptr) from this.

The returned smart pointer uses the control block of this.

Instead of using this->shared_from_this(this), you could use this to create pointers directly from member variables, by invocing this->shared_from_this(&this->member_variable).

Exceptions
std::bad_weak_ptrIf the smart pointer can not be created. This occurs when invoked during constructor of derived type, or during its destruction. Note that if unowned_cycle is used to construct the base, this method will always throw std::bad_weak_ptr.

Friends And Related Function Documentation

◆ allocate_cycle()

template<typename T , typename Alloc , typename... Args>
auto allocate_cycle ( Alloc  alloc,
Args &&...  args 
) -> cycle_gptr<T>
related

Allocate a new instance of T, using the specificied allocator.

Ensures the type T is instantiated correctly, with its control block.

Template Parameters
TThe type of object to instantiate.
Parameters
allocThe allocator to use for allocating the control block.
argsThe arguments passed to the constructor of type T.
Returns
A cycle_gptr to the new instance of T.
Exceptions
std::bad_allocif allocating a generation fails.

◆ make_cycle()

template<typename T , typename... Args>
auto make_cycle ( Args &&...  args) -> cycle_gptr<T>
related

Allocate a new instance of T, using the default allocator.

Ensures the type T is instantiated correctly, with its control block.

Equivalent to calling allocate_cycle<T>(std::allocator<T>(), args...).

Template Parameters
TThe type of object to instantiate.
Parameters
argsThe arguments passed to the constructor of type T.
Returns
A cycle_gptr to the new instance of T.
Exceptions
std::bad_allocif allocating a generation fails.

The documentation for this class was generated from the following file: