A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
config-store-save.cc
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 A : public Object
10 {
11 public:
12  static TypeId GetTypeId (void) {
13  static TypeId tid = TypeId ("ns3::A")
14  .SetParent<Object> ()
15  .AddAttribute ("TestInt16", "help text",
16  IntegerValue (-2),
17  MakeIntegerAccessor (&A::m_int16),
18  MakeIntegerChecker<int16_t> ())
19  ;
20  return tid;
21  }
22  int16_t m_int16;
23 };
24 
25 NS_OBJECT_ENSURE_REGISTERED (A);
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 {
33  Config::SetDefault ("ns3::A::TestInt16", IntegerValue (-5));
34 
35  Ptr<A> a_obj = CreateObject<A> ();
36  NS_ABORT_MSG_UNLESS (a_obj->m_int16 == -5, "Cannot set A's integer attribute via Config::SetDefault");
37 
38  Ptr<A> a2_obj = CreateObject<A> ();
39  a2_obj->SetAttribute ("TestInt16", IntegerValue (-3));
40  IntegerValue iv;
41  a2_obj->GetAttribute ("TestInt16", iv);
42  NS_ABORT_MSG_UNLESS (iv.Get () == -3, "Cannot set A's integer attribute via SetAttribute");
43 
44  // These test objects are not rooted in any ns-3 configuration namespace.
45  // This is usually done automatically for ns3 nodes and channels, but
46  // we can establish a new root and anchor one of them there (note; we
47  // can't use two objects of the same type as roots). Rooting one of these
48  // is necessary for it to show up in the config namespace so that
49  // ConfigureAttributes() will work below.
51 
52 #ifdef HAVE_LIBXML2
53  // Output config store to XML format
54  Config::SetDefault ("ns3::ConfigStore::Filename", StringValue ("output-attributes.xml"));
55  Config::SetDefault ("ns3::ConfigStore::FileFormat", StringValue ("Xml"));
56  Config::SetDefault ("ns3::ConfigStore::Mode", StringValue ("Save"));
57  ConfigStore outputConfig;
58  outputConfig.ConfigureDefaults ();
59  outputConfig.ConfigureAttributes ();
60 #endif /* HAVE_LIBXML2 */
61 
62  // Output config store to txt format
63  Config::SetDefault ("ns3::ConfigStore::Filename", StringValue ("output-attributes.txt"));
64  Config::SetDefault ("ns3::ConfigStore::FileFormat", StringValue ("RawText"));
65  Config::SetDefault ("ns3::ConfigStore::Mode", StringValue ("Save"));
66  ConfigStore outputConfig2;
67  outputConfig2.ConfigureDefaults ();
68  outputConfig2.ConfigureAttributes ();
69 
70  Simulator::Run ();
71 
73 }
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
hold variables of type string
Definition: string.h:19
static void Run(void)
Definition: simulator.cc:157
Hold a signed integer type.
Definition: integer.h:45
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if cond is false.
Definition: abort.h:131
static void Destroy(void)
Definition: simulator.cc:121
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:667
void GetAttribute(std::string name, AttributeValue &value) const
Definition: object-base.cc:198
void RegisterRootNamespaceObject(Ptr< Object > obj)
Definition: config.cc:745
a base class which provides memory management and object aggregation
Definition: object.h:63
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:160
a unique identifier for an interface.
Definition: type-id.h:44
TypeId SetParent(TypeId tid)
Definition: type-id.cc:471