core: iterator: re-design b_iterator as a b_object interface
This commit is contained in:
106
core/iterator.c
106
core/iterator.c
@@ -1,37 +1,97 @@
|
||||
#include <blue/core/iterator.h>
|
||||
|
||||
b_status b_iterator_cleanup(struct b_iterator *it)
|
||||
{
|
||||
if (it->it_ops && it->it_ops->it_close) {
|
||||
return it->it_ops->it_close(it);
|
||||
}
|
||||
/*** PRIVATE DATA *************************************************************/
|
||||
|
||||
struct b_iterator_p {
|
||||
enum b_status it_status;
|
||||
};
|
||||
|
||||
/*** PRIVATE FUNCTIONS ********************************************************/
|
||||
|
||||
static enum b_status iterator_get_status(const struct b_iterator_p *it)
|
||||
{
|
||||
return it->it_status;
|
||||
}
|
||||
|
||||
static enum b_status iterator_set_status(struct b_iterator_p *it, b_status status)
|
||||
{
|
||||
it->it_status = status;
|
||||
return B_SUCCESS;
|
||||
}
|
||||
|
||||
bool b_iterator_next(struct b_iterator *it)
|
||||
{
|
||||
if (it->it_ops && it->it_ops->it_next) {
|
||||
return it->it_ops->it_next(it);
|
||||
}
|
||||
/*** PUBLIC FUNCTIONS *********************************************************/
|
||||
|
||||
return false;
|
||||
b_iterator *b_iterator_begin(b_iterable *it)
|
||||
{
|
||||
B_CLASS_DISPATCH_VIRTUAL_0(b_iterable, B_TYPE_ITERABLE, NULL, it_begin, it);
|
||||
}
|
||||
|
||||
b_status b_iterator_erase(struct b_iterator *it)
|
||||
const b_iterator *b_iterator_cbegin(const b_iterable *it)
|
||||
{
|
||||
if (it->it_ops && it->it_ops->it_erase) {
|
||||
return it->it_ops->it_erase(it);
|
||||
}
|
||||
|
||||
return B_ERR_NOT_SUPPORTED;
|
||||
B_CLASS_DISPATCH_VIRTUAL_0(b_iterable, B_TYPE_ITERABLE, NULL, it_cbegin, it);
|
||||
}
|
||||
|
||||
bool b_iterator_is_valid(const struct b_iterator *it)
|
||||
enum b_status b_iterator_get_status(const b_iterator *it)
|
||||
{
|
||||
if (it->it_ops && it->it_ops->it_is_valid) {
|
||||
return it->it_ops->it_is_valid(it);
|
||||
}
|
||||
|
||||
return false;
|
||||
B_CLASS_DISPATCH_STATIC_0(B_TYPE_ITERATOR, iterator_get_status, it);
|
||||
}
|
||||
|
||||
enum b_status b_iterator_set_status(const b_iterator *it, b_status status)
|
||||
{
|
||||
B_CLASS_DISPATCH_STATIC(B_TYPE_ITERATOR, iterator_set_status, it, status);
|
||||
}
|
||||
|
||||
b_status b_iterator_move_next(b_iterator *it)
|
||||
{
|
||||
B_CLASS_DISPATCH_VIRTUAL_0(
|
||||
b_iterator, B_TYPE_ITERATOR, B_ERR_NOT_SUPPORTED, it_move_next, it);
|
||||
}
|
||||
|
||||
b_iterator_value b_iterator_get_value(b_iterator *it)
|
||||
{
|
||||
B_CLASS_DISPATCH_VIRTUAL_0(
|
||||
b_iterator, B_TYPE_ITERATOR, B_ITERATOR_VALUE_NULL,
|
||||
it_get_value, it);
|
||||
}
|
||||
|
||||
const b_iterator_value b_iterator_get_cvalue(const b_iterator *it)
|
||||
{
|
||||
B_CLASS_DISPATCH_VIRTUAL_0(
|
||||
b_iterator, B_TYPE_ITERATOR, B_ITERATOR_VALUE_NULL,
|
||||
it_get_cvalue, it);
|
||||
}
|
||||
|
||||
b_status b_iterator_erase(b_iterator *it)
|
||||
{
|
||||
B_CLASS_DISPATCH_VIRTUAL_0(
|
||||
b_iterator, B_TYPE_ITERATOR, B_ERR_NOT_SUPPORTED, it_erase, it);
|
||||
}
|
||||
|
||||
/*** CLASS DEFINITION *********************************************************/
|
||||
|
||||
// ---- b_iterator DEFINITION
|
||||
B_TYPE_CLASS_DEFINITION_BEGIN(b_iterator)
|
||||
B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT)
|
||||
B_INTERFACE_ENTRY(to_string) = NULL;
|
||||
B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT)
|
||||
B_TYPE_CLASS_DEFINITION_END(b_iterator)
|
||||
|
||||
B_TYPE_DEFINITION_BEGIN(b_iterator)
|
||||
B_TYPE_FLAGS(B_TYPE_F_ABSTRACT);
|
||||
B_TYPE_ID(0xfd40b67f, 0x7087, 0x40a9, 0x8fd8, 0x8ae27bd58c9e);
|
||||
B_TYPE_CLASS(b_iterator_class);
|
||||
B_TYPE_INSTANCE_PRIVATE(struct b_iterator_p);
|
||||
B_TYPE_DEFINITION_END(b_iterator)
|
||||
|
||||
// ---- b_iterable DEFINITION
|
||||
B_TYPE_CLASS_DEFINITION_BEGIN(b_iterable)
|
||||
B_TYPE_CLASS_INTERFACE_BEGIN(b_object, B_TYPE_OBJECT)
|
||||
B_INTERFACE_ENTRY(to_string) = NULL;
|
||||
B_TYPE_CLASS_INTERFACE_END(b_object, B_TYPE_OBJECT)
|
||||
B_TYPE_CLASS_DEFINITION_END(b_iterable)
|
||||
|
||||
B_TYPE_DEFINITION_BEGIN(b_iterable)
|
||||
B_TYPE_FLAGS(B_TYPE_F_ABSTRACT);
|
||||
B_TYPE_ID(0x4bbabf2d, 0xfc5d, 0x40cc, 0x89fc, 0x164085e47f73);
|
||||
B_TYPE_CLASS(b_iterable_class);
|
||||
B_TYPE_DEFINITION_END(b_iterable)
|
||||
|
||||
Reference in New Issue
Block a user