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
|
|
|
|
|
2022-11-17 02:48:58 +00:00
|
|
|
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)));
|
2022-11-17 02:48:58 +00:00
|
|
|
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)));
|
2022-11-17 02:48:58 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-11-17 02:48:58 +00:00
|
|
|
#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-11-17 02:48:58 +00:00
|
|
|
|
2022-04-01 04:06:53 +00:00
|
|
|
template <class T>
|
|
|
|
inline void AuSwap(T &a, T &b)
|
|
|
|
{
|
2022-11-17 02:48:58 +00:00
|
|
|
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);
|
2022-11-17 02:48:58 +00:00
|
|
|
return;
|
|
|
|
#else
|
|
|
|
static_assert(false, "cannot swap T");
|
|
|
|
#endif
|
2022-04-01 04:06:53 +00:00
|
|
|
}
|