CR2RE Pipeline Reference Manual 1.6.10
hdrl_iter.c
1/*
2 * This file is part of the HDRL
3 * Copyright (C) 2013 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*-----------------------------------------------------------------------------
25 Includes
26-----------------------------------------------------------------------------*/
27
28#include "hdrl_iter.h"
29#include "hdrl_types.h"
30#include <cpl.h>
31
32/*-----------------------------------------------------------------------------
33 Static
34 -----------------------------------------------------------------------------*/
35
36/*----------------------------------------------------------------------------*/
41/*----------------------------------------------------------------------------*/
42
49struct _hdrl_iter_ {
50 /* returns next value in sequence iterated on (e.g. image, imagelist) */
51 hdrl_iter_next_f * next;
52 /* optional, resets the iteration to the first element */
53 hdrl_iter_reset_f * reset;
54 /* optional, returns length of the iterator */
55 hdrl_iter_length_f * length;
56 /* state destructor */
57 hdrl_free * destructor;
58 /* iterator flags */
59 hdrl_iter_flags flags;
60 /* state structure of iterator */
61 void * state;
62};
63
64/* ---------------------------------------------------------------------------*/
94/* ---------------------------------------------------------------------------*/
95hdrl_iter *
96hdrl_iter_init(hdrl_iter_next_f * next,
97 hdrl_iter_reset_f * reset,
98 hdrl_iter_length_f * length,
99 hdrl_free * destructor,
100 hdrl_iter_flags flags,
101 void * state)
102{
103 /* exactly one each set */
104 hdrl_iter_flags inout = (HDRL_ITER_INPUT | HDRL_ITER_OUTPUT);
105 hdrl_iter_flags retflags = (HDRL_ITER_IMAGE | HDRL_ITER_IMAGELIST);
106 cpl_ensure(((flags & inout) == HDRL_ITER_INPUT) ||
107 ((flags & inout) == HDRL_ITER_OUTPUT),
108 CPL_ERROR_ILLEGAL_INPUT, NULL);
109 cpl_ensure(((flags & retflags) == HDRL_ITER_IMAGE) ||
110 ((flags & retflags) == HDRL_ITER_IMAGELIST),
111 CPL_ERROR_ILLEGAL_INPUT, NULL);
112 cpl_ensure(state, CPL_ERROR_NULL_INPUT, NULL);
113 cpl_ensure(next, CPL_ERROR_NULL_INPUT, NULL);
114 cpl_ensure(flags != 0, CPL_ERROR_NULL_INPUT, NULL);
115 hdrl_iter * it = cpl_malloc(sizeof(*it));
116 it->next = next;
117 it->length = length;
118 it->reset = reset;
119 it->destructor = destructor ? destructor : &cpl_free;
120 it->flags = flags;
121 it->state = state;
122 return it;
123}
124
125/* ---------------------------------------------------------------------------*/
132/* ---------------------------------------------------------------------------*/
133void * hdrl_iter_state(const hdrl_iter * it)
134{
135 cpl_ensure(it, CPL_ERROR_NULL_INPUT, NULL);
136
137 return it->state;
138}
139
140/* ---------------------------------------------------------------------------*/
148/* ---------------------------------------------------------------------------*/
149cpl_boolean hdrl_iter_check(hdrl_iter * it, hdrl_iter_flags flags)
150{
151 cpl_ensure(it, CPL_ERROR_NULL_INPUT, CPL_FALSE);
152
153 return (it->flags & flags) == flags;
154}
155
158/* ---------------------------------------------------------------------------*/
165/* ---------------------------------------------------------------------------*/
166void hdrl_iter_delete(hdrl_iter * it)
167{
168 if (it) {
169 if (it->destructor) {
170 it->destructor(it);
171 }
172 cpl_free(it);
173 }
174}
175
176/* ---------------------------------------------------------------------------*/
182/* ---------------------------------------------------------------------------*/
183void * hdrl_iter_next(hdrl_iter * it)
184{
185 cpl_ensure(it, CPL_ERROR_NULL_INPUT, NULL);
186
187 return it->next(it);
188}
189
190/* ---------------------------------------------------------------------------*/
195/* ---------------------------------------------------------------------------*/
196void hdrl_iter_reset(hdrl_iter * it)
197{
198 if (!it) {
199 cpl_error_set_message(cpl_func, CPL_ERROR_NULL_INPUT,
200 "Iterator Null");
201 } else if (it->reset == NULL) {
202 cpl_error_set_message(cpl_func, CPL_ERROR_UNSUPPORTED_MODE,
203 "Iterator has no reset method");
204 } else {
205 it->reset(it);
206 }
207}
208
209/* ---------------------------------------------------------------------------*/
215/* ---------------------------------------------------------------------------*/
216cpl_size hdrl_iter_length(hdrl_iter * it)
217{
218 if (!it) {
219 cpl_error_set_message(cpl_func, CPL_ERROR_NULL_INPUT,
220 "Iterator Null");
221 return -1;
222 } else if (it->length == NULL) {
223 cpl_error_set_message(cpl_func, CPL_ERROR_UNSUPPORTED_MODE,
224 "Iterator has no length method");
225 return -1;
226 }
227 return it->length(it);
228}
229