libRocket
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ElementWrapper.h
Go to the documentation of this file.
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 
28 #ifndef ROCKETCOREPYTHONELEMENTWRAPPER_H
29 #define ROCKETCOREPYTHONELEMENTWRAPPER_H
30 
31 namespace Rocket {
32 namespace Core {
33 namespace Python {
34 
45 template <typename T>
46 class ElementWrapper : public T
47 {
48 public:
49  ElementWrapper( PyObject* self, const char* tag ) : self( self ), T( tag )
50  {
51  // Force remove reference, to reduce the count to 0 as python has instanced
52  // this object. If the call originally came from C++ (PyBRElementInstancer)
53  // then it will add its own reference
54  T::RemoveReference();
55 
56  // If the C++ reference count is not 0 at this point, it means additional references have been added
57  // during the classes constructor. We have to propogate these references into python.
58  for (int i = 0; i < this->T::GetReferenceCount(); i++)
59  Py_INCREF(self);
60  }
61  virtual ~ElementWrapper() {}
62 
64  virtual void* GetScriptObject() const { return self; }
65 
66  // Propogate add ref's into python
67  virtual void AddReference()
68  {
69  Py_INCREF( self );
70 
71  T::AddReference();
72  }
73 
74  // Propogate remove ref's into python
75  virtual void RemoveReference()
76  {
77  T::RemoveReference();
78 
79  Py_DECREF( self );
80  }
81 
82  virtual void OnReferenceDeactivate()
83  {
84  // Ignore reference deactivated
85  }
86 
87 private:
88  // Python representation of this object
89  PyObject* self;
90 };
91 
92 }
93 }
94 }
95 
96 #endif
virtual void OnReferenceDeactivate()
Definition: ElementWrapper.h:82
virtual void AddReference()
Definition: ElementWrapper.h:67
Definition: Clipboard.h:33
ElementWrapper(PyObject *self, const char *tag)
Definition: ElementWrapper.h:49
virtual void * GetScriptObject() const
Return the python script object associated with this element.
Definition: ElementWrapper.h:64
virtual void RemoveReference()
Definition: ElementWrapper.h:75
Definition: ElementWrapper.h:46
virtual ~ElementWrapper()
Definition: ElementWrapper.h:61