author | Gustavo Carneiro <gjcarneiro@gmail.com> |
Sun, 02 Mar 2014 23:48:27 +0000 | |
changeset 10642 | 2a4d3f9d09fd |
parent 10537 | affa046899f3 |
child 10660 | 0865638cacc9 |
permissions | -rw-r--r-- |
10642
2a4d3f9d09fd
Fixes to support Python >= 3.3 in ns3 python bindings
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10537
diff
changeset
|
1 |
from __future__ import print_function |
2a4d3f9d09fd
Fixes to support Python >= 3.3 in ns3 python bindings
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10537
diff
changeset
|
2 |
import sys |
3408 | 3 |
import re |
4 |
||
5 |
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
|
6 |
from pybindgen import ReturnValue, Parameter |
3408 | 7 |
from pybindgen.cppmethod import CustomCppMethodWrapper, CustomCppConstructorWrapper |
8 |
from pybindgen.typehandlers.codesink import MemoryCodeSink |
|
9 |
from pybindgen.typehandlers import ctypeparser |
|
10537
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
10 |
from pybindgen.typehandlers.base import ForwardWrapperBase |
3421
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
11 |
from pybindgen import cppclass |
3408 | 12 |
import warnings |
13 |
||
14 |
from pybindgen.typehandlers.base import CodeGenerationError |
|
15 |
||
16 |
import sys |
|
17 |
||
18 |
class SmartPointerTransformation(typehandlers.TypeTransformation): |
|
19 |
""" |
|
20 |
This class provides a "type transformation" that tends to support |
|
21 |
NS-3 smart pointers. Parameters such as "Ptr<Foo> foo" are |
|
22 |
transformed into something like Parameter.new("Foo*", "foo", |
|
23 |
transfer_ownership=False). Return values such as Ptr<Foo> are |
|
24 |
transformed into ReturnValue.new("Foo*", |
|
25 |
caller_owns_return=False). Since the underlying objects have |
|
26 |
reference counting, PyBindGen does the right thing. |
|
27 |
""" |
|
28 |
def __init__(self): |
|
29 |
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
|
30 |
self.rx = re.compile(r'(ns3::|::ns3::|)Ptr<([^>]+)>\s*$') |
10642
2a4d3f9d09fd
Fixes to support Python >= 3.3 in ns3 python bindings
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10537
diff
changeset
|
31 |
print("{!r}".format(self), file=sys.stderr) |
3408 | 32 |
|
33 |
def _get_untransformed_type_traits(self, name): |
|
34 |
m = self.rx.match(name) |
|
35 |
is_const = False |
|
36 |
if m is None: |
|
10642
2a4d3f9d09fd
Fixes to support Python >= 3.3 in ns3 python bindings
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10537
diff
changeset
|
37 |
print("{!r} did not match".format(name), file=sys.stderr) |
3408 | 38 |
return None, False |
39 |
else: |
|
40 |
name1 = m.group(2).strip() |
|
41 |
if name1.startswith('const '): |
|
42 |
name1 = name1[len('const '):] |
|
43 |
is_const = True |
|
44 |
if name1.endswith(' const'): |
|
45 |
name1 = name1[:-len(' const')] |
|
46 |
is_const = True |
|
47 |
new_name = name1+' *' |
|
48 |
||
49 |
if new_name.startswith('::'): |
|
50 |
new_name = new_name[2:] |
|
51 |
return new_name, is_const |
|
52 |
||
53 |
def get_untransformed_name(self, name): |
|
54 |
new_name, dummy_is_const = self._get_untransformed_type_traits(name) |
|
55 |
return new_name |
|
56 |
||
57 |
def create_type_handler(self, type_handler, *args, **kwargs): |
|
58 |
if issubclass(type_handler, Parameter): |
|
59 |
kwargs['transfer_ownership'] = False |
|
60 |
elif issubclass(type_handler, ReturnValue): |
|
61 |
kwargs['caller_owns_return'] = False |
|
62 |
else: |
|
63 |
raise AssertionError |
|
64 |
||
65 |
## fix the ctype, add ns3:: namespace |
|
66 |
orig_ctype, is_const = self._get_untransformed_type_traits(args[0]) |
|
67 |
if is_const: |
|
10642
2a4d3f9d09fd
Fixes to support Python >= 3.3 in ns3 python bindings
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10537
diff
changeset
|
68 |
correct_ctype = 'ns3::Ptr< {} const >'.format(orig_ctype[:-2]) |
3408 | 69 |
else: |
10642
2a4d3f9d09fd
Fixes to support Python >= 3.3 in ns3 python bindings
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10537
diff
changeset
|
70 |
correct_ctype = 'ns3::Ptr< {} >'.format(orig_ctype[:-2]) |
3408 | 71 |
args = tuple([correct_ctype] + list(args[1:])) |
72 |
||
73 |
handler = type_handler(*args, **kwargs) |
|
74 |
handler.set_tranformation(self, orig_ctype) |
|
75 |
return handler |
|
76 |
||
77 |
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
|
78 |
return 'const_cast<%s> (ns3::PeekPointer (%s))' % (type_handler.untransformed_ctype, expression) |
3408 | 79 |
|
80 |
def transform(self, type_handler, declarations, code_block, expression): |
|
81 |
assert type_handler.untransformed_ctype[-1] == '*' |
|
82 |
return 'ns3::Ptr< %s > (%s)' % (type_handler.untransformed_ctype[:-1], expression) |
|
83 |
||
84 |
## register the type transformation |
|
85 |
transf = SmartPointerTransformation() |
|
86 |
typehandlers.return_type_matcher.register_transformation(transf) |
|
87 |
typehandlers.param_type_matcher.register_transformation(transf) |
|
88 |
del transf |
|
89 |
||
90 |
||
91 |
class CallbackImplProxyMethod(typehandlers.ReverseWrapperBase): |
|
92 |
""" |
|
93 |
Class that generates a proxy virtual method that calls a similarly named python method. |
|
94 |
""" |
|
95 |
||
96 |
def __init__(self, return_value, parameters): |
|
97 |
super(CallbackImplProxyMethod, self).__init__(return_value, parameters) |
|
98 |
||
99 |
def generate_python_call(self): |
|
100 |
"""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
|
101 |
build_params = self.build_params.get_parameters(force_tuple_creation=True) |
3408 | 102 |
if build_params[0][0] == '"': |
103 |
build_params[0] = '(char *) ' + build_params[0] |
|
104 |
args = self.before_call.declare_variable('PyObject*', 'args') |
|
105 |
self.before_call.write_code('%s = Py_BuildValue(%s);' |
|
106 |
% (args, ', '.join(build_params))) |
|
107 |
self.before_call.add_cleanup_code('Py_DECREF(%s);' % args) |
|
108 |
self.before_call.write_code('py_retval = PyObject_CallObject(m_callback, %s);' % args) |
|
109 |
self.before_call.write_error_check('py_retval == NULL') |
|
110 |
self.before_call.add_cleanup_code('Py_DECREF(py_retval);') |
|
111 |
||
112 |
||
113 |
||
114 |
||
115 |
def generate_callback_classes(out, callbacks): |
|
116 |
for callback_impl_num, template_parameters in enumerate(callbacks): |
|
117 |
sink = MemoryCodeSink() |
|
118 |
cls_name = "ns3::Callback< %s >" % ', '.join(template_parameters) |
|
119 |
#print >> sys.stderr, "***** trying to register callback: %r" % cls_name |
|
120 |
class_name = "PythonCallbackImpl%i" % callback_impl_num |
|
121 |
sink.writeln(''' |
|
122 |
class %s : public ns3::CallbackImpl<%s> |
|
123 |
{ |
|
124 |
public: |
|
125 |
PyObject *m_callback; |
|
126 |
%s(PyObject *callback) |
|
127 |
{ |
|
128 |
Py_INCREF(callback); |
|
129 |
m_callback = callback; |
|
130 |
} |
|
131 |
virtual ~%s() |
|
132 |
{ |
|
10142
3dc19f3ac5a7
bug 1754: add missing GIL lock in generated callback destructor
Alexander Afanasyev <alexander.afanasyev@ucla.edu>
parents:
6984
diff
changeset
|
133 |
PyGILState_STATE __py_gil_state; |
3dc19f3ac5a7
bug 1754: add missing GIL lock in generated callback destructor
Alexander Afanasyev <alexander.afanasyev@ucla.edu>
parents:
6984
diff
changeset
|
134 |
__py_gil_state = (PyEval_ThreadsInitialized() ? PyGILState_Ensure() : (PyGILState_STATE) 0); |
3408 | 135 |
Py_DECREF(m_callback); |
136 |
m_callback = NULL; |
|
10142
3dc19f3ac5a7
bug 1754: add missing GIL lock in generated callback destructor
Alexander Afanasyev <alexander.afanasyev@ucla.edu>
parents:
6984
diff
changeset
|
137 |
PyGILState_Release(__py_gil_state); |
3408 | 138 |
} |
139 |
||
140 |
virtual bool IsEqual(ns3::Ptr<const ns3::CallbackImplBase> other_base) const |
|
141 |
{ |
|
142 |
const %s *other = dynamic_cast<const %s*> (ns3::PeekPointer (other_base)); |
|
143 |
if (other != NULL) |
|
144 |
return (other->m_callback == m_callback); |
|
145 |
else |
|
146 |
return false; |
|
147 |
} |
|
148 |
||
149 |
''' % (class_name, ', '.join(template_parameters), class_name, class_name, class_name, class_name)) |
|
150 |
sink.indent() |
|
151 |
callback_return = template_parameters[0] |
|
152 |
return_ctype = ctypeparser.parse_type(callback_return) |
|
153 |
if ('const' in return_ctype.remove_modifiers()): |
|
154 |
kwargs = {'is_const': True} |
|
155 |
else: |
|
156 |
kwargs = {} |
|
157 |
try: |
|
158 |
return_type = ReturnValue.new(str(return_ctype), **kwargs) |
|
10642
2a4d3f9d09fd
Fixes to support Python >= 3.3 in ns3 python bindings
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10537
diff
changeset
|
159 |
except (typehandlers.TypeLookupError, typehandlers.TypeConfigurationError) as ex: |
3408 | 160 |
warnings.warn("***** Unable to register callback; Return value '%s' error (used in %s): %r" |
161 |
% (callback_return, cls_name, ex), |
|
162 |
Warning) |
|
163 |
continue |
|
164 |
||
165 |
arguments = [] |
|
166 |
ok = True |
|
167 |
callback_parameters = [arg for arg in template_parameters[1:] if arg != 'ns3::empty'] |
|
168 |
for arg_num, arg_type in enumerate(callback_parameters): |
|
169 |
arg_name = 'arg%i' % (arg_num+1) |
|
170 |
||
171 |
param_ctype = ctypeparser.parse_type(arg_type) |
|
172 |
if ('const' in param_ctype.remove_modifiers()): |
|
173 |
kwargs = {'is_const': True} |
|
174 |
else: |
|
175 |
kwargs = {} |
|
176 |
try: |
|
177 |
arguments.append(Parameter.new(str(param_ctype), arg_name, **kwargs)) |
|
10642
2a4d3f9d09fd
Fixes to support Python >= 3.3 in ns3 python bindings
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10537
diff
changeset
|
178 |
except (typehandlers.TypeLookupError, typehandlers.TypeConfigurationError) as ex: |
3408 | 179 |
warnings.warn("***** Unable to register callback; parameter '%s %s' error (used in %s): %r" |
180 |
% (arg_type, arg_name, cls_name, ex), |
|
181 |
Warning) |
|
182 |
ok = False |
|
183 |
if not ok: |
|
184 |
continue |
|
185 |
||
186 |
wrapper = CallbackImplProxyMethod(return_type, arguments) |
|
187 |
wrapper.generate(sink, 'operator()', decl_modifiers=[]) |
|
188 |
||
189 |
sink.unindent() |
|
190 |
sink.writeln('};\n') |
|
191 |
sink.flush_to(out) |
|
192 |
||
193 |
class PythonCallbackParameter(Parameter): |
|
194 |
"Class handlers" |
|
195 |
CTYPES = [cls_name] |
|
10642
2a4d3f9d09fd
Fixes to support Python >= 3.3 in ns3 python bindings
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10537
diff
changeset
|
196 |
print("***** registering callback handler: %r" % ctypeparser.normalize_type_string(cls_name), file=sys.stderr) |
3408 | 197 |
DIRECTIONS = [Parameter.DIRECTION_IN] |
198 |
PYTHON_CALLBACK_IMPL_NAME = class_name |
|
199 |
TEMPLATE_ARGS = template_parameters |
|
200 |
||
201 |
def convert_python_to_c(self, wrapper): |
|
202 |
"parses python args to get C++ value" |
|
203 |
assert isinstance(wrapper, typehandlers.ForwardWrapperBase) |
|
204 |
||
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
|
205 |
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
|
206 |
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
|
207 |
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
|
208 |
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
|
209 |
'!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
|
210 |
'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
|
211 |
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
|
212 |
'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
|
213 |
'%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
|
214 |
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
|
215 |
% (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
|
216 |
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
|
217 |
'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
|
218 |
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
|
219 |
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
|
220 |
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
|
221 |
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
|
222 |
'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
|
223 |
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
|
224 |
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
|
225 |
|
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
226 |
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
|
227 |
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
|
228 |
|
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
229 |
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
|
230 |
'!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
|
231 |
'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
|
232 |
|
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
233 |
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
|
234 |
% (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
|
235 |
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
|
236 |
|
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
237 |
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
|
238 |
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
|
239 |
|
4cb7e6908c38
Python: add default_value support to our custom Callback type handlers
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4554
diff
changeset
|
240 |
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
|
241 |
|
3408 | 242 |
|
243 |
def convert_c_to_python(self, wrapper): |
|
244 |
raise typehandlers.NotSupportedError("Reverse wrappers for ns3::Callback<...> types " |
|
245 |
"(python using callbacks defined in C++) not implemented.") |
|
246 |
||
247 |
||
248 |
# def write_preamble(out): |
|
249 |
# pybindgen.write_preamble(out) |
|
250 |
# out.writeln("#include \"ns3/everything.h\"") |
|
251 |
||
252 |
||
253 |
||
254 |
def Simulator_customizations(module): |
|
255 |
Simulator = module['ns3::Simulator'] |
|
256 |
||
257 |
## Simulator::Schedule(delay, callback, ...user..args...) |
|
258 |
Simulator.add_custom_method_wrapper("Schedule", "_wrap_Simulator_Schedule", |
|
259 |
flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"]) |
|
260 |
||
261 |
||
262 |
## Simulator::ScheduleNow(callback, ...user..args...) |
|
263 |
Simulator.add_custom_method_wrapper("ScheduleNow", "_wrap_Simulator_ScheduleNow", |
|
264 |
flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"]) |
|
265 |
||
266 |
||
267 |
## Simulator::ScheduleDestroy(callback, ...user..args...) |
|
268 |
Simulator.add_custom_method_wrapper("ScheduleDestroy", "_wrap_Simulator_ScheduleDestroy", |
|
269 |
flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"]) |
|
270 |
||
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
|
271 |
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
|
272 |
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
|
273 |
|
3408 | 274 |
|
275 |
def CommandLine_customizations(module): |
|
276 |
CommandLine = module['ns3::CommandLine'] |
|
277 |
CommandLine.add_method('Parse', None, [ArgvParam(None, 'argv')], |
|
278 |
is_static=False) |
|
3929
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3770
diff
changeset
|
279 |
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
|
280 |
flags=["METH_VARARGS", "METH_KEYWORDS"]) |
3408 | 281 |
|
282 |
||
283 |
def Object_customizations(module): |
|
284 |
## --------------------------------------------------------------------- |
|
285 |
## Here we generate custom constructor code for all classes that |
|
286 |
## derive from ns3::Object. The custom constructors are needed in |
|
287 |
## order to support kwargs only and to translate kwargs into ns3 |
|
288 |
## attributes, etc. |
|
289 |
## --------------------------------------------------------------------- |
|
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
|
290 |
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
|
291 |
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
|
292 |
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
|
293 |
return |
3408 | 294 |
|
295 |
## add a GetTypeId method to all generatd helper classes |
|
296 |
def helper_class_hook(helper_class): |
|
297 |
decl = """ |
|
298 |
static ns3::TypeId GetTypeId (void) |
|
299 |
{ |
|
300 |
static ns3::TypeId tid = ns3::TypeId ("%s") |
|
301 |
.SetParent< %s > () |
|
302 |
; |
|
303 |
return tid; |
|
304 |
}""" % (helper_class.name, helper_class.class_.full_name) |
|
305 |
||
306 |
helper_class.add_custom_method(decl) |
|
307 |
helper_class.add_post_generation_code( |
|
308 |
"NS_OBJECT_ENSURE_REGISTERED (%s);" % helper_class.name) |
|
309 |
Object.add_helper_class_hook(helper_class_hook) |
|
310 |
||
6134
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
311 |
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
|
312 |
parameters, construct_type_name): |
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
313 |
assert lvalue |
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
314 |
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
|
315 |
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
|
316 |
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
|
317 |
% cpp_class.full_name) |
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
318 |
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
|
319 |
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
|
320 |
% cpp_class.full_name) |
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
321 |
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
|
322 |
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
|
323 |
|
e01b68222e60
Fix Python bindings ns3.Object subclassing bug
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6212
diff
changeset
|
324 |
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
|
325 |
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
|
326 |
code_block.write_code("ns3::CompleteConstruct(%s);" % (lvalue, )) |
3408 | 327 |
|
6134
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
328 |
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
|
329 |
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
|
330 |
|
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
331 |
|
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
332 |
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
|
333 |
# 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
|
334 |
# 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
|
335 |
|
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
336 |
# 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
|
337 |
# 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
|
338 |
# 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
|
339 |
# 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
|
340 |
|
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
341 |
# 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
|
342 |
# 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
|
343 |
# 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
|
344 |
|
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
345 |
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
|
346 |
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
|
347 |
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
|
348 |
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
|
349 |
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
|
350 |
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
|
351 |
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
|
352 |
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
|
353 |
|
3753
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3574
diff
changeset
|
354 |
|
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3574
diff
changeset
|
355 |
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
|
356 |
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
|
357 |
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
|
358 |
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
|
359 |
|
3929
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3770
diff
changeset
|
360 |
|
4196
ed59d07c5373
Python: wrap std::ostream/ofstream, for ascii tracing.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4086
diff
changeset
|
361 |
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
|
362 |
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
|
363 |
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
|
364 |
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
|
365 |
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
|
366 |
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
|
367 |
('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
|
368 |
('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
|
369 |
('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
|
370 |
('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
|
371 |
('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
|
372 |
('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
|
373 |
]) |
ed59d07c5373
Python: wrap std::ostream/ofstream, for ascii tracing.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4086
diff
changeset
|
374 |
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
|
375 |
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
|
376 |
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
|
377 |
|
15c619b2ca1d
Modular bindings: add the std::ios::openmode constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6943
diff
changeset
|
378 |
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
|
379 |
|
6984
15c619b2ca1d
Modular bindings: add the std::ios::openmode constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6943
diff
changeset
|
380 |
|
10537
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
381 |
class IosOpenmodeParam(Parameter): |
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
382 |
|
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
383 |
DIRECTIONS = [Parameter.DIRECTION_IN] |
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
384 |
CTYPES = ['std::ios::openmode', 'std::_Ios_Openmode'] |
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
385 |
|
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
386 |
def convert_c_to_python(self, wrapper): |
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
387 |
assert isinstance(wrapper, ReverseWrapperBase) |
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
388 |
wrapper.build_params.add_parameter('i', [self.value]) |
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
389 |
|
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
390 |
def convert_python_to_c(self, wrapper): |
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
391 |
assert isinstance(wrapper, ForwardWrapperBase) |
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
392 |
name = wrapper.declarations.declare_variable("std::ios::openmode", self.name, self.default_value) |
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
393 |
wrapper.parse_params.add_parameter('i', ['&'+name], self.name, optional=bool(self.default_value)) |
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
394 |
wrapper.call_params.append(name) |
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
395 |
|
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
396 |
|
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
397 |
|
6984
15c619b2ca1d
Modular bindings: add the std::ios::openmode constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6943
diff
changeset
|
398 |
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
|
399 |
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
|
400 |
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
|
401 |
% (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
|
402 |
|
e86d97896f3f
Add python support for std::ios::openmode and its constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6134
diff
changeset
|
403 |
|
e86d97896f3f
Add python support for std::ios::openmode and its constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6134
diff
changeset
|
404 |
|
5911
993998a62a6a
Bug 786 - Make Ipv4Address hashable from Python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5752
diff
changeset
|
405 |
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
|
406 |
module.body.writeln(''' |
993998a62a6a
Bug 786 - Make Ipv4Address hashable from Python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5752
diff
changeset
|
407 |
long |
993998a62a6a
Bug 786 - Make Ipv4Address hashable from Python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5752
diff
changeset
|
408 |
_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
|
409 |
{ |
993998a62a6a
Bug 786 - Make Ipv4Address hashable from Python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5752
diff
changeset
|
410 |
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
|
411 |
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
|
412 |
} |
993998a62a6a
Bug 786 - Make Ipv4Address hashable from Python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5752
diff
changeset
|
413 |
''') |
993998a62a6a
Bug 786 - Make Ipv4Address hashable from Python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5752
diff
changeset
|
414 |
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
|
415 |
module['Ipv4Address'].pytype.slots['tp_hash'] = "_ns3_Ipv4Address_tp_hash" |