Avro C++

AvroTraits.hh
Go to the documentation of this file.
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
19 #ifndef avro_AvroTraits_hh__
20 #define avro_AvroTraits_hh__
21
22 #include "Config.hh"
23 #include "Types.hh"
24 #include <cstdint>
25 #include <type_traits>
26
31 namespace avro {
32
37 template<typename T>
38 struct is_serializable : public std::false_type {};
39
40 template<typename T>
41 struct is_promotable : public std::false_type {};
42
43 template<typename T>
44 struct type_to_avro {
45 static const Type type = AVRO_NUM_TYPES;
46 };
47
55 template<class T>
56 struct is_defined {
57
58 typedef char yes[1];
59
60 typedef char no[2];
61
62 template<class U>
63 static yes &test(char (*)[sizeof(U)]) { throw 0; }
64
65 template<class U>
66 static no &test(...) { throw 0; }
67
68 static const bool value = sizeof(test<T>(0)) == sizeof(yes);
69 };
70
77 template<class T>
78 struct is_not_defined {
79
80 typedef char yes[1];
81
82 typedef char no[2];
83
84 template<class U>
85 static yes &test(char (*)[sizeof(U)]) { throw 0; }
86
87 template<class U>
88 static no &test(...) { throw 0; }
89
90 static const bool value = sizeof(test<T>(0)) == sizeof(no);
91 };
92
93 #define DEFINE_PRIMITIVE(CTYPE, AVROTYPE) \
94 template<> \
95 struct is_serializable<CTYPE> : public std::true_type {}; \
96 \
97 template<> \
98 struct type_to_avro<CTYPE> { \
99 static const Type type = AVROTYPE; \
100 };
101
102 #define DEFINE_PROMOTABLE_PRIMITIVE(CTYPE, AVROTYPE) \
103 template<> \
104 struct is_promotable<CTYPE> : public std::true_type {}; \
105 \
106 DEFINE_PRIMITIVE(CTYPE, AVROTYPE)
107
108 DEFINE_PROMOTABLE_PRIMITIVE(int32_t, AVRO_INT)
109 DEFINE_PROMOTABLE_PRIMITIVE(int64_t, AVRO_LONG)
110 DEFINE_PROMOTABLE_PRIMITIVE(float, AVRO_FLOAT)
111 DEFINE_PRIMITIVE(double, AVRO_DOUBLE)
112 DEFINE_PRIMITIVE(bool, AVRO_BOOL)
113 DEFINE_PRIMITIVE(Null, AVRO_NULL)
114 DEFINE_PRIMITIVE(std::string, AVRO_STRING)
115 DEFINE_PRIMITIVE(std::vector<uint8_t>, AVRO_BYTES)
116
117 } // namespace avro
118
119 #endif
avro::AVRO_NULL
@ AVRO_NULL
Definition: Types.hh:40
avro::type_to_avro
Definition: AvroTraits.hh:44
avro::AVRO_LONG
@ AVRO_LONG
Definition: Types.hh:36
avro::is_serializable
Define an is_serializable trait for types we can serialize natively.
Definition: AvroTraits.hh:38
avro::is_defined
Check if a T is a complete type i.e.
Definition: AvroTraits.hh:56
avro::AVRO_FLOAT
@ AVRO_FLOAT
Definition: Types.hh:37
avro::AVRO_BOOL
@ AVRO_BOOL
Definition: Types.hh:39
avro::AVRO_STRING
@ AVRO_STRING
Definition: Types.hh:33
avro::AVRO_NUM_TYPES
@ AVRO_NUM_TYPES
Definition: Types.hh:49
avro::AVRO_BYTES
@ AVRO_BYTES
Definition: Types.hh:34
avro::is_not_defined
Similar to is_defined, but used to check if T is not defined.
Definition: AvroTraits.hh:78
avro::is_promotable
Definition: AvroTraits.hh:41
avro::AVRO_INT
@ AVRO_INT
Definition: Types.hh:35
avro
A bunch of templates and specializations for encoding and decoding specific types.
Definition: AvroParse.hh:30
avro::AVRO_DOUBLE
@ AVRO_DOUBLE
Definition: Types.hh:38
avro::Null
define a type to represent Avro Null in template functions
Definition: Types.hh:101
avro::Type
Type
The "type" for the schema.
Definition: Types.hh:31