All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
as_iterator.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2014 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_util.h>
21 #include <aerospike/as_val.h>
22 
23 #include <stdbool.h>
24 
25 /******************************************************************************
26  * TYPES
27  ******************************************************************************/
28 
29 struct as_iterator_hooks_s;
30 
31 /**
32  * Iterator Object
33  */
34 typedef struct as_iterator_s {
35 
36  /**
37  * @private
38  * If TRUE, then free this instance.
39  */
40  bool free;
41 
42  /**
43  * Data for the iterator.
44  */
45  void * data;
46 
47  /**
48  * Hooks for subtypes of as_iterator.
49  */
50  const struct as_iterator_hooks_s * hooks;
51 
52 } as_iterator;
53 
54 /**
55  * Iterator Function Hooks
56  */
57 typedef struct as_iterator_hooks_s {
58 
59  /**
60  * Releases the subtype of as_iterator.
61  */
62  bool (* destroy)(as_iterator *);
63 
64  /**
65  * Tests whether there is another element in the iterator.
66  */
67  bool (* has_next)(const as_iterator *);
68 
69  /**
70  * Read the next value.
71  */
72  const as_val * (* next)(as_iterator *);
73 
75 
76 /******************************************************************************
77  * INSTANCE FUNCTIONS
78  ******************************************************************************/
79 
80 /**
81  * Initialize a stack allocated iterator.
82  */
83 as_iterator * as_iterator_init(as_iterator * iterator, bool free, void * data, const as_iterator_hooks * hooks);
84 
85 /**
86  * Destroys the iterator and releasing associated resources.
87  */
88 void as_iterator_destroy(as_iterator * iterator);
89 
90 /******************************************************************************
91  * VALUE FUNCTIONS
92  ******************************************************************************/
93 
94 /**
95  * Tests if there are more values available in the iterator.
96  *
97  * @param iterator The iterator to be tested.
98  *
99  * @return true if there are more values, otherwise false.
100  */
101 static inline bool as_iterator_has_next(const as_iterator * iterator)
102 {
103  return as_util_hook(has_next, false, iterator);
104 }
105 
106 /**
107  * Attempts to get the next value from the iterator.
108  * This will return the next value, and iterate past the value.
109  *
110  * @param iterator The iterator to get the next value from.
111  *
112  * @return the next value available in the iterator.
113  */
114 static inline const as_val * as_iterator_next(as_iterator * iterator)
115 {
116  return as_util_hook(next, NULL, iterator);
117 }
void * data
Definition: as_iterator.h:45
Definition: as_val.h:51
as_iterator * as_iterator_init(as_iterator *iterator, bool free, void *data, const as_iterator_hooks *hooks)
static bool as_iterator_has_next(const as_iterator *iterator)
Definition: as_iterator.h:101
#define as_util_hook(hook, default, object, args...)
Definition: as_util.h:32
const struct as_iterator_hooks_s * hooks
Definition: as_iterator.h:50
void as_iterator_destroy(as_iterator *iterator)
static const as_val * as_iterator_next(as_iterator *iterator)
Definition: as_iterator.h:114