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  *     http://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 "Boost.hh"
24 #include "Types.hh"
25 
26 /// \file
27 ///
28 /// Define an is_serializable trait for types we can serialize natively.
29 /// New types will need to define the trait as well.
30 
31 namespace internal_avro {
32 
33 template <typename T>
34 struct is_serializable : public boost::false_type {};
35 
36 template <typename T>
37 struct is_promotable : public boost::false_type {};
38 
39 template <typename T>
40 struct type_to_avro {
41   static const Type type = AVRO_NUM_TYPES;
42 };
43 
44 #define DEFINE_PRIMITIVE(CTYPE, AVROTYPE)                     \
45   template <>                                                 \
46   struct is_serializable<CTYPE> : public boost::true_type {}; \
47   template <>                                                 \
48   struct type_to_avro<CTYPE> {                                \
49     static const Type type = AVROTYPE;                        \
50   };
51 
52 #define DEFINE_PROMOTABLE_PRIMITIVE(CTYPE, AVROTYPE)        \
53   template <>                                               \
54   struct is_promotable<CTYPE> : public boost::true_type {}; \
55   DEFINE_PRIMITIVE(CTYPE, AVROTYPE)
56 
57 DEFINE_PROMOTABLE_PRIMITIVE(int32_t, AVRO_INT)
58 DEFINE_PROMOTABLE_PRIMITIVE(int64_t, AVRO_LONG)
59 DEFINE_PROMOTABLE_PRIMITIVE(float, AVRO_FLOAT)
60 DEFINE_PRIMITIVE(double, AVRO_DOUBLE)
61 DEFINE_PRIMITIVE(bool, AVRO_BOOL)
62 DEFINE_PRIMITIVE(Null, AVRO_NULL)
63 DEFINE_PRIMITIVE(std::string, AVRO_STRING)
64 DEFINE_PRIMITIVE(std::vector<uint8_t>, AVRO_BYTES)
65 
66 }  // namespace internal_avro
67 
68 #endif
69