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 
9 class ConfigExample : public Object
10 {
11 public:
12  static TypeId GetTypeId (void) {
13  static TypeId tid = TypeId ("ns3::ConfigExample")
14  .SetParent<Object> ()
15  .AddAttribute ("TestInt16", "help text",
16  IntegerValue (-2),
18  MakeIntegerChecker<int16_t> ())
19  ;
20  return tid;
21  }
22  int16_t m_int16;
23 };
24 
26 
27 // Assign a new default value to A::TestInt16 (-5)
28 // Configure a TestInt16 value for a special instance of A (to -3)
29 // View the output from the config store
30 //
31 int main (int argc, char *argv[])
32 {
34  cmd.Parse (argc, argv);
35 
36  Config::SetDefault ("ns3::ConfigExample::TestInt16", IntegerValue (-5));
37 
38  Ptr<ConfigExample> a_obj = CreateObject<ConfigExample> ();
39  NS_ABORT_MSG_UNLESS (a_obj->m_int16 == -5, "Cannot set ConfigExample's integer attribute via Config::SetDefault");
40 
41  Ptr<ConfigExample> b_obj = CreateObject<ConfigExample> ();
42  b_obj->SetAttribute ("TestInt16", IntegerValue (-3));
43  IntegerValue iv;
44  b_obj->GetAttribute ("TestInt16", iv);
45  NS_ABORT_MSG_UNLESS (iv.Get () == -3, "Cannot set ConfigExample's integer attribute via SetAttribute");
46 
47  // These test objects are not rooted in any ns-3 configuration namespace.
48  // This is usually done automatically for ns3 nodes and channels, but
49  // we can establish a new root and anchor one of them there (note; we
50  // can't use two objects of the same type as roots). Rooting one of these
51  // is necessary for it to show up in the config namespace so that
52  // ConfigureAttributes() will work below.
54 
55 #ifdef HAVE_LIBXML2
56  // Output config store to XML format
57  Config::SetDefault ("ns3::ConfigStore::Filename", StringValue ("output-attributes.xml"));
58  Config::SetDefault ("ns3::ConfigStore::FileFormat", StringValue ("Xml"));
59  Config::SetDefault ("ns3::ConfigStore::Mode", StringValue ("Save"));
60  ConfigStore outputConfig;
61  outputConfig.ConfigureDefaults ();
62  outputConfig.ConfigureAttributes ();
63 #endif /* HAVE_LIBXML2 */
64 
65  // Output config store to txt format
66  Config::SetDefault ("ns3::ConfigStore::Filename", StringValue ("output-attributes.txt"));
67  Config::SetDefault ("ns3::ConfigStore::FileFormat", StringValue ("RawText"));
68  Config::SetDefault ("ns3::ConfigStore::Mode", StringValue ("Save"));
69  ConfigStore outputConfig2;
70  outputConfig2.ConfigureDefaults ();
71  outputConfig2.ConfigureAttributes ();
72 
73  Simulator::Run ();
74 
76 }
Introspection did not find any typical Config paths.
Definition: config-store.h:54
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
Hold variables of type string.
Definition: string.h:41
static void Run(void)
Run the simulation.
Definition: simulator.cc:201
Hold a signed integer type.
Definition: integer.h:44
void RegisterRootNamespaceObject(Ptr< Object > obj)
Definition: config.cc:852
tuple cmd
Definition: second.py:35
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
int64_t Get(void) const
Definition: integer.cc:35
Parse command-line arguments.
Definition: command-line.h:205
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:165
void ConfigureDefaults(void)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void GetAttribute(std::string name, AttributeValue &value) const
Get the value of an attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:229
void ConfigureAttributes(void)
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if a condition is false, with a message.
Definition: abort.h:144
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:774
static TypeId GetTypeId(void)
void Parse(int argc, char *argv[])
Parse the program arguments.
A base class which provides memory management and object aggregation.
Definition: object.h:87
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:191
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904