From 34160210b4e5797245f5842857accdc52fc38ff1 Mon Sep 17 00:00:00 2001 From: Raj Bhattacharjea Date: Wed, 2 May 2007 15:23:35 -0400 Subject: [PATCH] Added default value sample --- SConstruct | 6 +++ samples/main-default-value.cc | 85 +++++++++++++++++++++++++++++++++++ src/core/command-line.h | 2 +- 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 samples/main-default-value.cc diff --git a/SConstruct b/SConstruct index 69253dcd7..b0500e997 100644 --- a/SConstruct +++ b/SConstruct @@ -382,6 +382,12 @@ sample_sp2p.set_executable() sample_sp2p.add_deps(['core', 'simulator', 'node', 'p2p']) sample_sp2p.add_source('main-simple-p2p.cc') +sample_default_value = build.Ns3Module('sample-default-value', 'samples') +sample_default_value.set_executable() +ns3.add(sample_default_value) +sample_default_value.add_deps(['core', 'simulator', 'node', 'p2p']) +sample_default_value.add_source('main-default-value.cc') + # examples example_simple_p2p = build.Ns3Module('simple-p2p', 'examples') example_simple_p2p.set_executable() diff --git a/samples/main-default-value.cc b/samples/main-default-value.cc new file mode 100644 index 000000000..b3a7b2b2d --- /dev/null +++ b/samples/main-default-value.cc @@ -0,0 +1,85 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ + +#include +#include "ns3/default-value.h" +#include "ns3/command-line.h" +#include "ns3/debug.h" + +using namespace ns3; + +// +// This sample file demonstrates how to take some simple member +// variables and hook them into the default variable system +// Typically, you will establish a static variable to maintain the current +// value of the default parameter. Then as other code require the values of +// the defaults, they query them with GetValue() to get the present value. +static BooleanDefaultValue defaultTestBool1 ("testBool1", "helpBool", true); +static IntegerDefaultValue defaultTestInt1 ("testInt1", "helpInt1", 33); +static IntegerDefaultValue defaultTestInt2 ("testInt2", "helpInt2", 47); + +// +// This test class demonstrates the declaration of variables that +// may be overridden by the default-value system +// +// You will see in the core ns-3 modules that many member variables +// can be overridden in this manner +// +class TestClass { +public: + TestClass(); + virtual ~TestClass () {} + + bool m_testBool1; + int m_testInt1; + uint32_t m_testInt2; +}; + +// +// In the constructor, you can assign default values in the initializer +// list such as below; note that the instance of the created TestClass +// will have the values as dictated by the current value of the default. +// This means that the behavior of this class can be changed on the fly with +// calls to bind. +// +TestClass::TestClass () : + m_testBool1(defaultTestBool1.GetValue()), + m_testInt1(defaultTestInt1.GetValue()), + m_testInt2(defaultTestInt2.GetValue()) +{ +} +using std::cout; +int main (int argc, char* argv[]) +{ + //The following allows the default values established so far to be hooked + //into the command line argument processing unit. Essentially, the command + //line processor is aware of the DefaultValues that have been registered, and + //will accept command line overrides of these. The call automatically + //provides a --help option in addition to allowing overrides of defaults. + uint32_t loops = 0; + CommandLine::AddArgValue("loops","a test of the command line",loops); + CommandLine::Parse(argc,argv); + + //utilize the loops variable to show that it can be read from the command line + if(loops>0) + { + cout<<"You requested "<m_testBool1 << ")"); + NS_DEBUG_UNCOND("TestInt1 default value (" << testclass->m_testInt1 << ")"); + NS_DEBUG_UNCOND("TestInt2 default value (" << testclass->m_testInt2 << ")"); + delete testclass; + + return 0; +} diff --git a/src/core/command-line.h b/src/core/command-line.h index a9f2393a7..f3186fde5 100644 --- a/src/core/command-line.h +++ b/src/core/command-line.h @@ -68,7 +68,7 @@ template bool CommandLine::UserDefaultValue::DoParseValue (const std::string &value) { - std::ostringstream iss; + std::istringstream iss; iss.str (value); T v; iss >> v;