traits.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*-
  2. * Copyright 2012 Matthew Endsley
  3. * All rights reserved
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted providing that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  18. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  22. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  23. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #ifndef TINYSTL_TRAITS_H
  27. #define TINYSTL_TRAITS_H
  28. #include "new.h"
  29. #if defined(__GNUC__) && defined(__is_pod)
  30. # define TINYSTL_TRY_POD_OPTIMIZATION(t) __is_pod(t)
  31. #elif defined(_MSC_VER)
  32. # define TINYSTL_TRY_POD_OPTIMIZATION(t) (!__is_class(t) || __is_pod(t))
  33. #else
  34. # define TINYSTL_TRY_POD_OPTIMIZATION(t) false
  35. #endif
  36. namespace tinystl {
  37. template<typename T, bool pod = TINYSTL_TRY_POD_OPTIMIZATION(T)> struct pod_traits {};
  38. template<typename T, T t> struct swap_holder;
  39. template<typename T>
  40. static inline void move_impl(T& a, T& b, ...) {
  41. a = b;
  42. }
  43. template<typename T>
  44. static inline void move_impl(T& a, T& b, T*, swap_holder<void (T::*)(T&), &T::swap>* = 0) {
  45. a.swap(b);
  46. }
  47. template<typename T>
  48. static inline void move(T& a, T&b) {
  49. move_impl(a, b, (T*)0);
  50. }
  51. template<typename T>
  52. static inline void move_construct_impl(T* a, T& b, ...) {
  53. new(placeholder(), a) T(b);
  54. }
  55. template<typename T>
  56. static inline void move_construct_impl(T* a, T& b, void*, swap_holder<void (T::*)(T&), &T::swap>* = 0) {
  57. // If your type T does not have a default constructor, simply insert:
  58. // struct tinystl_nomove_construct;
  59. // in the class definition
  60. new(placeholder(), a) T;
  61. a->swap(b);
  62. }
  63. template<typename T>
  64. static inline void move_construct_impl(T* a, T& b, T*, typename T::tinystl_nomove_construct* = 0) {
  65. new(placeholder(), a) T(b);
  66. }
  67. template<typename T>
  68. static inline void move_construct(T* a, T& b) {
  69. move_construct_impl(a, b, (T*)0);
  70. }
  71. }
  72. #endif