libRocket
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
WrapperIter.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 #if !defined(BOOST_PP_IS_ITERATING)
29 # error PyWrapperIter - do not include this file!
30 #endif
31 
32 #define N BOOST_PP_ITERATION()
33 
45 #define WRAPPER_PARAM(x, n, d) , d
46 
47 template < typename T BOOST_PP_COMMA_IF(N) BOOST_PP_ENUM_PARAMS_Z(1, N, typename A) >
48 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
49 {
50 public:
51  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))
52  {
53  // Set self to NULL, so we can trap the reference deactivated and not pass it down
54  this->self = NULL;
55 
56  // We have to remove the C++ reference count that all C++ objects start with here,
57  // otherwise if an object is created in python and destroyed by python, the C++ ref count will
58  // remain 1. The PyWrapperInstancer will increase the refcount again, to ensure a correct refcount
59  // if the object was created by C++
60  T::RemoveReference();
61 
62  // If the C++ reference count is not 0 at this point, it means additional references have been added
63  // during the classes constructor. We have to propogate these references into python.
64  for (int i = 0; i < this->T::GetReferenceCount(); i++)
65  Py_INCREF(self);
66 
67  // Store self
68  this->self = self;
69  }
70 
71  Wrapper(PyObject* self, const T& other) : T(other)
72  {
73  this->self = self;
74  }
75 
76  virtual ~Wrapper()
77  {
78  // We should only be deleted when python says the refcnt is 0, if we
79  // are being deleted prematurely, something is wrong!
80  ROCKET_ASSERTMSG(self->ob_refcnt == 0, "Python object being cleared up prematurely, reference count not 0.");
81  ROCKET_ASSERT(this->T::GetReferenceCount() == 0);
82  }
83 
85  virtual void AddReference()
86  {
87  Py_INCREF(self);
88 
89  T::AddReference();
90  }
91 
93  virtual void RemoveReference()
94  {
95  T::RemoveReference();
96 
97  Py_DECREF(self);
98  }
99 
100  virtual int GetReferenceCount()
101  {
102  // C++ reference counts are always reflected in the python ref count
103  return self->ob_refcnt;
104  }
105 
106  virtual void OnReferenceDeactivate()
107  {
108  // If self is NULL, don't pass the call down, as this is the initial
109  // T::RemoveReference from the constructor
110  if (self)
111  T::OnReferenceDeactivate();
112  }
113 
114  // Script object access
115  virtual void* GetScriptObject() const { return self; }
116 
117 protected:
118  PyObject* self;
119 };
120 
121 #undef WRAPPER_PARAM
122 
#define NULL
Definition: Types.h:33
virtual void RemoveReference()
Overrride RemoveReference so we can push the call through to python.
Definition: WrapperIter.h:93
virtual void AddReference()
Override AddReference so we can push the call through to python.
Definition: WrapperIter.h:85
#define ROCKET_ASSERT(x)
Definition: Debug.h:66
#define WRAPPER_MAX_ARGS
Definition: Wrapper.h:51
#define WRAPPER_PARAM(x, n, d)
Definition: WrapperIter.h:45
#define N
Definition: WrapperIter.h:32
Wrapper(PyObject *self BOOST_PP_COMMA_IF(N) BOOST_PP_ENUM_BINARY_PARAMS_Z(1, N, A, a))
Definition: WrapperIter.h:51
#define ROCKET_ASSERTMSG(x, m)
Definition: Debug.h:74