Boost logo

Boost :

From: E. Gladyshev (egladysh_at_[hidden])
Date: 2003-08-26 19:59:56


--- Peter Dimov <pdimov_at_[hidden]> wrote:
> E. Gladyshev wrote:
> > --- Peter Dimov <pdimov_at_[hidden]> wrote:
> >
> >> unless there are very solid reasons for the allocator parameter. ;-)
> >
> > I agree, but the problme is that I don't know whether I have a solid
> reason or not.
> > I have to ship my class to someone else. She might have a solid reason, I
> don't know.
> > If I supply an allocator parameter, then I am covered and I can sleep
> well. :)
>
> Well... if you ask me, it is better to not offer the functionality until
> someone explicitly asks for it and gives a reasonable scenario as an
> example. Features are easy to add, hard to remove, and there is always the
> possibility that you "pre-included" the wrong feature.

Peter, using your example I put together an allocator class that can be used in shared_ptr and STL
at the same time. It is not too pretty but it works. If we could only customize the counter
allocations, that would be it.

namespace boostx
{
        template< typename A >
        class allocator : public A
        {
        public:
                typedef A::value_type type;
                allocator() {}

                type* create()
                {
                        type* p = allocate(1, 0);
                        try
                        {
                                p = new(p) type;
                        }
                        catch(...)
                        {
                                deallocate( p, 1 );
                                throw;
                        }
                        return p;
                }

                void operator()( type* p )
                {
                        try
                        {
                                p->~T();
                        }
                        catch(...)
                        {
                                deallocate(p, 1);
                                throw;
                        }
                        deallocate(p, 1);
                }
        };
};

template < class T, class A = boostx::allocator< std::allocator<T> > >
struct X
{
        boost::shared_ptr<T> _data;
        std::vector<T,A> _list;

        X( const A& al = A() ) : _list(al)
        {
                A tmp(al);
                _data = boost::shared_ptr<T>( tmp.create(), tmp );
        }
};

OR if you don't mind dropping the 'const' from the constructor

template < typename T, class A = boostx::allocator< std::allocator<T> > >
struct X
{
        boost::shared_ptr<T> _data;
        std::vector<T,A> _list;

        X( A& al = A() ) : _list(al), _data( al.create(), al )
        {
        }
};

Eugene

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com


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