summaryrefslogtreecommitdiffstats
path: root/dcoppython/shell/marshaller.cpp
blob: 1aaebc38c16583074ee06126da9cedb4cb3d739f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/***************************************************************************
 *   Copyright (C) 2003 by Julian Rockey                                   *
 *   linux@jrockey.com                                                     *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "marshaller.h"

#include "pcop.h"
#include "importedmodules.h"

#include <tqdatastream.h>

#include <tqrect.h>
#include <tqfont.h>
#include <tqcolor.h>
#include <tqpointarray.h>
#include <tqdatetime.h>
#include <dcopref.h>

#include <kurl.h>

#if PY_VERSION_HEX < 0x02050000
typedef int Py_ssize_t;
#endif

namespace PythonDCOP {

#include "marshal_funcs.h"

  Marshaller::Marshaller()
  {
    initFuncs();
  }

  Marshaller::~Marshaller()
  {
  }

  bool Marshaller::marsh_private(const PCOPType &type,
                                 PyObject *obj,
                                 TQDataStream *str) const
  {

    TQString ty = type.type();

    if (ty=="TQStringList")
      return marshalList(PCOPType("TQString"), obj, str);
    if (ty=="QCStringList")
      return marshalList(PCOPType("TQCString"), obj, str);
    if (ty=="TQValueList" && type.leftType())
      return marshalList(*type.leftType(), obj, str);
    if (ty=="TQMap" && type.leftType() && type.rightType())
      return marshalDict(*type.leftType(), *type.rightType(), obj, str);

    if (!m_marsh_funcs.contains(ty)) return false;
    return m_marsh_funcs[ty](obj,str);
  }

  PyObject *Marshaller::demarsh_private(const PCOPType &type,
                                        TQDataStream *str) const
  {
    TQString ty = type.type();

    if (ty=="TQStringList")
      return demarshalList(PCOPType("TQString"), str);
    if (ty=="QCStringList")
      return demarshalList(PCOPType("TQCString"), str);
    if (ty=="TQValueList" && type.leftType())
      return demarshalList(*type.leftType(), str);
    if (ty=="TQMap" && type.leftType() && type.rightType())
      return demarshalDict(*type.leftType(), *type.rightType(), str);

    if (!m_demarsh_funcs.contains(ty)) {
      Py_INCREF(Py_None);
      return Py_None;
    }

    PyObject *result = m_demarsh_funcs[ty](str);
    if (!result) {
      Py_INCREF(Py_None);
      return Py_None;
    }

    return result;
  }

  bool Marshaller::marshalList(const PCOPType &list_type,
                               PyObject *obj,
                               TQDataStream *str) const {
    if (!PyList_Check(obj)) return false;

    int count = PyList_Size(obj);

    for(int c=0;c<count;c++)
      if (!list_type.isMarshallable( PyList_GetItem(obj,c) ) )
        return false;

    if (str) {
      (*str) << (Q_INT32)count;
      for(int c=0; c<count; c++)
        list_type.marshal( PyList_GetItem(obj,c), *str );
    }

    return true;
  }

  PyObject *Marshaller::demarshalList(const PCOPType &list_type,
                                      TQDataStream *str) const {
    Q_UINT32 count;
    (*str) >> count;

    PyObject *obj = PyList_New(count);
    for(Q_UINT32 c=0;c<count;c++) {
      PyList_SetItem(obj, c, list_type.demarshal(*str));
    }
    return obj;
  }

  bool Marshaller::marshalDict(const PCOPType &key_type,
                               const PCOPType &value_type,
                               PyObject *obj,
                               TQDataStream *str) const {
    if (!PyDict_Check(obj)) return false;


    Py_ssize_t c=0;
    PyObject *key, *val;
    while (PyDict_Next(obj, &c, &key, &val)==1)
      if (!key_type.isMarshallable(key) ||
          !value_type.isMarshallable(val))
        return false;

    if (str) {
      Q_INT32 count = (Q_INT32)PyDict_Size(obj);
      (*str) << count;
      c=0;
      while (PyDict_Next(obj, &c, &key, &val)==1) {
        key_type.marshal(key,*str);
        value_type.marshal(val,*str);
      }
    }
    return true;
  }

  PyObject *Marshaller::demarshalDict(const PCOPType &key_type,
                                      const PCOPType &value_type,
                                      TQDataStream *str) const {
    PyObject *obj = PyDict_New();
    Q_INT32 count;
    (*str) >> count;
    for(Q_INT32 c=0;c<count;c++) {
      PyObject *key = key_type.demarshal(*str);
      PyObject *value = value_type.demarshal(*str);
      PyDict_SetItem(obj,key,value);
    }
    return obj;
  }


  Marshaller *Marshaller::m_instance = new Marshaller;


}