
#include <stdlib.h>

#include "iterator.h"

struct s_Iterator
{
    void *data;

    IteratorNextDelegate    next;
    IteratorValueDelegate   value;
    IteratorDestroyDelegate destroy;
};

Iterator *
iterator_new (void *data, IteratorNextDelegate next, 
        IteratorValueDelegate value, IteratorDestroyDelegate destroy)
{
    Iterator *iter = malloc (sizeof (Iterator));

    iter->data = data;
    iter->next = next;
    iter->value = value;
    iter->destroy = destroy;

    return (iter);
}

IteratorStatus
iterator_next (Iterator *iter)
{
    return (iter->next (iter->data));
}

void *
iterator_value (Iterator *iter)
{
    return (iter->value (iter->data));
}

void
iterator_destroy (void *ptr)
{
    Iterator *iter = (Iterator *) ptr;

    if (iter->destroy != NULL && iter->data != NULL)
    {
        iter->destroy (iter->data);
    }

    free (iter);
}
