A Discrete-Event Network Simulator
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),
28  MakeIntegerChecker<int16_t> ())
29  ;
30  return tid;
31  }
32  int16_t m_int16;
33 };
34 
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.
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 
113 }
Example class to demonstrate use of the ns-3 Config Store.
static TypeId GetTypeId(void)
Get the type ID.
int16_t m_int16
value to configure
Parse command-line arguments.
Definition: command-line.h:229
Introspection did not find any typical Config paths.
Definition: config-store.h:60
void ConfigureDefaults(void)
Configure the default values.
void ConfigureAttributes(void)
Configure the attribute values.
Hold a signed integer type.
Definition: integer.h:44
int64_t Get(void) const
Definition: integer.cc:35
A base class which provides memory management and object aggregation.
Definition: object.h:88
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
Hold variables of type string.
Definition: string.h:41
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
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
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
void RegisterRootNamespaceObject(Ptr< Object > obj)
Definition: config.cc:946
#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:45
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:35