Boost logo

Boost Users :

From: Michal Kandulski (Michal.Kandulski_at_[hidden])
Date: 2005-08-05 14:07:06


Hi!

I'd like to implement a pimpl, pointing to a stack "allocated" buffer
by means of palcement new/destroy.

There would be no problem but there are alignment issues.

What really I need is:
- a way to check by te means of Boost wheater that an object of class
   X can be placed (with placement new) in a buffer of given adress
   and size:
- a way of getting a pointer to X (within the buffer) where the
   object could be paced

// file.hpp:

struct X
{
   X();
   ~X();

private:

   struct Impl;
   const static int max_size = 100;
   Impl* m_pimpl;
   char m_impl_buffer[max_size];
   bool m_on_stack;
};

// file.cpp:

#include "file.hpp"

struct X::Impl
{
   // members
};

X::X()
{
   m_on_stack = value_of(max_size is big enough for an object of X);
   if (m_on_stack)
     m_pimpl = new (m_impl_buffer)Impl; // any alignment stuff here???
   else
     m_pimpl = new Impl;
}

X::~X()
{
   if (m_on_stack)
     m_pimpl->~Impl();
   else
     delete m_pimpl;
}

I'll be gratefull for any help,

Michal


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net