A Discrete-Event Network Simulator
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
6using 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 */
19class ConfigExample : public Object
20{
21 public:
22 /**
23 * \brief Get the type ID.
24 * \return the object TypeId
25 */
27 {
28 static TypeId tid = TypeId("ns3::ConfigExample")
30 .AddAttribute("TestInt16",
31 "help text",
32 IntegerValue(-2),
34 MakeIntegerChecker<int16_t>());
35 return tid;
36 }
37
38 int16_t m_int16; ///< value to configure
39};
40
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//
47int
48main(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.
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
119
121
122 return 0;
123}
Example class to demonstrate use of the ns-3 Config Store.
static TypeId GetTypeId()
Get the type ID.
int16_t m_int16
value to configure
Parse command-line arguments.
Definition: command-line.h:232
Introspection did not find any typical Config paths.
Definition: config-store.h:61
void ConfigureDefaults()
Configure the default values.
void ConfigureAttributes()
Configure the attribute values.
Hold a signed integer type.
Definition: integer.h:45
int64_t Get() const
Definition: integer.cc:37
A base class which provides memory management and object aggregation.
Definition: object.h:89
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
Hold variables of type string.
Definition: string.h:56
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:932
Ptr< const AttributeAccessor > MakeIntegerAccessor(T1 a1)
Definition: integer.h:46
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:894
void RegisterRootNamespaceObject(Ptr< Object > obj)
Definition: config.cc:1009
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if a condition is false, with a message.
Definition: abort.h:144
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.