unique_any.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // Copyright Antony Polukhin, 2020-2023.
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. // See http://www.boost.org/libs/any for Documentation.
  7. #ifndef BOOST_ANYS_UNIQUE_ANY_HPP_INCLUDED
  8. #define BOOST_ANYS_UNIQUE_ANY_HPP_INCLUDED
  9. #include <boost/config.hpp>
  10. #ifdef BOOST_HAS_PRAGMA_ONCE
  11. # pragma once
  12. #endif
  13. #ifdef BOOST_NO_CXX11_RVALUE_REFERENCES
  14. #error Header <boost/any/unique_any.hpp> requires C++11 compatible compiler with move semantics
  15. #endif
  16. #ifdef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
  17. #error Header <boost/any/unique_any.hpp> requires C++11 compatible compiler with defaulted functions
  18. #endif
  19. #ifdef BOOST_NO_CXX11_SMART_PTR
  20. #error Header <boost/any/unique_any.hpp> requires C++11 compatible standard library with std::unique_ptr
  21. #endif
  22. #include <memory>
  23. #ifdef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
  24. #include <initializer_list>
  25. #endif
  26. #include <utility>
  27. #include <boost/any/bad_any_cast.hpp>
  28. #include <boost/core/addressof.hpp>
  29. #include <boost/type_traits/decay.hpp>
  30. namespace boost { namespace anys {
  31. template <class T>
  32. struct in_place_type_t
  33. {
  34. };
  35. #if !defined(BOOST_NO_CXX14_VARIABLE_TEMPLATES)
  36. template <class T>
  37. constexpr in_place_type_t<T> in_place_type{};
  38. #endif
  39. class unique_any {
  40. public:
  41. BOOST_CONSTEXPR unique_any() BOOST_NOEXCEPT = default;
  42. unique_any(unique_any&& other) BOOST_NOEXCEPT = default;
  43. // Perfect forwarding of T
  44. template<typename T>
  45. unique_any(T&& value
  46. , typename boost::disable_if<boost::is_same<unique_any&, T> >::type* = 0 // disable if value has type `unique_any&`
  47. , typename boost::disable_if<boost::is_const<T> >::type* = 0) // disable if value has type `const T&&`
  48. : content(new holder< typename boost::decay<T>::type >(std::forward<T>(value)))
  49. {
  50. }
  51. template<class T, class... Args>
  52. explicit unique_any(in_place_type_t<T>, Args&&... args)
  53. : content(new holder<typename boost::decay<T>::type>(std::forward<Args>(args)...))
  54. {
  55. }
  56. #ifdef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
  57. template <class T, class U, class... Args>
  58. explicit unique_any(in_place_type_t<T>, std::initializer_list<U> il, Args&&... args)
  59. : content(new holder<typename boost::decay<T>::type>(il, std::forward<Args>(args)...))
  60. {
  61. }
  62. #endif
  63. ~unique_any() BOOST_NOEXCEPT = default;
  64. unique_any & operator=(unique_any&& rhs) BOOST_NOEXCEPT = default;
  65. template <class T>
  66. unique_any & operator=(T&& rhs)
  67. {
  68. unique_any(std::forward<T>(rhs)).swap(*this);
  69. return *this;
  70. }
  71. template<class T, class... Args>
  72. typename boost::decay<T>::type& emplace(Args&&... args) {
  73. content = std::unique_ptr<placeholder>(
  74. new holder<typename boost::decay<T>::type>(std::forward<Args>(args)...)
  75. );
  76. }
  77. #ifdef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
  78. template<class T, class U, class... Args>
  79. typename boost::decay<T>::type& emplace(initializer_list<U> il, Args&&... args) {
  80. content = std::unique_ptr<placeholder>(
  81. new holder<typename boost::decay<T>::type>(il, std::forward<Args>(args)...)
  82. );
  83. }
  84. #endif
  85. void reset() BOOST_NOEXCEPT
  86. {
  87. content.reset();
  88. }
  89. unique_any& swap(unique_any& rhs) BOOST_NOEXCEPT
  90. {
  91. content.swap(rhs.content);
  92. return *this;
  93. }
  94. bool has_value() const BOOST_NOEXCEPT
  95. {
  96. return !content;
  97. }
  98. const boost::typeindex::type_info& type() const BOOST_NOEXCEPT
  99. {
  100. return content ? content->type() : boost::typeindex::type_id<void>().type_info();
  101. }
  102. private: // types
  103. class BOOST_SYMBOL_VISIBLE placeholder
  104. {
  105. virtual ~placeholder()
  106. {
  107. }
  108. virtual const boost::typeindex::type_info& type() const BOOST_NOEXCEPT = 0;
  109. };
  110. template<typename T>
  111. class holder BOOST_FINAL: public placeholder
  112. {
  113. public: // structors
  114. template <class... Args>
  115. holder(Args&&... args)
  116. : held(std::forward<Args>(args)...)
  117. {
  118. }
  119. #ifdef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
  120. template <class U, class... Args>
  121. holder(std::initializer_list<U> il, Args&&... args)
  122. : held(il, std::forward<Args>(args)...)
  123. {
  124. }
  125. #endif
  126. const boost::typeindex::type_info& type() const BOOST_NOEXCEPT BOOST_OVERRIDE
  127. {
  128. return boost::typeindex::type_id<T>().type_info();
  129. }
  130. public: // representation
  131. T held;
  132. };
  133. private: // representation
  134. template<typename T>
  135. friend T * any_cast(unique_any *) BOOST_NOEXCEPT;
  136. template<typename T>
  137. friend T * unsafe_any_cast(unique_any *) BOOST_NOEXCEPT;
  138. std::unique_ptr<placeholder> content;
  139. };
  140. inline void swap(unique_any & lhs, unique_any & rhs) BOOST_NOEXCEPT
  141. {
  142. lhs.swap(rhs);
  143. }
  144. template<typename T>
  145. T * any_cast(unique_any * operand) BOOST_NOEXCEPT
  146. {
  147. return operand && operand->type() == boost::typeindex::type_id<T>()
  148. ? boost::addressof(
  149. static_cast<unique_any::holder<BOOST_DEDUCED_TYPENAME remove_cv<T>::type>&>(*operand->content).held
  150. )
  151. : 0;
  152. }
  153. template<typename T>
  154. inline const T * any_cast(const unique_any * operand) BOOST_NOEXCEPT
  155. {
  156. return any_cast<T>(const_cast<unique_any *>(operand));
  157. }
  158. template<typename T>
  159. T any_cast(unique_any & operand)
  160. {
  161. typedef BOOST_DEDUCED_TYPENAME remove_reference<T>::type nonref;
  162. nonref * result = any_cast<nonref>(boost::addressof(operand));
  163. if(!result)
  164. boost::throw_exception(bad_any_cast());
  165. // Attempt to avoid construction of a temporary object in cases when
  166. // `T` is not a reference. Example:
  167. // `static_cast<std::string>(*result);`
  168. // which is equal to `std::string(*result);`
  169. typedef BOOST_DEDUCED_TYPENAME boost::conditional<
  170. boost::is_reference<T>::value,
  171. T,
  172. BOOST_DEDUCED_TYPENAME boost::add_reference<T>::type
  173. >::type ref_type;
  174. #ifdef BOOST_MSVC
  175. # pragma warning(push)
  176. # pragma warning(disable: 4172) // "returning address of local variable or temporary" but *result is not local!
  177. #endif
  178. return static_cast<ref_type>(*result);
  179. #ifdef BOOST_MSVC
  180. # pragma warning(pop)
  181. #endif
  182. }
  183. template<typename T>
  184. inline T any_cast(const unique_any & operand)
  185. {
  186. typedef BOOST_DEDUCED_TYPENAME remove_reference<T>::type nonref;
  187. return any_cast<const nonref &>(const_cast<unique_any &>(operand));
  188. }
  189. #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
  190. template<typename T>
  191. inline T any_cast(unique_any&& operand)
  192. {
  193. BOOST_STATIC_ASSERT_MSG(
  194. boost::is_rvalue_reference<T&&>::value /*true if T is rvalue or just a value*/
  195. || boost::is_const< typename boost::remove_reference<T>::type >::value,
  196. "boost::any_cast shall not be used for getting nonconst references to temporary objects"
  197. );
  198. return any_cast<T>(operand);
  199. }
  200. #endif
  201. // Note: The "unsafe" versions of any_cast are not part of the
  202. // public interface and may be removed at any time. They are
  203. // required where we know what type is stored in the any and can't
  204. // use typeid() comparison, e.g., when our types may travel across
  205. // different shared libraries.
  206. template<typename T>
  207. inline T * unsafe_any_cast(unique_any * operand) BOOST_NOEXCEPT
  208. {
  209. return boost::addressof(
  210. static_cast<unique_any::holder<T>&>(*operand->content)->held
  211. );
  212. }
  213. template<typename T>
  214. inline const T * unsafe_any_cast(const unique_any * operand) BOOST_NOEXCEPT
  215. {
  216. return unsafe_any_cast<T>(const_cast<unique_any *>(operand));
  217. }
  218. } // namespace anys
  219. using boost::anys::any_cast;
  220. using boost::anys::unsafe_any_cast;
  221. } // namespace boost
  222. #endif // BOOST_ANYS_UNIQUE_ANY_HPP_INCLUDED