A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
API
config-store-save.cc
Go to the documentation of this file.
1
#include "ns3/config-store-module.h"
2
#include "ns3/core-module.h"
3
4
#include <iostream>
5
6
using namespace
ns3
;
7
14
class
ConfigExample
:
public
Object
15
{
16
public
:
21
static
TypeId
GetTypeId
()
22
{
23
static
TypeId
tid =
TypeId
(
"ns3::ConfigExample"
)
24
.
SetParent
<
Object
>()
25
.AddAttribute(
"TestInt16"
,
26
"help text"
,
27
IntegerValue
(-2),
28
MakeIntegerAccessor
(&
ConfigExample::m_int16
),
29
MakeIntegerChecker<int16_t>());
30
return
tid;
31
}
32
33
int16_t
m_int16
;
34
};
35
36
NS_OBJECT_ENSURE_REGISTERED
(
ConfigExample
);
37
38
// Assign a new default value to A::TestInt16 (-5)
39
// Configure a TestInt16 value for a special instance of A (to -3)
40
// View the output from the config store
41
//
42
int
43
main(
int
argc,
char
* argv[])
44
{
45
std::string loadfile;
46
47
CommandLine
cmd
(__FILE__);
48
cmd
.Usage(
"Without arguments, write out ConfigStore defaults, globals, and\n"
49
"test object ConfigExample attributes to text file output-attributes.txt\n"
50
"and (when XML supported) output-attributes.xml. Optionally set\n"
51
"attributes to write out using --load <filename> where <filename> is a\n"
52
"previously saved config-store file to load.\n"
53
"Observe load behavior by setting environment variable NS_LOG=RawTextConfig."
);
54
cmd
.AddValue(
"load"
,
"Relative path to config-store input file"
, loadfile);
55
cmd
.Parse(argc, argv);
56
57
if
(!loadfile.empty())
58
{
59
Config::SetDefault
(
"ns3::ConfigStore::Filename"
,
StringValue
(loadfile));
60
if
(loadfile.substr(loadfile.size() - 4) ==
".xml"
)
61
{
62
Config::SetDefault
(
"ns3::ConfigStore::FileFormat"
,
StringValue
(
"Xml"
));
63
}
64
else
65
{
66
Config::SetDefault
(
"ns3::ConfigStore::FileFormat"
,
StringValue
(
"RawText"
));
67
}
68
Config::SetDefault
(
"ns3::ConfigStore::Mode"
,
StringValue
(
"Load"
));
69
ConfigStore
loadConfig;
70
loadConfig.
ConfigureDefaults
();
71
loadConfig.
ConfigureAttributes
();
72
}
73
74
Config::SetDefault
(
"ns3::ConfigExample::TestInt16"
,
IntegerValue
(-5));
75
76
Ptr<ConfigExample>
a_obj = CreateObject<ConfigExample>();
77
NS_ABORT_MSG_UNLESS
(a_obj->m_int16 == -5,
78
"Cannot set ConfigExample's integer attribute via Config::SetDefault"
);
79
80
Ptr<ConfigExample>
b_obj = CreateObject<ConfigExample>();
81
b_obj->SetAttribute(
"TestInt16"
,
IntegerValue
(-3));
82
IntegerValue
iv;
83
b_obj->GetAttribute(
"TestInt16"
, iv);
84
NS_ABORT_MSG_UNLESS
(iv.
Get
() == -3,
85
"Cannot set ConfigExample's integer attribute via SetAttribute"
);
86
87
// These test objects are not rooted in any ns-3 configuration namespace.
88
// This is usually done automatically for ns3 nodes and channels, but
89
// we can establish a new root and anchor one of them there (note; we
90
// can't use two objects of the same type as roots). Rooting one of these
91
// is necessary for it to show up in the config namespace so that
92
// ConfigureAttributes() will work below.
93
Config::RegisterRootNamespaceObject
(b_obj);
94
95
#ifdef HAVE_LIBXML2
96
// Output config store to XML format
97
Config::SetDefault
(
"ns3::ConfigStore::Filename"
,
StringValue
(
"output-attributes.xml"
));
98
Config::SetDefault
(
"ns3::ConfigStore::FileFormat"
,
StringValue
(
"Xml"
));
99
Config::SetDefault
(
"ns3::ConfigStore::Mode"
,
StringValue
(
"Save"
));
100
ConfigStore
outputConfig;
101
outputConfig.
ConfigureDefaults
();
102
outputConfig.
ConfigureAttributes
();
103
#endif
/* HAVE_LIBXML2 */
104
105
// Output config store to txt format
106
Config::SetDefault
(
"ns3::ConfigStore::Filename"
,
StringValue
(
"output-attributes.txt"
));
107
Config::SetDefault
(
"ns3::ConfigStore::FileFormat"
,
StringValue
(
"RawText"
));
108
Config::SetDefault
(
"ns3::ConfigStore::Mode"
,
StringValue
(
"Save"
));
109
ConfigStore
outputConfig2;
110
outputConfig2.
ConfigureDefaults
();
111
outputConfig2.
ConfigureAttributes
();
112
113
Simulator::Run
();
114
115
Simulator::Destroy
();
116
117
return
0;
118
}
ConfigExample
Example class to demonstrate use of the ns-3 Config Store.
Definition:
config-store-save.cc:15
ConfigExample::GetTypeId
static TypeId GetTypeId()
Get the type ID.
Definition:
config-store-save.cc:21
ConfigExample::m_int16
int16_t m_int16
value to configure
Definition:
config-store-save.cc:33
ns3::CommandLine
Parse command-line arguments.
Definition:
command-line.h:232
ns3::ConfigStore
Definition:
config-store.h:61
ns3::ConfigStore::ConfigureDefaults
void ConfigureDefaults()
Configure the default values.
Definition:
config-store.cc:192
ns3::ConfigStore::ConfigureAttributes
void ConfigureAttributes()
Configure the attribute values.
Definition:
config-store.cc:185
ns3::IntegerValue
Hold a signed integer type.
Definition:
integer.h:45
ns3::IntegerValue::Get
int64_t Get() const
Definition:
integer.cc:37
ns3::Object
A base class which provides memory management and object aggregation.
Definition:
object.h:89
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition:
ptr.h:77
ns3::Simulator::Destroy
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition:
simulator.cc:142
ns3::Simulator::Run
static void Run()
Run the simulation.
Definition:
simulator.cc:178
ns3::StringValue
Hold variables of type string.
Definition:
string.h:56
ns3::TypeId
a unique identifier for an interface.
Definition:
type-id.h:59
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition:
type-id.cc:931
ns3::Config::SetDefault
void SetDefault(std::string name, const AttributeValue &value)
Definition:
config.cc:890
ns3::Config::RegisterRootNamespaceObject
void RegisterRootNamespaceObject(Ptr< Object > obj)
Definition:
config.cc:1005
NS_ABORT_MSG_UNLESS
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if a condition is false, with a message.
Definition:
abort.h:144
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition:
object-base.h:46
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::MakeIntegerAccessor
Ptr< const AttributeAccessor > MakeIntegerAccessor(T1 a1)
Definition:
integer.h:46
second.cmd
cmd
Definition:
second.py:40
src
config-store
examples
config-store-save.cc
Generated on Sun Mar 3 2024 17:10:55 for ns-3 by
1.9.1