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