os/ossrv/ossrv_pub/boost_apis/boost/iostreams/detail/optional.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
// (C) Copyright Jonathan Turkanis 2005.
sl@0
     2
// Distributed under the Boost Software License, Version 1.0. (See accompanying
sl@0
     3
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
sl@0
     4
sl@0
     5
// See http://www.boost.org/libs/iostreams for documentation.
sl@0
     6
sl@0
     7
// Recent changes to Boost.Optional involving assigment broke Boost.Iostreams,
sl@0
     8
// in a way which could be remedied only by relying on the deprecated reset
sl@0
     9
// functions; with VC6, even reset didn't work. Until this problem is 
sl@0
    10
// understood, Iostreams will use a private version of optional with a smart 
sl@0
    11
// pointer interface.
sl@0
    12
sl@0
    13
#ifndef BOOST_IOSTREAMS_DETAIL_OPTIONAL_HPP_INCLUDED
sl@0
    14
#define BOOST_IOSTREAMS_DETAIL_OPTIONAL_HPP_INCLUDED
sl@0
    15
sl@0
    16
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
sl@0
    17
# pragma once
sl@0
    18
#endif
sl@0
    19
sl@0
    20
#include <cassert>
sl@0
    21
#include <boost/mpl/int.hpp>
sl@0
    22
#include <boost/type_traits/aligned_storage.hpp>
sl@0
    23
#include <boost/type_traits/alignment_of.hpp>
sl@0
    24
sl@0
    25
namespace boost { namespace iostreams { namespace detail {
sl@0
    26
sl@0
    27
// Taken from <boost/optional.hpp>.
sl@0
    28
template<class T>
sl@0
    29
class aligned_storage
sl@0
    30
{
sl@0
    31
    // Borland ICEs if unnamed unions are used for this!
sl@0
    32
    union dummy_u
sl@0
    33
    {
sl@0
    34
        char data[ sizeof(T) ];
sl@0
    35
        BOOST_DEDUCED_TYPENAME type_with_alignment<
sl@0
    36
          ::boost::alignment_of<T>::value >::type aligner_;
sl@0
    37
    } dummy_ ;
sl@0
    38
sl@0
    39
  public:
sl@0
    40
sl@0
    41
    void const* address() const { return &dummy_.data[0]; }
sl@0
    42
    void      * address()       { return &dummy_.data[0]; }
sl@0
    43
};
sl@0
    44
sl@0
    45
template<typename T>
sl@0
    46
class optional {
sl@0
    47
public:
sl@0
    48
    typedef T element_type;
sl@0
    49
    optional() : initialized_(false) { }
sl@0
    50
    optional(const T& t) : initialized_(false) { reset(t); }
sl@0
    51
    ~optional() { reset(); }
sl@0
    52
    T& operator*() 
sl@0
    53
    { 
sl@0
    54
        assert(initialized_);
sl@0
    55
        return *static_cast<T*>(address()); 
sl@0
    56
    }
sl@0
    57
    const T& operator*() const
sl@0
    58
    { 
sl@0
    59
        assert(initialized_);
sl@0
    60
        return *static_cast<const T*>(address()); 
sl@0
    61
    }
sl@0
    62
    T* operator->() 
sl@0
    63
    { 
sl@0
    64
        assert(initialized_);
sl@0
    65
        return static_cast<T*>(address()); 
sl@0
    66
    }
sl@0
    67
    const T* operator->() const
sl@0
    68
    { 
sl@0
    69
        assert(initialized_);
sl@0
    70
        return static_cast<const T*>(address()); 
sl@0
    71
    }
sl@0
    72
    T* get() 
sl@0
    73
    { 
sl@0
    74
        assert(initialized_);
sl@0
    75
        return static_cast<T*>(address()); 
sl@0
    76
    }
sl@0
    77
    const T* get() const
sl@0
    78
    { 
sl@0
    79
        assert(initialized_);
sl@0
    80
        return static_cast<const T*>(address()); 
sl@0
    81
    }
sl@0
    82
    void reset() 
sl@0
    83
    {
sl@0
    84
        if (initialized_) { 
sl@0
    85
        #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) || \
sl@0
    86
            BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600)) \
sl@0
    87
            /**/
sl@0
    88
            T* t = static_cast<T*>(address());
sl@0
    89
            t->~T();
sl@0
    90
        #else
sl@0
    91
            static_cast<T*>(address())->T::~T();
sl@0
    92
        #endif
sl@0
    93
            initialized_ = false;
sl@0
    94
        }
sl@0
    95
    }
sl@0
    96
    void reset(const T& t) 
sl@0
    97
    {
sl@0
    98
        reset();
sl@0
    99
        new (address()) T(t); 
sl@0
   100
        initialized_ = true;
sl@0
   101
    }
sl@0
   102
private:
sl@0
   103
    optional(const optional&);
sl@0
   104
    optional& operator=(const optional&);
sl@0
   105
    void* address() { return &storage_; }
sl@0
   106
    const void* address() const { return &storage_; }
sl@0
   107
    aligned_storage<T>  storage_;
sl@0
   108
    bool                initialized_;
sl@0
   109
};
sl@0
   110
sl@0
   111
} } } // End namespaces detail, iostreams, boost.
sl@0
   112
sl@0
   113
#endif // #ifndef BOOST_IOSTREAMS_DETAIL_OPTIONAL_HPP_INCLUDED