You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

202 lines
6.6 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/util/std_swap.hpp
  3. *
  4. * Purpose: Contains the std_swap() function.
  5. *
  6. * Created: 27th June 2005
  7. * Updated: 12th August 2010
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2005-2010, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file stlsoft/util/std_swap.hpp
  40. *
  41. * \brief [C++ only] Swap utility functions
  42. * (\ref group__library__utility "Utility" Library).
  43. */
  44. #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_STD_SWAP
  45. #define STLSOFT_INCL_STLSOFT_UTIL_HPP_STD_SWAP
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define STLSOFT_VER_STLSOFT_UTIL_HPP_STD_SWAP_MAJOR 1
  48. # define STLSOFT_VER_STLSOFT_UTIL_HPP_STD_SWAP_MINOR 2
  49. # define STLSOFT_VER_STLSOFT_UTIL_HPP_STD_SWAP_REVISION 2
  50. # define STLSOFT_VER_STLSOFT_UTIL_HPP_STD_SWAP_EDIT 19
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Auto-generation and compatibility
  54. */
  55. /*
  56. [<[STLSOFT-AUTO:NO-UNITTEST]>]
  57. */
  58. /* /////////////////////////////////////////////////////////////////////////
  59. * Includes
  60. */
  61. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  62. # include <stlsoft/stlsoft.h>
  63. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  64. #if !defined(STLSOFT_CF_std_NAMESPACE)
  65. # define STLSOFT_STD_SWAP_NO_USE_STD
  66. #endif /* !STLSOFT_CF_std_NAMESPACE */
  67. #if !defined(STLSOFT_STD_SWAP_NO_USE_STD)
  68. # include <algorithm>
  69. #endif /* STLSOFT_STD_SWAP_NO_USE_STD */
  70. /* /////////////////////////////////////////////////////////////////////////
  71. * Namespace
  72. */
  73. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  74. #ifndef _STLSOFT_NO_NAMESPACE
  75. namespace stlsoft_std_swap
  76. {
  77. #endif /* _STLSOFT_NO_NAMESPACE */
  78. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  79. /* /////////////////////////////////////////////////////////////////////////
  80. * Functions
  81. */
  82. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  83. // This is defined only when the std namespace is not.
  84. #ifdef STLSOFT_STD_SWAP_NO_USE_STD
  85. # ifndef _STLSOFT_NO_NAMESPACE
  86. namespace stlsoft_nostd_util
  87. {
  88. # endif /* _STLSOFT_NO_NAMESPACE */
  89. template<ss_typename_param_k T>
  90. inline void swap(T& lhs, T& rhs)
  91. {
  92. T tmp(lhs);
  93. lhs = rhs;
  94. rhs = tmp;
  95. }
  96. # ifndef _STLSOFT_NO_NAMESPACE
  97. } // namespace stlsoft_nostd_util
  98. # endif /* _STLSOFT_NO_NAMESPACE */
  99. #endif /* STLSOFT_STD_SWAP_NO_USE_STD */
  100. /** \brief Calls std::swap on the arguments, but may also use
  101. * Argument-Dependent Lookup) to use a specialised form.
  102. *
  103. * \ingroup group__library__utility
  104. *
  105. * \note For compilers, such as Open Watcom, that do not have a functional
  106. * standard library implementation, the swap() implementation from
  107. * stlsoft::stlsoft_nostd_util is used.
  108. *
  109. * \param lhs The left hand parameter
  110. * \param rhs The right hand parameter
  111. */
  112. template<ss_typename_param_k T>
  113. inline void std_swap(T& lhs, T& rhs)
  114. {
  115. // Here we introduce the 'std' implementation, via a 'using namespace std'
  116. // directive, so that Argument-Dependent Lookup can have the best grab
  117. #ifdef STLSOFT_STD_SWAP_NO_USE_STD
  118. # ifndef _STLSOFT_NO_NAMESPACE
  119. using namespace stlsoft_nostd_util;
  120. # endif /* !_STLSOFT_NO_NAMESPACE */
  121. #else /* ? STLSOFT_STD_SWAP_NO_USE_STD */
  122. # ifdef STLSOFT_CF_std_NAMESPACE
  123. # if ( ( defined(STLSOFT_COMPILER_IS_INTEL) && \
  124. defined(_MSC_VER)) || \
  125. defined(STLSOFT_COMPILER_IS_MSVC)) && \
  126. _MSC_VER < 1310
  127. using std::swap;
  128. # else /* ? compiler */
  129. using namespace std;
  130. # endif /* compiler */
  131. # endif /* STLSOFT_CF_std_NAMESPACE */
  132. #endif /* STLSOFT_STD_SWAP_NO_USE_STD */
  133. swap(lhs, rhs);
  134. }
  135. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  136. /* ////////////////////////////////////////////////////////////////////// */
  137. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  138. #ifndef _STLSOFT_NO_NAMESPACE
  139. } // namespace stlsoft_std_swap
  140. #endif /* _STLSOFT_NO_NAMESPACE */
  141. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  142. #ifndef _STLSOFT_NO_NAMESPACE
  143. namespace stlsoft
  144. {
  145. /// Calls std::swap on the arguments, but may also use Argument-Dependent
  146. /// Lookup to use a specialised form
  147. ///
  148. /// \note For compilers, such as Open Watcom, that do not have a functional
  149. /// standard library implementation, the swap() implementation from
  150. /// stlsoft::stlsoft_nostd_util is used.
  151. ///
  152. /// \param lhs The left hand parameter
  153. /// \param rhs The right hand parameter
  154. template<ss_typename_param_k T>
  155. inline void std_swap(T& lhs, T& rhs)
  156. {
  157. // Defer to std_swap implementation in stlsoft_std_swap namespace
  158. //
  159. // This is to 'isolate' the selection of the swap() algorithm out of
  160. // the stlsoft namespace, so both stlsoft and std get equal byte at the
  161. // type resolution
  162. stlsoft_std_swap::std_swap(lhs, rhs);
  163. }
  164. } // namespace stlsoft
  165. #endif /* _STLSOFT_NO_NAMESPACE */
  166. /* ////////////////////////////////////////////////////////////////////// */
  167. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_STD_SWAP */
  168. /* ///////////////////////////// end of file //////////////////////////// */