Modify

Opened 16 years ago

Closed 15 years ago

#34 closed Feature Requests (Rejected)

enhance shared_ptr with traits class

Reported by: mkvonarx Owned by: Peter Dimov
Milestone: Component: smart_ptr
Version: None Severity:
Keywords: Cc:

Description

I suggest introducing a traits class for the boost 
smart pointers (shared_ptr and shared_array only) that 
take over responsibility for reference counting: 
ref_count_traits.

With such a design, every user of the boost smart 
pointers could add specific additional features like 
thread safety, objects knowing about being reference 
counted, objects handling reference counting 
themselves (but still be used via boost smart 
pointers) etc.

The default implementation of ref_count_traits could 
look somehow like this:

template<typename T> class ref_count_traits
{
public:
  struct ref_counted_ptr
  {
    typedef long ref_count_counter_type;
  
    explicit ref_count_ptr_impl(T* p):
      px(p)
    {
      try
      {
        pn = new ref_count_counter_type(0);
      }
      catch(...)
      {
        delete p;
        throw;
      }
    }

    T*                       px;     // contained 
pointer
    ref_count_counter_type*  pn;     // ptr to 
reference counter
  };

  typedef ref_counted_ptr ref_count_ptr_type;

  static T* get(const ref_count_ptr_type& pr)
  {
    return pr.px;
  }

  static ref_count_counter_type count(const 
ref_count_ptr_type& pr)
  {
    return pr.pn;
  }

  static void inc(const ref_count_ptr_type& pr)
  {
    ++*pr.pn;
  }

  static void dec(const ref_count_ptr_type& pr)
  {
    if(--*pr.pn == 0)
    {
      delete pr.pn;
      delete pr.px;
    }
  }
};



Attachments (0)

Change History (1)

comment:1 Changed 15 years ago by Peter Dimov

Status: assignedclosed

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Peter Dimov.
The resolution will be deleted.

Add Comment


E-mail address and name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.