Merge branch 'master' into next
[sliver-openvswitch.git] / lib / ovsdb-types.h
1 /* Copyright (c) 2009, 2010 Nicira Networks
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef OVSDB_TYPES_H
17 #define OVSDB_TYPES_H 1
18
19 #include <stdbool.h>
20 #include <stdint.h>
21 #include "compiler.h"
22 #include "uuid.h"
23
24 struct json;
25
26 /* An atomic type: one that OVSDB regards as a single unit of data. */
27 enum ovsdb_atomic_type {
28     OVSDB_TYPE_VOID,            /* No value. */
29     OVSDB_TYPE_INTEGER,         /* Signed 64-bit integer. */
30     OVSDB_TYPE_REAL,            /* IEEE 754 double-precision floating point. */
31     OVSDB_TYPE_BOOLEAN,         /* True or false. */
32     OVSDB_TYPE_STRING,          /* UTF-8 string. */
33     OVSDB_TYPE_UUID,            /* RFC 4122 UUID referencing a table row. */
34     OVSDB_N_TYPES
35 };
36
37 static inline bool ovsdb_atomic_type_is_valid(enum ovsdb_atomic_type);
38 static inline bool ovsdb_atomic_type_is_valid_key(enum ovsdb_atomic_type);
39 bool ovsdb_atomic_type_from_string(const char *, enum ovsdb_atomic_type *);
40 struct ovsdb_error *ovsdb_atomic_type_from_json(enum ovsdb_atomic_type *,
41                                                 const struct json *);
42 const char *ovsdb_atomic_type_to_string(enum ovsdb_atomic_type);
43 struct json *ovsdb_atomic_type_to_json(enum ovsdb_atomic_type);
44 \f
45 /* An OVSDB type.
46  *
47  * Several rules constrain the valid types.  See ovsdb_type_is_valid() (in
48  * ovsdb-types.c) for details.
49  *
50  * If 'value_type' is OVSDB_TYPE_VOID, 'n_min' is 1, and 'n_max' is 1, then the
51  * type is a single atomic 'key_type'.
52  *
53  * If 'value_type' is OVSDB_TYPE_VOID and 'n_min' or 'n_max' (or both) has a
54  * value other than 1, then the type is a set of 'key_type'.  If 'n_min' is 0
55  * and 'n_max' is 1, then the type can also be considered an optional
56  * 'key_type'.
57  *
58  * If 'value_type' is not OVSDB_TYPE_VOID, then the type is a map from
59  * 'key_type' to 'value_type'.  If 'n_min' is 0 and 'n_max' is 1, then the type
60  * can also be considered an optional pair of 'key_type' and 'value_type'.
61  */
62 struct ovsdb_type {
63     enum ovsdb_atomic_type key_type;
64     enum ovsdb_atomic_type value_type;
65     unsigned int n_min;
66     unsigned int n_max;         /* UINT_MAX stands in for "unlimited". */
67 };
68
69 #define OVSDB_TYPE_SCALAR_INITIALIZER(KEY_TYPE) \
70         { KEY_TYPE, OVSDB_TYPE_VOID, 1, 1 }
71
72 extern const struct ovsdb_type ovsdb_type_integer;
73 extern const struct ovsdb_type ovsdb_type_real;
74 extern const struct ovsdb_type ovsdb_type_boolean;
75 extern const struct ovsdb_type ovsdb_type_string;
76 extern const struct ovsdb_type ovsdb_type_uuid;
77
78 bool ovsdb_type_is_valid(const struct ovsdb_type *);
79
80 static inline bool ovsdb_type_is_scalar(const struct ovsdb_type *);
81 static inline bool ovsdb_type_is_optional(const struct ovsdb_type *);
82 static inline bool ovsdb_type_is_composite(const struct ovsdb_type *);
83 static inline bool ovsdb_type_is_set(const struct ovsdb_type *);
84 static inline bool ovsdb_type_is_map(const struct ovsdb_type *);
85
86 char *ovsdb_type_to_english(const struct ovsdb_type *);
87
88 struct ovsdb_error *ovsdb_type_from_json(struct ovsdb_type *,
89                                          const struct json *)
90     WARN_UNUSED_RESULT;
91 struct json *ovsdb_type_to_json(const struct ovsdb_type *);
92 \f
93 /* Inline function implementations. */
94
95 static inline bool
96 ovsdb_atomic_type_is_valid(enum ovsdb_atomic_type atomic_type)
97 {
98     return atomic_type >= 0 && atomic_type < OVSDB_N_TYPES;
99 }
100
101 static inline bool
102 ovsdb_atomic_type_is_valid_key(enum ovsdb_atomic_type atomic_type)
103 {
104     /* XXX should we disallow reals or booleans as keys? */
105     return ovsdb_atomic_type_is_valid(atomic_type);
106 }
107
108 static inline bool ovsdb_type_is_scalar(const struct ovsdb_type *type)
109 {
110     return (type->value_type == OVSDB_TYPE_VOID
111             && type->n_min == 1 && type->n_max == 1);
112 }
113
114 static inline bool ovsdb_type_is_optional(const struct ovsdb_type *type)
115 {
116     return type->n_min == 0;
117 }
118
119 static inline bool ovsdb_type_is_composite(const struct ovsdb_type *type)
120 {
121     return type->n_max > 1;
122 }
123
124 static inline bool ovsdb_type_is_set(const struct ovsdb_type *type)
125 {
126     return (type->value_type == OVSDB_TYPE_VOID
127             && (type->n_min != 1 || type->n_max != 1));
128 }
129
130 static inline bool ovsdb_type_is_map(const struct ovsdb_type *type)
131 {
132     return type->value_type != OVSDB_TYPE_VOID;
133 }
134
135 #endif /* ovsdb-types.h */