package FC_Solve::QueueInC;

use strict;
use warnings;

use Config;

use FC_Solve::InlineWrap (
    C => <<'EOF',
#define FCS_DBM_USE_OFFLOADING_QUEUE

#include "offloading_queue.h"

typedef struct
{
    fcs_offloading_queue_t q;
} QueueInC;

SV* _proto_new(int num_items_per_page, const char * offload_dir_path, long queue_id) {
        QueueInC * s;

        New(42, s, 1, QueueInC);

        fcs_offloading_queue__init(&(s->q), num_items_per_page, strdup(offload_dir_path), queue_id);
        SV*      obj_ref = newSViv(0);
        SV*      obj = newSVrv(obj_ref, "FC_Solve::QueueInC");
        sv_setiv(obj, (IV)s);
        SvREADONLY_on(obj);
        return obj_ref;
}

static GCC_INLINE QueueInC * deref(SV * const obj) {
    return (QueueInC*)SvIV(SvRV(obj));
}

static GCC_INLINE fcs_offloading_queue_t * q(SV * const obj) {
    return &(deref(obj)->q);
}

void insert(SV* obj, int item_i) {
    fcs_offloading_queue_item_t item = (fcs_offloading_queue_item_t)item_i;
    fcs_offloading_queue__insert(q(obj), &item);
}

SV* extract(SV* obj) {
    fcs_offloading_queue_item_t item;

    return (fcs_offloading_queue__extract(q(obj), &item))
    ? newSViv((int)item)
    : &PL_sv_undef;
}

long get_num_inserted(SV* obj) {
    return q(obj)->num_inserted;
}

long get_num_items_in_queue(SV* obj) {
    return q(obj)->num_items_in_queue;
}

long get_num_extracted(SV* obj) {
    return q(obj)->num_extracted;
}

void DESTROY(SV* obj) {
  QueueInC * s = deref(obj);
  free(s->q.offload_dir_path);
  fcs_offloading_queue__destroy(&s->q);
  Safefree(s);
}

EOF
    LIBS => "-L" . $ENV{FCS_PATH},
);

sub new
{
    my ($class, $args) = @_;

    return FC_Solve::QueueInC::_proto_new($args->{num_items_per_page}, $args->{offload_dir_path}, ($args->{queue_id} || 0));
}

1;

