author | Tom Henderson <tomh@tomh.org> |
Tue, 16 Aug 2011 15:49:05 -0700 | |
changeset 7438 | 3aeb5ac5af62 |
child 7453 | 1626895e0ebc |
permissions | -rw-r--r-- |
7438
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
2 |
#include "ns3/core-module.h" |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
3 |
#include "ns3/config-store-module.h" |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
4 |
|
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
5 |
#include <iostream> |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
6 |
|
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
7 |
using namespace ns3; |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
8 |
|
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
9 |
class A : public Object |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
10 |
{ |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
11 |
public: |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
12 |
static TypeId GetTypeId (void) { |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
13 |
static TypeId tid = TypeId ("ns3::A") |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
14 |
.SetParent<Object> () |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
15 |
.AddAttribute ("TestInt16", "help text", |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
16 |
IntegerValue (-2), |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
17 |
MakeIntegerAccessor (&A::m_int16), |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
18 |
MakeIntegerChecker<int16_t> ()) |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
19 |
; |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
20 |
return tid; |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
21 |
} |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
22 |
int16_t m_int16; |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
23 |
}; |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
24 |
|
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
25 |
NS_OBJECT_ENSURE_REGISTERED (A); |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
26 |
|
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
27 |
// Assign a new default value to A::TestInt16 (-5) |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
28 |
// Configure a TestInt16 value for a special instance of A (to -3) |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
29 |
// View the output from the config store |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
30 |
// |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
31 |
int main (int argc, char *argv[]) |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
32 |
{ |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
33 |
Config::SetDefault ("ns3::A::TestInt16", IntegerValue (-5)); |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
34 |
|
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
35 |
Ptr<A> a_obj = CreateObject<A> (); |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
36 |
NS_ABORT_MSG_UNLESS (a_obj->m_int16 == -5, "Cannot set A's integer attribute via Config::SetDefault"); |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
37 |
|
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
38 |
Ptr<A> a2_obj = CreateObject<A> (); |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
39 |
a2_obj->SetAttribute ("TestInt16", IntegerValue (-3)); |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
40 |
IntegerValue iv; |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
41 |
a2_obj->GetAttribute ("TestInt16", iv); |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
42 |
NS_ABORT_MSG_UNLESS (iv.Get () == -3, "Cannot set A's integer attribute via SetAttribute"); |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
43 |
|
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
44 |
// These test objects are not rooted in any ns-3 configuration namespace. |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
45 |
// This is usually done automatically for ns3 nodes and channels, but |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
46 |
// we can establish a new root and anchor one of them there (note; we |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
47 |
// can't use two objects of the same type as roots). Rooting one of these |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
48 |
// is necessary for it to show up in the config namespace so that |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
49 |
// ConfigureAttributes() will work below. |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
50 |
Config::RegisterRootNamespaceObject (a2_obj); |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
51 |
|
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
52 |
// Output config store to XML format |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
53 |
Config::SetDefault ("ns3::ConfigStore::Filename", StringValue ("output-attributes.xml")); |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
54 |
Config::SetDefault ("ns3::ConfigStore::FileFormat", StringValue ("Xml")); |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
55 |
Config::SetDefault ("ns3::ConfigStore::Mode", StringValue ("Save")); |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
56 |
ConfigStore outputConfig; |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
57 |
outputConfig.ConfigureDefaults (); |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
58 |
outputConfig.ConfigureAttributes (); |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
59 |
|
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
60 |
// Output config store to txt format |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
61 |
Config::SetDefault ("ns3::ConfigStore::Filename", StringValue ("output-attributes.txt")); |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
62 |
Config::SetDefault ("ns3::ConfigStore::FileFormat", StringValue ("RawText")); |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
63 |
Config::SetDefault ("ns3::ConfigStore::Mode", StringValue ("Save")); |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
64 |
ConfigStore outputConfig2; |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
65 |
outputConfig2.ConfigureDefaults (); |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
66 |
outputConfig2.ConfigureAttributes (); |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
67 |
|
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
68 |
Simulator::Run (); |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
69 |
|
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
70 |
Simulator::Destroy (); |
3aeb5ac5af62
Add config-store example and update documentation
Tom Henderson <tomh@tomh.org>
parents:
diff
changeset
|
71 |
} |