Boost logo

Boost :

From: David B. Held (dheld_at_[hidden])
Date: 2002-05-17 11:00:49


"William E. Kempf" <williamkempf_at_[hidden]> wrote in message
news:OE481UbDBRArVc4Iu1B000008f4_at_hotmail.com...
> [...]
> There are similarities between the ScopedLocking pattern and smart
> pointers, but there are enough differences to make a lot of the concepts
> that are valid for smart pointers to be invalid (or at least dangerous)
> for "smart locks".

Could you please provide some more details? I'm thinking of a set of
policies like this:

// Storage Policy
template <typename Mutex>
class lock_storage
{
protected:
    typedef Mutex& stored_type;
    typedef bool pointer_type;
    typedef bool& reference_type;

    explicit lock_storage(stored_type const& m, bool initially_locked =
true)
    : mutex_(m), locked_(false)
    {
        if (initially_locked) lock();
    }

    void swap(lock_storage& rhs)
    { mutex_.swap(rhs.mutex_); }

    bool is_valid() const
    { return locked(); }

public:
    friend inline pointer_type get_impl(lock_storage const& lock)
    { return lock.locked_; }

    friend inline reference_type get_impl_ref(lock_storage const& lock)
    { return lock.locked_; }

    bool locked() const { return locked_; }

protected:
    void destroy()
    { if (locked_) unlock(); }

    void lock()
    {
        if (locked_) throw lock_error();
        lock_ops<Mutex>::lock(mutex_);
        locked_ = true;
    }

    void unlock()
    {
        if (!locked_) throw lock_error();
        lock_ops<Mutex>::unlock(mutex_);
        locked_ = false;
    }

private:
    stored_type& mutex_;
    bool locked_;
};

// Ownership Policy
template <typename Mutex>
class lock_move
{
protected:
    lock_move()
    { }

    static bool clone(bool& locked)
    {
        locked = false;
        return true;
    }

    static bool release(Mutex const&)
    { return true; }

    void swap(lock_move&)
    { }

    enum { destructive_copy_tag = true };
};

You would then use the disallow_conversion and no_check policies
for the remaining policies.

smart_ptr<M, lock_move, no_check, disallow_conversion, lock_storage>

should then give you a ScopedLock with move semantics. Comments?

Dave


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk