1 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
2 // (C) Copyright 2005-2007 Jonathan Turkanis
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
6 // See http://www.boost.org/libs/iostreams for documentation.
8 // Recent changes to Boost.Optional involving assigment broke Boost.Iostreams,
9 // in a way which could be remedied only by relying on the deprecated reset
10 // functions; with VC6, even reset didn't work. Until this problem is
11 // understood, Iostreams will use a private version of optional with a smart
14 #ifndef BOOST_IOSTREAMS_DETAIL_OPTIONAL_HPP_INCLUDED
15 #define BOOST_IOSTREAMS_DETAIL_OPTIONAL_HPP_INCLUDED
21 #include <boost/assert.hpp>
22 #include <boost/mpl/int.hpp>
23 #include <boost/type_traits/aligned_storage.hpp>
24 #include <boost/type_traits/alignment_of.hpp>
26 namespace boost { namespace iostreams { namespace detail {
28 // Taken from <boost/optional.hpp>.
32 // Borland ICEs if unnamed unions are used for this!
35 char data[ sizeof(T) ];
36 BOOST_DEDUCED_TYPENAME type_with_alignment<
37 ::boost::alignment_of<T>::value >::type aligner_;
42 void const* address() const { return &dummy_.data[0]; }
43 void * address() { return &dummy_.data[0]; }
49 typedef T element_type;
50 optional() : initialized_(false) { }
51 optional(const T& t) : initialized_(false) { reset(t); }
52 ~optional() { reset(); }
55 BOOST_ASSERT(initialized_);
56 return *static_cast<T*>(address());
58 const T& operator*() const
60 BOOST_ASSERT(initialized_);
61 return *static_cast<const T*>(address());
65 BOOST_ASSERT(initialized_);
66 return static_cast<T*>(address());
68 const T* operator->() const
70 BOOST_ASSERT(initialized_);
71 return static_cast<const T*>(address());
75 BOOST_ASSERT(initialized_);
76 return static_cast<T*>(address());
80 BOOST_ASSERT(initialized_);
81 return static_cast<const T*>(address());
86 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) || \
87 BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600)) \
89 T* t = static_cast<T*>(address());
92 static_cast<T*>(address())->T::~T();
97 void reset(const T& t)
100 new (address()) T(t);
104 optional(const optional&);
105 optional& operator=(const optional&);
106 void* address() { return &storage_; }
107 const void* address() const { return &storage_; }
108 aligned_storage<T> storage_;
112 } } } // End namespaces detail, iostreams, boost.
114 #endif // #ifndef BOOST_IOSTREAMS_DETAIL_OPTIONAL_HPP_INCLUDED