DsrTraits.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2025 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. // These custom traits allow implementing template functions that can work with SIMD types when needed, without exposing simd.h in headers.
  24. #ifndef DFPSR_TRAITS
  25. #define DFPSR_TRAITS
  26. #include <stdint.h>
  27. namespace dsr {
  28. // Subset of std::integral_constant.
  29. template <typename T, T VALUE>
  30. struct DSR_PROPERTY {
  31. static constexpr T value = VALUE;
  32. };
  33. // Custom implementation of std::false_type.
  34. using DSR_TRAIT_FALSE = DSR_PROPERTY<bool, false>;
  35. // Custom implementation of std::true_type.
  36. using DSR_TRAIT_TRUE = DSR_PROPERTY<bool, true>;
  37. // Custom implementation of std::is_same.
  38. template <typename T, typename U>
  39. struct DsrTrait_SameType { static const bool value = false; };
  40. template <typename T>
  41. struct DsrTrait_SameType<T, T> { static const bool value = true; };
  42. // Custom implementation of std::enable_if.
  43. template<bool B, class T = void>
  44. struct DsrTrait_EnableIf;
  45. template<class T>
  46. struct DsrTrait_EnableIf<true, T> {
  47. using type = T;
  48. };
  49. // Place this as a template argument to disable the template function when false.
  50. #define DSR_ENABLE_IF(TRAIT) \
  51. typename = typename DsrTrait_EnableIf<TRAIT>::type
  52. // Properties are given to single types.
  53. #define DSR_DECLARE_PROPERTY(PROPERTY_NAME) \
  54. template <typename T> struct PROPERTY_NAME : DSR_TRAIT_FALSE {};
  55. #define DSR_APPLY_PROPERTY(PROPERTY_NAME, TYPE_NAME) \
  56. template <> struct PROPERTY_NAME<TYPE_NAME> : DSR_TRAIT_TRUE {};
  57. #define DSR_CHECK_PROPERTY(PROPERTY_NAME, TYPE_NAME) \
  58. (PROPERTY_NAME<TYPE_NAME>::value)
  59. // Relations are given to pairs of types.
  60. #define DSR_DECLARE_RELATION(RELATION_NAME) \
  61. template <typename T, typename U> struct RELATION_NAME : DSR_TRAIT_FALSE {};
  62. #define DSR_APPLY_RELATION(RELATION_NAME, TYPE_A, TYPE_B) \
  63. template <> struct RELATION_NAME<TYPE_A, TYPE_B> : DSR_TRAIT_TRUE {};
  64. #define DSR_CHECK_RELATION(RELATION_NAME, TYPE_A, TYPE_B) \
  65. (RELATION_NAME<TYPE_A, TYPE_B>::value)
  66. DSR_DECLARE_PROPERTY(DsrTrait_Any_U8)
  67. DSR_APPLY_PROPERTY(DsrTrait_Any_U8, uint8_t)
  68. DSR_DECLARE_PROPERTY(DsrTrait_Any_U16)
  69. DSR_APPLY_PROPERTY(DsrTrait_Any_U16, uint16_t)
  70. DSR_DECLARE_PROPERTY(DsrTrait_Any_U32)
  71. DSR_APPLY_PROPERTY(DsrTrait_Any_U32, uint32_t)
  72. DSR_DECLARE_PROPERTY(DsrTrait_Any_I32)
  73. DSR_APPLY_PROPERTY(DsrTrait_Any_I32, int32_t)
  74. DSR_DECLARE_PROPERTY(DsrTrait_Any_F32)
  75. DSR_APPLY_PROPERTY(DsrTrait_Any_F32, float)
  76. DSR_DECLARE_PROPERTY(DsrTrait_Scalar_SignedInteger)
  77. DSR_APPLY_PROPERTY(DsrTrait_Scalar_SignedInteger, int8_t)
  78. DSR_APPLY_PROPERTY(DsrTrait_Scalar_SignedInteger, int16_t)
  79. DSR_APPLY_PROPERTY(DsrTrait_Scalar_SignedInteger, int32_t)
  80. DSR_APPLY_PROPERTY(DsrTrait_Scalar_SignedInteger, int64_t)
  81. DSR_DECLARE_PROPERTY(DsrTrait_Scalar_UnsignedInteger)
  82. DSR_APPLY_PROPERTY(DsrTrait_Scalar_UnsignedInteger, uint8_t)
  83. DSR_APPLY_PROPERTY(DsrTrait_Scalar_UnsignedInteger, uint16_t)
  84. DSR_APPLY_PROPERTY(DsrTrait_Scalar_UnsignedInteger, uint32_t)
  85. DSR_APPLY_PROPERTY(DsrTrait_Scalar_UnsignedInteger, uint64_t)
  86. DSR_DECLARE_PROPERTY(DsrTrait_Scalar_Floating)
  87. DSR_APPLY_PROPERTY(DsrTrait_Scalar_Floating, float)
  88. DSR_APPLY_PROPERTY(DsrTrait_Scalar_Floating, double)
  89. DSR_DECLARE_PROPERTY(DsrTrait_Scalar_Integer)
  90. DSR_APPLY_PROPERTY(DsrTrait_Scalar_Integer, int8_t)
  91. DSR_APPLY_PROPERTY(DsrTrait_Scalar_Integer, int16_t)
  92. DSR_APPLY_PROPERTY(DsrTrait_Scalar_Integer, int32_t)
  93. DSR_APPLY_PROPERTY(DsrTrait_Scalar_Integer, int64_t)
  94. DSR_APPLY_PROPERTY(DsrTrait_Scalar_Integer, uint8_t)
  95. DSR_APPLY_PROPERTY(DsrTrait_Scalar_Integer, uint16_t)
  96. DSR_APPLY_PROPERTY(DsrTrait_Scalar_Integer, uint32_t)
  97. DSR_APPLY_PROPERTY(DsrTrait_Scalar_Integer, uint64_t)
  98. DSR_DECLARE_PROPERTY(DsrTrait_Scalar)
  99. DSR_APPLY_PROPERTY(DsrTrait_Scalar, int8_t)
  100. DSR_APPLY_PROPERTY(DsrTrait_Scalar, int16_t)
  101. DSR_APPLY_PROPERTY(DsrTrait_Scalar, int32_t)
  102. DSR_APPLY_PROPERTY(DsrTrait_Scalar, int64_t)
  103. DSR_APPLY_PROPERTY(DsrTrait_Scalar, uint8_t)
  104. DSR_APPLY_PROPERTY(DsrTrait_Scalar, uint16_t)
  105. DSR_APPLY_PROPERTY(DsrTrait_Scalar, uint32_t)
  106. DSR_APPLY_PROPERTY(DsrTrait_Scalar, uint64_t)
  107. DSR_APPLY_PROPERTY(DsrTrait_Scalar, float)
  108. DSR_APPLY_PROPERTY(DsrTrait_Scalar, double)
  109. }
  110. #endif