// This file contains the C++ code necessary marshal and demarshal
// all the _simple_ types that dcoppython can understand.
// "Simple" types are types that do not contain other types.
// So, int and TQString are simple types; TQDict, TQMap and TQStringList are not.
// This file is processed by gen_marshal_code.py to produce a header
// file, which is included by marshaller.cpp
//
// Marshalling:
//   The code in the "marshal" section has the following variables available:
//     PyObject * obj; // the object to marshal
//     TQDataStream *str; // the stream to marshal to
//   The function should return true if the object can be marshalled.
//   str may be NULL. If so, the function should ignore the actually marshalling
//   and merely return true or false, depending on whether the object _could_
//   be marshalled.
//
// Demarshalling:
//   The code in the "demarshal" section has the following variables available:
//     TQDataStream *str; // the stream to demarshal from
//   The function should return a PyObject* which is a reference to the
//   newly created object. Ownership of the reference should be passed to
//   the caller. The function can return null if for any reason it
//   could not demarshal.

type: void
%% marshall
{
  Q_UNUSED(str); // stop warnings
  Q_UNUSED(obj);
  return true;
}
%% demarshal
{
  Q_UNUSED(str); // stop warnings
  Py_INCREF(Py_None);
  return Py_None;
}

type: bool
%doc as int b (1=True, 2=False)
%doc info Any Python object is converted to bool by the standard Python truth test.
%% from_pyobj
  {
    *ok=true;
    return PyObject_IsTrue(obj);
  }
%% to_pyobj
  {
    return PyLong_FromLong(val ? 1 : 0);
  }
%% marshal
  {
    if (str) {
      bool ok;
      bool b = fromPyObject_bool(obj,&ok);
      (*str) << (TQ_INT8)b;
    }
    return true;
  }
%% demarshal
  {
    TQ_INT8 i;
    (*str) >> i;
    return toPyObject_bool(i!=0);
  }
%%

type:int
%doc as int i
%% marshal
  {
    if (!PyLong_Check(obj)) return false;
    if (str) {
      (*str) << (TQ_INT32)PyLong_AsLong(obj);
    }
    return true;
  }
%% demarshal
  {
    TQ_INT32 i;
    (*str) >> i;
    return PyLong_FromLong( (long)i );
  }
%%

type:uint
%doc as int i
%% marshal
  {
    if (!PyLong_Check(obj)) return false;
    if (str) {
      (*str) << (TQ_INT32)PyLong_AsLong(obj);
    }
    return true;
  }
%% demarshal
  {
    TQ_INT32 i;
    (*str) >> i;
    return PyLong_FromLong( (long)i );
  }
%%

type:double
%doc as float i
%% marshal
  {
    if (!PyFloat_Check(obj)) return false;
    if (str) {
      (*str) << PyFloat_AsDouble(obj);
    }
    return true;
  }
%% demarshal
  {
    double d;
    (*str) >> d;
    return PyFloat_FromDouble(d);
  }
%%

type:uchar
%doc as str c
%doc as int c
%% marshal
  {
    if (PyBytes_Check(obj) && PyBytes_Size(obj)==1) {
      if (str) {
        char *c = PyBytes_AS_STRING(obj);
        (*str) << (*c);
      }
      return true;
    }

    if (PyLong_Check(obj)) {
      if (str) {
        long l = PyLong_AsLong(obj);
        TQ_UINT8 c = (TQ_UINT8)(l & 0xff);
        (*str) << c;
      }
      return true;
    }

    return false;
  }
%%demarshal
  {
    TQ_UINT8 c;
    (*str) >> c;
    return PyBytes_FromStringAndSize((const char *)(&c),1);
  }
%%

type:char
%doc as int c
%% marshal
  {
    if (PyLong_Check(obj)) {
      if (str) {
        long l = PyLong_AsLong(obj);
        TQ_INT8 c = (TQ_INT8)(l & 0xff);
        (*str) << c;
      }
      return true;
    }

    return false;
  }
%%demarshal
  {
    TQ_INT8 c;
    (*str) >> c;
    return PyLong_FromLong((long)c);
  }
%%


type:TQByteArray
%% marshal
  {
    Py_buffer view;
    if (PyObject_GetBuffer(obj, &view, PyBUF_WRITABLE|PyBUF_SIMPLE))
    {
      return false;
    }

    int size = view.len;
    if (size < 0)
    {
      PyBuffer_Release(&view);
      return false;
    }

    if (str)
    {
      TQByteArray a;
      a.setRawData((const char*)view.buf, size);
      (*str) << a;
      a.resetRawData((const char*)view.buf, size);
    }

    PyBuffer_Release(&view);
    return true;
  }
%% demarshal
  {
    // Demarshal to a writable buffer object
    TQByteArray a;
    (*str) >> a;

    uint size = a.size();
    char *data = a.data();

    // Demarshal to a writable buffer object
    return PyBytes_FromStringAndSize(data, size);
  }
%%

type:TQString
%doc as str s
%% marshal
  {
    if (!PyBytes_Check(obj)) return false;
    if (str) {
      TQString s = TQString::fromUtf8(PyBytes_AS_STRING(obj));
      (*str) << s;
    }
    return true;
  }
%% demarshal
  {
    TQString s;
    (*str) >> s;
    return PyBytes_FromString( s.utf8().data() );
  }
%%

type:TQCString
%doc as str s
%% marshal
  {
    if (!PyBytes_Check(obj)) return false;
    if (str) {
      TQCString s( PyBytes_AS_STRING(obj) );
      (*str) << s;
    }
    return true;
  }
%% demarshal
  {
    TQCString s;
    (*str) >> s;
    return PyBytes_FromString( s.data() );
  }
%%

type:TQRect
%doc as ( (int x1, int y1), (int x2, int y2) )
%doc as ( int x1, int y1, int x2, int y2 )
%% from_pyobj
{
  int xp1, yp1, xp2, yp2;
  TQRect r;
  *ok=false;
  if (!PyTuple_Check(obj)) return r;
  if (!PyArg_ParseTuple(obj, (char*)"(ii)(ii)", &xp1, &yp1, &xp2, &yp2) &&
      !PyArg_ParseTuple(obj, (char*)"iiii", &xp1, &yp1, &xp2, &yp2))
    return r;
  r.setCoords( xp1, yp1, xp2, yp2 );
  *ok=true;
   return r;
}
%% to_pyobj
{
  int xp1, yp1, xp2, yp2;
  val.coords(&xp1,&yp1,&xp2,&yp2);
  return Py_BuildValue((char*)"(ii)(ii)", xp1, yp1, xp2, yp2);
}

%% marshal
%defaultcode
%% demarshal
%defaultcode
%%

type:TQPoint
%doc as (int x, int y)
%% from_pyobj
{
  int x,y;
  TQPoint p;
  *ok=false;
  if (!PyTuple_Check(obj)) return p;
  if (!PyArg_ParseTuple(obj, (char*)"ii", &x, &y))
    return p;
  p.setX(x);
  p.setY(y);
  *ok=true;
  return p;
}
%% to_pyobj
{
  return Py_BuildValue((char*)"ii", val.x(), val.y() );
}
%% marshall
%defaultcode
%% demarshall
%defaultcode
%%

type:TQSize
%doc as (int width, int height)
%% from_pyobj
{
  int w,h;
  TQSize sz;
  *ok=false;
  if (!PyTuple_Check(obj)) return sz;
  if (!PyArg_ParseTuple(obj, (char*)"ii", &w, &h))
    return sz;
  sz.setWidth(w);
  sz.setHeight(h);
  *ok=true;
  return sz;
}
%% to_pyobj
{
  return Py_BuildValue((char*)"ii", val.width(), val.height() );
}
%% marshall
%defaultcode
%% demarshall
%defaultcode
%%

type:TQColor
%doc as (int red, int green, int blue)
%% from_pyobj
{
  int r,g,b;
  TQColor c;
  *ok=false;
  if (!PyTuple_Check(obj)) return c;
  if (!PyArg_ParseTuple(obj, (char*)"iii", &r, &g, &b))
    return c;
  c.setRgb(r,g,b);
  *ok=true;
  return c;
}
%% to_pyobj
{
  return Py_BuildValue((char*)"iii", val.red(), val.green(), val.blue() );
}
%% marshall
%defaultcode
%% demarshall
%defaultcode
%%

type:TQPointArray
%doc as [ (int x, int y), (int x, int y), (int x, int y), ... ]
%% from_pyobj
{
  *ok=false;
  if (!PyList_Check(obj)) return TQPointArray();
  int size = PyList_Size(obj);
  TQPointArray pa(size);
  for(int c=0;c<size;c++) {
    TQPoint p = fromPyObject_TQPoint(PyList_GetItem(obj,c), ok);
    if (!*ok) return false;
    pa.setPoint(c,p);
  }
  *ok=true;
  return pa;
}
%% to_pyobj
{
  PyObject *obj = PyList_New(val.size());
  if (!obj) return NULL;
  for(uint c=0;c<val.size();c++) {
    PyObject *tuple = toPyObject_TQPoint( val.point(c) );
    PyList_SetItem(obj, c, tuple);
//    Py_DECREF(tuple);
  }
  return obj;
}
%% marshall
%defaultcode
%% demarshall
%defaultcode
%%

type:TQDate
%doc as (int year, int month, int day)
%% from_pyobj
{
  *ok=false;
  if (!PyTuple_Check(obj)) return TQDate();
  int y,m,d;
  if (!PyArg_ParseTuple(obj, (char*)"iii", &y, &m, &d))
    return TQDate();
  *ok=true;
  return TQDate(y,m,d);
}
%% to_pyobj
{
  return Py_BuildValue((char*)"iii", val.year(), val.month(), val.day() );
}
%% marshal
%defaultcode
%% demarshal
%defaultcode
%%

type:TQTime
%doc as (int hour, int minute, int second=0, int millisecond=0)
%% from_pyobj
{
  *ok=false;
  if (!PyTuple_Check(obj)) return TQTime();
  int h,m,s=0,ms=0;
  if (!PyArg_ParseTuple(obj, (char*)"ii|ii", &h, &m, &s, &ms))
    return TQTime();
  *ok=true;
  return TQTime(h,m,s,ms);
}
%% to_pyobj
{
  return Py_BuildValue((char*)"iiii", val.hour(), val.minute(), val.second(), val.msec() );
}
%% marshal
%defaultcode
%% demarshal
%defaultcode
%%

type:TQDateTime
%doc as ( (int year, int month, int day), (int hour, int minute, int second=0, int millsecond=0) )
%doc as long unixDate
%% from_pyobj
{
  *ok=false;

  if (PyLong_Check(obj)) {
    *ok=true;
    TQDateTime dt;
    dt.setTime_t( (uint)PyLong_AsLong(obj) );
    return dt;
  }

  PyObject *date_tuple, *time_tuple;
  if (PyArg_ParseTuple(obj, (char*)"OO", &date_tuple, &time_tuple)) {
    TQDateTime dt;
    dt.setTime( fromPyObject_TQTime(time_tuple, ok) );
    if (*ok) dt.setDate( fromPyObject_TQDate(date_tuple, ok) );
    return dt;
  }

  return TQDateTime();
}
%% to_pyobj
{
  PyObject *date_tuple = toPyObject_TQDate( val.date() );
  PyObject *time_tuple = toPyObject_TQTime( val.time() );
  return Py_BuildValue((char*)"OO", date_tuple, time_tuple );
}
%% marshal
%defaultcode
%% demarshal
%defaultcode
%%

type:KURL
%doc as str url
%% from_pyobj
{
  *ok=false;
  if (!PyBytes_Check(obj)) return KURL();
  *ok=true;
  return KURL( TQString::fromUtf8(PyBytes_AS_STRING(obj)) );
}
%% to_pyobj
{
  return PyBytes_FromString( val.prettyURL().utf8().data() );
}
%% marshal
%defaultcode
%% demarshal
%defaultcode
%%

type:DCOPRef
%% from_pyobj
{
  if (PyType_Check(obj) &&
      PyObject_HasAttrString(obj, (char*)"appname") &&
      PyObject_HasAttrString(obj, (char*)"name")) {
    PyObject *appname = PyObject_GetAttrString(obj, (char*)"appname");
    PyObject *name = PyObject_GetAttrString(obj, (char*)"name");
    if (PyBytes_Check(appname) && PyBytes_Check(name)) {
      char *c_appname = PyBytes_AS_STRING(appname);
      char *c_name = PyBytes_AS_STRING(name);
      DCOPRef ref;
      ref.setRef(TQCString(c_appname), TQCString(c_name) );
      Py_DECREF(appname);
      Py_DECREF(name);
      *ok=true;
      return ref;
    }
    Py_DECREF(appname);
    Py_DECREF(name);
  }
  *ok=false;
  return DCOPRef();
}
%% to_pyobj
{
  if (val.isNull()) {
    Py_INCREF(Py_None);
    return Py_None;
  }
  return ImportedModules::instance()->createDCOPObject(val.app(), val.object() );
}
%% marshal
%defaultcode
%% demarshal
%defaultcode
%%


// type:DCOPRef
// %doc as (str app, str obj, str type)
// %doc as (str app, str obj)
// %% from_pyobj
// {
//   *ok=false;
//   char *dcopref_app=NULL, *dcopref_obj=NULL, *dcopref_type=NULL;
//   if (PyArg_ParseTuple(obj,(char*)"ss|s", &dcopref_app, &dcopref_obj, &dcopref_type)) {
//     *ok=true;
//     if (dcopref_type) {
//       DCOPRef dr(TQCString(dcopref_app), TQCString(dcopref_obj), TQCString(dcopref_type));
//       return dr;
//     }
//     DCOPRef dr(TQCString(dcopref_app), TQCString(dcopref_obj));
//     return dr;
//   }
//   return DCOPRef();
// }
// %% to_pyobj
// {
//   return Py_BuildValue((char*)"sss", val.app().data(), val.obj().data(), val.type().data() );
// }
// %% marshal
// %defaultcode
// %% demarshal
// %defaultcode
// %%

// type:TQFont
// %% marshal
// %constructor ("default")
// %dict-map family:string,rawName:string
// %% demarshal
// %dict-map
// %%
