ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> boost
boost
RE: [boost] Mini-review request: aligned_storage.hpp
by Eric Friedman other posts by this author
Aug 7 2002 4:01AM messages near this date
[boost] Mini-review request: aligned_storage.hpp | Re: [boost] Mini-review request: aligned_storage.hpp
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@[...].com] 
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@[...].com
www.gosierra.com



_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Thread:
Fernando Cacciola
Eric Friedman
Fernando Cacciola
Douglas Gregor
Fernando Cacciola
Douglas Gregor
Itay Maman
David Abrahams
Eric Friedman
Itay Maman
Fernando Cacciola
David Abrahams
Eric Friedman
Douglas Gregor

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved