/*** 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 struct AuHasSwap { template static constexpr AuTrueType Test(decltype(static_cast(&C::Swap))); template static constexpr AuFalseType Test(...); using type = decltype(Test(0)); }; template struct AuHasswap { template static constexpr AuTrueType Test(decltype(static_cast(&C::swap))); template static constexpr AuFalseType Test(...); using type = decltype(Test(0)); }; template constexpr inline bool AuAuHasSwap_v = AuHasSwap::type::value; template constexpr inline bool AuAuHasswap_v = AuHasswap::type::value; } template inline T AuExchange(T &obj, U &&newValue) { T oldValue = AuMove(obj); obj = AuForward(newValue); return oldValue; } #if !defined(AURORA_RUNTIME_SWAP) && !defined(AURORA_SWAP_COCKBLOCK) #define AURORA_RUNTIME_SWAP std::swap #endif template inline void AuSwap(T &a, T &b) { if constexpr (__audetail::AuAuHasSwap_v) { a.Swap(b); return; } if constexpr (__audetail::AuAuHasswap_v) { a.swap(b); return; } #if defined(AURORA_RUNTIME_SWAP) AURORA_RUNTIME_SWAP(a, b); return; #else static_assert(false, "cannot swap T"); #endif }