Boost logo

Boost :

From: David B. Held (dheld_at_[hidden])
Date: 2002-05-21 14:43:25

"Peter Dimov" <pdimov_at_[hidden]> wrote in message
> [...]
> Can't the design be improved?


> Small changes in behavior should correspond to small changes
> in implementation, or to put it another way, less code.

I guess that depends on what constitutes "small changes". ;)
In this case, one problem is that two data members are stored
(both the mutex ref itself, and a bool for safer locking/unlocking).
But really, I'm afraid the Storage policy is as small as possible.
It encapsulates the locking behaviour that Bill designed, which
includes the ability to lock/unlock arbitrarily. If this feature were
removed, it would be possible to write something like this:

// Storage Policy template
template <typename Resource, typename Acquire, typename Release>
class basic_resource
    typedef Resource stored_type;
    typedef bool pointer_type;
    typedef bool& reference_type;

    typedef Acquire acquire;
    typedef Release release;

    explicit basic_resource(stored_type const& r)
    : resource_(r)

    void swap(basic_resource& rhs)
    { resource_.swap(rhs.resource_); }

    friend inline pointer_type get_impl(basic_resource const& lock)
    { return true; }

    friend inline reference_type get_impl_ref(basic_resource const& lock)
    { return true; }

    void destroy()
    { release(resource_); }

    stored_type& resource_;

// Storage Policy for scoped auto lock
template <typename Mutex>
class lock_storage : public basic_resource<
    typedef basic_resource<
> base_type;

    // Are these necessary?
    typedef typename base_type::stored_type stored_type;
    typedef typename base_type::pointer_type pointer_type;
    typedef typename base_type::reference_type reference_type;

    explicit lock_storage(stored_type const& mutex)
    : base_type(mutex)
    { }

This code, along with the proposed move policy base class
should make it a snap to create new auto_resource types with
no_copy, move, or shared semantics, optimal space usage,
and whatever checking/conversion you like. Is this sufficiently


Boost list run by bdawes at, gregod at, cpdaniel at, john at