ElementWrapper.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 ROCKETCOREPYTHONELEMENTWRAPPER_H
  28. #define ROCKETCOREPYTHONELEMENTWRAPPER_H
  29. namespace Rocket {
  30. namespace Core {
  31. namespace Python {
  32. /**
  33. Python Wrapper class for Elements
  34. This class maintains is a standard boost wrapper class that
  35. the self pointer for an element and provides virtual
  36. overrides for the hook methods.
  37. @author Lloyd Weehuizen
  38. */
  39. template <typename T>
  40. class ElementWrapper : public T
  41. {
  42. public:
  43. ElementWrapper( PyObject* self, const char* tag ) : self( self ), T( tag )
  44. {
  45. // Force remove reference, to reduce the count to 0 as python has instanced
  46. // this object. If the call originally came from C++ (PyBRElementInstancer)
  47. // then it will add its own reference
  48. T::RemoveReference();
  49. // If the C++ reference count is not 0 at this point, it means additional references have been added
  50. // during the classes constructor. We have to propogate these references into python.
  51. for (int i = 0; i < this->T::GetReferenceCount(); i++)
  52. Py_INCREF(self);
  53. }
  54. virtual ~ElementWrapper() {}
  55. /// Return the python script object associated with this element
  56. virtual void* GetScriptObject() const { return self; }
  57. // Propogate add ref's into python
  58. virtual void AddReference()
  59. {
  60. Py_INCREF( self );
  61. T::AddReference();
  62. }
  63. // Propogate remove ref's into python
  64. virtual void RemoveReference()
  65. {
  66. T::RemoveReference();
  67. Py_DECREF( self );
  68. }
  69. virtual void OnReferenceDeactivate()
  70. {
  71. // Ignore reference deactivated
  72. }
  73. private:
  74. // Python representation of this object
  75. PyObject* self;
  76. };
  77. }
  78. }
  79. }
  80. #endif