Utilities.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #ifndef ROCKETCOREPYTHONUTILITIES_H
  28. #define ROCKETCOREPYTHONUTILITIES_H
  29. #include "../Variant.h"
  30. #include "Header.h"
  31. #include "Python.h"
  32. namespace Rocket {
  33. namespace Core {
  34. namespace Python {
  35. class ROCKETCOREPYTHON_API Utilities
  36. {
  37. public:
  38. /// Is the specific method name callable
  39. static bool IsCallable(PyObject* self, const char* method_name);
  40. /// Call a python function
  41. /// @param[in] object Python Dictionary or Object
  42. /// @param[in] method_name function or method name to call
  43. /// @param[in] args Tuple of arguments
  44. /// @returns The python result
  45. template <typename R>
  46. static R Call(PyObject* object, const char* method_name, const python::tuple& args)
  47. {
  48. PyObject* result = PythonCall(object, method_name, args);
  49. python::converter::return_from_python<R> converter;
  50. return converter(result);
  51. }
  52. /// Call a python function
  53. /// @param[in] object Python Dictionary or Object to lookup the method in
  54. /// @param[in] method_name function or method name to call
  55. /// @param[in] args Tuple of arguments
  56. /// @returns True on success
  57. static bool Call(PyObject* object, const char* method_name, const python::tuple& args);
  58. /// Creates a boost python object from an existing C++ object pointer
  59. /// @param[in] object The C++ object to convert
  60. /// @returns A boost::python::object referencing the same object
  61. template< typename T >
  62. static inline python::object MakeObject(T* object)
  63. {
  64. // Constructs a boost::object using a result_converter to get a pointer to the existing object
  65. typename python::return_by_value::apply< T* >::type result_converter;
  66. return python::object(python::handle<>(result_converter(object)));
  67. }
  68. /// Creates a new python object (copy) from a C++ object
  69. /// @param[in] object The C++ object to convert
  70. /// @returns A boost::python::object that contains a copy of the object
  71. template< typename T >
  72. static inline python::object MakeObject(const T& object)
  73. {
  74. // Constructs a boost::object copy of the C++ object
  75. return python::object(object);
  76. }
  77. /// Print any pending exceptions to stderr
  78. /// @param[in] clear_error Clear the error flag
  79. static void PrintError(bool clear_error = false);
  80. /// Converts a python object into an Rocket variant.
  81. /// @param[out] variant The variant to convert the object into.
  82. /// @param[in] object The python object to convert.
  83. /// @return True if the conversion was successful, false otherwise.
  84. static bool ConvertToVariant(Variant& variant, PyObject* object);
  85. private:
  86. static PyObject* PythonCall(PyObject* object, const char* method_name, const python::tuple& args );
  87. };
  88. // Template Specialisation for converting from a PyObject*
  89. template<>
  90. inline python::object Utilities::MakeObject< PyObject >(PyObject* object)
  91. {
  92. return python::object(python::handle<>(python::borrowed(object)));
  93. }
  94. }
  95. }
  96. }
  97. #endif