author | Tom Henderson <tomh@tomh.org> |
Mon, 28 Sep 2015 20:27:25 -0700 | |
changeset 11676 | 05ea1489e509 |
parent 10969 | 99f95535826f |
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*$') |
10660
0865638cacc9
fixes for Python 2.6 compatibility
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10642
diff
changeset
|
31 |
print("{0!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: |
|
10660
0865638cacc9
fixes for Python 2.6 compatibility
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10642
diff
changeset
|
37 |
print("{0!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: |
|
10660
0865638cacc9
fixes for Python 2.6 compatibility
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10642
diff
changeset
|
68 |
correct_ctype = 'ns3::Ptr< {0} const >'.format(orig_ctype[:-2]) |
3408 | 69 |
else: |
10660
0865638cacc9
fixes for Python 2.6 compatibility
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10642
diff
changeset
|
70 |
correct_ctype = 'ns3::Ptr< {0} >'.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 |
||
10969
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
115 |
def register_callback_classes(out, callbacks): |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
116 |
for callback_impl_num, template_parameters in enumerate(callbacks): |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
117 |
cls_name = "ns3::Callback< %s >" % ', '.join(template_parameters) |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
118 |
#print >> sys.stderr, "***** trying to register callback: %r" % cls_name |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
119 |
class_name = "PythonCallbackImpl%i" % callback_impl_num |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
120 |
|
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
121 |
class PythonCallbackParameter(Parameter): |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
122 |
"Class handlers" |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
123 |
CTYPES = [cls_name] |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
124 |
print("***** registering callback handler: %r (%r)" % (ctypeparser.normalize_type_string(cls_name), cls_name), file=sys.stderr) |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
125 |
DIRECTIONS = [Parameter.DIRECTION_IN] |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
126 |
PYTHON_CALLBACK_IMPL_NAME = class_name |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
127 |
TEMPLATE_ARGS = template_parameters |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
128 |
DISABLED = False |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
129 |
|
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
130 |
def convert_python_to_c(self, wrapper): |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
131 |
"parses python args to get C++ value" |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
132 |
assert isinstance(wrapper, typehandlers.ForwardWrapperBase) |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
133 |
|
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
134 |
if self.DISABLED: |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
135 |
raise CodeGenerationError("wrapper could not be generated") |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
136 |
|
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
137 |
if self.default_value is None: |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
138 |
py_callback = wrapper.declarations.declare_variable('PyObject*', self.name) |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
139 |
wrapper.parse_params.add_parameter('O', ['&'+py_callback], self.name) |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
140 |
wrapper.before_call.write_error_check( |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
141 |
'!PyCallable_Check(%s)' % py_callback, |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
142 |
'PyErr_SetString(PyExc_TypeError, "parameter \'%s\' must be callbale");' % self.name) |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
143 |
callback_impl = wrapper.declarations.declare_variable( |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
144 |
'ns3::Ptr<%s>' % self.PYTHON_CALLBACK_IMPL_NAME, |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
145 |
'%s_cb_impl' % self.name) |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
146 |
wrapper.before_call.write_code("%s = ns3::Create<%s> (%s);" |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
147 |
% (callback_impl, self.PYTHON_CALLBACK_IMPL_NAME, py_callback)) |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
148 |
wrapper.call_params.append( |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
149 |
'ns3::Callback<%s> (%s)' % (', '.join(self.TEMPLATE_ARGS), callback_impl)) |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
150 |
else: |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
151 |
py_callback = wrapper.declarations.declare_variable('PyObject*', self.name, 'NULL') |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
152 |
wrapper.parse_params.add_parameter('O', ['&'+py_callback], self.name, optional=True) |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
153 |
value = wrapper.declarations.declare_variable( |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
154 |
'ns3::Callback<%s>' % ', '.join(self.TEMPLATE_ARGS), |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
155 |
self.name+'_value', |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
156 |
self.default_value) |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
157 |
|
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
158 |
wrapper.before_call.write_code("if (%s) {" % (py_callback,)) |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
159 |
wrapper.before_call.indent() |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
160 |
|
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
161 |
wrapper.before_call.write_error_check( |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
162 |
'!PyCallable_Check(%s)' % py_callback, |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
163 |
'PyErr_SetString(PyExc_TypeError, "parameter \'%s\' must be callbale");' % self.name) |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
164 |
|
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
165 |
wrapper.before_call.write_code("%s = ns3::Callback<%s> (ns3::Create<%s> (%s));" |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
166 |
% (value, ', '.join(self.TEMPLATE_ARGS), |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
167 |
self.PYTHON_CALLBACK_IMPL_NAME, py_callback)) |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
168 |
|
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
169 |
wrapper.before_call.unindent() |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
170 |
wrapper.before_call.write_code("}") # closes: if (py_callback) { |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
171 |
|
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
172 |
wrapper.call_params.append(value) |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
173 |
|
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
174 |
|
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
175 |
def convert_c_to_python(self, wrapper): |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
176 |
raise typehandlers.NotSupportedError("Reverse wrappers for ns3::Callback<...> types " |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
177 |
"(python using callbacks defined in C++) not implemented.") |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
178 |
|
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
179 |
|
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
180 |
def generate_callback_classes(module, callbacks): |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
181 |
out = module.after_forward_declarations |
3408 | 182 |
for callback_impl_num, template_parameters in enumerate(callbacks): |
183 |
sink = MemoryCodeSink() |
|
184 |
cls_name = "ns3::Callback< %s >" % ', '.join(template_parameters) |
|
185 |
#print >> sys.stderr, "***** trying to register callback: %r" % cls_name |
|
186 |
class_name = "PythonCallbackImpl%i" % callback_impl_num |
|
187 |
sink.writeln(''' |
|
188 |
class %s : public ns3::CallbackImpl<%s> |
|
189 |
{ |
|
190 |
public: |
|
191 |
PyObject *m_callback; |
|
192 |
%s(PyObject *callback) |
|
193 |
{ |
|
194 |
Py_INCREF(callback); |
|
195 |
m_callback = callback; |
|
196 |
} |
|
197 |
virtual ~%s() |
|
198 |
{ |
|
10142
3dc19f3ac5a7
bug 1754: add missing GIL lock in generated callback destructor
Alexander Afanasyev <alexander.afanasyev@ucla.edu>
parents:
6984
diff
changeset
|
199 |
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
|
200 |
__py_gil_state = (PyEval_ThreadsInitialized() ? PyGILState_Ensure() : (PyGILState_STATE) 0); |
3408 | 201 |
Py_DECREF(m_callback); |
202 |
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
|
203 |
PyGILState_Release(__py_gil_state); |
3408 | 204 |
} |
205 |
||
206 |
virtual bool IsEqual(ns3::Ptr<const ns3::CallbackImplBase> other_base) const |
|
207 |
{ |
|
208 |
const %s *other = dynamic_cast<const %s*> (ns3::PeekPointer (other_base)); |
|
209 |
if (other != NULL) |
|
210 |
return (other->m_callback == m_callback); |
|
211 |
else |
|
212 |
return false; |
|
213 |
} |
|
214 |
||
215 |
''' % (class_name, ', '.join(template_parameters), class_name, class_name, class_name, class_name)) |
|
216 |
sink.indent() |
|
217 |
callback_return = template_parameters[0] |
|
218 |
return_ctype = ctypeparser.parse_type(callback_return) |
|
219 |
if ('const' in return_ctype.remove_modifiers()): |
|
220 |
kwargs = {'is_const': True} |
|
221 |
else: |
|
222 |
kwargs = {} |
|
223 |
try: |
|
224 |
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
|
225 |
except (typehandlers.TypeLookupError, typehandlers.TypeConfigurationError) as ex: |
3408 | 226 |
warnings.warn("***** Unable to register callback; Return value '%s' error (used in %s): %r" |
227 |
% (callback_return, cls_name, ex), |
|
228 |
Warning) |
|
229 |
continue |
|
230 |
||
231 |
arguments = [] |
|
232 |
ok = True |
|
233 |
callback_parameters = [arg for arg in template_parameters[1:] if arg != 'ns3::empty'] |
|
234 |
for arg_num, arg_type in enumerate(callback_parameters): |
|
235 |
arg_name = 'arg%i' % (arg_num+1) |
|
236 |
||
237 |
param_ctype = ctypeparser.parse_type(arg_type) |
|
238 |
if ('const' in param_ctype.remove_modifiers()): |
|
239 |
kwargs = {'is_const': True} |
|
240 |
else: |
|
241 |
kwargs = {} |
|
242 |
try: |
|
243 |
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
|
244 |
except (typehandlers.TypeLookupError, typehandlers.TypeConfigurationError) as ex: |
3408 | 245 |
warnings.warn("***** Unable to register callback; parameter '%s %s' error (used in %s): %r" |
246 |
% (arg_type, arg_name, cls_name, ex), |
|
247 |
Warning) |
|
248 |
ok = False |
|
249 |
if not ok: |
|
10969
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
250 |
try: |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
251 |
typehandlers.return_type_matcher.lookup(cls_name)[0].DISABLED = True |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
252 |
except typehandlers.TypeLookupError: |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
253 |
pass |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
254 |
try: |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
255 |
typehandlers.param_type_matcher.lookup(cls_name)[0].DISABLED = True |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
256 |
except typehandlers.TypeLookupError: |
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
257 |
pass |
3408 | 258 |
continue |
259 |
||
260 |
wrapper = CallbackImplProxyMethod(return_type, arguments) |
|
261 |
wrapper.generate(sink, 'operator()', decl_modifiers=[]) |
|
10969
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
262 |
|
3408 | 263 |
sink.unindent() |
264 |
sink.writeln('};\n') |
|
10969
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
265 |
print("Flushing to ", out, file=sys.stderr) |
3408 | 266 |
sink.flush_to(out) |
267 |
||
268 |
||
269 |
# def write_preamble(out): |
|
270 |
# pybindgen.write_preamble(out) |
|
271 |
# out.writeln("#include \"ns3/everything.h\"") |
|
272 |
||
273 |
||
274 |
||
275 |
def Simulator_customizations(module): |
|
276 |
Simulator = module['ns3::Simulator'] |
|
277 |
||
278 |
## Simulator::Schedule(delay, callback, ...user..args...) |
|
279 |
Simulator.add_custom_method_wrapper("Schedule", "_wrap_Simulator_Schedule", |
|
280 |
flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"]) |
|
281 |
||
282 |
||
283 |
## Simulator::ScheduleNow(callback, ...user..args...) |
|
284 |
Simulator.add_custom_method_wrapper("ScheduleNow", "_wrap_Simulator_ScheduleNow", |
|
285 |
flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"]) |
|
286 |
||
287 |
||
288 |
## Simulator::ScheduleDestroy(callback, ...user..args...) |
|
289 |
Simulator.add_custom_method_wrapper("ScheduleDestroy", "_wrap_Simulator_ScheduleDestroy", |
|
290 |
flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"]) |
|
291 |
||
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
|
292 |
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
|
293 |
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
|
294 |
|
3408 | 295 |
|
296 |
def CommandLine_customizations(module): |
|
297 |
CommandLine = module['ns3::CommandLine'] |
|
298 |
CommandLine.add_method('Parse', None, [ArgvParam(None, 'argv')], |
|
299 |
is_static=False) |
|
3929
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3770
diff
changeset
|
300 |
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
|
301 |
flags=["METH_VARARGS", "METH_KEYWORDS"]) |
3408 | 302 |
|
303 |
||
304 |
def Object_customizations(module): |
|
305 |
## --------------------------------------------------------------------- |
|
306 |
## Here we generate custom constructor code for all classes that |
|
307 |
## derive from ns3::Object. The custom constructors are needed in |
|
308 |
## order to support kwargs only and to translate kwargs into ns3 |
|
309 |
## attributes, etc. |
|
310 |
## --------------------------------------------------------------------- |
|
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
|
311 |
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
|
312 |
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
|
313 |
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
|
314 |
return |
3408 | 315 |
|
316 |
## add a GetTypeId method to all generatd helper classes |
|
317 |
def helper_class_hook(helper_class): |
|
318 |
decl = """ |
|
319 |
static ns3::TypeId GetTypeId (void) |
|
320 |
{ |
|
321 |
static ns3::TypeId tid = ns3::TypeId ("%s") |
|
322 |
.SetParent< %s > () |
|
323 |
; |
|
324 |
return tid; |
|
325 |
}""" % (helper_class.name, helper_class.class_.full_name) |
|
326 |
||
327 |
helper_class.add_custom_method(decl) |
|
328 |
helper_class.add_post_generation_code( |
|
329 |
"NS_OBJECT_ENSURE_REGISTERED (%s);" % helper_class.name) |
|
330 |
Object.add_helper_class_hook(helper_class_hook) |
|
331 |
||
6134
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
332 |
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
|
333 |
parameters, construct_type_name): |
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
334 |
assert lvalue |
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
335 |
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
|
336 |
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
|
337 |
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
|
338 |
% cpp_class.full_name) |
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
339 |
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
|
340 |
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
|
341 |
% cpp_class.full_name) |
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
342 |
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
|
343 |
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
|
344 |
|
e01b68222e60
Fix Python bindings ns3.Object subclassing bug
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6212
diff
changeset
|
345 |
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
|
346 |
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
|
347 |
code_block.write_code("ns3::CompleteConstruct(%s);" % (lvalue, )) |
3408 | 348 |
|
6134
5921c218e692
Realign Python ns3.Object constructors with C++
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5911
diff
changeset
|
349 |
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
|
350 |
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
|
351 |
|
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
352 |
|
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
353 |
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
|
354 |
# 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
|
355 |
# 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
|
356 |
|
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
357 |
# 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
|
358 |
# 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
|
359 |
# 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
|
360 |
# 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
|
361 |
|
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
362 |
# 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
|
363 |
# 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
|
364 |
# 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
|
365 |
|
b9424c43753d
Python: make helper class methods using attribute optional parameters work.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3412
diff
changeset
|
366 |
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
|
367 |
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
|
368 |
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
|
369 |
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
|
370 |
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
|
371 |
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
|
372 |
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
|
373 |
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
|
374 |
|
3753
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3574
diff
changeset
|
375 |
|
a84a48233eb3
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3574
diff
changeset
|
376 |
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
|
377 |
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
|
378 |
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
|
379 |
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
|
380 |
|
3929
909b0a724ed3
Bug 289: CommandLine::AddValue is not wrapped
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
3770
diff
changeset
|
381 |
|
4196
ed59d07c5373
Python: wrap std::ostream/ofstream, for ascii tracing.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4086
diff
changeset
|
382 |
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
|
383 |
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
|
384 |
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
|
385 |
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
|
386 |
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
|
387 |
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
|
388 |
('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
|
389 |
('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
|
390 |
('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
|
391 |
('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
|
392 |
('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
|
393 |
('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
|
394 |
]) |
ed59d07c5373
Python: wrap std::ostream/ofstream, for ascii tracing.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
4086
diff
changeset
|
395 |
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
|
396 |
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
|
397 |
ofstream.add_method('close', None, []) |
10969
99f95535826f
Python bindings: fix generation of callback wrappers with the newest pybindgen
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10660
diff
changeset
|
398 |
|
6984
15c619b2ca1d
Modular bindings: add the std::ios::openmode constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6943
diff
changeset
|
399 |
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
|
400 |
|
6984
15c619b2ca1d
Modular bindings: add the std::ios::openmode constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6943
diff
changeset
|
401 |
|
10537
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
402 |
class IosOpenmodeParam(Parameter): |
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
403 |
|
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
404 |
DIRECTIONS = [Parameter.DIRECTION_IN] |
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
405 |
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
|
406 |
|
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
407 |
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
|
408 |
assert isinstance(wrapper, ReverseWrapperBase) |
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
409 |
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
|
410 |
|
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
411 |
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
|
412 |
assert isinstance(wrapper, ForwardWrapperBase) |
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
413 |
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
|
414 |
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
|
415 |
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
|
416 |
|
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
417 |
|
affa046899f3
Add a custom typehandler for std::ios::openmode, fixes #1815
Gustavo Carneiro <gjcarneiro@gmail.com>
parents:
10142
diff
changeset
|
418 |
|
6984
15c619b2ca1d
Modular bindings: add the std::ios::openmode constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6943
diff
changeset
|
419 |
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
|
420 |
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
|
421 |
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
|
422 |
% (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
|
423 |
|
e86d97896f3f
Add python support for std::ios::openmode and its constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6134
diff
changeset
|
424 |
|
e86d97896f3f
Add python support for std::ios::openmode and its constants
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
6134
diff
changeset
|
425 |
|
5911
993998a62a6a
Bug 786 - Make Ipv4Address hashable from Python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5752
diff
changeset
|
426 |
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
|
427 |
module.body.writeln(''' |
993998a62a6a
Bug 786 - Make Ipv4Address hashable from Python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5752
diff
changeset
|
428 |
long |
993998a62a6a
Bug 786 - Make Ipv4Address hashable from Python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5752
diff
changeset
|
429 |
_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
|
430 |
{ |
993998a62a6a
Bug 786 - Make Ipv4Address hashable from Python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5752
diff
changeset
|
431 |
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
|
432 |
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
|
433 |
} |
993998a62a6a
Bug 786 - Make Ipv4Address hashable from Python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5752
diff
changeset
|
434 |
''') |
993998a62a6a
Bug 786 - Make Ipv4Address hashable from Python
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
5752
diff
changeset
|
435 |
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
|
436 |
module['Ipv4Address'].pytype.slots['tp_hash'] = "_ns3_Ipv4Address_tp_hash" |