Jeff Thompson | 86b6d64 | 2013-10-17 15:01:56 -0700 | [diff] [blame^] | 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.) |
| 5 | |
| 6 | // See http://www.boost.org/libs/iostreams for documentation. |
| 7 | |
| 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 |
| 12 | // pointer interface. |
| 13 | |
| 14 | #ifndef NDNBOOST_IOSTREAMS_DETAIL_OPTIONAL_HPP_INCLUDED |
| 15 | #define NDNBOOST_IOSTREAMS_DETAIL_OPTIONAL_HPP_INCLUDED |
| 16 | |
| 17 | #if defined(_MSC_VER) && (_MSC_VER >= 1020) |
| 18 | # pragma once |
| 19 | #endif |
| 20 | |
| 21 | #include <ndnboost/assert.hpp> |
| 22 | #include <ndnboost/mpl/int.hpp> |
| 23 | #include <ndnboost/type_traits/aligned_storage.hpp> |
| 24 | #include <ndnboost/type_traits/alignment_of.hpp> |
| 25 | |
| 26 | namespace ndnboost { namespace iostreams { namespace detail { |
| 27 | |
| 28 | // Taken from <ndnboost/optional.hpp>. |
| 29 | template<class T> |
| 30 | class aligned_storage |
| 31 | { |
| 32 | // Borland ICEs if unnamed unions are used for this! |
| 33 | union dummy_u |
| 34 | { |
| 35 | char data[ sizeof(T) ]; |
| 36 | NDNBOOST_DEDUCED_TYPENAME type_with_alignment< |
| 37 | ::ndnboost::alignment_of<T>::value >::type aligner_; |
| 38 | } dummy_ ; |
| 39 | |
| 40 | public: |
| 41 | |
| 42 | void const* address() const { return &dummy_.data[0]; } |
| 43 | void * address() { return &dummy_.data[0]; } |
| 44 | }; |
| 45 | |
| 46 | template<typename T> |
| 47 | class optional { |
| 48 | public: |
| 49 | typedef T element_type; |
| 50 | optional() : initialized_(false) { } |
| 51 | optional(const T& t) : initialized_(false) { reset(t); } |
| 52 | ~optional() { reset(); } |
| 53 | T& operator*() |
| 54 | { |
| 55 | NDNBOOST_ASSERT(initialized_); |
| 56 | return *static_cast<T*>(address()); |
| 57 | } |
| 58 | const T& operator*() const |
| 59 | { |
| 60 | NDNBOOST_ASSERT(initialized_); |
| 61 | return *static_cast<const T*>(address()); |
| 62 | } |
| 63 | T* operator->() |
| 64 | { |
| 65 | NDNBOOST_ASSERT(initialized_); |
| 66 | return static_cast<T*>(address()); |
| 67 | } |
| 68 | const T* operator->() const |
| 69 | { |
| 70 | NDNBOOST_ASSERT(initialized_); |
| 71 | return static_cast<const T*>(address()); |
| 72 | } |
| 73 | T* get() |
| 74 | { |
| 75 | NDNBOOST_ASSERT(initialized_); |
| 76 | return static_cast<T*>(address()); |
| 77 | } |
| 78 | const T* get() const |
| 79 | { |
| 80 | NDNBOOST_ASSERT(initialized_); |
| 81 | return static_cast<const T*>(address()); |
| 82 | } |
| 83 | void reset() |
| 84 | { |
| 85 | if (initialized_) { |
| 86 | #if NDNBOOST_WORKAROUND(__BORLANDC__, NDNBOOST_TESTED_AT(0x564)) || \ |
| 87 | NDNBOOST_WORKAROUND(__IBMCPP__, NDNBOOST_TESTED_AT(600)) \ |
| 88 | /**/ |
| 89 | T* t = static_cast<T*>(address()); |
| 90 | t->~T(); |
| 91 | #else |
| 92 | static_cast<T*>(address())->T::~T(); |
| 93 | #endif |
| 94 | initialized_ = false; |
| 95 | } |
| 96 | } |
| 97 | void reset(const T& t) |
| 98 | { |
| 99 | reset(); |
| 100 | new (address()) T(t); |
| 101 | initialized_ = true; |
| 102 | } |
| 103 | private: |
| 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_; |
| 109 | bool initialized_; |
| 110 | }; |
| 111 | |
| 112 | } } } // End namespaces detail, iostreams, boost. |
| 113 | |
| 114 | #endif // #ifndef NDNBOOST_IOSTREAMS_DETAIL_OPTIONAL_HPP_INCLUDED |