WrapperIter.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #if !defined(BOOST_PP_IS_ITERATING)
  28. # error PyWrapperIter - do not include this file!
  29. #endif
  30. #define N BOOST_PP_ITERATION()
  31. /**
  32. Generic Python Wrapper, using Boost Preprocessor Iteration to Generate Variants
  33. Relies on a Rocket::Core::ReferenceCountable base class for reference counting.
  34. Traps ReferenceActivated/Deactivated calls and makes them behave like
  35. they should for python based objects.
  36. @author Lloyd Weehuizen
  37. */
  38. #define WRAPPER_PARAM(x, n, d) , d
  39. template < typename T BOOST_PP_COMMA_IF(N) BOOST_PP_ENUM_PARAMS_Z(1, N, typename A) >
  40. class Wrapper< T BOOST_PP_COMMA_IF(N) BOOST_PP_ENUM_PARAMS_Z(1, N, A) BOOST_PP_REPEAT_1( BOOST_PP_SUB(BOOST_PP_INC(WRAPPER_MAX_ARGS), N), WRAPPER_PARAM, WrapperNone) > : public T
  41. {
  42. public:
  43. Wrapper(PyObject* self BOOST_PP_COMMA_IF(N) BOOST_PP_ENUM_BINARY_PARAMS_Z(1, N, A, a)) : T(BOOST_PP_ENUM_PARAMS_Z(1, N, a))
  44. {
  45. // Set self to NULL, so we can trap the reference deactivated and not pass it down
  46. this->self = NULL;
  47. // We have to remove the C++ reference count that all C++ objects start with here,
  48. // otherwise if an object is created in python and destroyed by python, the C++ ref count will
  49. // remain 1. The PyWrapperInstancer will increase the refcount again, to ensure a correct refcount
  50. // if the object was created by C++
  51. T::RemoveReference();
  52. // If the C++ reference count is not 0 at this point, it means additional references have been added
  53. // during the classes constructor. We have to propogate these references into python.
  54. for (int i = 0; i < this->T::GetReferenceCount(); i++)
  55. Py_INCREF(self);
  56. // Store self
  57. this->self = self;
  58. }
  59. Wrapper(PyObject* self, const T& other) : T(other)
  60. {
  61. this->self = self;
  62. }
  63. virtual ~Wrapper()
  64. {
  65. // We should only be deleted when python says the refcnt is 0, if we
  66. // are being deleted prematurely, something is wrong!
  67. ROCKET_ASSERTMSG(self->ob_refcnt == 0, "Python object being cleared up prematurely, reference count not 0.");
  68. ROCKET_ASSERT(this->T::GetReferenceCount() == 0);
  69. }
  70. /// Override AddReference so we can push the call through to python
  71. virtual void AddReference()
  72. {
  73. Py_INCREF(self);
  74. T::AddReference();
  75. }
  76. /// Overrride RemoveReference so we can push the call through to python
  77. virtual void RemoveReference()
  78. {
  79. T::RemoveReference();
  80. Py_DECREF(self);
  81. }
  82. virtual int GetReferenceCount()
  83. {
  84. // C++ reference counts are always reflected in the python ref count
  85. return self->ob_refcnt;
  86. }
  87. virtual void OnReferenceDeactivate()
  88. {
  89. // If self is NULL, don't pass the call down, as this is the initial
  90. // T::RemoveReference from the constructor
  91. if (self)
  92. T::OnReferenceDeactivate();
  93. }
  94. // Script object access
  95. virtual void* GetScriptObject() const { return self; }
  96. protected:
  97. PyObject* self;
  98. };
  99. #undef WRAPPER_PARAM