Loading...
Searching...
No Matches
as_stream.h
Go to the documentation of this file.
1/*
2 * Copyright 2008-2024 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
25extern "C" {
26#endif
27
28/******************************************************************************
29 * MACROS
30 *****************************************************************************/
31
32#define AS_STREAM_END ((void *) 0)
33
34/******************************************************************************
35 * TYPES
36 *****************************************************************************/
37
38struct as_stream_hooks_s;
39
40/**
41 * Stream Status Codes
42 */
43typedef 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 */
56typedef 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 */
82typedef 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 */
104AS_EXTERN void *as_stream_malloc(size_t size);
105AS_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 */
122static 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 */
142static 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 * @relatesalso as_stream
159 */
160static inline void as_stream_destroy(as_stream * stream)
161{
162 as_util_hook(destroy, 1, stream);
163 if ( stream && stream->free ) {
164 as_stream_free(stream);
165 }
166}
167
168/******************************************************************************
169 * VALUE FUNCTIONS
170 *****************************************************************************/
171
172/**
173 * Get the source for the stream
174 *
175 * @param stream The stream to get the source from
176 *
177 * @return pointer to the source of the stream
178 *
179 * @relatesalso as_stream
180 */
181static inline void * as_stream_source(const as_stream * stream)
182{
183 return (stream ? stream->data : NULL);
184}
185
186/**
187 * Reads a value from the stream
188 *
189 * @param stream The stream to be read.
190 *
191 * @return the element read from the stream or STREAM_END
192 *
193 * @relatesalso as_stream
194 */
195static inline as_val * as_stream_read(const as_stream * stream)
196{
197 return as_util_hook(read, NULL, stream);
198}
199
200/**
201 * Is the stream readable? Tests whether the stream has a read function.
202 *
203 * @param stream The stream to test.
204 *
205 * @return true if the stream can be read from
206 *
207 * @relatesalso as_stream
208 */
209static inline bool as_stream_readable(const as_stream * stream)
210{
211 return stream != NULL && stream->hooks != NULL && stream->hooks->read;
212}
213
214/**
215 * Write a value to the stream
216 *
217 * @param stream The stream to write to.
218 * @param value The element to write to the stream.
219 *
220 * @return AS_STREAM_OK on success, otherwise is failure.
221 *
222 * @relatesalso as_stream
223 */
224static inline as_stream_status as_stream_write(const as_stream * stream, as_val * value)
225{
226 return as_util_hook(write, AS_STREAM_ERR, stream, value);
227}
228
229
230/**
231 * Is the stream writable? Tests whether the stream has a write function.
232 *
233 * @param stream The stream to test.
234 *
235 * @return true if the stream can be written to.
236 *
237 * @relatesalso as_stream
238 */
239static inline bool as_stream_writable(const as_stream * stream)
240{
241 return stream != NULL && stream->hooks != NULL && stream->hooks->write;
242}
243
244#ifdef __cplusplus
245} // end extern "C"
246#endif
uint8_t data[0]
Definition as_proto.h:3
#define AS_EXTERN
Definition as_std.h:25
as_stream_status
Definition as_stream.h:43
@ AS_STREAM_ERR
Definition as_stream.h:45
@ AS_STREAM_OK
Definition as_stream.h:44
AS_EXTERN void * as_stream_malloc(size_t size)
AS_EXTERN void as_stream_free(void *ptr)
#define as_util_hook(hook, default, object,...)
Definition as_util.h:34
static bool as_stream_readable(const as_stream *stream)
Definition as_stream.h:209
static as_stream_status as_stream_write(const as_stream *stream, as_val *value)
Definition as_stream.h:224
static void as_stream_destroy(as_stream *stream)
Definition as_stream.h:160
static void * as_stream_source(const as_stream *stream)
Definition as_stream.h:181
bool free
Definition as_stream.h:62
const struct as_stream_hooks_s * hooks
Definition as_stream.h:72
static as_stream * as_stream_new(void *data, const as_stream_hooks *hooks)
Definition as_stream.h:142
void * data
Definition as_stream.h:67
static as_stream * as_stream_init(as_stream *stream, void *data, const as_stream_hooks *hooks)
Definition as_stream.h:122
static bool as_stream_writable(const as_stream *stream)
Definition as_stream.h:239
static as_val * as_stream_read(const as_stream *stream)
Definition as_stream.h:195