~stub/ubuntu/trusty/avro-c/trunk

« back to all changes in this revision

Viewing changes to src/avro/legacy.h

  • Committer: Stuart Bishop
  • Date: 2015-05-14 11:53:53 UTC
  • Revision ID: stuart@stuartbishop.net-20150514115353-0cvnrcyohcq5l7yj
Tags: upstream-1.7.7
Import upstream version 1.7.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
 
3
 * contributor license agreements.  See the NOTICE file distributed with
 
4
 * this work for additional information regarding copyright ownership.
 
5
 * The ASF licenses this file to you under the Apache License, Version 2.0
 
6
 * (the "License"); you may not use this file except in compliance with
 
7
 * the License.  You may obtain a copy of the License at
 
8
 *
 
9
 * http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
11
 * Unless required by applicable law or agreed to in writing, software
 
12
 * distributed under the License is distributed on an "AS IS" BASIS,
 
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 
14
 * implied.  See the License for the specific language governing
 
15
 * permissions and limitations under the License.
 
16
 */
 
17
 
 
18
#ifndef AVRO_LEGACY_H
 
19
#define AVRO_LEGACY_H
 
20
#ifdef __cplusplus
 
21
extern "C" {
 
22
#define CLOSE_EXTERN }
 
23
#else
 
24
#define CLOSE_EXTERN
 
25
#endif
 
26
 
 
27
#include <avro/platform.h>
 
28
#include <stdio.h>
 
29
 
 
30
#include <avro/basics.h>
 
31
#include <avro/data.h>
 
32
#include <avro/schema.h>
 
33
#include <avro/value.h>
 
34
 
 
35
/*
 
36
 * This file defines the deprecated interface for handling Avro values.
 
37
 * It's here solely for backwards compatibility.  New code should use
 
38
 * the avro_value_t interface (defined in avro/value.h).  The
 
39
 * avro_datum_t type has been replaced by the “generic” implementation
 
40
 * of the value interface, which is defined in avro/generic.h.  You can
 
41
 * also use your own application-specific types as Avro values by
 
42
 * defining your own avro_value_t implementation for them.
 
43
 */
 
44
 
 
45
/**
 
46
 * A function used to free a bytes, string, or fixed buffer once it is
 
47
 * no longer needed by the datum that wraps it.
 
48
 */
 
49
 
 
50
typedef void
 
51
(*avro_free_func_t)(void *ptr, size_t sz);
 
52
 
 
53
/**
 
54
 * An avro_free_func_t that frees the buffer using the custom allocator
 
55
 * provided to avro_set_allocator.
 
56
 */
 
57
 
 
58
void
 
59
avro_alloc_free_func(void *ptr, size_t sz);
 
60
 
 
61
/*
 
62
 * Datum constructors.  Each datum stores a reference to the schema that
 
63
 * the datum is an instance of.  The primitive datum constructors don't
 
64
 * need to take in an explicit avro_schema_t parameter, since there's
 
65
 * only one schema that they could be an instance of.  The complex
 
66
 * constructors do need an explicit schema parameter.
 
67
 */
 
68
 
 
69
typedef struct avro_obj_t *avro_datum_t;
 
70
avro_datum_t avro_string(const char *str);
 
71
avro_datum_t avro_givestring(const char *str,
 
72
                             avro_free_func_t free);
 
73
avro_datum_t avro_bytes(const char *buf, int64_t len);
 
74
avro_datum_t avro_givebytes(const char *buf, int64_t len,
 
75
                            avro_free_func_t free);
 
76
avro_datum_t avro_int32(int32_t i);
 
77
avro_datum_t avro_int64(int64_t l);
 
78
avro_datum_t avro_float(float f);
 
79
avro_datum_t avro_double(double d);
 
80
avro_datum_t avro_boolean(int8_t i);
 
81
avro_datum_t avro_null(void);
 
82
avro_datum_t avro_record(avro_schema_t schema);
 
83
avro_datum_t avro_enum(avro_schema_t schema, int i);
 
84
avro_datum_t avro_fixed(avro_schema_t schema,
 
85
                        const char *bytes, const int64_t size);
 
86
avro_datum_t avro_givefixed(avro_schema_t schema,
 
87
                            const char *bytes, const int64_t size,
 
88
                            avro_free_func_t free);
 
89
avro_datum_t avro_map(avro_schema_t schema);
 
90
avro_datum_t avro_array(avro_schema_t schema);
 
91
avro_datum_t avro_union(avro_schema_t schema,
 
92
                        int64_t discriminant, const avro_datum_t datum);
 
93
 
 
94
/**
 
95
 * Returns the schema that the datum is an instance of.
 
96
 */
 
97
 
 
98
avro_schema_t avro_datum_get_schema(const avro_datum_t datum);
 
99
 
 
100
/*
 
101
 * Constructs a new avro_datum_t instance that's appropriate for holding
 
102
 * values of the given schema.
 
103
 */
 
104
 
 
105
avro_datum_t avro_datum_from_schema(const avro_schema_t schema);
 
106
 
 
107
/* getters */
 
108
int avro_string_get(avro_datum_t datum, char **p);
 
109
int avro_bytes_get(avro_datum_t datum, char **bytes, int64_t * size);
 
110
int avro_int32_get(avro_datum_t datum, int32_t * i);
 
111
int avro_int64_get(avro_datum_t datum, int64_t * l);
 
112
int avro_float_get(avro_datum_t datum, float *f);
 
113
int avro_double_get(avro_datum_t datum, double *d);
 
114
int avro_boolean_get(avro_datum_t datum, int8_t * i);
 
115
 
 
116
int avro_enum_get(const avro_datum_t datum);
 
117
const char *avro_enum_get_name(const avro_datum_t datum);
 
118
int avro_fixed_get(avro_datum_t datum, char **bytes, int64_t * size);
 
119
int avro_record_get(const avro_datum_t record, const char *field_name,
 
120
                    avro_datum_t * value);
 
121
 
 
122
/*
 
123
 * A helper macro that extracts the value of the given field of a
 
124
 * record.
 
125
 */
 
126
 
 
127
#define avro_record_get_field_value(rc, rec, typ, fname, ...)   \
 
128
        do {                                                    \
 
129
                avro_datum_t  field = NULL;                     \
 
130
                (rc) = avro_record_get((rec), (fname), &field); \
 
131
                if (rc) break;                                  \
 
132
                (rc) = avro_##typ##_get(field, __VA_ARGS__);    \
 
133
        } while (0)
 
134
 
 
135
 
 
136
int avro_map_get(const avro_datum_t datum, const char *key,
 
137
                 avro_datum_t * value);
 
138
/*
 
139
 * For maps, the "index" for each entry is based on the order that they
 
140
 * were added to the map.
 
141
 */
 
142
int avro_map_get_key(const avro_datum_t datum, int index,
 
143
                     const char **key);
 
144
int avro_map_get_index(const avro_datum_t datum, const char *key,
 
145
                       int *index);
 
146
size_t avro_map_size(const avro_datum_t datum);
 
147
int avro_array_get(const avro_datum_t datum, int64_t index, avro_datum_t * value);
 
148
size_t avro_array_size(const avro_datum_t datum);
 
149
 
 
150
/*
 
151
 * These accessors allow you to query the current branch of a union
 
152
 * value, returning either the branch's discriminant value or the
 
153
 * avro_datum_t of the branch.  A union value can be uninitialized, in
 
154
 * which case the discriminant will be -1 and the datum NULL.
 
155
 */
 
156
 
 
157
int64_t avro_union_discriminant(const avro_datum_t datum);
 
158
avro_datum_t avro_union_current_branch(avro_datum_t datum);
 
159
 
 
160
/* setters */
 
161
int avro_string_set(avro_datum_t datum, const char *p);
 
162
int avro_givestring_set(avro_datum_t datum, const char *p,
 
163
                        avro_free_func_t free);
 
164
 
 
165
int avro_bytes_set(avro_datum_t datum, const char *bytes, const int64_t size);
 
166
int avro_givebytes_set(avro_datum_t datum, const char *bytes,
 
167
                       const int64_t size,
 
168
                       avro_free_func_t free);
 
169
 
 
170
int avro_int32_set(avro_datum_t datum, const int32_t i);
 
171
int avro_int64_set(avro_datum_t datum, const int64_t l);
 
172
int avro_float_set(avro_datum_t datum, const float f);
 
173
int avro_double_set(avro_datum_t datum, const double d);
 
174
int avro_boolean_set(avro_datum_t datum, const int8_t i);
 
175
 
 
176
int avro_enum_set(avro_datum_t datum, const int symbol_value);
 
177
int avro_enum_set_name(avro_datum_t datum, const char *symbol_name);
 
178
int avro_fixed_set(avro_datum_t datum, const char *bytes, const int64_t size);
 
179
int avro_givefixed_set(avro_datum_t datum, const char *bytes,
 
180
                       const int64_t size,
 
181
                       avro_free_func_t free);
 
182
 
 
183
int avro_record_set(avro_datum_t record, const char *field_name,
 
184
                    avro_datum_t value);
 
185
 
 
186
/*
 
187
 * A helper macro that sets the value of the given field of a record.
 
188
 */
 
189
 
 
190
#define avro_record_set_field_value(rc, rec, typ, fname, ...)   \
 
191
        do {                                                    \
 
192
                avro_datum_t  field = NULL;                     \
 
193
                (rc) = avro_record_get((rec), (fname), &field); \
 
194
                if (rc) break;                                  \
 
195
                (rc) = avro_##typ##_set(field, __VA_ARGS__);    \
 
196
        } while (0)
 
197
 
 
198
int avro_map_set(avro_datum_t map, const char *key,
 
199
                 avro_datum_t value);
 
200
int avro_array_append_datum(avro_datum_t array_datum,
 
201
                            avro_datum_t datum);
 
202
 
 
203
/*
 
204
 * This function selects the active branch of a union value, and can be
 
205
 * safely called on an existing union to change the current branch.  If
 
206
 * the branch changes, we'll automatically construct a new avro_datum_t
 
207
 * for the new branch's schema type.  If the desired branch is already
 
208
 * the active branch of the union, we'll leave the existing datum
 
209
 * instance as-is.  The branch datum will be placed into the "branch"
 
210
 * parameter, regardless of whether we have to create a new datum
 
211
 * instance or not.
 
212
 */
 
213
 
 
214
int avro_union_set_discriminant(avro_datum_t unionp,
 
215
                                int discriminant,
 
216
                                avro_datum_t *branch);
 
217
 
 
218
/**
 
219
 * Resets a datum instance.  For arrays and maps, this frees all
 
220
 * elements and clears the container.  For records and unions, this
 
221
 * recursively resets any child datum instances.
 
222
 */
 
223
 
 
224
int
 
225
avro_datum_reset(avro_datum_t value);
 
226
 
 
227
/* reference counting */
 
228
avro_datum_t avro_datum_incref(avro_datum_t value);
 
229
void avro_datum_decref(avro_datum_t value);
 
230
 
 
231
void avro_datum_print(avro_datum_t value, FILE * fp);
 
232
 
 
233
int avro_datum_equal(avro_datum_t a, avro_datum_t b);
 
234
 
 
235
/*
 
236
 * Returns a string containing the JSON encoding of an Avro value.  You
 
237
 * must free this string when you're done with it, using the standard
 
238
 * free() function.  (*Not* using the custom Avro allocator.)
 
239
 */
 
240
 
 
241
int avro_datum_to_json(const avro_datum_t datum,
 
242
                       int one_line, char **json_str);
 
243
 
 
244
 
 
245
int avro_schema_datum_validate(avro_schema_t
 
246
                               expected_schema, avro_datum_t datum);
 
247
 
 
248
/*
 
249
 * An avro_value_t implementation for avro_datum_t objects.
 
250
 */
 
251
 
 
252
avro_value_iface_t *
 
253
avro_datum_class(void);
 
254
 
 
255
/*
 
256
 * Creates a new avro_value_t instance for the given datum.
 
257
 */
 
258
 
 
259
int
 
260
avro_datum_as_value(avro_value_t *value, avro_datum_t src);
 
261
 
 
262
 
 
263
CLOSE_EXTERN
 
264
#endif