author | Gustavo J. A. M. Carneiro <gjc@inescporto.pt> |
Wed, 30 Mar 2011 18:44:36 +0100 | |
changeset 6984 | 15c619b2ca1d |
parent 6943 | 9ae2596b385d |
child 10142 | 3dc19f3ac5a7 |
permissions | -rw-r--r-- |
3408 | 1 |
import re |
2 |
||
3 |
from pybindgen.typehandlers import base as typehandlers |
|
3473
6bce86ea4778
Require new PyBindGen; make it work for Python 2.3.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3421
diff
changeset
|
4 |
from pybindgen import ReturnValue, Parameter |
3408 | 5 |
from pybindgen.cppmethod import CustomCppMethodWrapper, CustomCppConstructorWrapper |
6 |
from pybindgen.typehandlers.codesink import MemoryCodeSink |
|
7 |
from pybindgen.typehandlers import ctypeparser |
|
3421
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
8 |
from pybindgen import cppclass |
3408 | 9 |
import warnings |
10 |
||
11 |
from pybindgen.typehandlers.base import CodeGenerationError |
|
12 |
||
13 |
import sys |
|
14 |
||
15 |
class SmartPointerTransformation(typehandlers.TypeTransformation): |
|
16 |
""" |
|
17 |
This class provides a "type transformation" that tends to support |
|
18 |
NS-3 smart pointers. Parameters such as "Ptr<Foo> foo" are |
|
19 |
transformed into something like Parameter.new("Foo*", "foo", |
|
20 |
transfer_ownership=False). Return values such as Ptr<Foo> are |
|
21 |
transformed into ReturnValue.new("Foo*", |
|
22 |
caller_owns_return=False). Since the underlying objects have |
|
23 |
reference counting, PyBindGen does the right thing. |
|
24 |
""" |
|
25 |
def __init__(self): |
|
26 |
super(SmartPointerTransformation, self).__init__() |
|
6134
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
27 |
self.rx = re.compile(r'(ns3::|::ns3::|)Ptr<([^>]+)>\s*$') |
3408 | 28 |
|
29 |
def _get_untransformed_type_traits(self, name): |
|
30 |
m = self.rx.match(name) |
|
31 |
is_const = False |
|
32 |
if m is None: |
|
33 |
return None, False |
|
34 |
else: |
|
35 |
name1 = m.group(2).strip() |
|
36 |
if name1.startswith('const '): |
|
37 |
name1 = name1[len('const '):] |
|
38 |
is_const = True |
|
39 |
if name1.endswith(' const'): |
|
40 |
name1 = name1[:-len(' const')] |
|
41 |
is_const = True |
|
42 |
new_name = name1+' *' |
|
43 |
||
44 |
if new_name.startswith('::'): |
|
45 |
new_name = new_name[2:] |
|
46 |
return new_name, is_const |
|
47 |
||
48 |
def get_untransformed_name(self, name): |
|
49 |
new_name, dummy_is_const = self._get_untransformed_type_traits(name) |
|
50 |
return new_name |
|
51 |
||
52 |
def create_type_handler(self, type_handler, *args, **kwargs): |
|
53 |
if issubclass(type_handler, Parameter): |
|
54 |
kwargs['transfer_ownership'] = False |
|
55 |
elif issubclass(type_handler, ReturnValue): |
|
56 |
kwargs['caller_owns_return'] = False |
|
57 |
else: |
|
58 |
raise AssertionError |
|
59 |
||
60 |
## fix the ctype, add ns3:: namespace |
|
61 |
orig_ctype, is_const = self._get_untransformed_type_traits(args[0]) |
|
62 |
if is_const: |
|
63 |
correct_ctype = 'ns3::Ptr< %s const >' % orig_ctype[:-2] |
|
64 |
else: |
|
65 |
correct_ctype = 'ns3::Ptr< %s >' % orig_ctype[:-2] |
|
66 |
args = tuple([correct_ctype] + list(args[1:])) |
|
67 |
||
68 |
handler = type_handler(*args, **kwargs) |
|
69 |
handler.set_tranformation(self, orig_ctype) |
|
70 |
return handler |
|
71 |
||
72 |
def untransform(self, type_handler, declarations, code_block, expression): |
|
3574
b6804efbe16b
New pybindgen and API rescan. Extensive API description files changes because pybindgen now handles consts differently.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3573
diff
changeset
|
73 |
return 'const_cast<%s> (ns3::PeekPointer (%s))' % (type_handler.untransformed_ctype, expression) |
3408 | 74 |
|
75 |
def transform(self, type_handler, declarations, code_block, expression): |
|
76 |
assert type_handler.untransformed_ctype[-1] == '*' |
|
77 |
return 'ns3::Ptr< %s > (%s)' % (type_handler.untransformed_ctype[:-1], expression) |
|
78 |
||
79 |
## register the type transformation |
|
80 |
transf = SmartPointerTransformation() |
|
81 |
typehandlers.return_type_matcher.register_transformation(transf) |
|
82 |
typehandlers.param_type_matcher.register_transformation(transf) |
|
83 |
del transf |
|
84 |
||
85 |
||
86 |
class ArgvParam(Parameter): |
|
87 |
""" |
|
88 |
Converts a python list-of-strings argument to a pair of 'int argc, |
|
89 |
char *argv[]' arguments to pass into C. |
|
90 |
||
91 |
One Python argument becomes two C function arguments -> it's a miracle! |
|
92 |
||
93 |
Note: this parameter type handler is not registered by any name; |
|
94 |
must be used explicitly. |
|
95 |
""" |
|
96 |
||
97 |
DIRECTIONS = [Parameter.DIRECTION_IN] |
|
98 |
CTYPES = [] |
|
99 |
||
100 |
def convert_c_to_python(self, wrapper): |
|
101 |
raise NotImplementedError |
|
102 |
||
103 |
def convert_python_to_c(self, wrapper): |
|
104 |
py_name = wrapper.declarations.declare_variable('PyObject*', 'py_' + self.name) |
|
105 |
argc_var = wrapper.declarations.declare_variable('int', 'argc') |
|
106 |
name = wrapper.declarations.declare_variable('char**', self.name) |
|
107 |
idx = wrapper.declarations.declare_variable('Py_ssize_t', 'idx') |
|
108 |
wrapper.parse_params.add_parameter('O!', ['&PyList_Type', '&'+py_name], self.name) |
|
109 |
||
110 |
#wrapper.before_call.write_error_check('!PyList_Check(%s)' % py_name) # XXX |
|
111 |
||
112 |
wrapper.before_call.write_code("%s = (char **) malloc(sizeof(char*)*PyList_Size(%s));" |
|
113 |
% (name, py_name)) |
|
114 |
wrapper.before_call.add_cleanup_code('free(%s);' % name) |
|
115 |
wrapper.before_call.write_code(''' |
|
116 |
for (%(idx)s = 0; %(idx)s < PyList_Size(%(py_name)s); %(idx)s++) |
|
117 |
{ |
|
118 |
''' % vars()) |
|
119 |
wrapper.before_call.sink.indent() |
|
120 |
wrapper.before_call.write_code(''' |
|
121 |
PyObject *item = PyList_GET_ITEM(%(py_name)s, %(idx)s); |
|
122 |
''' % vars()) |
|
123 |
#wrapper.before_call.write_error_check('item == NULL') |
|
124 |
wrapper.before_call.write_error_check( |
|
125 |
'!PyString_Check(item)', |
|
126 |
failure_cleanup=('PyErr_SetString(PyExc_TypeError, ' |
|
127 |
'"argument %s must be a list of strings");') % self.name) |
|
128 |
wrapper.before_call.write_code( |
|
129 |
'%s[%s] = PyString_AsString(item);' % (name, idx)) |
|
130 |
wrapper.before_call.sink.unindent() |
|
131 |
wrapper.before_call.write_code('}') |
|
132 |
wrapper.before_call.write_code('%s = PyList_Size(%s);' % (argc_var, py_name)) |
|
133 |
||
134 |
wrapper.call_params.append(argc_var) |
|
135 |
wrapper.call_params.append(name) |
|
136 |
||
137 |
||
138 |
class CallbackImplProxyMethod(typehandlers.ReverseWrapperBase): |
|
139 |
""" |
|
140 |
Class that generates a proxy virtual method that calls a similarly named python method. |
|
141 |
""" |
|
142 |
||
143 |
def __init__(self, return_value, parameters): |
|
144 |
super(CallbackImplProxyMethod, self).__init__(return_value, parameters) |
|
145 |
||
146 |
def generate_python_call(self): |
|
147 |
"""code to call the python method""" |
|
3412
518719e905a0
Fix a problem with callback proxies with zero or one parameters.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3408
diff
changeset
|
148 |
build_params = self.build_params.get_parameters(force_tuple_creation=True) |
3408 | 149 |
if build_params[0][0] == '"': |
150 |
build_params[0] = '(char *) ' + build_params[0] |
|
151 |
args = self.before_call.declare_variable('PyObject*', 'args') |
|
152 |
self.before_call.write_code('%s = Py_BuildValue(%s);' |
|
153 |
% (args, ', '.join(build_params))) |
|
154 |
self.before_call.add_cleanup_code('Py_DECREF(%s);' % args) |
|
155 |
self.before_call.write_code('py_retval = PyObject_CallObject(m_callback, %s);' % args) |
|
156 |
self.before_call.write_error_check('py_retval == NULL') |
|
157 |
self.before_call.add_cleanup_code('Py_DECREF(py_retval);') |
|
158 |
||
159 |
||
160 |
||
161 |
||
162 |
def generate_callback_classes(out, callbacks): |
|
163 |
for callback_impl_num, template_parameters in enumerate(callbacks): |
|
164 |
sink = MemoryCodeSink() |
|
165 |
cls_name = "ns3::Callback< %s >" % ', '.join(template_parameters) |
|
166 |
#print >> sys.stderr, "***** trying to register callback: %r" % cls_name |
|
167 |
class_name = "PythonCallbackImpl%i" % callback_impl_num |
|
168 |
sink.writeln(''' |
|
169 |
class %s : public ns3::CallbackImpl<%s> |
|
170 |
{ |
|
171 |
public: |
|
172 |
PyObject *m_callback; |
|
173 |
%s(PyObject *callback) |
|
174 |
{ |
|
175 |
Py_INCREF(callback); |
|
176 |
m_callback = callback; |
|
177 |
} |
|
178 |
virtual ~%s() |
|
179 |
{ |
|
180 |
Py_DECREF(m_callback); |
|
181 |
m_callback = NULL; |
|
182 |
} |
|
183 |
||
184 |
virtual bool IsEqual(ns3::Ptr<const ns3::CallbackImplBase> other_base) const |
|
185 |
{ |
|
186 |
const %s *other = dynamic_cast<const %s*> (ns3::PeekPointer (other_base)); |
|
187 |
if (other != NULL) |
|
188 |
return (other->m_callback == m_callback); |
|
189 |
else |
|
190 |
return false; |
|
191 |
} |
|
192 |
||
193 |
''' % (class_name, ', '.join(template_parameters), class_name, class_name, class_name, class_name)) |
|
194 |
sink.indent() |
|
195 |
callback_return = template_parameters[0] |
|
196 |
return_ctype = ctypeparser.parse_type(callback_return) |
|
197 |
if ('const' in return_ctype.remove_modifiers()): |
|
198 |
kwargs = {'is_const': True} |
|
199 |
else: |
|
200 |
kwargs = {} |
|
201 |
try: |
|
202 |
return_type = ReturnValue.new(str(return_ctype), **kwargs) |
|
203 |
except (typehandlers.TypeLookupError, typehandlers.TypeConfigurationError), ex: |
|
204 |
warnings.warn("***** Unable to register callback; Return value '%s' error (used in %s): %r" |
|
205 |
% (callback_return, cls_name, ex), |
|
206 |
Warning) |
|
207 |
continue |
|
208 |
||
209 |
arguments = [] |
|
210 |
ok = True |
|
211 |
callback_parameters = [arg for arg in template_parameters[1:] if arg != 'ns3::empty'] |
|
212 |
for arg_num, arg_type in enumerate(callback_parameters): |
|
213 |
arg_name = 'arg%i' % (arg_num+1) |
|
214 |
||
215 |
param_ctype = ctypeparser.parse_type(arg_type) |
|
216 |
if ('const' in param_ctype.remove_modifiers()): |
|
217 |
kwargs = {'is_const': True} |
|
218 |
else: |
|
219 |
kwargs = {} |
|
220 |
try: |
|
221 |
arguments.append(Parameter.new(str(param_ctype), arg_name, **kwargs)) |
|
222 |
except (typehandlers.TypeLookupError, typehandlers.TypeConfigurationError), ex: |
|
223 |
warnings.warn("***** Unable to register callback; parameter '%s %s' error (used in %s): %r" |
|
224 |
% (arg_type, arg_name, cls_name, ex), |
|
225 |
Warning) |
|
226 |
ok = False |
|
227 |
if not ok: |
|
228 |
continue |
|
229 |
||
230 |
wrapper = CallbackImplProxyMethod(return_type, arguments) |
|
231 |
wrapper.generate(sink, 'operator()', decl_modifiers=[]) |
|
232 |
||
233 |
sink.unindent() |
|
234 |
sink.writeln('};\n') |
|
235 |
sink.flush_to(out) |
|
236 |
||
237 |
class PythonCallbackParameter(Parameter): |
|
238 |
"Class handlers" |
|
239 |
CTYPES = [cls_name] |
|
6896
fb47685b1dad
Modular bindings: add the Callback<...> type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6290
diff
changeset
|
240 |
print >> sys.stderr, "***** registering callback handler: %r" % ctypeparser.normalize_type_string(cls_name) |
3408 | 241 |
DIRECTIONS = [Parameter.DIRECTION_IN] |
242 |
PYTHON_CALLBACK_IMPL_NAME = class_name |
|
243 |
TEMPLATE_ARGS = template_parameters |
|
244 |
||
245 |
def convert_python_to_c(self, wrapper): |
|
246 |
"parses python args to get C++ value" |
|
247 |
assert isinstance(wrapper, typehandlers.ForwardWrapperBase) |
|
248 |
||
5752
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
249 |
if self.default_value is None: |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
250 |
py_callback = wrapper.declarations.declare_variable('PyObject*', self.name) |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
251 |
wrapper.parse_params.add_parameter('O', ['&'+py_callback], self.name) |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
252 |
wrapper.before_call.write_error_check( |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
253 |
'!PyCallable_Check(%s)' % py_callback, |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
254 |
'PyErr_SetString(PyExc_TypeError, "parameter \'%s\' must be callbale");' % self.name) |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
255 |
callback_impl = wrapper.declarations.declare_variable( |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
256 |
'ns3::Ptr<%s>' % self.PYTHON_CALLBACK_IMPL_NAME, |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
257 |
'%s_cb_impl' % self.name) |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
258 |
wrapper.before_call.write_code("%s = ns3::Create<%s> (%s);" |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
259 |
% (callback_impl, self.PYTHON_CALLBACK_IMPL_NAME, py_callback)) |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
260 |
wrapper.call_params.append( |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
261 |
'ns3::Callback<%s> (%s)' % (', '.join(self.TEMPLATE_ARGS), callback_impl)) |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
262 |
else: |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
263 |
py_callback = wrapper.declarations.declare_variable('PyObject*', self.name, 'NULL') |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
264 |
wrapper.parse_params.add_parameter('O', ['&'+py_callback], self.name, optional=True) |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
265 |
value = wrapper.declarations.declare_variable( |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
266 |
'ns3::Callback<%s>' % ', '.join(self.TEMPLATE_ARGS), |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
267 |
self.name+'_value', |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
268 |
self.default_value) |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
269 |
|
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
270 |
wrapper.before_call.write_code("if (%s) {" % (py_callback,)) |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
271 |
wrapper.before_call.indent() |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
272 |
|
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
273 |
wrapper.before_call.write_error_check( |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
274 |
'!PyCallable_Check(%s)' % py_callback, |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
275 |
'PyErr_SetString(PyExc_TypeError, "parameter \'%s\' must be callbale");' % self.name) |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
276 |
|
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
277 |
wrapper.before_call.write_code("%s = ns3::Callback<%s> (ns3::Create<%s> (%s));" |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
278 |
% (value, ', '.join(self.TEMPLATE_ARGS), |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
279 |
self.PYTHON_CALLBACK_IMPL_NAME, py_callback)) |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
280 |
|
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
281 |
wrapper.before_call.unindent() |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
282 |
wrapper.before_call.write_code("}") # closes: if (py_callback) { |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
283 |
|
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
284 |
wrapper.call_params.append(value) |
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
285 |
|
3408 | 286 |
|
287 |
def convert_c_to_python(self, wrapper): |
|
288 |
raise typehandlers.NotSupportedError("Reverse wrappers for ns3::Callback<...> types " |
|
289 |
"(python using callbacks defined in C++) not implemented.") |
|
290 |
||
291 |
||
292 |
# def write_preamble(out): |
|
293 |
# pybindgen.write_preamble(out) |
|
294 |
# out.writeln("#include \"ns3/everything.h\"") |
|
295 |
||
296 |
||
297 |
||
298 |
def Simulator_customizations(module): |
|
299 |
Simulator = module['ns3::Simulator'] |
|
300 |
||
301 |
## Simulator::Schedule(delay, callback, ...user..args...) |
|
302 |
Simulator.add_custom_method_wrapper("Schedule", "_wrap_Simulator_Schedule", |
|
303 |
flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"]) |
|
304 |
||
305 |
||
306 |
## Simulator::ScheduleNow(callback, ...user..args...) |
|
307 |
Simulator.add_custom_method_wrapper("ScheduleNow", "_wrap_Simulator_ScheduleNow", |
|
308 |
flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"]) |
|
309 |
||
310 |
||
311 |
## Simulator::ScheduleDestroy(callback, ...user..args...) |
|
312 |
Simulator.add_custom_method_wrapper("ScheduleDestroy", "_wrap_Simulator_ScheduleDestroy", |
|
313 |
flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"]) |
|
314 |
||
4086
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3929
diff
changeset
|
315 |
Simulator.add_custom_method_wrapper("Run", "_wrap_Simulator_Run", |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3929
diff
changeset
|
316 |
flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"]) |
37dbf76b4c66
Bug 375: Ctrl-C does not always work when running Python simulations
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3929
diff
changeset
|
317 |
|
3408 | 318 |
|
319 |
def CommandLine_customizations(module): |
|
320 |
CommandLine = module['ns3::CommandLine'] |
|
321 |
CommandLine.add_method('Parse', None, [ArgvParam(None, 'argv')], |
|
322 |
is_static=False) |
|
3929
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3770
diff
changeset
|
323 |
CommandLine.add_custom_method_wrapper("AddValue", "_wrap_CommandLine_AddValue", |
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3770
diff
changeset
|
324 |
flags=["METH_VARARGS", "METH_KEYWORDS"]) |
3408 | 325 |
|
326 |
||
327 |
def Object_customizations(module): |
|
328 |
## --------------------------------------------------------------------- |
|
329 |
## Here we generate custom constructor code for all classes that |
|
330 |
## derive from ns3::Object. The custom constructors are needed in |
|
331 |
## order to support kwargs only and to translate kwargs into ns3 |
|
332 |
## attributes, etc. |
|
333 |
## --------------------------------------------------------------------- |
|
6943
9ae2596b385d
Modular bindings: fix binding code generation when the module does not use ns3::Object
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6896
diff
changeset
|
334 |
try: |
9ae2596b385d
Modular bindings: fix binding code generation when the module does not use ns3::Object
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6896
diff
changeset
|
335 |
Object = module['ns3::Object'] |
9ae2596b385d
Modular bindings: fix binding code generation when the module does not use ns3::Object
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6896
diff
changeset
|
336 |
except KeyError: |
9ae2596b385d
Modular bindings: fix binding code generation when the module does not use ns3::Object
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6896
diff
changeset
|
337 |
return |
3408 | 338 |
|
339 |
## add a GetTypeId method to all generatd helper classes |
|
340 |
def helper_class_hook(helper_class): |
|
341 |
decl = """ |
|
342 |
static ns3::TypeId GetTypeId (void) |
|
343 |
{ |
|
344 |
static ns3::TypeId tid = ns3::TypeId ("%s") |
|
345 |
.SetParent< %s > () |
|
346 |
; |
|
347 |
return tid; |
|
348 |
}""" % (helper_class.name, helper_class.class_.full_name) |
|
349 |
||
350 |
helper_class.add_custom_method(decl) |
|
351 |
helper_class.add_post_generation_code( |
|
352 |
"NS_OBJECT_ENSURE_REGISTERED (%s);" % helper_class.name) |
|
353 |
Object.add_helper_class_hook(helper_class_hook) |
|
354 |
||
6134
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
355 |
def ns3_object_instance_creation_function(cpp_class, code_block, lvalue, |
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
356 |
parameters, construct_type_name): |
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
357 |
assert lvalue |
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
358 |
assert not lvalue.startswith('None') |
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
359 |
if cpp_class.cannot_be_constructed: |
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
360 |
raise CodeGenerationError("%s cannot be constructed (%s)" |
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
361 |
% cpp_class.full_name) |
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
362 |
if cpp_class.incomplete_type: |
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
363 |
raise CodeGenerationError("%s cannot be constructed (incomplete type)" |
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
364 |
% cpp_class.full_name) |
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
365 |
code_block.write_code("%s = new %s(%s);" % (lvalue, construct_type_name, parameters)) |
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
366 |
code_block.write_code("%s->Ref ();" % (lvalue)) |
6290
e01b68222e60
Fix Python bindings ns3.Object subclassing bug
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6212
diff
changeset
|
367 |
|
e01b68222e60
Fix Python bindings ns3.Object subclassing bug
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6212
diff
changeset
|
368 |
def ns3_object_post_instance_creation_function(cpp_class, code_block, lvalue, |
e01b68222e60
Fix Python bindings ns3.Object subclassing bug
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6212
diff
changeset
|
369 |
parameters, construct_type_name): |
6134
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
370 |
code_block.write_code("ns3::CompleteConstruct(%s);" % (lvalue, )) |
3408 | 371 |
|
6134
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
372 |
Object.set_instance_creation_function(ns3_object_instance_creation_function) |
6290
e01b68222e60
Fix Python bindings ns3.Object subclassing bug
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6212
diff
changeset
|
373 |
Object.set_post_instance_creation_function(ns3_object_post_instance_creation_function) |
3421
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
374 |
|
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
375 |
|
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
376 |
def Attribute_customizations(module): |
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
377 |
# Fix up for the "const AttributeValue &v = EmptyAttribute()" |
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
378 |
# case, as used extensively by helper classes. |
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
379 |
|
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
380 |
# Here's why we need to do this: pybindgen.gccxmlscanner, when |
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
381 |
# scanning parameter default values, is only provided with the |
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
382 |
# value as a simple C expression string. (py)gccxml does not |
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
383 |
# report the type of the default value. |
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
384 |
|
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
385 |
# As a workaround, here we iterate over all parameters of all |
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
386 |
# methods of all classes and tell pybindgen what is the type of |
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
387 |
# the default value for attributes. |
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
388 |
|
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
389 |
for cls in module.classes: |
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
390 |
for meth in cls.get_all_methods(): |
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
391 |
for param in meth.parameters: |
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
392 |
if isinstance(param, cppclass.CppClassRefParameter): |
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
393 |
if param.cpp_class.name == 'AttributeValue' \ |
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
394 |
and param.default_value is not None \ |
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
395 |
and param.default_value_type is None: |
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
396 |
param.default_value_type = 'ns3::EmptyAttributeValue' |
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
397 |
|
3753
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3574
diff
changeset
|
398 |
|
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3574
diff
changeset
|
399 |
def TypeId_customizations(module): |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3574
diff
changeset
|
400 |
TypeId = module['ns3::TypeId'] |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3574
diff
changeset
|
401 |
TypeId.add_custom_method_wrapper("LookupByNameFailSafe", "_wrap_TypeId_LookupByNameFailSafe", |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3574
diff
changeset
|
402 |
flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"]) |
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3574
diff
changeset
|
403 |
|
3929
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3770
diff
changeset
|
404 |
|
4196
ed59d07c5373
Python: wrap std::ostream/ofstream, for ascii tracing.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4086
diff
changeset
|
405 |
def add_std_ofstream(module): |
ed59d07c5373
Python: wrap std::ostream/ofstream, for ascii tracing.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4086
diff
changeset
|
406 |
module.add_include('<fstream>') |
ed59d07c5373
Python: wrap std::ostream/ofstream, for ascii tracing.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4086
diff
changeset
|
407 |
ostream = module.add_class('ostream', foreign_cpp_namespace='::std') |
ed59d07c5373
Python: wrap std::ostream/ofstream, for ascii tracing.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4086
diff
changeset
|
408 |
ostream.set_cannot_be_constructed("abstract base class") |
ed59d07c5373
Python: wrap std::ostream/ofstream, for ascii tracing.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4086
diff
changeset
|
409 |
ofstream = module.add_class('ofstream', foreign_cpp_namespace='::std', parent=ostream) |
ed59d07c5373
Python: wrap std::ostream/ofstream, for ascii tracing.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4086
diff
changeset
|
410 |
ofstream.add_enum('openmode', [ |
ed59d07c5373
Python: wrap std::ostream/ofstream, for ascii tracing.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4086
diff
changeset
|
411 |
('app', 'std::ios_base::app'), |
ed59d07c5373
Python: wrap std::ostream/ofstream, for ascii tracing.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4086
diff
changeset
|
412 |
('ate', 'std::ios_base::ate'), |
ed59d07c5373
Python: wrap std::ostream/ofstream, for ascii tracing.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4086
diff
changeset
|
413 |
('binary', 'std::ios_base::binary'), |
ed59d07c5373
Python: wrap std::ostream/ofstream, for ascii tracing.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4086
diff
changeset
|
414 |
('in', 'std::ios_base::in'), |
ed59d07c5373
Python: wrap std::ostream/ofstream, for ascii tracing.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4086
diff
changeset
|
415 |
('out', 'std::ios_base::out'), |
ed59d07c5373
Python: wrap std::ostream/ofstream, for ascii tracing.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4086
diff
changeset
|
416 |
('trunc', 'std::ios_base::trunc'), |
ed59d07c5373
Python: wrap std::ostream/ofstream, for ascii tracing.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4086
diff
changeset
|
417 |
]) |
ed59d07c5373
Python: wrap std::ostream/ofstream, for ascii tracing.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4086
diff
changeset
|
418 |
ofstream.add_constructor([Parameter.new("const char *", 'filename'), |
ed59d07c5373
Python: wrap std::ostream/ofstream, for ascii tracing.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4086
diff
changeset
|
419 |
Parameter.new("::std::ofstream::openmode", 'mode', default_value="std::ios_base::out")]) |
ed59d07c5373
Python: wrap std::ostream/ofstream, for ascii tracing.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4086
diff
changeset
|
420 |
ofstream.add_method('close', None, []) |
6984
15c619b2ca1d
Modular bindings: add the std::ios::openmode constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6943
diff
changeset
|
421 |
|
15c619b2ca1d
Modular bindings: add the std::ios::openmode constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6943
diff
changeset
|
422 |
add_std_ios_openmode(module) |
4196
ed59d07c5373
Python: wrap std::ostream/ofstream, for ascii tracing.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4086
diff
changeset
|
423 |
|
6984
15c619b2ca1d
Modular bindings: add the std::ios::openmode constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6943
diff
changeset
|
424 |
|
15c619b2ca1d
Modular bindings: add the std::ios::openmode constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6943
diff
changeset
|
425 |
def add_std_ios_openmode(module): |
6212
e86d97896f3f
Add python support for std::ios::openmode and its constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6134
diff
changeset
|
426 |
import pybindgen.typehandlers.base |
e86d97896f3f
Add python support for std::ios::openmode and its constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6134
diff
changeset
|
427 |
for alias in "std::_Ios_Openmode", "std::ios::openmode": |
e86d97896f3f
Add python support for std::ios::openmode and its constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6134
diff
changeset
|
428 |
pybindgen.typehandlers.base.param_type_matcher.add_type_alias(alias, "int") |
e86d97896f3f
Add python support for std::ios::openmode and its constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6134
diff
changeset
|
429 |
|
e86d97896f3f
Add python support for std::ios::openmode and its constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6134
diff
changeset
|
430 |
for flag in 'in', 'out', 'ate', 'app', 'trunc', 'binary': |
e86d97896f3f
Add python support for std::ios::openmode and its constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6134
diff
changeset
|
431 |
module.after_init.write_code('PyModule_AddIntConstant(m, (char *) "STD_IOS_%s", std::ios::%s);' |
e86d97896f3f
Add python support for std::ios::openmode and its constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6134
diff
changeset
|
432 |
% (flag.upper(), flag)) |
e86d97896f3f
Add python support for std::ios::openmode and its constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6134
diff
changeset
|
433 |
|
e86d97896f3f
Add python support for std::ios::openmode and its constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6134
diff
changeset
|
434 |
|
e86d97896f3f
Add python support for std::ios::openmode and its constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6134
diff
changeset
|
435 |
|
5911
993998a62a6a
Bug 786 - Make Ipv4Address hashable from Python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5752
diff
changeset
|
436 |
def add_ipv4_address_tp_hash(module): |
993998a62a6a
Bug 786 - Make Ipv4Address hashable from Python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5752
diff
changeset
|
437 |
module.body.writeln(''' |
993998a62a6a
Bug 786 - Make Ipv4Address hashable from Python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5752
diff
changeset
|
438 |
long |
993998a62a6a
Bug 786 - Make Ipv4Address hashable from Python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5752
diff
changeset
|
439 |
_ns3_Ipv4Address_tp_hash (PyObject *obj) |
993998a62a6a
Bug 786 - Make Ipv4Address hashable from Python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5752
diff
changeset
|
440 |
{ |
993998a62a6a
Bug 786 - Make Ipv4Address hashable from Python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5752
diff
changeset
|
441 |
PyNs3Ipv4Address *addr = reinterpret_cast<PyNs3Ipv4Address *> (obj); |
993998a62a6a
Bug 786 - Make Ipv4Address hashable from Python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5752
diff
changeset
|
442 |
return static_cast<long> (ns3::Ipv4AddressHash () (*addr->obj)); |
993998a62a6a
Bug 786 - Make Ipv4Address hashable from Python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5752
diff
changeset
|
443 |
} |
993998a62a6a
Bug 786 - Make Ipv4Address hashable from Python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5752
diff
changeset
|
444 |
''') |
993998a62a6a
Bug 786 - Make Ipv4Address hashable from Python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5752
diff
changeset
|
445 |
module.header.writeln('long _ns3_Ipv4Address_tp_hash (PyObject *obj);') |
993998a62a6a
Bug 786 - Make Ipv4Address hashable from Python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5752
diff
changeset
|
446 |
module['Ipv4Address'].pytype.slots['tp_hash'] = "_ns3_Ipv4Address_tp_hash" |