traits.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*-
  2. * Copyright 2012-2018 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 <tinystl/new.h>
  29. #if defined(__GNUC__)
  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. template<typename T>
  72. struct remove_reference {
  73. typedef T type;
  74. };
  75. template<typename T>
  76. struct remove_reference<T&> {
  77. typedef T type;
  78. };
  79. template<typename T>
  80. struct remove_reference<T&&> {
  81. typedef T type;
  82. };
  83. template<typename T>
  84. struct remove_const {
  85. typedef T type;
  86. };
  87. template<typename T>
  88. struct remove_const<const T> {
  89. typedef T type;
  90. };
  91. template<typename T>
  92. struct remove_const<const T&> {
  93. typedef T& type;
  94. };
  95. template<typename T>
  96. struct remove_const<const T&&> {
  97. typedef T&& type;
  98. };
  99. template<typename T>
  100. struct remove_const_reference {
  101. typedef typename remove_reference<typename remove_const<T>::type>::type type;
  102. };
  103. }
  104. #endif