core: iterator: re-design b_iterator as a b_object interface
This commit is contained in:
@@ -1,26 +1,63 @@
|
||||
#ifndef BLUELIB_CORE_ITERATOR_H_
|
||||
#define BLUELIB_CORE_ITERATOR_H_
|
||||
#ifndef BLUE_CORE_ITERATOR_H_
|
||||
#define BLUE_CORE_ITERATOR_H_
|
||||
|
||||
#include <blue/core/status.h>
|
||||
#include <blue/core/macros.h>
|
||||
#include <blue/core/misc.h>
|
||||
#include <blue/core/status.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
struct b_iterator;
|
||||
B_DECLS_BEGIN;
|
||||
|
||||
typedef struct b_iterator_ops {
|
||||
b_status (*it_close)(struct b_iterator *);
|
||||
bool (*it_next)(struct b_iterator *);
|
||||
b_status (*it_erase)(struct b_iterator *);
|
||||
bool (*it_is_valid)(const struct b_iterator *);
|
||||
} b_iterator_ops;
|
||||
#define b_foreach(type, var, iterator) \
|
||||
for (type var = (type)b_iterator_value(iterator).v_int; var != NULL; \
|
||||
b_iterator_next(iterator), \
|
||||
var = (type)b_iterator_value(iterator).v_int)
|
||||
|
||||
typedef struct b_iterator {
|
||||
const b_iterator_ops *it_ops;
|
||||
} b_iterator;
|
||||
#define B_ITERATOR_VALUE_INT(v) ((b_iterator_value) {.v_int = (v)})
|
||||
#define B_ITERATOR_VALUE_PTR(v) ((b_iterator_value) {.v_ptr = (v)})
|
||||
#define B_ITERATOR_VALUE_CPTR(v) ((const b_iterator_value) {.v_cptr = (v)})
|
||||
#define B_ITERATOR_VALUE_NULL ((b_iterator_value) {})
|
||||
#define B_ITERATOR_VALUE_IS_NULL(v) ((v)->v_ptr == NULL)
|
||||
|
||||
BLUE_API b_status b_iterator_close(b_iterator *it);
|
||||
BLUE_API bool b_iterator_next(b_iterator *it);
|
||||
#define B_TYPE_ITERATOR (b_iterator_get_type())
|
||||
#define B_TYPE_ITERABLE (b_iterable_get_type())
|
||||
|
||||
typedef union b_iterator_value {
|
||||
uintptr_t v_int;
|
||||
void *v_ptr;
|
||||
const void *v_cptr;
|
||||
} b_iterator_value;
|
||||
|
||||
B_DECLARE_TYPE(b_iterator);
|
||||
B_DECLARE_TYPE(b_iterable);
|
||||
|
||||
B_TYPE_CLASS_DECLARATION_BEGIN(b_iterator)
|
||||
b_status (*it_move_next)(const b_iterator *);
|
||||
b_status (*it_erase)(b_iterator *);
|
||||
b_iterator_value (*it_get_value)(b_iterator *);
|
||||
const b_iterator_value (*it_get_cvalue)(const b_iterator *);
|
||||
B_TYPE_CLASS_DECLARATION_END(b_iterator)
|
||||
|
||||
B_TYPE_CLASS_DECLARATION_BEGIN(b_iterable)
|
||||
b_iterator *(*it_begin)(b_iterable *);
|
||||
const b_iterator *(*it_cbegin)(const b_iterable *);
|
||||
B_TYPE_CLASS_DECLARATION_END(b_iterable)
|
||||
|
||||
BLUE_API b_type b_iterator_get_type(void);
|
||||
BLUE_API b_type b_iterable_get_type(void);
|
||||
|
||||
BLUE_API b_iterator *b_iterator_begin(b_iterable *it);
|
||||
BLUE_API const b_iterator *b_iterator_cbegin(const b_iterable *it);
|
||||
|
||||
BLUE_API b_status b_iterator_get_status(const b_iterator *it);
|
||||
BLUE_API b_status b_iterator_set_status(const b_iterator *it, b_status status);
|
||||
|
||||
BLUE_API b_status b_iterator_next(const b_iterator *it);
|
||||
BLUE_API b_iterator_value b_iterator_get_value(b_iterator *it);
|
||||
BLUE_API const b_iterator_value b_iterator_get_cvalue(const b_iterator *it);
|
||||
BLUE_API b_status b_iterator_erase(b_iterator *it);
|
||||
BLUE_API bool b_iterator_is_valid(const b_iterator *it);
|
||||
BLUE_API b_status b_iterator_is_valid(const b_iterator *it);
|
||||
|
||||
B_DECLS_END;
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user