Boost logo

Boost :

From: Pablo Aguilar (pablo.aguilar_at_[hidden])
Date: 2005-08-11 23:53:57


Hi,

First of all, thanks to everyone who's commented on the proposed any
alternative.

Given that the alignment issue doesn't seem to be easy to solve, I'm
thinking of giving up on trying to align correctly, and just skip the
"in place storing of value" optimization when it might be an issue.

So I'm wondering, is the following a proper way to detect alignment
problems?

<code>

typedef void* any_buffer_base_type; // could be customized
int const ANY_BUFFER_SIZE = sizeof(any_buffer_base_type);

union object_holder
{
   char buffer[ANY_BUFFER_SIZE];
   void* pointer;

   any_buffer_base_type alignment_dummy_;
};
        
template<typename T>
struct optimize
{
   enum
   {
       T_size_ = sizeof(T)
     , base_size_ = sizeof(any_buffer_base_type)
     , size_ok_ = (T_size_ <= base_size_)

     , T_align_ = alignment_of<T>::value
     , base_align_= alignment_of<any_buffer_base_type>::value
     , align_ok_ = ((base_align_ % T_align_) == 0)

     , value = size_ok_ && align_ok_
   };
};

</code>

I looked at Optional's alignment tecnique, and couldn't come up with how
to use it, given that the stored type for any changes at runtime. If
there's something I missed about this, any help would be welcome.

My current solution is to use the above shown code (assuming it works,
of course!), and fall back on heap based storage when optimization is
deemed not possible.

The 2nd question I had to ask is, how should any's alignment type be
specified, with a configuration macro or through a template parameter?

I haven't posted an updated version, given that there's some issues
(other than alignment) still requiring attention.

Thanks in advance for any help you might provide,

Pablo Aguilar


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