boost.png (6897 bytes)basic_intrusive_ref_counter class template

Introduction
Synopsis
Members

Introduction

The basic_intrusive_ref_counter class template implements a reference counter for a defived user's class that is intended to be used with intrusive_ptr. The base class has associated intrusive_ptr_add_ref and intrusive_ptr_release functions which modify the reference counter as needed and destroy the user's object when the counter drops to zero.

The class template is parameterized on CounterPolicyT, which is a policy that defines the nature of the reference counter. Boost.SmartPtr provides two such policies: thread_unsafe_counter and thread_safe_counter. The former instructs the basic_intrusive_ref_counter base class to use a counter only suitable for a single-threaded use. Pointers to a single object that uses this kind of reference counter must not be used in different threads. The latter policy makes the reference counter thread-safe, unless the target platform doesn't support threading. Since in modern systems support for threading is common, a shorthand intrusive_ref_counter is provided, which is an alias for basic_intrusive_ref_counter<thread_safe_counter>.

Synopsis

namespace boost {

  template<class CounterPolicyT> class basic_intrusive_ref_counter {

    public:

      basic_intrusive_ref_counter();
      basic_intrusive_ref_counter(basic_intrusive_ref_counter const & r);

      virtual ~basic_intrusive_ref_counter();

      basic_intrusive_ref_counter & operator=(basic_intrusive_ref_counter const & r) noexcept;

      unsigned int use_count() const noexcept;
  };

  struct thread_unsafe_counter;
  struct thread_safe_counter;

  typedef basic_intrusive_ref_counter<thread_safe_counter> intrusive_ref_counter;

}

Members

constructors

basic_intrusive_ref_counter();

Postconditions: use_count() == 0.

Throws: nothing.

Notes: The pointer to the constructed object is expected to be passed to intrusive_ptr constructor, assignment operator or reset() method, which would increment the reference counter.

basic_intrusive_ref_counter(basic_intrusive_ref_counter const &);

Postconditions: use_count() == 0.

Throws: nothing.

Notes: The pointer to the constructed object is expected to be passed to intrusive_ptr constructor, assignment operator or reset() method, which would increment the reference counter.

destructor

virtual ~basic_intrusive_ref_counter();

Effects: Destroys the counter object.

assignment

basic_intrusive_ref_counter & operator=(basic_intrusive_ref_counter const & r) noexcept;

Effects: Does nothing, reference counter is not modified.

Returns: *this.

use_count

unsigned int use_count() const noexcept;

Returns: The current value of the reference counter.

Throws: nothing.

Notes: The returned value may not be actual in multi-threaded applications.


$Date: 2013-08-31 23:37:43 +0400 (Sat, 31 Aug 2013) $

Copyright © 2013 Andrey Semashev. 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.