Permalink
Browse files

Add an abstract iterator class

  • Loading branch information...
tagoh committed Oct 26, 2012
1 parent 5addf31 commit fc35c4a2cea8989a8b40038338ae86c0a249d782
@@ -68,6 +68,7 @@ liblangtag_gobject_built_private_headers = \
lt-extlang.gir.h \
lt-grandfathered-db.gir.h \
lt-grandfathered.gir.h \
+ lt-iter.gir.h \
lt-lang-db.gir.h \
lt-lang.gir.h \
lt-list.gir.h \
@@ -93,6 +94,7 @@ liblangtag_gobject_built_sources = \
lt-extlang.gir.c \
lt-grandfathered-db.gir.c \
lt-grandfathered.gir.c \
+ lt-iter.gir.c \
lt-lang-db.gir.c \
lt-lang.gir.c \
lt-list.gir.c \
View
@@ -51,6 +51,7 @@ liblangtag_public_headers = \
lt-extlang-db.h \
lt-grandfathered.h \
lt-grandfathered-db.h \
+ lt-iter.h \
lt-lang.h \
lt-lang-db.h \
lt-list.h \
@@ -72,6 +73,7 @@ liblangtag_private_headers = \
lt-extension-private.h \
lt-extlang-private.h \
lt-grandfathered-private.h \
+ lt-iter-private.h \
lt-lang-private.h \
lt-mem.h \
lt-messages.h \
@@ -103,6 +105,7 @@ liblangtag_sources = \
lt-extlang-db.c \
lt-grandfathered.c \
lt-grandfathered-db.c \
+ lt-iter.c \
lt-lang.c \
lt-lang-db.c \
lt-list.c \
View
@@ -18,6 +18,7 @@
#include <liblangtag/lt-database.h>
#include <liblangtag/lt-ext-module.h>
#include <liblangtag/lt-extension.h>
+#include <liblangtag/lt-iter.h>
#include <liblangtag/lt-list.h>
#include <liblangtag/lt-string.h>
#include <liblangtag/lt-tag.h>
@@ -0,0 +1,36 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * lt-iter-private.h
+ * Copyright (C) 2011-2012 Akira TAGOH
+ *
+ * Authors:
+ * Akira TAGOH <akira@tagoh.org>
+ *
+ * You may distribute under the terms of either the GNU
+ * Lesser General Public License or the Mozilla Public
+ * License, as specified in the README file.
+ */
+#ifndef __LT_ITER_PRIVATE_H__
+#define __LT_ITER_PRIVATE_H__
+
+#include "lt-mem.h"
+#include "lt-iter.h"
+
+LT_BEGIN_DECLS
+
+typedef lt_iter_t * (* lt_iter_init_func_t) (lt_iter_tmpl_t *tmpl);
+typedef void (* lt_iter_fini_func_t) (lt_iter_t *iter);
+typedef lt_bool_t (* lt_iter_next_func_t) (lt_iter_t *iter,
+ lt_pointer_t *key,
+ lt_pointer_t *value);
+
+struct _lt_iter_tmpl_t {
+ lt_mem_t parent;
+ lt_iter_init_func_t init;
+ lt_iter_fini_func_t fini;
+ lt_iter_next_func_t next;
+};
+
+LT_END_DECLS
+
+#endif /* __LT_ITER_PRIVATE_H__ */
View
@@ -0,0 +1,62 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * lt-iter.c
+ * Copyright (C) 2011-2012 Akira TAGOH
+ *
+ * Authors:
+ * Akira TAGOH <akira@tagoh.org>
+ *
+ * You may distribute under the terms of either the GNU
+ * Lesser General Public License or the Mozilla Public
+ * License, as specified in the README file.
+ */
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include "lt-messages.h"
+#include "lt-iter-private.h"
+
+/*< private >*/
+
+/*< protected >*/
+
+/*< public >*/
+lt_iter_t *
+lt_iter_init(lt_iter_tmpl_t *tmpl)
+{
+ lt_iter_t *retval;
+
+ lt_return_val_if_fail (tmpl != NULL, NULL);
+ lt_return_val_if_fail (tmpl->init != NULL, NULL);
+
+ retval = tmpl->init(tmpl);
+ if (retval)
+ retval->target = tmpl;
+
+ return retval;
+}
+
+void
+lt_iter_finish(lt_iter_t *iter)
+{
+ lt_return_if_fail (iter != NULL);
+ lt_return_if_fail (iter->target != NULL);
+
+ iter->target->fini(iter);
+
+ free(iter);
+}
+
+lt_bool_t
+lt_iter_next(lt_iter_t *iter,
+ lt_pointer_t *key,
+ lt_pointer_t *val)
+{
+ lt_return_val_if_fail (iter != NULL, FALSE);
+ lt_return_val_if_fail (iter->target != NULL, FALSE);
+ lt_return_val_if_fail (iter->target->next != NULL, FALSE);
+
+ return iter->target->next(iter, key, val);
+}
View
@@ -0,0 +1,39 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * lt-iter.h
+ * Copyright (C) 2011-2012 Akira TAGOH
+ *
+ * Authors:
+ * Akira TAGOH <akira@tagoh.org>
+ *
+ * You may distribute under the terms of either the GNU
+ * Lesser General Public License or the Mozilla Public
+ * License, as specified in the README file.
+ */
+#if !defined (__LANGTAG_H__INSIDE) && !defined (__LANGTAG_COMPILATION)
+#error "Only <liblangtag/langtag.h> can be included directly."
+#endif
+
+#ifndef __LT_ITER_H__
+#define __LT_ITER_H__
+
+#include <liblangtag/lt-macros.h>
+
+LT_BEGIN_DECLS
+
+typedef struct _lt_iter_tmpl_t lt_iter_tmpl_t;
+typedef struct _lt_iter_t lt_iter_t;
+
+struct _lt_iter_t {
+ lt_iter_tmpl_t *target;
+};
+
+lt_iter_t *lt_iter_init (lt_iter_tmpl_t *tmpl);
+void lt_iter_finish(lt_iter_t *iter);
+lt_bool_t lt_iter_next (lt_iter_t *iter,
+ lt_pointer_t *key,
+ lt_pointer_t *val);
+
+LT_END_DECLS
+
+#endif /* __LT_ITER_H__ */
Oops, something went wrong.

0 comments on commit fc35c4a

Please sign in to comment.