ovsdb: Drop regular expression constraints.
[cascardo/ovs.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 <float.h>
20 #include <stdbool.h>
21 #include <stdint.h>
22 #include "compiler.h"
23 #include "uuid.h"
24
25 struct json;
26
27 /* An atomic type: one that OVSDB regards as a single unit of data. */
28 enum ovsdb_atomic_type {
29     OVSDB_TYPE_VOID,            /* No value. */
30     OVSDB_TYPE_INTEGER,         /* Signed 64-bit integer. */
31     OVSDB_TYPE_REAL,            /* IEEE 754 double-precision floating point. */
32     OVSDB_TYPE_BOOLEAN,         /* True or false. */
33     OVSDB_TYPE_STRING,          /* UTF-8 string. */
34     OVSDB_TYPE_UUID,            /* RFC 4122 UUID referencing a table row. */
35     OVSDB_N_TYPES
36 };
37
38 static inline bool ovsdb_atomic_type_is_valid(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 atomic type plus optional constraints. */
46
47 struct ovsdb_base_type {
48     enum ovsdb_atomic_type type;
49     union {
50         struct ovsdb_integer_constraints {
51             int64_t min;        /* minInteger or INT64_MIN. */
52             int64_t max;        /* maxInteger or INT64_MAX. */
53         } integer;
54
55         struct ovsdb_real_constraints {
56             double min;         /* minReal or -DBL_MAX. */
57             double max;         /* minReal or DBL_MAX. */
58         } real;
59
60         /* No constraints for Boolean types. */
61
62         struct ovsdb_string_constraints {
63             unsigned int minLen; /* minLength or 0. */
64             unsigned int maxLen; /* maxLength or UINT_MAX. */
65         } string;
66
67         struct ovsdb_uuid_constraints {
68             char *refTableName; /* Name of referenced table, or NULL. */
69             struct ovsdb_table *refTable; /* Referenced table, if available. */
70         } uuid;
71     } u;
72 };
73
74 #define OVSDB_BASE_VOID_INIT    { .type = OVSDB_TYPE_VOID }
75 #define OVSDB_BASE_INTEGER_INIT { .type = OVSDB_TYPE_INTEGER,           \
76                                   .u.integer = { INT64_MIN, INT64_MAX } }
77 #define OVSDB_BASE_REAL_INIT    { .type = OVSDB_TYPE_REAL,          \
78                                   .u.real = { -DBL_MAX, DBL_MAX } }
79 #define OVSDB_BASE_BOOLEAN_INIT { .type = OVSDB_TYPE_BOOLEAN }
80 #define OVSDB_BASE_STRING_INIT  { .type = OVSDB_TYPE_STRING,    \
81                                   .u.string = { 0, UINT_MAX } }
82 #define OVSDB_BASE_UUID_INIT    { .type = OVSDB_TYPE_UUID,      \
83                                   .u.uuid = { NULL, NULL } }
84
85 void ovsdb_base_type_init(struct ovsdb_base_type *, enum ovsdb_atomic_type);
86 void ovsdb_base_type_clone(struct ovsdb_base_type *,
87                            const struct ovsdb_base_type *);
88 void ovsdb_base_type_destroy(struct ovsdb_base_type *);
89
90 bool ovsdb_base_type_is_valid(const struct ovsdb_base_type *);
91 bool ovsdb_base_type_has_constraints(const struct ovsdb_base_type *);
92 void ovsdb_base_type_clear_constraints(struct ovsdb_base_type *);
93
94 struct ovsdb_error *ovsdb_base_type_from_json(struct ovsdb_base_type *,
95                                               const struct json *)
96     WARN_UNUSED_RESULT;
97 struct json *ovsdb_base_type_to_json(const struct ovsdb_base_type *);
98 \f
99 /* An OVSDB type.
100  *
101  * Several rules constrain the valid types.  See ovsdb_type_is_valid() (in
102  * ovsdb-types.c) for details.
103  *
104  * If 'value_type' is OVSDB_TYPE_VOID, 'n_min' is 1, and 'n_max' is 1, then the
105  * type is a single atomic 'key_type'.
106  *
107  * If 'value_type' is OVSDB_TYPE_VOID and 'n_min' or 'n_max' (or both) has a
108  * value other than 1, then the type is a set of 'key_type'.  If 'n_min' is 0
109  * and 'n_max' is 1, then the type can also be considered an optional
110  * 'key_type'.
111  *
112  * If 'value_type' is not OVSDB_TYPE_VOID, then the type is a map from
113  * 'key_type' to 'value_type'.  If 'n_min' is 0 and 'n_max' is 1, then the type
114  * can also be considered an optional pair of 'key_type' and 'value_type'.
115  */
116 struct ovsdb_type {
117     struct ovsdb_base_type key;
118     struct ovsdb_base_type value;
119     unsigned int n_min;
120     unsigned int n_max;         /* UINT_MAX stands in for "unlimited". */
121 };
122
123 #define OVSDB_TYPE_SCALAR_INITIALIZER(KEY) { KEY, OVSDB_BASE_VOID_INIT, 1, 1 }
124
125 extern const struct ovsdb_type ovsdb_type_integer;
126 extern const struct ovsdb_type ovsdb_type_real;
127 extern const struct ovsdb_type ovsdb_type_boolean;
128 extern const struct ovsdb_type ovsdb_type_string;
129 extern const struct ovsdb_type ovsdb_type_uuid;
130
131 void ovsdb_type_clone(struct ovsdb_type *, const struct ovsdb_type *);
132 void ovsdb_type_destroy(struct ovsdb_type *);
133
134 bool ovsdb_type_is_valid(const struct ovsdb_type *);
135
136 static inline bool ovsdb_type_is_scalar(const struct ovsdb_type *);
137 static inline bool ovsdb_type_is_optional(const struct ovsdb_type *);
138 static inline bool ovsdb_type_is_composite(const struct ovsdb_type *);
139 static inline bool ovsdb_type_is_set(const struct ovsdb_type *);
140 static inline bool ovsdb_type_is_map(const struct ovsdb_type *);
141
142 char *ovsdb_type_to_english(const struct ovsdb_type *);
143
144 struct ovsdb_error *ovsdb_type_from_json(struct ovsdb_type *,
145                                          const struct json *)
146     WARN_UNUSED_RESULT;
147 struct json *ovsdb_type_to_json(const struct ovsdb_type *);
148 \f
149 /* Inline function implementations. */
150
151 static inline bool
152 ovsdb_atomic_type_is_valid(enum ovsdb_atomic_type atomic_type)
153 {
154     return atomic_type >= 0 && atomic_type < OVSDB_N_TYPES;
155 }
156
157 static inline bool ovsdb_type_is_scalar(const struct ovsdb_type *type)
158 {
159     return (type->value.type == OVSDB_TYPE_VOID
160             && type->n_min == 1 && type->n_max == 1);
161 }
162
163 static inline bool ovsdb_type_is_optional(const struct ovsdb_type *type)
164 {
165     return type->n_min == 0;
166 }
167
168 static inline bool ovsdb_type_is_composite(const struct ovsdb_type *type)
169 {
170     return type->n_max > 1;
171 }
172
173 static inline bool ovsdb_type_is_set(const struct ovsdb_type *type)
174 {
175     return (type->value.type == OVSDB_TYPE_VOID
176             && (type->n_min != 1 || type->n_max != 1));
177 }
178
179 static inline bool ovsdb_type_is_map(const struct ovsdb_type *type)
180 {
181     return type->value.type != OVSDB_TYPE_VOID;
182 }
183
184 #endif /* ovsdb-types.h */