Sprout/sprout/utility/swap.hpp

60 lines
1.9 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2014-01-08 07:48:12 +00:00
Copyright (c) 2011-2014 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)
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(swap(a, b)))
{
swap(a, b);
}
template<typename T, std::size_t N>
inline SPROUT_CXX14_CONSTEXPR void
swap_impl(T (& a)[N], T (& b)[N])
2013-12-02 15:57:57 +00:00
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_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)
SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(sprout_swap_detail::swap_impl(lhs, rhs)))
{
return sprout_swap_detail::swap_impl(lhs, rhs);
}
} // namespace sprout
#endif // #ifndef SPROUT_UTILITY_SWAP_HPP