A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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),
17  MakeIntegerAccessor (&ConfigExample::m_int16),
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 {
33  Config::SetDefault ("ns3::ConfigExample::TestInt16", IntegerValue (-5));
34 
35  Ptr<ConfigExample> a_obj = CreateObject<ConfigExample> ();
36  NS_ABORT_MSG_UNLESS (a_obj->m_int16 == -5, "Cannot set ConfigExample's integer attribute via Config::SetDefault");
37 
38  Ptr<ConfigExample> b_obj = CreateObject<ConfigExample> ();
39  b_obj->SetAttribute ("TestInt16", IntegerValue (-3));
40  IntegerValue iv;
41  b_obj->GetAttribute ("TestInt16", iv);
42  NS_ABORT_MSG_UNLESS (iv.Get () == -3, "Cannot set ConfigExample'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 }
Doxygen introspection did not find any typical Config paths.
Definition: config-store.h:34
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:60
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
hold variables of type string
Definition: string.h:18
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
Hold a signed integer type.
Definition: integer.h:45
int64_t Get(void) const
int main(int argc, char *argv[])
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
void ConfigureDefaults(void)
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:667
void GetAttribute(std::string name, AttributeValue &value) const
Definition: object-base.cc:214
void RegisterRootNamespaceObject(Ptr< Object > obj)
Definition: config.cc:745
void ConfigureAttributes(void)
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if cond is false.
Definition: abort.h:136
static TypeId GetTypeId(void)
a base class which provides memory management and object aggregation
Definition: object.h:64
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:176
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610