Boost logo

Boost :

From: Eric Friedman (ebf_at_[hidden])
Date: 2002-08-06 23:01:22


I'm not sure if the intent of your proposal is to "Keep It Simple,
Stupid", but I have developed far more full-featured aligned-storage and
incomplete-type facilities, which may be of interest to the Boost
community.

They are available in the sandbox:

  boost/aligned_storage.hpp
  boost/incomplete.hpp
  boost/incomplete_fwd.hpp
  boost/utility/safe_swap.hpp
  boost/utility/safe_assign.hpp

Note, however, the semantics for my boost::aligned_storage varies some
from yours (to allow greater genericity of usage):

  boost::aligned_storage<size_t Size, size_t Alignment = -1>

...where an omitted Alignment (i.e. an Alignment of '-1') provides for
maximally-aligned storage, leveraging boost::detail::max_align (which I
think BTW should be promoted out of the detail namespace).

Basic usage of my aligned_storage (to mirror the example you provide in
your header file) is as follows:

  void foo()
  {
     aligned_storage< sizeof(MyClass), alignment_of<MyClass>::value >
buffer;
     MyClass Value;
     buffer.construct_as<MyClass>(Value);
     buffer.get_as<MyClass>().foo();
     buffer.destroy_as<MyClass>();
  }

Additional features of my boost::aligned_storage include:

 * aligned_storage::construct_as<T> STATIC_ASSERTs that both size and
alignment of T "fits" the storage
 * aligned_storage::swap_as<T> and aligned_storage::assign_as<T> provide
strong exception-safety guarantee
 * convienent memcpy syntax (via aligned_storage::memcpy_in and
aligned_storage::memcpy_out)
 * support for incomplete types

I find the last feature most exciting, as it allows for an efficient,
stack-based implementation of Herb Sutter's Pimpl idiom that satisfies
the strong exception-safety guarantee:

  // BEGIN CODE SAMPLE

  // x.h
  #include "boost/incomplete.hpp"
  
  class X {
     struct Impl;

     // Efficient, stack-allocated pimpl is easy to use...
     static const size_t sizeofimpl = /* some value >= sizeof(Impl) */;
     boost::incomplete<Impl, sizeofimpl> pimpl_;

     // ...but we could use the conventional heap-allocated pimpl like
this:
     // boost::incomplete<Impl> pimpl_;

  public:
     X();
     X(const X&);
     ~X();
     X& operator=(const X&);
     void swap(X&);
  };

  // x.cpp
  #include "x.h"

  struct X::Impl {
     // implementation data
  };

  X::X() : pimpl_() { }
  X::X(const X& other) : pimpl_(other.pimpl_) { }
  X::~X() { }
  X& X::operator=(const X& rhs) { pimpl_ = rhs.pimpl_; }
  void swap(X& other) { pimpl_.swap(other.pimpl_); }

  // END CODE SAMPLE

Note that the above x.cpp would work, unchanged, if heap-allocation were
preferred over stack-allocation (e.g. if the user did not want to
maintain x.h by incrementing X::sizeofimpl when necessary).

Let me know what you think.

Thanks,
Eric Friedman

-----Original Message-----
From: Fernando Cacciola [mailto:fcacciola_at_[hidden]]
Sent: Tuesday, August 06, 2002 4:04 PM
To: boost developers
Subject: [boost] Mini-review request: aligned_storage.hpp

Hi everyone,

This aligned storage stuff has been discussed on the list too many
times.

Douglas Gregor finally added "type_with_alignment.hpp" to type traits,
and suggested aligned_storage to go on 'utility'.

AFAIK, it never made it to CVS though, so I'm requesting here a formal
review of it so that it can be finally added.

(I'll attach the very small header file in the next message)

This implementation just wraps type_with_alignment, which is already
distributed with 1.28.0, so I figure that the core technique does work
already.

I haven't wrote documentation for it **yet**. It's so small that I
wanted to see first were exactly will it fit into (utility?).

Thanks,

Fernando Cacciola
Sierra s.r.l.
fcacciola_at_[hidden]
www.gosierra.com


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