optional.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #ifndef BOOST_LEAF_DETAIL_OPTIONAL_HPP_INCLUDED
  2. #define BOOST_LEAF_DETAIL_OPTIONAL_HPP_INCLUDED
  3. // Copyright 2018-2023 Emil Dotchevski and Reverge Studios, Inc.
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/leaf/config.hpp>
  7. #include <utility>
  8. #include <new>
  9. namespace boost { namespace leaf {
  10. namespace leaf_detail
  11. {
  12. template <class T>
  13. class optional
  14. {
  15. int key_;
  16. union { T value_; };
  17. public:
  18. typedef T value_type;
  19. BOOST_LEAF_CONSTEXPR optional() noexcept:
  20. key_(0)
  21. {
  22. }
  23. BOOST_LEAF_CONSTEXPR optional( optional const & x ):
  24. key_(x.key_)
  25. {
  26. if( x.key_ )
  27. (void) new (&value_) T( x.value_ );
  28. }
  29. BOOST_LEAF_CONSTEXPR optional( optional && x ) noexcept:
  30. key_(x.key_)
  31. {
  32. if( x.key_ )
  33. {
  34. (void) new (&value_) T( std::move(x.value_) );
  35. x.reset();
  36. }
  37. }
  38. BOOST_LEAF_CONSTEXPR optional( int key, T const & v ):
  39. key_(key),
  40. value_(v)
  41. {
  42. BOOST_LEAF_ASSERT(!empty());
  43. }
  44. BOOST_LEAF_CONSTEXPR optional( int key, T && v ) noexcept:
  45. key_(key),
  46. value_(std::move(v))
  47. {
  48. BOOST_LEAF_ASSERT(!empty());
  49. }
  50. BOOST_LEAF_CONSTEXPR optional & operator=( optional const & x )
  51. {
  52. reset();
  53. if( int key = x.key() )
  54. {
  55. load(key, x.value_);
  56. key_ = key;
  57. }
  58. return *this;
  59. }
  60. BOOST_LEAF_CONSTEXPR optional & operator=( optional && x ) noexcept
  61. {
  62. reset();
  63. if( int key = x.key() )
  64. {
  65. load(key, std::move(x.value_));
  66. x.reset();
  67. }
  68. return *this;
  69. }
  70. ~optional() noexcept
  71. {
  72. reset();
  73. }
  74. BOOST_LEAF_CONSTEXPR bool empty() const noexcept
  75. {
  76. return key_==0;
  77. }
  78. BOOST_LEAF_CONSTEXPR int key() const noexcept
  79. {
  80. return key_;
  81. }
  82. BOOST_LEAF_CONSTEXPR void reset() noexcept
  83. {
  84. if( key_ )
  85. {
  86. value_.~T();
  87. key_=0;
  88. }
  89. }
  90. BOOST_LEAF_CONSTEXPR T & load( int key )
  91. {
  92. BOOST_LEAF_ASSERT(key);
  93. reset();
  94. (void) new(&value_) T;
  95. key_=key;
  96. return value_;
  97. }
  98. BOOST_LEAF_CONSTEXPR T & load( int key, T const & v )
  99. {
  100. BOOST_LEAF_ASSERT(key);
  101. reset();
  102. (void) new(&value_) T(v);
  103. key_=key;
  104. return value_;
  105. }
  106. BOOST_LEAF_CONSTEXPR T & load( int key, T && v ) noexcept
  107. {
  108. BOOST_LEAF_ASSERT(key);
  109. reset();
  110. (void) new(&value_) T(std::move(v));
  111. key_=key;
  112. return value_;
  113. }
  114. BOOST_LEAF_CONSTEXPR T const * has_value(int key) const noexcept
  115. {
  116. BOOST_LEAF_ASSERT(key);
  117. return key_==key ? &value_ : nullptr;
  118. }
  119. BOOST_LEAF_CONSTEXPR T * has_value(int key) noexcept
  120. {
  121. BOOST_LEAF_ASSERT(key);
  122. return key_==key ? &value_ : nullptr;
  123. }
  124. BOOST_LEAF_CONSTEXPR T const & value(int key) const & noexcept
  125. {
  126. BOOST_LEAF_ASSERT(has_value(key) != 0);
  127. (void) key;
  128. return value_;
  129. }
  130. BOOST_LEAF_CONSTEXPR T & value(int key) & noexcept
  131. {
  132. BOOST_LEAF_ASSERT(has_value(key) != 0);
  133. (void) key;
  134. return value_;
  135. }
  136. BOOST_LEAF_CONSTEXPR T const && value(int key) const && noexcept
  137. {
  138. BOOST_LEAF_ASSERT(has_value(key) != 0);
  139. (void) key;
  140. return value_;
  141. }
  142. BOOST_LEAF_CONSTEXPR T value(int key) && noexcept
  143. {
  144. BOOST_LEAF_ASSERT(has_value(key) != 0);
  145. (void) key;
  146. T tmp(std::move(value_));
  147. reset();
  148. return tmp;
  149. }
  150. };
  151. }
  152. } }
  153. #endif