//genMethods "-g" "-t" "int*" "-d" "basic" "-y" "display_intptr" "-w" "free_intptr" "-m" "mark_intptr" 
#include <stk.h>
#include <LINK/stkWrapper/LINK_STk.h>

#include <LINK/stkWrapper/Wrapper.h>
#include <LINK/stkWrapper/Register.h>

void display_intptr(SCM, SCM, int);
void mark_intptr(SCM);
void free_intptr(SCM);

typedef int* _int_;

#define _int_DATA(x) ((int**) (EXTDATA(x)))
#define _int_IS_DATA(x) (TYPEP(x, tc_CXXwrapper) && \
		TYPEP(STk_slot_ref(x,val_scm), tc_CXXwrapper) && \
		(EXTID(STk_slot_ref(x,val_scm)) == Wrapper<int* >::type))
#define _int_ISNT_DATA(x) (!IS_DATA(x))

#define _int_CXX_DATA(x) (*(int**) (EXTDATA(VAL(x))))
#define _int_CXX_IS_DATA(x) (INSTANCEP(x) && \
		TYPEP(STk_slot_ref(x,val_scm), tc_CXXwrapper) && \
		(EXTID(STk_slot_ref(x,val_scm)) == Wrapper<int* >::type))
#define _int_CXX_ISNT_DATA(x) (!CXX_IS_DATA(x))


void display_intptr(SCM c, SCM port, int mode)
{
        ostrstream oss;
        if (*_int_DATA(c))
        oss << **_int_DATA(c);
        oss << ends;
        char *buffer = oss.str();
        Puts(buffer, FILEPTR(port));
        delete buffer;
}

void mark_intptr(SCM p)
{
        //cout << "mark_intptr" << endl;
        Register<int>::markEntry(*_int_DATA(p));
}

void free_intptr(SCM p)
{
        //cout << "free_intptr" << endl;
        if (!EXTSTATICP(p)) {
                delete _int_DATA(p);
        }
}

PRIMITIVE MakeIntCmd(SCM num)
{
        Tcl_HashEntry *p;
        SCM objptr;
        int *ap, **app;
        int not_found;

        if (NINTEGERP(num))
                Err("<integer> expected", num);
        ap = new int(INTEGER(num));
        //Register<int>::createEntry(ap);
        Register<int>::createEntry(Wrapper<int>::type,
                                       Wrapper<int>::name, ap);
        app = new int *(ap);
        return STk_make_CXXwrapper(Wrapper<int*>::type,
                        Wrapper<int*>::name, (void *) app, LINK_DYNAMIC);
}




// define your new methods above 
// and register them using STk_add_new_cpp_primitive() below

void genExtra_int_Methods() 
{
        STk_add_new_cpp_primitive("make-int", tc_subr_1,
                                        (PRIMITIVE (*)(...))MakeIntCmd);
}
