123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- #ifndef BOOST_JSON_PILFER_HPP
- #define BOOST_JSON_PILFER_HPP
- #include <boost/json/detail/config.hpp>
- #include <type_traits>
- #include <utility>
- namespace boost {
- namespace json {
- template<class T>
- class pilfered
- {
- T& t_;
- public:
-
- explicit
- constexpr
- pilfered(T&& t) noexcept
- : t_(t)
- {
- }
-
- constexpr T&
- get() const noexcept
- {
- return t_;
- }
-
- constexpr T*
- operator->() const noexcept
- {
-
- return reinterpret_cast<T*>(
- const_cast<char *>(
- &reinterpret_cast<
- const volatile char &>(t_)));
- }
- };
- #ifndef BOOST_JSON_DOCS
- namespace detail_pilfer {
- template<class>
- struct not_pilfered
- {
- };
- }
- #endif
- template<class T>
- struct is_pilfer_constructible
- #ifndef BOOST_JSON_DOCS
- : std::integral_constant<bool,
- std::is_nothrow_move_constructible<T>::value ||
- (
- std::is_nothrow_constructible<
- T, pilfered<T> >::value &&
- ! std::is_nothrow_constructible<
- T, detail_pilfer::not_pilfered<T> >::value
- )>
- #endif
- {
- };
- template<class T>
- auto
- pilfer(T&& t) noexcept ->
- typename std::conditional<
- std::is_nothrow_constructible<
- typename std::remove_reference<T>::type,
- pilfered<typename
- std::remove_reference<T>::type> >::value &&
- ! std::is_nothrow_constructible<
- typename std::remove_reference<T>::type,
- detail_pilfer::not_pilfered<typename
- std::remove_reference<T>::type> >::value,
- pilfered<typename std::remove_reference<T>::type>,
- typename std::remove_reference<T>::type&&
- >::type
- {
- using U =
- typename std::remove_reference<T>::type;
- static_assert(
- is_pilfer_constructible<U>::value, "");
- return typename std::conditional<
- std::is_nothrow_constructible<
- U, pilfered<U> >::value &&
- ! std::is_nothrow_constructible<
- U, detail_pilfer::not_pilfered<U> >::value,
- pilfered<U>, U&&
- >::type(std::move(t));
- }
- }
- }
- #endif
|