optional.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. // See http://www.boost.org/libs/iostreams for documentation.
  6. // Recent changes to Boost.Optional involving assigment broke Boost.Iostreams,
  7. // in a way which could be remedied only by relying on the deprecated reset
  8. // functions; with VC6, even reset didn't work. Until this problem is
  9. // understood, Iostreams will use a private version of optional with a smart
  10. // pointer interface.
  11. #ifndef BOOST_IOSTREAMS_DETAIL_OPTIONAL_HPP_INCLUDED
  12. #define BOOST_IOSTREAMS_DETAIL_OPTIONAL_HPP_INCLUDED
  13. #if defined(_MSC_VER)
  14. # pragma once
  15. #endif
  16. #include <boost/assert.hpp>
  17. #include <boost/mpl/int.hpp>
  18. #include <boost/type_traits/aligned_storage.hpp>
  19. #include <boost/type_traits/alignment_of.hpp>
  20. namespace boost { namespace iostreams { namespace detail {
  21. // Taken from <boost/optional.hpp>.
  22. template<class T>
  23. class aligned_storage
  24. {
  25. // Borland ICEs if unnamed unions are used for this!
  26. union dummy_u
  27. {
  28. char data[ sizeof(T) ];
  29. BOOST_DEDUCED_TYPENAME type_with_alignment<
  30. ::boost::alignment_of<T>::value >::type aligner_;
  31. } dummy_ ;
  32. public:
  33. void const* address() const { return &dummy_.data[0]; }
  34. void * address() { return &dummy_.data[0]; }
  35. };
  36. template<typename T>
  37. class optional {
  38. public:
  39. typedef T element_type;
  40. optional() : initialized_(false) { }
  41. optional(const T& t) : initialized_(false) { reset(t); }
  42. ~optional() { reset(); }
  43. T& operator*()
  44. {
  45. BOOST_ASSERT(initialized_);
  46. return *static_cast<T*>(address());
  47. }
  48. const T& operator*() const
  49. {
  50. BOOST_ASSERT(initialized_);
  51. return *static_cast<const T*>(address());
  52. }
  53. T* operator->()
  54. {
  55. BOOST_ASSERT(initialized_);
  56. return static_cast<T*>(address());
  57. }
  58. const T* operator->() const
  59. {
  60. BOOST_ASSERT(initialized_);
  61. return static_cast<const T*>(address());
  62. }
  63. T* get()
  64. {
  65. BOOST_ASSERT(initialized_);
  66. return static_cast<T*>(address());
  67. }
  68. const T* get() const
  69. {
  70. BOOST_ASSERT(initialized_);
  71. return static_cast<const T*>(address());
  72. }
  73. void reset()
  74. {
  75. if (initialized_) {
  76. #if BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x564)) || \
  77. BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(600)) \
  78. /**/
  79. T* t = static_cast<T*>(address());
  80. t->~T();
  81. #else
  82. static_cast<T*>(address())->T::~T();
  83. #endif
  84. initialized_ = false;
  85. }
  86. }
  87. void reset(const T& t)
  88. {
  89. reset();
  90. new (address()) T(t);
  91. initialized_ = true;
  92. }
  93. private:
  94. optional(const optional&);
  95. optional& operator=(const optional&);
  96. void* address() { return &storage_; }
  97. const void* address() const { return &storage_; }
  98. aligned_storage<T> storage_;
  99. bool initialized_;
  100. };
  101. } } } // End namespaces detail, iostreams, boost.
  102. #endif // #ifndef BOOST_IOSTREAMS_DETAIL_OPTIONAL_HPP_INCLUDED