Converters.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "../../../Include/Rocket/Core/Variant.h"
  29. #include "../../../Include/Rocket/Core/Dictionary.h"
  30. #include "../../../Include/Rocket/Core/Python/ConverterScriptObject.h"
  31. #include "../../../Include/Rocket/Core/ElementDocument.h"
  32. #include "EventListener.h"
  33. #include <boost/python/suite/indexing/vector_indexing_suite.hpp>
  34. namespace Rocket {
  35. namespace Core {
  36. namespace Python {
  37. // Boost helper class for converting from Variant to a python object
  38. struct VariantConverter
  39. {
  40. VariantConverter()
  41. {
  42. // Register custom Variant to python converter
  43. boost::python::to_python_converter< Variant, VariantConverter >();
  44. boost::python::to_python_converter< Variant*, VariantConverter >();
  45. // Register the python to variant converter
  46. boost::python::converter::registry::push_back(&Convertible, &Construct, boost::python::type_id< Variant >());
  47. }
  48. static PyObject* convert(Variant* variant)
  49. {
  50. if (!variant)
  51. {
  52. Py_INCREF(Py_None);
  53. return Py_None;
  54. }
  55. return convert(*variant);
  56. }
  57. static PyObject* convert(const Variant& variant)
  58. {
  59. PyObject* object = NULL;
  60. switch (variant.GetType())
  61. {
  62. case Variant::STRING:
  63. {
  64. object = PyString_FromString(variant.Get< String >().CString());
  65. }
  66. break;
  67. case Variant::INT:
  68. {
  69. object = PyInt_FromLong(variant.Get< int >());
  70. }
  71. break;
  72. case Variant::WORD:
  73. {
  74. object = PyInt_FromLong(variant.Get< word >());
  75. }
  76. break;
  77. case Variant::FLOAT:
  78. {
  79. object = PyFloat_FromDouble(variant.Get< float >());
  80. }
  81. break;
  82. case Variant::VECTOR2:
  83. {
  84. python::object o(variant.Get< Vector2f >());
  85. object = o.ptr();
  86. Py_INCREF( object );
  87. }
  88. break;
  89. case Variant::SCRIPTINTERFACE:
  90. {
  91. object = (PyObject*)(variant.Get< ScriptInterface* >())->GetScriptObject();
  92. Py_INCREF(object);
  93. }
  94. break;
  95. default:
  96. {
  97. object = Py_None;
  98. Py_INCREF(object);
  99. }
  100. break;
  101. }
  102. return object;
  103. }
  104. static void* Convertible(PyObject* object)
  105. {
  106. if (!PyString_Check(object) && !PyInt_Check(object) && !PyFloat_Check(object))
  107. return 0;
  108. return object;
  109. }
  110. static void Construct(PyObject* object, boost::python::converter::rvalue_from_python_stage1_data* data)
  111. {
  112. void* storage = ((boost::python::converter::rvalue_from_python_storage< Variant >*)data)->storage.bytes;
  113. Variant* variant = new (storage) Variant();
  114. if (PyString_Check(object))
  115. {
  116. variant->Set(String(PyString_AsString(object)));
  117. }
  118. else if (PyInt_Check(object))
  119. {
  120. variant->Set((int)PyInt_AsLong(object));
  121. }
  122. else if (PyFloat_Check(object))
  123. {
  124. variant->Set((float)PyFloat_AsDouble(object));
  125. }
  126. else
  127. {
  128. boost::python::throw_error_already_set();
  129. return;
  130. }
  131. data->convertible = storage;
  132. }
  133. };
  134. // String Converter
  135. struct StringConverter
  136. {
  137. StringConverter()
  138. {
  139. boost::python::to_python_converter< String, StringConverter >();
  140. boost::python::converter::registry::push_back( &Convertible, &Construct, boost::python::type_id< String >() );
  141. }
  142. static PyObject* convert(const String& s)
  143. {
  144. return boost::python::incref(boost::python::object(s.CString()).ptr());
  145. }
  146. static void* Convertible(PyObject* obj_ptr)
  147. {
  148. if (!PyString_Check(obj_ptr))
  149. return 0;
  150. return obj_ptr;
  151. }
  152. static void Construct(PyObject* obj_ptr, boost::python::converter::rvalue_from_python_stage1_data* data)
  153. {
  154. const char* value = PyString_AsString(obj_ptr);
  155. if (value == 0)
  156. boost::python::throw_error_already_set();
  157. void* storage = ((boost::python::converter::rvalue_from_python_storage< String >*)data)->storage.bytes;
  158. new (storage) String(value);
  159. data->convertible = storage;
  160. }
  161. };
  162. // Helper class for converting from python to an event listener
  163. struct EventListenerFromPython
  164. {
  165. EventListenerFromPython()
  166. {
  167. python::converter::registry::insert( &Convert, python::type_id<EventListener>() );
  168. }
  169. static void* Convert( PyObject* object )
  170. {
  171. return new EventListener( object );
  172. }
  173. };
  174. void RegisterPythonConverters()
  175. {
  176. StringConverter();
  177. VariantConverter();
  178. ConverterScriptObject< ScriptInterface >();
  179. EventListenerFromPython();
  180. ConverterScriptObject< Context >();
  181. ConverterScriptObject< Element >();
  182. ConverterScriptObject< ElementDocument >();
  183. }
  184. }
  185. }
  186. }