View | Details | Raw Unified | Return to bug 289
Collapse All | Expand All

(-)a/bindings/python/callbacks_list.py (+1 lines)
 Lines 5-10    Link Here 
5
    ['void', 'ns3::Ptr<ns3::Socket>', 'unsigned int', 'ns3::empty', 'ns3::empty', 'ns3::empty', 'ns3::empty'],
5
    ['void', 'ns3::Ptr<ns3::Socket>', 'unsigned int', 'ns3::empty', 'ns3::empty', 'ns3::empty', 'ns3::empty'],
6
    ['void', 'ns3::Ptr<ns3::Socket>', 'ns3::Address const&', 'ns3::empty', 'ns3::empty', 'ns3::empty', 'ns3::empty'],
6
    ['void', 'ns3::Ptr<ns3::Socket>', 'ns3::Address const&', 'ns3::empty', 'ns3::empty', 'ns3::empty', 'ns3::empty'],
7
    ['bool', 'ns3::Ptr<ns3::Socket>', 'ns3::Address const&', 'ns3::empty', 'ns3::empty', 'ns3::empty', 'ns3::empty'],
7
    ['bool', 'ns3::Ptr<ns3::Socket>', 'ns3::Address const&', 'ns3::empty', 'ns3::empty', 'ns3::empty', 'ns3::empty'],
8
    ['bool', 'std::string', 'ns3::empty', 'ns3::empty', 'ns3::empty', 'ns3::empty', 'ns3::empty'],
8
    ['bool', 'ns3::Ptr<ns3::NetDevice>', 'ns3::Ptr<ns3::Packet const>', 'unsigned short', 'ns3::Address const&', 'ns3::Address const&', 'ns3::NetDevice::PacketType'],
9
    ['bool', 'ns3::Ptr<ns3::NetDevice>', 'ns3::Ptr<ns3::Packet const>', 'unsigned short', 'ns3::Address const&', 'ns3::Address const&', 'ns3::NetDevice::PacketType'],
9
    ['bool', 'ns3::Ptr<ns3::NetDevice>', 'ns3::Ptr<ns3::Packet const>', 'unsigned short', 'ns3::Address const&', 'ns3::empty', 'ns3::empty'],
10
    ['bool', 'ns3::Ptr<ns3::NetDevice>', 'ns3::Ptr<ns3::Packet const>', 'unsigned short', 'ns3::Address const&', 'ns3::empty', 'ns3::empty'],
10
    ['void', 'ns3::Ptr<ns3::NetDevice>', 'ns3::Ptr<ns3::Packet const>', 'unsigned short', 'ns3::Address const&', 'ns3::Address const&', 'ns3::NetDevice::PacketType'],
11
    ['void', 'ns3::Ptr<ns3::NetDevice>', 'ns3::Ptr<ns3::Packet const>', 'unsigned short', 'ns3::Address const&', 'ns3::Address const&', 'ns3::NetDevice::PacketType'],
(-)a/bindings/python/ns3_module_core.py (-3 / +7 lines)
 Lines 12-18    Link Here 
12
    ## callback.h: ns3::CallbackImplBase [class]
12
    ## callback.h: ns3::CallbackImplBase [class]
13
    module.add_class('CallbackImplBase', allow_subclassing=True, memory_policy=cppclass.ReferenceCountingMethodsPolicy(incref_method='Ref', decref_method='Unref', peekref_method='GetReferenceCount'))
13
    module.add_class('CallbackImplBase', allow_subclassing=True, memory_policy=cppclass.ReferenceCountingMethodsPolicy(incref_method='Ref', decref_method='Unref', peekref_method='GetReferenceCount'))
14
    ## command-line.h: ns3::CommandLine [class]
14
    ## command-line.h: ns3::CommandLine [class]
15
    module.add_class('CommandLine')
15
    module.add_class('CommandLine', allow_subclassing=True)
16
    ## system-mutex.h: ns3::CriticalSection [class]
16
    ## system-mutex.h: ns3::CriticalSection [class]
17
    module.add_class('CriticalSection')
17
    module.add_class('CriticalSection')
18
    ## global-value.h: ns3::GlobalValue [class]
18
    ## global-value.h: ns3::GlobalValue [class]
 Lines 347-352    Link Here 
347
    cls.add_constructor([param('ns3::CommandLine const &', 'arg0')])
347
    cls.add_constructor([param('ns3::CommandLine const &', 'arg0')])
348
    ## command-line.h: ns3::CommandLine::CommandLine() [constructor]
348
    ## command-line.h: ns3::CommandLine::CommandLine() [constructor]
349
    cls.add_constructor([])
349
    cls.add_constructor([])
350
    ## command-line.h: void ns3::CommandLine::AddValue(std::string const & name, std::string const & help, ns3::Callback<bool, std::string, ns3::empty, ns3::empty, ns3::empty, ns3::empty, ns3::empty> callback) [member function]
351
    cls.add_method('AddValue', 
352
                   'void', 
353
                   [param('std::string const &', 'name'), param('std::string const &', 'help'), param('ns3::Callback< bool, std::string, ns3::empty, ns3::empty, ns3::empty, ns3::empty, ns3::empty >', 'callback')])
350
    return
354
    return
351
355
352
def register_Ns3CriticalSection_methods(root_module, cls):
356
def register_Ns3CriticalSection_methods(root_module, cls):
 Lines 1879-1885    Link Here 
1879
    cls.add_method('ConnectWithoutContext', 
1883
    cls.add_method('ConnectWithoutContext', 
1880
                   'void', 
1884
                   'void', 
1881
                   [param('ns3::CallbackBase const &', 'cb')])
1885
                   [param('ns3::CallbackBase const &', 'cb')])
1882
    ## traced-value.h: void ns3::TracedValue<unsigned int>::Connect(ns3::CallbackBase const & cb, std::basic_string<char,std::char_traits<char>,std::allocator<char> > path) [member function]
1886
    ## traced-value.h: void ns3::TracedValue<unsigned int>::Connect(ns3::CallbackBase const & cb, std::string path) [member function]
1883
    cls.add_method('Connect', 
1887
    cls.add_method('Connect', 
1884
                   'void', 
1888
                   'void', 
1885
                   [param('ns3::CallbackBase const &', 'cb'), param('std::string', 'path')])
1889
                   [param('ns3::CallbackBase const &', 'cb'), param('std::string', 'path')])
 Lines 1887-1893    Link Here 
1887
    cls.add_method('DisconnectWithoutContext', 
1891
    cls.add_method('DisconnectWithoutContext', 
1888
                   'void', 
1892
                   'void', 
1889
                   [param('ns3::CallbackBase const &', 'cb')])
1893
                   [param('ns3::CallbackBase const &', 'cb')])
1890
    ## traced-value.h: void ns3::TracedValue<unsigned int>::Disconnect(ns3::CallbackBase const & cb, std::basic_string<char,std::char_traits<char>,std::allocator<char> > path) [member function]
1894
    ## traced-value.h: void ns3::TracedValue<unsigned int>::Disconnect(ns3::CallbackBase const & cb, std::string path) [member function]
1891
    cls.add_method('Disconnect', 
1895
    cls.add_method('Disconnect', 
1892
                   'void', 
1896
                   'void', 
1893
                   [param('ns3::CallbackBase const &', 'cb'), param('std::string', 'path')])
1897
                   [param('ns3::CallbackBase const &', 'cb'), param('std::string', 'path')])
(-)a/bindings/python/ns3module_helpers.cc (+59 lines)
 Lines 1-3    Link Here 
1
#include "ns3/ref-count-base.h"
1
#include "ns3module.h"
2
#include "ns3module.h"
2
3
3
4
 Lines 220-222    Link Here 
220
    
221
    
221
    return (PyObject *) py_tid;
222
    return (PyObject *) py_tid;
222
}
223
}
224
225
226
class CommandLinePythonValueSetter : public ns3::RefCountBase
227
{
228
    PyObject *m_namespace;
229
    std::string m_variable;
230
public:
231
    CommandLinePythonValueSetter (PyObject *ns, std::string const &variable) {
232
        Py_INCREF(ns);
233
        m_namespace = ns;
234
        m_variable = variable;
235
    }
236
    bool Parse (std::string value) {
237
        PyObject *pyvalue = PyString_FromStringAndSize (value.data(), value.size());
238
        PyObject_SetAttrString (m_namespace, m_variable.c_str(), pyvalue);
239
        if (PyErr_Occurred()) {
240
            PyErr_Print();
241
            return false;
242
        }
243
        return true;
244
    }
245
    virtual ~CommandLinePythonValueSetter () {
246
        Py_DECREF (m_namespace);
247
        m_namespace = NULL;
248
    }
249
    
250
};
251
252
PyObject *
253
_wrap_CommandLine_AddValue(PyNs3CommandLine *self, PyObject *args, PyObject *kwargs,
254
                           PyObject **return_exception)
255
{
256
    const char *name, *help, *variable = NULL;
257
    PyObject *py_namespace = NULL;
258
    const char *keywords[] = {"name", "help", "variable", "namespace", NULL};
259
    
260
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "ss|sO", (char **) keywords, &name, &help, &variable, &py_namespace)) {
261
        PyObject *exc_type, *traceback;
262
        PyErr_Fetch(&exc_type, return_exception, &traceback);
263
        Py_XDECREF(exc_type);
264
        Py_XDECREF(traceback);
265
        return NULL;
266
    }
267
    
268
    if (variable == NULL) {
269
        variable = name;
270
    }
271
    if (py_namespace == NULL) {
272
        py_namespace = (PyObject *) self;
273
    }
274
275
    ns3::Ptr<CommandLinePythonValueSetter> setter = ns3::Create<CommandLinePythonValueSetter> (py_namespace, variable);
276
    self->obj->AddValue (name, help, ns3::MakeCallback (&CommandLinePythonValueSetter::Parse, setter));
277
278
    Py_INCREF(Py_None);
279
    return Py_None;
280
}
281
(-)a/bindings/python/ns3modulegen_core_customizations.py (-1 / +3 lines)
 Lines 292-297    Link Here 
292
    CommandLine = module['ns3::CommandLine']
292
    CommandLine = module['ns3::CommandLine']
293
    CommandLine.add_method('Parse', None, [ArgvParam(None, 'argv')],
293
    CommandLine.add_method('Parse', None, [ArgvParam(None, 'argv')],
294
                           is_static=False)
294
                           is_static=False)
295
    CommandLine.add_custom_method_wrapper("AddValue", "_wrap_CommandLine_AddValue",
296
                                          flags=["METH_VARARGS", "METH_KEYWORDS"])
295
297
296
298
297
def Object_customizations(module):
299
def Object_customizations(module):
 Lines 523-527    Link Here 
523
    TypeId = module['ns3::TypeId']
525
    TypeId = module['ns3::TypeId']
524
    TypeId.add_custom_method_wrapper("LookupByNameFailSafe", "_wrap_TypeId_LookupByNameFailSafe",
526
    TypeId.add_custom_method_wrapper("LookupByNameFailSafe", "_wrap_TypeId_LookupByNameFailSafe",
525
                                     flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"])
527
                                     flags=["METH_VARARGS", "METH_KEYWORDS", "METH_STATIC"])
526
    
527
528
529
(-)a/bindings/python/ns3modulescan.py (+5 lines)
 Lines 56-61    Link Here 
56
        'automatic_type_narrowing': 'true',
56
        'automatic_type_narrowing': 'true',
57
        'allow_subclassing': 'false',
57
        'allow_subclassing': 'false',
58
        },
58
        },
59
60
    '::ns3::CommandLine': {
61
        'allow_subclassing': 'true', # needed so that AddValue is able to set attributes on the object
62
        },
63
59
    'ns3::RandomVariable::RandomVariable(ns3::RandomVariableBase const & variable) [constructor]': {
64
    'ns3::RandomVariable::RandomVariable(ns3::RandomVariableBase const & variable) [constructor]': {
60
        'ignore': None,
65
        'ignore': None,
61
        },
66
        },
(-)a/bindings/python/wscript (+1 lines)
 Lines 280-285    Link Here 
280
   t1 > t2;
280
   t1 > t2;
281
}
281
}
282
282
283
283
}
284
}
284
"""
285
"""
285
    outfile.close()
286
    outfile.close()
(-)a/src/core/command-line.cc (+21 lines)
 Lines 253-258    Link Here 
253
    }
253
    }
254
}
254
}
255
255
256
bool
257
CommandLine::CallbackItem::Parse (std::string value)
258
{
259
  NS_LOG_DEBUG ("CommandLine::CallbackItem::Parse \"" << value << "\"");
260
  return m_callback (value);
261
}
262
263
void
264
CommandLine::AddValue (const std::string &name,
265
                       const std::string &help,
266
                       Callback<bool, std::string> callback)
267
{
268
  NS_LOG_FUNCTION (this << name << help << "callback");
269
  CallbackItem *item = new CallbackItem ();
270
  item->m_name = name;
271
  item->m_help = help;
272
  item->m_callback = callback;
273
  m_items.push_back (item);
274
}
275
276
256
} // namespace ns3
277
} // namespace ns3
257
278
258
#ifdef RUN_SELF_TESTS
279
#ifdef RUN_SELF_TESTS
(-)a/src/core/command-line.h (+20 lines)
 Lines 23-28    Link Here 
23
#include <string>
23
#include <string>
24
#include <sstream>
24
#include <sstream>
25
#include <list>
25
#include <list>
26
27
#include "ns3/callback.h"
26
28
27
namespace ns3 {
29
namespace ns3 {
28
30
 Lines 56-61    Link Here 
56
		 const std::string &help,
58
		 const std::string &help,
57
		 T &value);
59
		 T &value);
58
60
61
62
  /**
63
   * \param name the name of the user-supplied argument
64
   * \param help some help text used by --PrintHelp
65
   * \param callback a callback function that will be invoked to parse
66
   * and collect the value.  This normally used by language bindings.
67
   */
68
  void AddValue (const std::string &name,
69
		 const std::string &help,
70
                 Callback<bool, std::string> callback);
71
59
  /**
72
  /**
60
   * \param argc the 'argc' variable: number of arguments (including the
73
   * \param argc the 'argc' variable: number of arguments (including the
61
   *        main program name as first element).
74
   *        main program name as first element).
 Lines 82-87    Link Here 
82
    virtual bool Parse (std::string value);
95
    virtual bool Parse (std::string value);
83
    T *m_valuePtr;
96
    T *m_valuePtr;
84
  };
97
  };
98
  class CallbackItem : public Item
99
  {
100
  public:
101
    virtual bool Parse (std::string value);
102
    Callback<bool, std::string> m_callback;
103
  };
104
85
  void HandleArgument (std::string name, std::string value) const;
105
  void HandleArgument (std::string name, std::string value) const;
86
  void PrintHelp (void) const;
106
  void PrintHelp (void) const;
87
  void PrintGlobals (void) const;
107
  void PrintGlobals (void) const;
(-)a/utils/python-unit-tests.py (+21 lines)
 Lines 123-127    Link Here 
123
        
123
        
124
        self.assertRaises(KeyError, ns3.TypeId.LookupByNameFailSafe, "__InvalidTypeName__")
124
        self.assertRaises(KeyError, ns3.TypeId.LookupByNameFailSafe, "__InvalidTypeName__")
125
125
126
    def testCommandLine(self):
127
        cmd = ns3.CommandLine()
128
        cmd.AddValue("Test1", "this is a test option")
129
        cmd.AddValue("Test2", "this is a test option")
130
        cmd.AddValue("Test3", "this is a test option", variable="test_xxx")
131
        cmd.Test1 = None
132
        cmd.Test2 = None
133
        cmd.test_xxx = None
134
        class Foo:
135
            pass
136
        foo = Foo()
137
        foo.test_foo = None
138
        cmd.AddValue("Test4", "this is a test option", variable="test_foo", namespace=foo)
139
        
140
        cmd.Parse(["python", "--Test1=value1", "--Test2=value2", "--Test3=123", "--Test4=xpto"])
141
142
        self.assertEqual(cmd.Test1, "value1")
143
        self.assertEqual(cmd.Test2, "value2")
144
        self.assertEqual(cmd.test_xxx, "123")
145
        self.assertEqual(foo.test_foo, "xpto")
146
126
if __name__ == '__main__':
147
if __name__ == '__main__':
127
    unittest.main()
148
    unittest.main()

Return to bug 289