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 <stdint.h>
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> static yes& test(char(*)[sizeof(U)]) { throw 0; };
63 
64  template <class U> static no& test(...) { throw 0; };
65 
66  static const bool value = sizeof(test<T>(0)) == sizeof(yes);
67 };
68 
75 template <class T>
77 
78  typedef char yes[1];
79 
80  typedef char no[2];
81 
82  template <class U> static yes& test(char(*)[sizeof(U)]) { throw 0; };
83 
84  template <class U> static no& test(...) { throw 0; };
85 
86  static const bool value = sizeof(test<T>(0)) == sizeof(no);
87 };
88 
89 #define DEFINE_PRIMITIVE(CTYPE, AVROTYPE) \
90 template <> \
91 struct is_serializable<CTYPE> : public std::true_type{}; \
92 \
93 template <> \
94 struct type_to_avro<CTYPE> { \
95  static const Type type = AVROTYPE; \
96 };
97 
98 #define DEFINE_PROMOTABLE_PRIMITIVE(CTYPE, AVROTYPE) \
99 template <> \
100 struct is_promotable<CTYPE> : public std::true_type{}; \
101 \
102 DEFINE_PRIMITIVE(CTYPE, AVROTYPE)
103 
104 DEFINE_PROMOTABLE_PRIMITIVE(int32_t, AVRO_INT)
105 DEFINE_PROMOTABLE_PRIMITIVE(int64_t, AVRO_LONG)
106 DEFINE_PROMOTABLE_PRIMITIVE(float, AVRO_FLOAT)
107 DEFINE_PRIMITIVE(double, AVRO_DOUBLE)
108 DEFINE_PRIMITIVE(bool, AVRO_BOOL)
109 DEFINE_PRIMITIVE(Null, AVRO_NULL)
110 DEFINE_PRIMITIVE(std::string, AVRO_STRING)
111 DEFINE_PRIMITIVE(std::vector<uint8_t>, AVRO_BYTES)
112 
113 
114 } // namespace avro
115 
116 #endif
Definition: Types.hh:37
Definition: Types.hh:40
Check if a T is a complete type i.e.
Definition: AvroTraits.hh:56
Type
The "type" for the schema.
Definition: Types.hh:31
Definition: Types.hh:49
A bunch of templates and specializations for encoding and decoding specific types.
Definition: AvroParse.hh:30
Definition: Types.hh:39
Definition: Types.hh:33
Definition: AvroTraits.hh:41
Definition: Types.hh:35
Definition: Types.hh:34
Definition: Types.hh:38
define a type to identify Null in template functions
Definition: Types.hh:102
Similar to is_defined, but used to check if T is not defined.
Definition: AvroTraits.hh:76
Define an is_serializable trait for types we can serialize natively.
Definition: AvroTraits.hh:38
Definition: AvroTraits.hh:44
Definition: Types.hh:36