#include <PyBinner.h>
#include <ObserverToCallback.h>
#include <ColorAttribute.h>
#include <snprintf.h>

// ****************************************************************************
// Module: PyBinner
//
// Purpose: 
//   Bin particles
//
// Note:       Autogenerated by xml2python. Do not modify by hand!
//
// Programmer: xml2python
// Creation:   Thu Mar 30 12:05:26 PDT 2006
//
// ****************************************************************************

//
// This struct contains the Python type information and a Binner.
//
struct BinnerObject
{
    PyObject_HEAD
    Binner *data;
    bool owns;
};

//
// Internal prototypes
//
static PyObject *NewBinner(int);

std::string
PyBinner_ToString(const Binner *atts, const char *prefix)
{
    std::string str; 
    char tmpStr[1000]; 

    SNPRINTF(tmpStr, 1000, "%sdim1 = %d\n", prefix, atts->GetDim1());
    str += tmpStr;
    SNPRINTF(tmpStr, 1000, "%sdim2 = %d\n", prefix, atts->GetDim2());
    str += tmpStr;
    SNPRINTF(tmpStr, 1000, "%sdim3 = %d\n", prefix, atts->GetDim3());
    str += tmpStr;
    return str;
}

static PyObject *
Binner_Notify(PyObject *self, PyObject *args)
{
    BinnerObject *obj = (BinnerObject *)self;
    obj->data->Notify();
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
Binner_SetDim1(PyObject *self, PyObject *args)
{
    BinnerObject *obj = (BinnerObject *)self;

    int ival;
    if(!PyArg_ParseTuple(args, "i", &ival))
        return NULL;

    // Set the dim1 in the object.
    obj->data->SetDim1(ival);

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
Binner_GetDim1(PyObject *self, PyObject *args)
{
    BinnerObject *obj = (BinnerObject *)self;
    PyObject *retval = PyInt_FromLong(long(obj->data->GetDim1()));
    return retval;
}

static PyObject *
Binner_SetDim2(PyObject *self, PyObject *args)
{
    BinnerObject *obj = (BinnerObject *)self;

    int ival;
    if(!PyArg_ParseTuple(args, "i", &ival))
        return NULL;

    // Set the dim2 in the object.
    obj->data->SetDim2(ival);

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
Binner_GetDim2(PyObject *self, PyObject *args)
{
    BinnerObject *obj = (BinnerObject *)self;
    PyObject *retval = PyInt_FromLong(long(obj->data->GetDim2()));
    return retval;
}

static PyObject *
Binner_SetDim3(PyObject *self, PyObject *args)
{
    BinnerObject *obj = (BinnerObject *)self;

    int ival;
    if(!PyArg_ParseTuple(args, "i", &ival))
        return NULL;

    // Set the dim3 in the object.
    obj->data->SetDim3(ival);

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
Binner_GetDim3(PyObject *self, PyObject *args)
{
    BinnerObject *obj = (BinnerObject *)self;
    PyObject *retval = PyInt_FromLong(long(obj->data->GetDim3()));
    return retval;
}



static struct PyMethodDef Binner_methods[] = {
    {"Notify", Binner_Notify, METH_VARARGS},
    {"SetDim1", Binner_SetDim1, METH_VARARGS},
    {"GetDim1", Binner_GetDim1, METH_VARARGS},
    {"SetDim2", Binner_SetDim2, METH_VARARGS},
    {"GetDim2", Binner_GetDim2, METH_VARARGS},
    {"SetDim3", Binner_SetDim3, METH_VARARGS},
    {"GetDim3", Binner_GetDim3, METH_VARARGS},
    {NULL, NULL}
};

//
// Type functions
//

static void
Binner_dealloc(PyObject *v)
{
   BinnerObject *obj = (BinnerObject *)v;
   if(obj->owns)
       delete obj->data;
}

static int
Binner_compare(PyObject *v, PyObject *w)
{
    Binner *a = ((BinnerObject *)v)->data;
    Binner *b = ((BinnerObject *)w)->data;
    return (*a == *b) ? 0 : -1;
}

static PyObject *
Binner_getattr(PyObject *self, char *name)
{
    if(strcmp(name, "dim1") == 0)
        return Binner_GetDim1(self, NULL);
    if(strcmp(name, "dim2") == 0)
        return Binner_GetDim2(self, NULL);
    if(strcmp(name, "dim3") == 0)
        return Binner_GetDim3(self, NULL);

    return Py_FindMethod(Binner_methods, self, name);
}

static int
Binner_setattr(PyObject *self, char *name, PyObject *args)
{
    // Create a tuple to contain the arguments since all of the Set
    // functions expect a tuple.
    PyObject *tuple = PyTuple_New(1);
    PyTuple_SET_ITEM(tuple, 0, args);
    Py_INCREF(args);
    bool retval = false;

    if(strcmp(name, "dim1") == 0)
        retval = (Binner_SetDim1(self, tuple) != NULL);
    else if(strcmp(name, "dim2") == 0)
        retval = (Binner_SetDim2(self, tuple) != NULL);
    else if(strcmp(name, "dim3") == 0)
        retval = (Binner_SetDim3(self, tuple) != NULL);

    Py_DECREF(tuple);
    return retval ? 0 : -1;
}

static int
Binner_print(PyObject *v, FILE *fp, int flags)
{
    BinnerObject *obj = (BinnerObject *)v;
    fprintf(fp, "%s", PyBinner_ToString(obj->data, "").c_str());
    return 0;
}

PyObject *
Binner_str(PyObject *v)
{
    BinnerObject *obj = (BinnerObject *)v;
    return PyString_FromString(PyBinner_ToString(obj->data,"").c_str());
}

//
// The doc string for the class.
//
static char *Binner_Purpose = "Bin particles";

//
// The type description structure
//
static PyTypeObject BinnerType =
{
    //
    // Type header
    //
    PyObject_HEAD_INIT(&PyType_Type)
    0,                                   // ob_size
    "Binner",                    // tp_name
    sizeof(BinnerObject),        // tp_basicsize
    0,                                   // tp_itemsize
    //
    // Standard methods
    //
    (destructor)Binner_dealloc,  // tp_dealloc
    (printfunc)Binner_print,     // tp_print
    (getattrfunc)Binner_getattr, // tp_getattr
    (setattrfunc)Binner_setattr, // tp_setattr
    (cmpfunc)Binner_compare,     // tp_compare
    (reprfunc)0,                         // tp_repr
    //
    // Type categories
    //
    0,                                   // tp_as_number
    0,                                   // tp_as_sequence
    0,                                   // tp_as_mapping
    //
    // More methods
    //
    0,                                   // tp_hash
    0,                                   // tp_call
    (reprfunc)Binner_str,        // tp_str
    0,                                   // tp_getattro
    0,                                   // tp_setattro
    0,                                   // tp_as_buffer
    Py_TPFLAGS_CHECKTYPES,               // tp_flags
    Binner_Purpose,              // tp_doc
    0,                                   // tp_traverse
    0,                                   // tp_clear
    0,                                   // tp_richcompare
    0                                    // tp_weaklistoffset
};

//
// Helper functions for object allocation.
//

static Binner *defaultAtts = 0;
static Binner *currentAtts = 0;

static PyObject *
NewBinner(int useCurrent)
{
    BinnerObject *newObject;
    newObject = PyObject_NEW(BinnerObject, &BinnerType);
    if(newObject == NULL)
        return NULL;
    if(useCurrent && currentAtts != 0)
        newObject->data = new Binner(*currentAtts);
    else if(defaultAtts != 0)
        newObject->data = new Binner(*defaultAtts);
    else
        newObject->data = new Binner;
    newObject->owns = true;
    return (PyObject *)newObject;
}

static PyObject *
WrapBinner(const Binner *attr)
{
    BinnerObject *newObject;
    newObject = PyObject_NEW(BinnerObject, &BinnerType);
    if(newObject == NULL)
        return NULL;
    newObject->data = (Binner *)attr;
    newObject->owns = false;
    return (PyObject *)newObject;
}

///////////////////////////////////////////////////////////////////////////////
//
// Interface that is exposed to the VisIt module.
//
///////////////////////////////////////////////////////////////////////////////

PyObject *
Binner_new(PyObject *self, PyObject *args)
{
    int useCurrent = 0;
    if (!PyArg_ParseTuple(args, "i", &useCurrent))
    {
        if (!PyArg_ParseTuple(args, ""))
            return NULL;
        else
            PyErr_Clear();
    }

    return (PyObject *)NewBinner(useCurrent);
}

//
// Plugin method table. These methods are added to the visitmodule's methods.
//
static PyMethodDef BinnerMethods[] = {
    {"Binner", Binner_new, METH_VARARGS},
    {NULL,      NULL}        /* Sentinel */
};

static Observer *BinnerObserver = 0;

std::string
PyBinner_GetLogString()
{
    std::string s("Binner = Binner()\n");
    if(currentAtts != 0)
        s += PyBinner_ToString(currentAtts, "Binner.");
    return s;
}

static void
PyBinner_CallLogRoutine(Subject *subj, void *data)
{
    Binner *atts = (Binner *)subj;
    typedef void (*logCallback)(const std::string &);
    logCallback cb = (logCallback)data;

    if(cb != 0)
    {
        std::string s("Binner = Binner()\n");
        s += PyBinner_ToString(currentAtts, "Binner.");
        cb(s);
    }
}

void
PyBinner_StartUp(Binner *subj, void *data)
{
    if(subj == 0)
        return;

    currentAtts = subj;
    PyBinner_SetDefaults(subj);

    //
    // Create the observer that will be notified when the attributes change.
    //
    if(BinnerObserver == 0)
    {
        BinnerObserver = new ObserverToCallback(subj,
            PyBinner_CallLogRoutine, (void *)data);
    }

}

void
PyBinner_CloseDown()
{
    delete defaultAtts;
    defaultAtts = 0;
    delete BinnerObserver;
    BinnerObserver = 0;
}

PyMethodDef *
PyBinner_GetMethodTable(int *nMethods)
{
    *nMethods = 1;
    return BinnerMethods;
}

bool
PyBinner_Check(PyObject *obj)
{
    return (obj->ob_type == &BinnerType);
}

Binner *
PyBinner_FromPyObject(PyObject *obj)
{
    BinnerObject *obj2 = (BinnerObject *)obj;
    return obj2->data;
}

PyObject *
PyBinner_NewPyObject()
{
    return NewBinner(0);
}

PyObject *
PyBinner_WrapPyObject(const Binner *attr)
{
    return WrapBinner(attr);
}

void
PyBinner_SetDefaults(const Binner *atts)
{
    if(defaultAtts)
        delete defaultAtts;

    defaultAtts = new Binner(*atts);
}

