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