Debian Codegraph @

c/src/core/object/iterator.c

raw
1 /*
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 */
21
22 #include <proton/object.h>
23
24 #include "core/memory.h"
25
26 #include <stdlib.h>
27 #include <assert.h>
28
29 struct pn_iterator_t {
30 pn_iterator_next_t next;
31 size_t size;
32 void *state;
33 };
34
35 static void pn_iterator_initialize(void *object)
36 {
37 pn_iterator_t *it = (pn_iterator_t *) object;
38 it->next = NULL;
39 it->size = 0;
40 it->state = NULL;
41 }
42
43 static void pn_iterator_finalize(void *object)
44 {
45 pn_iterator_t *it = (pn_iterator_t *) object;
46 pni_mem_subdeallocate(pn_class(object), object, it->state);
47 }
48
49 #define CID_pn_iterator CID_pn_object
50 #define pn_iterator_hashcode NULL
51 #define pn_iterator_compare NULL
52 #define pn_iterator_inspect NULL
53
54 pn_iterator_t *pn_iterator()
55 {
56 static const pn_class_t clazz = PN_CLASS(pn_iterator);
57 pn_iterator_t *it = (pn_iterator_t *) pn_class_new(&clazz, sizeof(pn_iterator_t));
58 return it;
59 }
60
61 void *pn_iterator_start(pn_iterator_t *iterator, pn_iterator_next_t next,
62 size_t size) {
63 assert(iterator);
64 assert(next);
65 iterator->next = next;
66 if (iterator->size < size) {
67 iterator->state = pni_mem_subreallocate(pn_class(iterator), iterator, iterator->state, size);
68 }
69 return iterator->state;
70 }
71
72 void *pn_iterator_next(pn_iterator_t *iterator) {
73 assert(iterator);
74 if (iterator->next) {
75 void *result = iterator->next(iterator->state);
76 if (!result) iterator->next = NULL;
77 return result;
78 } else {
79 return NULL;
80 }
81 }