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