A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
authorGustavo J. A. M. Carneiro <gjc@inescporto.pt>
Wed, 15 Oct 2008 15:55:09 +0100
changeset 3753 a84a48233eb3
parent 3744 bb6876ea0851
child 3754 ee45d2d71f01
A more pythonic wrapper for ns3.TypeId.LookupByNameFailSafe
bindings/python/ns3_module_core.py
bindings/python/ns3_module_internet_stack.py
bindings/python/ns3module_helpers.cc
bindings/python/ns3modulegen.py
bindings/python/ns3modulegen_core_customizations.py
bindings/python/ns3modulescan.py
utils/python-unit-tests.py
--- a/bindings/python/ns3_module_core.py	Wed Oct 15 09:49:39 2008 +0200
+++ b/bindings/python/ns3_module_core.py	Wed Oct 15 15:55:09 2008 +0100
@@ -945,11 +945,6 @@
                    'ns3::TypeId', 
                    [param('std::string', 'name')], 
                    is_static=True)
-    ## type-id.h: static bool ns3::TypeId::LookupByNameFailSafe(std::string name, ns3::TypeId * tid) [member function]
-    cls.add_method('LookupByNameFailSafe', 
-                   'bool', 
-                   [param('std::string', 'name'), param('ns3::TypeId *', 'tid', transfer_ownership=False)], 
-                   is_static=True)
     ## type-id.h: ns3::Ptr<ns3::TraceSourceAccessor const> ns3::TypeId::LookupTraceSourceByName(std::string name) const [member function]
     cls.add_method('LookupTraceSourceByName', 
                    'ns3::Ptr< ns3::TraceSourceAccessor const >', 
--- a/bindings/python/ns3_module_internet_stack.py	Wed Oct 15 09:49:39 2008 +0200
+++ b/bindings/python/ns3_module_internet_stack.py	Wed Oct 15 15:55:09 2008 +0100
@@ -358,10 +358,11 @@
     cls.add_method('Insert', 
                    'void', 
                    [param('ns3::Ptr< ns3::Ipv4L4Protocol >', 'protocol')])
-    ## ipv4-l3-protocol.h: ns3::Ptr<ns3::Ipv4L4Protocol> ns3::Ipv4L3Protocol::GetProtocol(int protocolNumber) [member function]
+    ## ipv4-l3-protocol.h: ns3::Ptr<ns3::Ipv4L4Protocol> ns3::Ipv4L3Protocol::GetProtocol(int protocolNumber) const [member function]
     cls.add_method('GetProtocol', 
                    'ns3::Ptr< ns3::Ipv4L4Protocol >', 
-                   [param('int', 'protocolNumber')])
+                   [param('int', 'protocolNumber')], 
+                   is_const=True)
     ## ipv4-l3-protocol.h: void ns3::Ipv4L3Protocol::Remove(ns3::Ptr<ns3::Ipv4L4Protocol> protocol) [member function]
     cls.add_method('Remove', 
                    'void', 
--- a/bindings/python/ns3module_helpers.cc	Wed Oct 15 09:49:39 2008 +0200
+++ b/bindings/python/ns3module_helpers.cc	Wed Oct 15 15:55:09 2008 +0100
@@ -188,3 +188,35 @@
     return NULL;
 }
 
+
+PyObject *
+_wrap_TypeId_LookupByNameFailSafe(PyNs3TypeId *PYBINDGEN_UNUSED(dummy), PyObject *args, PyObject *kwargs,
+                                  PyObject **return_exception)
+{
+    bool ok;
+    const char *name;
+    Py_ssize_t name_len;
+    ns3::TypeId tid;
+    PyNs3TypeId *py_tid;
+    const char *keywords[] = {"name", NULL};
+    
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "s#", (char **) keywords, &name, &name_len)) {
+        PyObject *exc_type, *traceback;
+        PyErr_Fetch(&exc_type, return_exception, &traceback);
+        Py_XDECREF(exc_type);
+        Py_XDECREF(traceback);
+        return NULL;
+    }
+    ok = ns3::TypeId::LookupByNameFailSafe(std::string(name, name_len), &tid);
+    if (!ok)
+    {
+        PyErr_Format(PyExc_KeyError, "The ns3 type with name `%s' is not registered", name);
+        return NULL;
+    }
+
+    py_tid = PyObject_New(PyNs3TypeId, &PyNs3TypeId_Type);
+    py_tid->obj = new ns3::TypeId (tid);
+    PyNs3TypeId_wrapper_registry[(void *) py_tid->obj] = (PyObject *) py_tid;    
+    
+    return (PyObject *) py_tid;
+}
--- a/bindings/python/ns3modulegen.py	Wed Oct 15 09:49:39 2008 +0200
+++ b/bindings/python/ns3modulegen.py	Wed Oct 15 15:55:09 2008 +0100
@@ -85,6 +85,7 @@
 
     ns3modulegen_core_customizations.Simulator_customizations(root_module)
     ns3modulegen_core_customizations.CommandLine_customizations(root_module)
+    ns3modulegen_core_customizations.TypeId_customizations(root_module)
 
 
     for local_module in LOCAL_MODULES:
--- a/bindings/python/ns3modulegen_core_customizations.py	Wed Oct 15 09:49:39 2008 +0200
+++ b/bindings/python/ns3modulegen_core_customizations.py	Wed Oct 15 15:55:09 2008 +0100
@@ -517,3 +517,10 @@
                             and param.default_value_type is None:
                         param.default_value_type = 'ns3::EmptyAttributeValue'
 
+
+def TypeId_customizations(module):
+    TypeId = module['ns3::TypeId']
+    TypeId.add_custom_method_wrapper("LookupByNameFailSafe", "_wrap_TypeId_LookupByNameFailSafe",
+                                     flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"])
+    
+
--- a/bindings/python/ns3modulescan.py	Wed Oct 15 09:49:39 2008 +0200
+++ b/bindings/python/ns3modulescan.py	Wed Oct 15 15:55:09 2008 +0100
@@ -73,7 +73,7 @@
         'params': {'info':{'transfer_ownership': 'false'}}
         },
     'static bool ns3::TypeId::LookupByNameFailSafe(std::string name, ns3::TypeId * tid) [member function]': {
-        'params': {'tid': {'transfer_ownership': 'false'}}
+        'ignore': None, # manually wrapped in 
         },
     'bool ns3::TraceSourceAccessor::ConnectWithoutContext(ns3::ObjectBase * obj, ns3::CallbackBase const & cb) const [member function]': {
         'params': {'obj': {'transfer_ownership':'false'}}
--- a/utils/python-unit-tests.py	Wed Oct 15 09:49:39 2008 +0200
+++ b/utils/python-unit-tests.py	Wed Oct 15 15:55:09 2008 +0100
@@ -118,5 +118,11 @@
 
         self.assert_(c1 is c2)
 
+    def testTypeId(self):
+        typeId1 = ns3.TypeId.LookupByNameFailSafe("ns3::UdpSocketFactory")
+        self.assertEqual(typeId1.GetName (), "ns3::UdpSocketFactory")
+        
+        self.assertRaises(KeyError, ns3.TypeId.LookupByNameFailSafe, "__InvalidTypeName__")
+
 if __name__ == '__main__':
     unittest.main()