// Copyright (c) 2013 GitHub, Inc. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef ATOM_COMMON_SWAP_OR_ASSIGN_H_ #define ATOM_COMMON_SWAP_OR_ASSIGN_H_ #include "base/compiler_specific.h" namespace internal { #if defined(OS_WIN) template inline void SwapOrAssign(T& v1, const T& v2) { __if_exists(T::swap) { v1.swap(const_cast(v2)); } __if_not_exists(T::swap) { v1 = v2; } } template inline void SwapOrAssign(T*& v1, T* v2) { v1 = v2; } inline void SwapOrAssign(int& v1, int v2) { v1 = v2; } inline void SwapOrAssign(bool& v1, bool v2) { v1 = v2; } #else // defined(OS_WIN) // Helper to detect whether value has specified method. template class HasSwapMethod { typedef char one; typedef long two; template static one test(char[sizeof(&C::swap)]) ; template static two test(...); public: enum { value = sizeof(test(0)) == sizeof(char) }; }; template struct enable_if {}; template struct enable_if { typedef T type; }; template inline typename enable_if::value>::type SwapOrAssign( T& v1, const T& v2) { v1.swap(const_cast(v2)); } template inline typename enable_if::value>::type SwapOrAssign( T& v1, const T& v2) { v1 = v2; } #endif // !defined(OS_WIN) } // namespace internal #endif // ATOM_COMMON_SWAP_OR_ASSIGN_H_