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 {
44  cmd.Parse (argc, argv);
45 
46  Config::SetDefault ("ns3::ConfigExample::TestInt16", IntegerValue (-5));
47 
48  Ptr<ConfigExample> a_obj = CreateObject<ConfigExample> ();
49  NS_ABORT_MSG_UNLESS (a_obj->m_int16 == -5, "Cannot set ConfigExample's integer attribute via Config::SetDefault");
50 
51  Ptr<ConfigExample> b_obj = CreateObject<ConfigExample> ();
52  b_obj->SetAttribute ("TestInt16", IntegerValue (-3));
53  IntegerValue iv;
54  b_obj->GetAttribute ("TestInt16", iv);
55  NS_ABORT_MSG_UNLESS (iv.Get () == -3, "Cannot set ConfigExample's integer attribute via SetAttribute");
56 
57  // These test objects are not rooted in any ns-3 configuration namespace.
58  // This is usually done automatically for ns3 nodes and channels, but
59  // we can establish a new root and anchor one of them there (note; we
60  // can't use two objects of the same type as roots). Rooting one of these
61  // is necessary for it to show up in the config namespace so that
62  // ConfigureAttributes() will work below.
64 
65 #ifdef HAVE_LIBXML2
66  // Output config store to XML format
67  Config::SetDefault ("ns3::ConfigStore::Filename", StringValue ("output-attributes.xml"));
68  Config::SetDefault ("ns3::ConfigStore::FileFormat", StringValue ("Xml"));
69  Config::SetDefault ("ns3::ConfigStore::Mode", StringValue ("Save"));
70  ConfigStore outputConfig;
71  outputConfig.ConfigureDefaults ();
72  outputConfig.ConfigureAttributes ();
73 #endif /* HAVE_LIBXML2 */
74 
75  // Output config store to txt format
76  Config::SetDefault ("ns3::ConfigStore::Filename", StringValue ("output-attributes.txt"));
77  Config::SetDefault ("ns3::ConfigStore::FileFormat", StringValue ("RawText"));
78  Config::SetDefault ("ns3::ConfigStore::Mode", StringValue ("Save"));
79  ConfigStore outputConfig2;
80  outputConfig2.ConfigureDefaults ();
81  outputConfig2.ConfigureAttributes ();
82 
83  Simulator::Run ();
84 
86 }
Introspection did not find any typical Config paths.
Definition: config-store.h:59
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:45
Hold variables of type string.
Definition: string.h:41
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
Hold a signed integer type.
Definition: integer.h:44
cmd
Definition: second.py:35
int16_t m_int16
value to configure
void RegisterRootNamespaceObject(Ptr< Object > obj)
Definition: config.cc:860
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
Parse command-line arguments.
Definition: command-line.h:213
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
void ConfigureDefaults(void)
Configure the default values.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void ConfigureAttributes(void)
Configure the attribute values.
#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:782
Example class to demonstrate use of the ns-3 Config Store.
static TypeId GetTypeId(void)
Get the type ID.
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:185
a unique identifier for an interface.
Definition: type-id.h:58
void GetAttribute(std::string name, AttributeValue &value) const
Get the value of an attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:223
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
int64_t Get(void) const
Definition: integer.cc:35