Sprout/sprout/utility/swap.hpp

60 lines
1.8 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2015-01-10 10:13:57 +00:00
Copyright (c) 2011-2015 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
https://github.com/bolero-MURAKAMI/Sprout
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
2012-10-05 15:58:56 +00:00
#ifndef SPROUT_UTILITY_SWAP_HPP
#define SPROUT_UTILITY_SWAP_HPP
#include <utility>
2013-12-02 15:57:57 +00:00
#include <type_traits>
2012-10-05 15:58:56 +00:00
#include <sprout/config.hpp>
#include <sprout/workaround/std/cstddef.hpp>
2013-12-02 15:57:57 +00:00
#include <sprout/utility/move.hpp>
#include <sprout/type_traits/enabler_if.hpp>
2012-10-05 15:58:56 +00:00
namespace sprout_swap_detail {
using std::swap;
2013-12-02 15:57:57 +00:00
template<typename T, typename sprout::enabler_if<std::is_scalar<T>::value>::type = sprout::enabler>
inline SPROUT_CXX14_CONSTEXPR void
swap_impl(T& a, T& b) SPROUT_NOEXCEPT {
T temp = sprout::move(a);
a = sprout::move(b);
b = sprout::move(temp);
}
template<typename T, typename sprout::enabler_if<!std::is_scalar<T>::value>::type = sprout::enabler>
inline SPROUT_CXX14_CONSTEXPR void
2012-10-05 15:58:56 +00:00
swap_impl(T& a, T& b)
2014-08-14 13:55:49 +00:00
SPROUT_NOEXCEPT_IF_EXPR(swap(a, b))
2012-10-05 15:58:56 +00:00
{
swap(a, b);
}
template<typename T, std::size_t N>
inline SPROUT_CXX14_CONSTEXPR void
swap_impl(T (& a)[N], T (& b)[N])
2014-08-14 13:55:49 +00:00
SPROUT_NOEXCEPT_IF_EXPR(sprout_swap_detail::swap_impl(*a, *b))
{
for (std::size_t i = 0; i < N; ++i) {
2013-12-02 15:57:57 +00:00
sprout_swap_detail::swap_impl(a[i], b[i]);
}
2012-10-05 15:58:56 +00:00
}
} // namespace sprout_swap_detail
namespace sprout {
//
// 20.2.2 swap
2012-10-05 15:58:56 +00:00
//
template<typename T1, typename T2>
inline SPROUT_CXX14_CONSTEXPR void
2012-10-05 15:58:56 +00:00
swap(T1& lhs, T2& rhs)
2014-08-14 13:55:49 +00:00
SPROUT_NOEXCEPT_IF_EXPR(sprout_swap_detail::swap_impl(lhs, rhs))
2012-10-05 15:58:56 +00:00
{
return sprout_swap_detail::swap_impl(lhs, rhs);
}
} // namespace sprout
#endif // #ifndef SPROUT_UTILITY_SWAP_HPP