Boost logo

Boost :

Subject: Re: [boost] [ptr_container] ptr_vector across DLL boundaries
From: Thorsten Ottosen (thorsten.ottosen_at_[hidden])
Date: 2009-06-25 14:05:22


Robert Dailey skrev:
> On Thu, Jun 25, 2009 at 9:41 AM, Thorsten Ottosen <
> thorsten.ottosen_at_[hidden]> wrote:
>>
>> Do you think a stateful custom clone_allocator would allow you to do what
>> you want?
>
>
> Could you elaborate a bit on what you mean? What state would you give to
> clone_allocator?

That is up to you. The important fact is that allocator is stored as an
object, and/or that you can access and modify that object to your wishes.

> Keep in mind that clone_allocator needs to, at some point,
> be implemented IN the DLL itself, otherwise the correct 'delete' will not be
> called. Some sort of polymorphism would solve this, that's the most obvious
> solution I can think of. Either that, or instead of a functor, take a
> boost::function, and that way I can pass you a function pointer to call for
> clone allocation/deallocation. There's several easy ways of solving this
> particular issue that I can see.
>
> But I would be interested in hearing more about the idea you have. I'm not
> sure I fully understand just yet.

Let me give you an example of what I was trying to do. To avoid storing
duplicates of NullObjects, I want to let my clone_allocator

1. don't delete a null object

2. clone a null object by returning just a pointer (i.e. don't allocate
anything)

This is how I could do that:

template<class NullObject>
class null_object_clone_allocator
{
public:
      typedef NullObject null_object_type;

private:
      const NullObject* null_object_;

public:
      explicit null_object_clone_allocator( const NullObject& x )
       : null_object(&r)
      { }

      template< class U >
      U* allocate_clone( const U& r ) const
      {
          if( &r == null_object_ )
            return null_object_;
          return new_clone( r );
      }

      template< class U >
      void deallocate_clone( const U* r ) const
      {
          if( r != null_object_ )
             delete_clone( r );
      }
};

boost::ptr_vector<base,null_object_clone_allocator<null_base>> vec(
my_alloc) ;

-Thorsten


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