AuROXTL/Include/auROXTL/auSwapExchangeUtils.hpp

67 lines
1.6 KiB
C++
Raw Normal View History

2022-04-01 04:06:53 +00:00
/***
Copyright (C) 2022 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: auSwapExchangeUtils.hpp
Date: 2022-3-27
Author: Reece
***/
#pragma once
namespace __audetail
{
template <class T>
struct AuHasSwap
{
2023-04-17 03:30:17 +00:00
template <class C> static constexpr AuTrueType Test(decltype(static_cast<void(C:: *)(C &)>(&C::Swap)));
template <class C> static constexpr AuFalseType Test(...);
using type = decltype(Test<T>(0));
};
template <class T>
struct AuHasswap
{
2023-04-17 03:30:17 +00:00
template <class C> static constexpr AuTrueType Test(decltype(static_cast<void(C:: *)(C &)>(&C::swap)));
template <class C> static constexpr AuFalseType Test(...);
using type = decltype(Test<T>(0));
};
template <class T>
constexpr inline bool AuAuHasSwap_v = AuHasSwap<T>::type::value;
template <class T>
constexpr inline bool AuAuHasswap_v = AuHasswap<T>::type::value;
}
2022-04-01 04:06:53 +00:00
template <class T, class U = T>
inline T AuExchange(T &obj, U &&newValue)
{
T oldValue = AuMove(obj);
obj = AuForward<U>(newValue);
return oldValue;
}
#if !defined(AURORA_RUNTIME_SWAP) && !defined(AURORA_SWAP_COCKBLOCK)
2022-04-01 04:06:53 +00:00
#define AURORA_RUNTIME_SWAP std::swap
#endif
2022-04-01 04:06:53 +00:00
template <class T>
inline void AuSwap(T &a, T &b)
{
if constexpr (__audetail::AuAuHasSwap_v<T>)
{
a.Swap(b);
return;
}
if constexpr (__audetail::AuAuHasswap_v<T>)
{
a.swap(b);
return;
}
#if defined(AURORA_RUNTIME_SWAP)
2022-04-01 04:06:53 +00:00
AURORA_RUNTIME_SWAP(a, b);
return;
#else
static_assert(false, "cannot swap T");
#endif
2022-04-01 04:06:53 +00:00
}