Design summary: smart pointers in allocators

Phil Bouchard philippe@fornux.com
Fri Sep 5 08:41:00 GMT 2008


1)
I think _Pointer_adapter<> would be simpler if it was defaulting to generic 
smart pointers and specialized on C pointers as such:

template <typename _Pointer>
    class _Pointer_adapter
    {
        ...
        static element_type* get(pointer const& __p) const { return 
__p.get(); }
        static element_type* release(pointer const& __p) const { return 
__p.release(); }
    };

template <typename _Type>
    class _Pointer_adapter<_Type *>
    {
        ...
        static element_type* get(pointer __p) const { return __p; }
        static element_type* release(pointer& __p) const { pointer __tmp = 
__p; __p = 0; return __tmp; }
    };

BTW the inline keyword is not necessary for member functions defined inside 
the class.


2)
Now if _Pointer_adapter<> was designed similarly to 1) then we could use it 
the following way:

template <typename _Tp, typename _Alloc>
    void _List_base<_Tp, _Alloc>::_M_clear()
    {
        typedef _List_node_base<_Alloc> _Node_base;
        typedef typename _Node_base::pointer _Pointer;
        typedef typename _Node_base::value_type _Value_type;

        _Pointer * __cur = & this->_M_impl._M_node->_M_next;

        while (_Pointer_adapter<_Pointer>::get(* __cur))
        {
            _Pointer * __next = & (*__cur)->_M_next;
            _Value_type * __tmp = _Pointer_adapter<_Pointer>::release(* 
__cur);

            if (__tmp)
            {
                _M_impl._Node_Alloc_type::destroy(__tmp);
                _M_impl._Node_Alloc_type::deallocate(__tmp);
                break; // can't go on
            }

            __cur = __next;
        }


-Phil





More information about the Libstdc++ mailing list