summaryrefslogtreecommitdiff
path: root/src/wrapper/WrapHelper.h
blob: cd86439e96de09dc4f64587caac531bf7bf6da8b (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//

#ifndef _WrapHelper_H_
#define _WrapHelper_H_

#include "../api.h"
#include "../base/GLMHelper.h"
#include "../base/Exception.h"
#include "../base/ILogSink.h"

#include "../player/BoostPython.h"
#include "../player/Player.h"
#include "../player/TypeRegistry.h"

#include <string>

template<typename T> const T copyObject(const T& v) { return v; }

template <typename ContainerType>
struct to_tuple
{
    static PyObject* convert(ContainerType const& a)
    {
        boost::python::list result;
        typedef typename ContainerType::const_iterator const_iter;
        for(const_iter p=a.begin();p!=a.end();p++) {
            result.append(boost::python::object(*p));
        }
        return boost::python::incref(boost::python::tuple(result).ptr());
    }

    static const PyTypeObject* get_pytype() { return &PyTuple_Type; }
};

template <typename ContainerType>
struct to_list
{
    static PyObject* convert(ContainerType const& a)
    {
        boost::python::list result;
        typedef typename ContainerType::const_iterator const_iter;
        for(const_iter p=a.begin();p!=a.end();p++) {
            result.append(boost::python::object(*p));
        }
        return boost::python::incref(result.ptr());
    }

    static const PyTypeObject* get_pytype() { return &PyList_Type; }
};

template <typename MapType>
struct to_dict
{
    static PyObject* convert(MapType const& a)
    {
        boost::python::dict result;
        typedef typename MapType::const_iterator const_iter;
        for(const_iter p=a.begin();p!=a.end();p++){
                result[p->first] = p->second;
        }
        return boost::python::incref(boost::python::dict(result).ptr());
    }
    static const PyTypeObject* get_pytype() { return &PyDict_Type; }
};

struct default_policy
{
  static bool check_convertibility_per_element() { return false; }

  template <typename ContainerType>
  static bool check_size(boost::type<ContainerType>, std::size_t /*sz*/)
  {
    return true;
  }

  template <typename ContainerType>
  static void assert_size(boost::type<ContainerType>, std::size_t /*sz*/) {}

  template <typename ContainerType>
  static void reserve(ContainerType& a, std::size_t sz) {}
};

struct fixed_size_policy
{
  static bool check_convertibility_per_element() { return true; }

  template <typename ContainerType>
  static bool check_size(boost::type<ContainerType>, std::size_t sz)
  {
    return ContainerType::size() == sz;
  }

  template <typename ContainerType>
  static void assert_size(boost::type<ContainerType>, std::size_t sz)
  {
    if (!check_size(boost::type<ContainerType>(), sz)) {
      PyErr_SetString(PyExc_RuntimeError,
        "Insufficient elements for fixed-size array.");
      boost::python::throw_error_already_set();
    }
  }

  template <typename ContainerType>
  static void reserve(ContainerType& /*a*/, std::size_t sz)
  {
    if (sz > ContainerType::size()) {
      PyErr_SetString(PyExc_RuntimeError,
        "Too many elements for fixed-size array.");
      boost::python::throw_error_already_set();
    }
  }

  template <typename ContainerType, typename ValueType>
  static void set_value(ContainerType& a, std::size_t i, ValueType const& v)
  {
    reserve(a, i+1);
    a[i] = v;
  }
};
   
struct variable_capacity_policy : default_policy
{
  template <typename ContainerType>
  static void reserve(ContainerType& a, std::size_t sz)
  {
    a.reserve(sz);
  }

  template <typename ContainerType, typename ValueType>
  static void set_value(
    ContainerType& a,
    std::size_t
#if !defined(NDEBUG)
    i
#endif
    ,
    ValueType const& v)
  {
    assert(a.size() == i);
    a.push_back(v);
  }
};

struct fixed_capacity_policy : variable_capacity_policy
{
  template <typename ContainerType>
  static bool check_size(boost::type<ContainerType>, std::size_t sz)
  {
    return ContainerType::max_size() >= sz;
  }
};

struct linked_list_policy : default_policy
{
  template <typename ContainerType, typename ValueType>
  static void
  set_value(ContainerType& a, std::size_t /*i*/, ValueType const& v)
  {
    a.push_back(v);
  }
};

struct set_policy : default_policy
{
  template <typename ContainerType, typename ValueType>
  static void
  set_value(ContainerType& a, std::size_t /*i*/, ValueType const& v)
  {
    a.insert(v);
  }
};

template <typename ContainerType, typename ConversionPolicy>
struct from_python_sequence
{
  typedef typename ContainerType::value_type container_element_type;

  from_python_sequence()
  {
    boost::python::converter::registry::push_back(
      &convertible,
      &construct,
      boost::python::type_id<ContainerType>());
  }

  static void* convertible(PyObject* obj_ptr)
  {
    if (!(   PyList_Check(obj_ptr)
          || PyTuple_Check(obj_ptr)
          || PyIter_Check(obj_ptr)
          || PyRange_Check(obj_ptr)
          || (   !PyString_Check(obj_ptr)
              && !PyUnicode_Check(obj_ptr)
              && (   obj_ptr->ob_type == 0
                  || obj_ptr->ob_type->ob_type == 0
                  || obj_ptr->ob_type->ob_type->tp_name == 0
                  || std::strcmp(
                       obj_ptr->ob_type->ob_type->tp_name,
                       "Boost.Python.class") != 0)
              && PyObject_HasAttrString(obj_ptr, "__len__")
              && PyObject_HasAttrString(obj_ptr, "__getitem__")))) return 0;
    boost::python::handle<> obj_iter(
      boost::python::allow_null(PyObject_GetIter(obj_ptr)));
    if (!obj_iter.get()) { // must be convertible to an iterator
      PyErr_Clear();
      return 0;
    }
    if (ConversionPolicy::check_convertibility_per_element()) {
      int obj_size = int(PyObject_Length(obj_ptr));
      if (obj_size < 0) { // must be a measurable sequence
        PyErr_Clear();
        return 0;
      }
      if (!ConversionPolicy::check_size(
        boost::type<ContainerType>(), obj_size)) return 0;
      bool is_range = PyRange_Check(obj_ptr);
      std::size_t i=0;
      if (!all_elements_convertible(obj_iter, is_range, i)) return 0;
      if (!is_range) assert(i == (std::size_t)obj_size);
    }
    return obj_ptr;
  }

  // This loop factored out by Achim Domma to avoid Visual C++
  // Internal Compiler Error.
  static bool
  all_elements_convertible(
    boost::python::handle<>& obj_iter,
    bool is_range,
    std::size_t& i)
  {
    for(;;i++) {
      boost::python::handle<> py_elem_hdl(
        boost::python::allow_null(PyIter_Next(obj_iter.get())));
      if (PyErr_Occurred()) {
        PyErr_Clear();
        return false;
      }
      if (!py_elem_hdl.get()) break; // end of iteration
      boost::python::object py_elem_obj(py_elem_hdl);
      boost::python::extract<container_element_type>
        elem_proxy(py_elem_obj);
      if (!elem_proxy.check()) return false;
      if (is_range) break; // in a range all elements are of the same type
    }
    return true;
  }

  static void construct(
    PyObject* obj_ptr,
    boost::python::converter::rvalue_from_python_stage1_data* data)
  {
    boost::python::handle<> obj_iter(PyObject_GetIter(obj_ptr));
    void* storage = (
      (boost::python::converter::rvalue_from_python_storage<ContainerType>*)
        data)->storage.bytes;
    new (storage) ContainerType();
    data->convertible = storage;
    ContainerType& result = *((ContainerType*)storage);
    std::size_t i=0;
    for(;;i++) {
      boost::python::handle<> py_elem_hdl(
        boost::python::allow_null(PyIter_Next(obj_iter.get())));
      if (PyErr_Occurred()) boost::python::throw_error_already_set();
      if (!py_elem_hdl.get()) break; // end of iteration
      boost::python::object py_elem_obj(py_elem_hdl);
      boost::python::extract<container_element_type> elem_proxy(py_elem_obj);
      ConversionPolicy::set_value(result, i, elem_proxy());
    }
    ConversionPolicy::assert_size(boost::type<ContainerType>(), i);
  }
};

template<class T>
float deprecatedGet(T& node)
{
    throw avg::Exception(AVG_ERR_DEPRECATED, "Attribute has been removed from libavg.");
}

template<class T>
void deprecatedSet(T& node, float d)
{
    throw avg::Exception(AVG_ERR_DEPRECATED, "Attribute has been removed from libavg.");
}

namespace Vec2Helper
{
    int len(const glm::vec2&);
    float getX(const glm::vec2& pt);
    float getY(const glm::vec2& pt);
    void setX(glm::vec2& pt, float val);
    void setY(glm::vec2& pt, float val);
    void checkItemRange(int i);
    float getItem(const glm::vec2& pt, int i);
    void setItem(glm::vec2& pt, int i, float val);
    std::string str(const glm::vec2& pt);
    std::string repr(const glm::vec2& pt);
    long getHash(const glm::vec2& pt);
    glm::vec2 safeGetNormalized(const glm::vec2& pt);
    float getNorm(const glm::vec2& pt);
    float vecAngle(const glm::vec2& pt1, const glm::vec2& pt2);
}

class ConstVec2: public glm::vec2
{
public:
    ConstVec2();
    ConstVec2(const glm::vec2& other);
    glm::vec2 toVec2() const;
    //operator glm::vec2() const;
};

AVG_API void checkEmptyArgs(const boost::python::tuple &args, int numArgs=1);

template<const char * pszType> 
avg::ExportedObjectPtr createExportedObject(const boost::python::tuple &args,
        const boost::python::dict &attrs)
{
    checkEmptyArgs(args);
    return avg::TypeRegistry::get()->createObject(pszType, attrs);
}

template<const char * pszType> 
avg::NodePtr createNode(const boost::python::tuple &args,
        const boost::python::dict &attrs)
{
    checkEmptyArgs(args);
    return avg::Player::get()->createNode(pszType, attrs, args[0]);
};

template <typename T>struct Exception_to_python_exception
{
    static PyObject* convert (const T& ex)
    {
        PyObject *arglist = boost::python::incref(
                Py_BuildValue("(s)", ex.what()));
        return boost::python::incref(
                PyObject_CallObject(PyExc_RuntimeError, arglist));
    }
};


template<typename T> struct ExceptionTranslator
{
public:
    void operator()(const T& ex) const
    {
      PyErr_SetString(m_PyExcept, ex.what());
    }

    ExceptionTranslator(PyObject* py_except): m_PyExcept(py_except)
    {
      boost::python::register_exception_translator<T>(*this);
    }

    ExceptionTranslator(const ExceptionTranslator& other):m_PyExcept(other.m_PyExcept)
    {
    }

  private:

    PyObject* m_PyExcept;

};

template <typename T> void translateException(PyObject* e) {
  ExceptionTranslator<T> my_translator(e);
}

void exportMessages(boost::python::object& nodeClass, const std::string& sClassName);

void addPythonLogger(PyObject * self, PyObject * pyLogger);
void removePythonLogger(PyObject * self, PyObject * pyLogger);

void pytrace(PyObject * self, const avg::category_t& category, const avg::UTF8String& sMsg,
        avg::severity_t severity);

#endif