A Discrete-Event Network Simulator
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),
29  MakeIntegerChecker<int16_t>());
30  return tid;
31  }
32 
33  int16_t m_int16;
34 };
35 
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.
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 
116 
117  return 0;
118 }
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
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:931
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:890
void RegisterRootNamespaceObject(Ptr< Object > obj)
Definition: config.cc:1005
#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.
Ptr< const AttributeAccessor > MakeIntegerAccessor(T1 a1)
Definition: integer.h:46
cmd
Definition: second.py:40