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_Types_hh__
20 #define avro_Types_hh__
21
22 #include <iostream>
23
24 #include "Config.hh"
25
26 namespace avro {
27
28 /**
29 * The "type" for the schema.
30 */
31 enum Type {
32
33 AVRO_STRING, /*!< String */
34 AVRO_BYTES, /*!< Sequence of variable length bytes data */
35 AVRO_INT, /*!< 32-bit integer */
36 AVRO_LONG, /*!< 64-bit integer */
37 AVRO_FLOAT, /*!< Floating point number */
38 AVRO_DOUBLE, /*!< Double precision floating point number */
39 AVRO_BOOL, /*!< Boolean value */
40 AVRO_NULL, /*!< Null */
41
42 AVRO_RECORD, /*!< Record, a sequence of fields */
43 AVRO_ENUM, /*!< Enumeration */
44 AVRO_ARRAY, /*!< Homogeneous array of some specific type */
45 AVRO_MAP, /*!< Homogeneous map from string to some specific type */
46 AVRO_UNION, /*!< Union of one or more types */
47 AVRO_FIXED, /*!< Fixed number of bytes */
48
49 AVRO_NUM_TYPES, /*!< Marker */
50
51 // The following is a pseudo-type used in implementation
52
53 AVRO_SYMBOLIC = AVRO_NUM_TYPES, /*!< User internally to avoid circular references. */
54 AVRO_UNKNOWN = -1 /*!< Used internally. */
55 };
56
57 /**
58 * Returns true if and only if the given type is a primitive.
59 * Primitive types are: string, bytes, int, long, float, double, boolean
60 * and null
61 */
isPrimitive(Type t)62 inline constexpr bool isPrimitive(Type t) noexcept {
63 return (t >= AVRO_STRING) && (t < AVRO_RECORD);
64 }
65
66 /**
67 * Returns true if and only if the given type is a non primitive valid type.
68 * Primitive types are: string, bytes, int, long, float, double, boolean
69 * and null
70 */
isCompound(Type t)71 inline constexpr bool isCompound(Type t) noexcept {
72 return (t >= AVRO_RECORD) && (t < AVRO_NUM_TYPES);
73 }
74
75 /**
76 * Returns true if and only if the given type is a valid avro type.
77 */
isAvroType(Type t)78 inline constexpr bool isAvroType(Type t) noexcept {
79 return (t >= AVRO_STRING) && (t < AVRO_NUM_TYPES);
80 }
81
82 /**
83 * Returns true if and only if the given type is within the valid range
84 * of enumeration.
85 */
isAvroTypeOrPseudoType(Type t)86 inline constexpr bool isAvroTypeOrPseudoType(Type t) noexcept {
87 return (t >= AVRO_STRING) && (t <= AVRO_NUM_TYPES);
88 }
89
90 /**
91 * Converts the given type into a string. Useful for generating messages.
92 */
93 AVRO_DECL const std::string &toString(Type type) noexcept;
94
95 /**
96 * Writes a string form of the given type into the given ostream.
97 */
98 AVRO_DECL std::ostream &operator<<(std::ostream &os, avro::Type type);
99
100 /// define a type to represent Avro Null in template functions
101 struct AVRO_DECL Null {};
102
103 /**
104 * Writes schema for null \p null type to \p os.
105 * \param os The ostream to write to.
106 * \param null The value to be written.
107 */
108 std::ostream &operator<<(std::ostream &os, const Null &null);
109
110 } // namespace avro
111
112 #endif
113