AvroTraits.hh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * https://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef avro_AvroTraits_hh__
  19. #define avro_AvroTraits_hh__
  20. #include "Config.hh"
  21. #include "Types.hh"
  22. #include <cstdint>
  23. #include <type_traits>
  24. /** @file
  25. *
  26. * This header contains type traits and similar utilities used by the library.
  27. */
  28. namespace avro {
  29. /**
  30. * Define an is_serializable trait for types we can serialize natively.
  31. * New types will need to define the trait as well.
  32. */
  33. template<typename T>
  34. struct is_serializable : public std::false_type {};
  35. template<typename T>
  36. struct is_promotable : public std::false_type {};
  37. template<typename T>
  38. struct type_to_avro {
  39. static const Type type = AVRO_NUM_TYPES;
  40. };
  41. /**
  42. * Check if a \p T is a complete type i.e. it is defined as opposed to just
  43. * declared.
  44. *
  45. * is_defined<T>::value will be true or false depending on whether T is a
  46. * complete type or not respectively.
  47. */
  48. template<class T>
  49. struct is_defined {
  50. typedef char yes[1];
  51. typedef char no[2];
  52. template<class U>
  53. static yes &test(char (*)[sizeof(U)]) { throw 0; }
  54. template<class U>
  55. static no &test(...) { throw 0; }
  56. static const bool value = sizeof(test<T>(0)) == sizeof(yes);
  57. };
  58. /**
  59. * Similar to is_defined, but used to check if T is not defined.
  60. *
  61. * is_not_defined<T>::value will be true or false depending on whether T is an
  62. * incomplete type or not respectively.
  63. */
  64. template<class T>
  65. struct is_not_defined {
  66. typedef char yes[1];
  67. typedef char no[2];
  68. template<class U>
  69. static yes &test(char (*)[sizeof(U)]) { throw 0; }
  70. template<class U>
  71. static no &test(...) { throw 0; }
  72. static const bool value = sizeof(test<T>(0)) == sizeof(no);
  73. };
  74. #define DEFINE_PRIMITIVE(CTYPE, AVROTYPE) \
  75. template<> \
  76. struct is_serializable<CTYPE> : public std::true_type {}; \
  77. \
  78. template<> \
  79. struct type_to_avro<CTYPE> { \
  80. static const Type type = AVROTYPE; \
  81. };
  82. #define DEFINE_PROMOTABLE_PRIMITIVE(CTYPE, AVROTYPE) \
  83. template<> \
  84. struct is_promotable<CTYPE> : public std::true_type {}; \
  85. \
  86. DEFINE_PRIMITIVE(CTYPE, AVROTYPE)
  87. DEFINE_PROMOTABLE_PRIMITIVE(int32_t, AVRO_INT)
  88. DEFINE_PROMOTABLE_PRIMITIVE(int64_t, AVRO_LONG)
  89. DEFINE_PROMOTABLE_PRIMITIVE(float, AVRO_FLOAT)
  90. DEFINE_PRIMITIVE(double, AVRO_DOUBLE)
  91. DEFINE_PRIMITIVE(bool, AVRO_BOOL)
  92. DEFINE_PRIMITIVE(Null, AVRO_NULL)
  93. DEFINE_PRIMITIVE(std::string, AVRO_STRING)
  94. DEFINE_PRIMITIVE(std::vector<uint8_t>, AVRO_BYTES)
  95. } // namespace avro
  96. #endif