All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_stream.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2018 Aerospike, Inc.
3  *
4  * Portions may be licensed to Aerospike, Inc. under one or more contributor
5  * license agreements.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8  * use this file except in compliance with the License. You may obtain a copy of
9  * the License at 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, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations under
15  * the License.
16  */
17 
18 #pragma once
19 
20 #include <aerospike/as_std.h>
21 #include <aerospike/as_util.h>
22 #include <aerospike/as_val.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 /******************************************************************************
29  * MACROS
30  *****************************************************************************/
31 
32 #define AS_STREAM_END ((void *) 0)
33 
34 /******************************************************************************
35  * TYPES
36  *****************************************************************************/
37 
38 struct as_stream_hooks_s;
39 
40 /**
41  * Stream Status Codes
42  */
43 typedef enum as_stream_status_e {
47 
48 /**
49  * Stream Interface
50  *
51  * To use the stream interface, you will need to create an instance
52  * via one of the implementations.
53  *
54  * @ingroup aerospike_t
55  */
56 typedef struct as_stream_s {
57 
58  /**
59  * Specifies whether the cf_free() can be used
60  * on this stream.
61  */
62  bool free;
63 
64  /**
65  * Context data for the stream.
66  */
67  void * data;
68 
69  /**
70  * Hooks for the stream
71  */
72  const struct as_stream_hooks_s * hooks;
73 
74 } as_stream;
75 
76 /**
77  * Stream Hooks
78  *
79  * An implementation of `as_rec` should provide implementations for each
80  * of the hooks.
81  */
82 typedef struct as_stream_hooks_s {
83 
84  /**
85  * Destroy the stream.
86  */
87  int (* destroy)(as_stream * stream);
88 
89  /**
90  * Read the next value from the stream.
91  */
92  as_val * (* read)(const as_stream * stream);
93 
94  /**
95  * Write a value to the stream.
96  */
97  as_stream_status (* write)(const as_stream * stream, as_val * value);
98 
100 
101 /**
102  * Wrapper functions to ensure each CF allocation-related function call has a unique line.
103  */
104 AS_EXTERN void *as_stream_malloc(size_t size);
105 AS_EXTERN void as_stream_free(void *ptr);
106 
107 /******************************************************************************
108  * INSTANCE FUNCTIONS
109  *****************************************************************************/
110 
111 /**
112  * Initializes a stack allocated as_stream for a given source and hooks.
113  *
114  * @param stream The stream to initialize.
115  * @param data The source feeding the stream
116  * @param hooks The hooks that interface with the source
117  *
118  * @return On success, the initialized stream. Otherwise NULL.
119  *
120  * @relatesalso as_stream
121  */
122 static inline as_stream * as_stream_init(as_stream * stream, void * data, const as_stream_hooks * hooks)
123 {
124  if ( !stream ) return stream;
125 
126  stream->free = false;
127  stream->data = data;
128  stream->hooks = hooks;
129  return stream;
130 }
131 
132 /**
133  * Creates a new heap allocated as_stream for a given source and hooks.
134  *
135  * @param data The source feeding the stream
136  * @param hooks The hooks that interface with the source
137  *
138  * @return On success, a new stream. Otherwise NULL.
139  *
140  * @relatesalso as_stream
141  */
142 static inline as_stream * as_stream_new(void * data, const as_stream_hooks * hooks)
143 {
144  as_stream * stream = (as_stream *) as_stream_malloc(sizeof(as_stream));
145  if ( !stream ) return stream;
146 
147  stream->free = true;
148  stream->data = data;
149  stream->hooks = hooks;
150  return stream;
151 }
152 
153 /**
154  * Destroy the as_stream and associated resources.
155  *
156  * @param stream The stream to destroy.
157  *
158  * @return 0 on success, otherwise 1.
159  *
160  * @relatesalso as_stream
161  */
162 static inline void as_stream_destroy(as_stream * stream)
163 {
164  as_util_hook(destroy, 1, stream);
165  if ( stream && stream->free ) {
166  as_stream_free(stream);
167  }
168 }
169 
170 /******************************************************************************
171  * VALUE FUNCTIONS
172  *****************************************************************************/
173 
174 /**
175  * Get the source for the stream
176  *
177  * @param stream The stream to get the source from
178  *
179  * @return pointer to the source of the stream
180  *
181  * @relatesalso as_stream
182  */
183 static inline void * as_stream_source(const as_stream * stream)
184 {
185  return (stream ? stream->data : NULL);
186 }
187 
188 /**
189  * Reads a value from the stream
190  *
191  * @param stream The stream to be read.
192  *
193  * @return the element read from the stream or STREAM_END
194  *
195  * @relatesalso as_stream
196  */
197 static inline as_val * as_stream_read(const as_stream * stream)
198 {
199  return as_util_hook(read, NULL, stream);
200 }
201 
202 /**
203  * Is the stream readable? Tests whether the stream has a read function.
204  *
205  * @param stream The stream to test.
206  *
207  * @return true if the stream can be read from
208  *
209  * @relatesalso as_stream
210  */
211 static inline bool as_stream_readable(const as_stream * stream)
212 {
213  return stream != NULL && stream->hooks != NULL && stream->hooks->read;
214 }
215 
216 /**
217  * Write a value to the stream
218  *
219  * @param stream The stream to write to.
220  * @param value The element to write to the stream.
221  *
222  * @return AS_STREAM_OK on success, otherwise is failure.
223  *
224  * @relatesalso as_stream
225  */
226 static inline as_stream_status as_stream_write(const as_stream * stream, as_val * value)
227 {
228  return as_util_hook(write, AS_STREAM_ERR, stream, value);
229 }
230 
231 
232 /**
233  * Is the stream writable? Tests whether the stream has a write function.
234  *
235  * @param stream The stream to test.
236  *
237  * @return true if the stream can be written to.
238  *
239  * @relatesalso as_stream
240  */
241 static inline bool as_stream_writable(const as_stream * stream)
242 {
243  return stream != NULL && stream->hooks != NULL && stream->hooks->write;
244 }
245 
246 #ifdef __cplusplus
247 } // end extern "C"
248 #endif
static as_stream * as_stream_init(as_stream *stream, void *data, const as_stream_hooks *hooks)
Definition: as_stream.h:122
AS_EXTERN void as_stream_free(void *ptr)
bool free
Definition: as_stream.h:62
AS_EXTERN void * as_stream_malloc(size_t size)
#define as_util_hook(hook, default, object,...)
Definition: as_util.h:34
static as_stream * as_stream_new(void *data, const as_stream_hooks *hooks)
Definition: as_stream.h:142
Definition: as_val.h:61
as_stream_status
Definition: as_stream.h:43
static as_stream_status as_stream_write(const as_stream *stream, as_val *value)
Definition: as_stream.h:226
#define AS_EXTERN
Definition: as_std.h:25
void * data
Definition: as_stream.h:67
static as_val * as_stream_read(const as_stream *stream)
Definition: as_stream.h:197
static bool as_stream_writable(const as_stream *stream)
Definition: as_stream.h:241
static void * as_stream_source(const as_stream *stream)
Definition: as_stream.h:183
const struct as_stream_hooks_s * hooks
Definition: as_stream.h:72
static bool as_stream_readable(const as_stream *stream)
Definition: as_stream.h:211
uint8_t data[0]
Definition: as_proto.h:38
static void as_stream_destroy(as_stream *stream)
Definition: as_stream.h:162