bindings/python/ns3modulegen_core_customizations.py
author Tom Henderson <tomh@tomh.org>
Mon, 28 Sep 2015 20:27:25 -0700
changeset 11676 05ea1489e509
parent 10969 99f95535826f
permissions -rw-r--r--
bug 2184: Integer overflow in MacLow
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     3
import re
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     4
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     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
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     7
from pybindgen.cppmethod import CustomCppMethodWrapper, CustomCppConstructorWrapper
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     8
from pybindgen.typehandlers.codesink import MemoryCodeSink
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     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
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    12
import warnings
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    13
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    14
from pybindgen.typehandlers.base import CodeGenerationError
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    15
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    16
import sys
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    17
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    18
class SmartPointerTransformation(typehandlers.TypeTransformation):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    19
    """
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    20
    This class provides a "type transformation" that tends to support
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    21
    NS-3 smart pointers.  Parameters such as "Ptr<Foo> foo" are
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    22
    transformed into something like Parameter.new("Foo*", "foo",
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    23
    transfer_ownership=False).  Return values such as Ptr<Foo> are
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    24
    transformed into ReturnValue.new("Foo*",
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    25
    caller_owns_return=False).  Since the underlying objects have
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    26
    reference counting, PyBindGen does the right thing.
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    27
    """
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    28
    def __init__(self):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    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
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    32
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    33
    def _get_untransformed_type_traits(self, name):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    34
        m = self.rx.match(name)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    35
        is_const = False
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    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
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    38
            return None, False
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    39
        else:
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    40
            name1 = m.group(2).strip()
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    41
            if name1.startswith('const '):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    42
                name1 = name1[len('const '):]
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    43
                is_const = True
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    44
            if name1.endswith(' const'):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    45
                name1 = name1[:-len(' const')]
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    46
                is_const = True
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    47
            new_name = name1+' *'
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    48
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    49
            if new_name.startswith('::'):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    50
                new_name = new_name[2:]
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    51
            return new_name, is_const
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    52
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    53
    def get_untransformed_name(self, name):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    54
        new_name, dummy_is_const = self._get_untransformed_type_traits(name)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    55
        return new_name
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    56
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    57
    def create_type_handler(self, type_handler, *args, **kwargs):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    58
        if issubclass(type_handler, Parameter):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    59
            kwargs['transfer_ownership'] = False
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    60
        elif issubclass(type_handler, ReturnValue):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    61
            kwargs['caller_owns_return'] = False
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    62
        else:
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    63
            raise AssertionError
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    64
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    65
        ## fix the ctype, add ns3:: namespace
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    66
        orig_ctype, is_const = self._get_untransformed_type_traits(args[0])
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    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
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    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
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    71
        args = tuple([correct_ctype] + list(args[1:]))
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    72
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    73
        handler = type_handler(*args, **kwargs)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    74
        handler.set_tranformation(self, orig_ctype)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    75
        return handler
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    76
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    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
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    79
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    80
    def transform(self, type_handler, declarations, code_block, expression):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    81
        assert type_handler.untransformed_ctype[-1] == '*'
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    82
        return 'ns3::Ptr< %s > (%s)' % (type_handler.untransformed_ctype[:-1], expression)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    83
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    84
## register the type transformation
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    85
transf = SmartPointerTransformation()
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    86
typehandlers.return_type_matcher.register_transformation(transf)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    87
typehandlers.param_type_matcher.register_transformation(transf)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    88
del transf
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    89
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    90
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    91
class CallbackImplProxyMethod(typehandlers.ReverseWrapperBase):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    92
    """
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    93
    Class that generates a proxy virtual method that calls a similarly named python method.
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    94
    """
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    95
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    96
    def __init__(self, return_value, parameters):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    97
        super(CallbackImplProxyMethod, self).__init__(return_value, parameters)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    98
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    99
    def generate_python_call(self):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   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
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   102
        if build_params[0][0] == '"':
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   103
            build_params[0] = '(char *) ' + build_params[0]
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   104
        args = self.before_call.declare_variable('PyObject*', 'args')
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   105
        self.before_call.write_code('%s = Py_BuildValue(%s);'
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   106
                                    % (args, ', '.join(build_params)))
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   107
        self.before_call.add_cleanup_code('Py_DECREF(%s);' % args)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   108
        self.before_call.write_code('py_retval = PyObject_CallObject(m_callback, %s);' % args)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   109
        self.before_call.write_error_check('py_retval == NULL')
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   110
        self.before_call.add_cleanup_code('Py_DECREF(py_retval);')
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   111
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   112
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   113
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   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
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   182
    for callback_impl_num, template_parameters in enumerate(callbacks):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   183
        sink = MemoryCodeSink()
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   184
        cls_name = "ns3::Callback< %s >" % ', '.join(template_parameters)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   185
        #print >> sys.stderr, "***** trying to register callback: %r" % cls_name
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   186
        class_name = "PythonCallbackImpl%i" % callback_impl_num
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   187
        sink.writeln('''
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   188
class %s : public ns3::CallbackImpl<%s>
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   189
{
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   190
public:
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   191
    PyObject *m_callback;
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   192
    %s(PyObject *callback)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   193
    {
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   194
        Py_INCREF(callback);
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   195
        m_callback = callback;
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   196
    }
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   197
    virtual ~%s()
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   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
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   201
        Py_DECREF(m_callback);
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   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
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   204
    }
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   205
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   206
    virtual bool IsEqual(ns3::Ptr<const ns3::CallbackImplBase> other_base) const
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   207
    {
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   208
        const %s *other = dynamic_cast<const %s*> (ns3::PeekPointer (other_base));
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   209
        if (other != NULL)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   210
            return (other->m_callback == m_callback);
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   211
        else
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   212
            return false;
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   213
    }
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   214
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   215
''' % (class_name, ', '.join(template_parameters), class_name, class_name, class_name, class_name))
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   216
        sink.indent()
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   217
        callback_return = template_parameters[0]
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   218
        return_ctype = ctypeparser.parse_type(callback_return)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   219
        if ('const' in return_ctype.remove_modifiers()):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   220
            kwargs = {'is_const': True}
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   221
        else:
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   222
            kwargs = {}
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   223
        try:
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   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
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   226
            warnings.warn("***** Unable to register callback; Return value '%s' error (used in %s): %r"
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   227
                          % (callback_return, cls_name, ex),
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   228
                          Warning)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   229
            continue
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   230
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   231
        arguments = []
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   232
        ok = True
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   233
        callback_parameters = [arg for arg in template_parameters[1:] if arg != 'ns3::empty']
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   234
        for arg_num, arg_type in enumerate(callback_parameters):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   235
            arg_name = 'arg%i' % (arg_num+1)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   236
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   237
            param_ctype = ctypeparser.parse_type(arg_type)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   238
            if ('const' in param_ctype.remove_modifiers()):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   239
                kwargs = {'is_const': True}
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   240
            else:
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   241
                kwargs = {}
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   242
            try:
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   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
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   245
                warnings.warn("***** Unable to register callback; parameter '%s %s' error (used in %s): %r"
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   246
                              % (arg_type, arg_name, cls_name, ex),
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   247
                              Warning)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   248
                ok = False
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   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
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   258
            continue
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   259
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   260
        wrapper = CallbackImplProxyMethod(return_type, arguments)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   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
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   263
        sink.unindent()
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   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
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   266
        sink.flush_to(out)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   267
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   268
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   269
# def write_preamble(out):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   270
#     pybindgen.write_preamble(out)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   271
#     out.writeln("#include \"ns3/everything.h\"")
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   272
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   273
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   274
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   275
def Simulator_customizations(module):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   276
    Simulator = module['ns3::Simulator']
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   277
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   278
    ## Simulator::Schedule(delay, callback, ...user..args...)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   279
    Simulator.add_custom_method_wrapper("Schedule", "_wrap_Simulator_Schedule",
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   280
                                        flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"])
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   281
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   282
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   283
    ## Simulator::ScheduleNow(callback, ...user..args...)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   284
    Simulator.add_custom_method_wrapper("ScheduleNow", "_wrap_Simulator_ScheduleNow",
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   285
                                        flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"])
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   286
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   287
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   288
    ## Simulator::ScheduleDestroy(callback, ...user..args...)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   289
    Simulator.add_custom_method_wrapper("ScheduleDestroy", "_wrap_Simulator_ScheduleDestroy",
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   290
                                        flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"])
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   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
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   295
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   296
def CommandLine_customizations(module):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   297
    CommandLine = module['ns3::CommandLine']
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   298
    CommandLine.add_method('Parse', None, [ArgvParam(None, 'argv')],
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   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
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   302
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   303
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   304
def Object_customizations(module):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   305
    ## ---------------------------------------------------------------------
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   306
    ## Here we generate custom constructor code for all classes that
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   307
    ## derive from ns3::Object.  The custom constructors are needed in
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   308
    ## order to support kwargs only and to translate kwargs into ns3
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   309
    ## attributes, etc.
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   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
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   315
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   316
    ## add a GetTypeId method to all generatd helper classes
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   317
    def helper_class_hook(helper_class):
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   318
        decl = """
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   319
static ns3::TypeId GetTypeId (void)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   320
{
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   321
  static ns3::TypeId tid = ns3::TypeId ("%s")
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   322
    .SetParent< %s > ()
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   323
    ;
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   324
  return tid;
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   325
}"""  % (helper_class.name, helper_class.class_.full_name)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   326
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   327
        helper_class.add_custom_method(decl)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   328
        helper_class.add_post_generation_code(
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   329
            "NS_OBJECT_ENSURE_REGISTERED (%s);" % helper_class.name)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   330
    Object.add_helper_class_hook(helper_class_hook)
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   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
2cc40b3e4fa5 python bindings
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   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"