A Discrete-Event Network Simulator
API
config-store.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@cutebugs.net>
19  */
20 
21 #include "config-store.h"
22 #include "raw-text-config.h"
23 #include "ns3/abort.h"
24 #include "ns3/string.h"
25 #include "ns3/log.h"
26 #include "ns3/simulator.h"
27 #include "ns3/attribute-construction-list.h"
28 #include "ns3/enum.h"
29 #include "ns3/boolean.h"
30 #include "ns3/config-store-config.h"
31 #ifdef HAVE_LIBXML2
32 #include "xml-config.h"
33 #endif
34 
35 #include <string>
36 #include <fstream>
37 #include <iostream>
38 #include <unistd.h>
39 #include <cstdlib>
40 
41 
42 namespace ns3 {
43 
44 NS_LOG_COMPONENT_DEFINE ("ConfigStore");
45 
46 NS_OBJECT_ENSURE_REGISTERED (ConfigStore);
47 
48 TypeId
50 {
51  static TypeId tid = TypeId ("ns3::ConfigStore")
53  .SetGroupName ("ConfigStore")
54  .AddAttribute ("Mode",
55  "Configuration mode",
59  ConfigStore::LOAD, "Load",
60  ConfigStore::SAVE, "Save"))
61  .AddAttribute ("Filename",
62  "The file where the configuration should be saved to or loaded from.",
63  StringValue (""),
66  .AddAttribute ("FileFormat",
67  "Type of file format",
71  ConfigStore::XML, "Xml"))
72  .AddAttribute ("SaveDeprecated",
73  "Save DEPRECATED attributes",
74  BooleanValue (true),
77  ;
78  return tid;
79 }
80 TypeId
82 {
83  return GetTypeId ();
84 }
85 
86 
88 {
89  NS_LOG_FUNCTION (this);
91 
92 #ifdef HAVE_LIBXML2
94  {
96  {
97  m_file = new XmlConfigSave ();
98  }
99  else if (m_mode == ConfigStore::LOAD)
100  {
101  m_file = new XmlConfigLoad ();
102  }
103  else
104  {
105  m_file = new NoneFileConfig ();
106  }
107  }
108 #else
110  {
112  {
113  NS_ABORT_MSG ("ConfigStore tried to read or write an XML file but XML is not supported.");
114  }
115  else
116  {
117  m_file = new NoneFileConfig ();
118  }
119  }
120 #endif /* HAVE_LIBXML2 */
121 
123  {
124  if (m_mode == ConfigStore::SAVE)
125  {
126  m_file = new RawTextConfigSave ();
127  }
128  else if (m_mode == ConfigStore::LOAD)
129  {
130  m_file = new RawTextConfigLoad ();
131  }
132  else
133  {
134  m_file = new NoneFileConfig ();
135  }
136  }
139 
140  NS_LOG_FUNCTION (this << ": format: " << m_fileFormat
141  << ", mode: " << m_mode
142  << ", file name: " << m_filename);
143 }
144 
146 {
147  NS_LOG_FUNCTION (this);
148  delete m_file;
149  m_file = 0;
150 }
151 
152 void
154 {
155  NS_LOG_FUNCTION (this << mode);
156  m_mode = mode;
157 }
158 void
160 {
161  NS_LOG_FUNCTION (this << format);
162  m_fileFormat = format;
163 }
164 void
165 ConfigStore::SetFilename (std::string filename)
166 {
167  NS_LOG_FUNCTION (this << filename);
168  m_filename = filename;
169 }
170 void
171 ConfigStore::SetSaveDeprecated (bool saveDeprecated)
172 {
173  NS_LOG_FUNCTION (this << saveDeprecated);
174  m_saveDeprecated = saveDeprecated;
175 }
176 
177 void
179 {
180  NS_LOG_FUNCTION (this);
181  m_file->Attributes ();
182 }
183 
184 void
186 {
187  NS_LOG_FUNCTION (this);
188  m_file->Default ();
189  m_file->Global ();
190 }
191 
192 std::ostream &
193 operator << (std::ostream & os, ConfigStore::Mode & mode)
194 {
195  switch (mode)
196  {
197  case ConfigStore::LOAD: os << "LOAD"; break;
198  case ConfigStore::SAVE: os << "SAVE"; break;
199  case ConfigStore::NONE: os << "NONE"; break;
200  default: os << "UNKNOWN";
201  }
202  return os;
203 }
204 
205 std::ostream &
206 operator << (std::ostream & os, ConfigStore::FileFormat & format)
207 {
208  switch (format)
209  {
210  case ConfigStore::XML: os << "XML"; break;
211  case ConfigStore::RAW_TEXT: os << "RAW_TEXT"; break;
212  }
213  return os;
214 }
215 
216 } // namespace ns3
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
ns3::ConfigStore::m_filename
std::string m_filename
store file name
Definition: config-store.h:125
ns3::BooleanValue
AttributeValue implementation for Boolean.
Definition: boolean.h:37
ns3::ConfigStore::GetInstanceTypeId
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Definition: config-store.cc:81
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::ConfigStore::SetSaveDeprecated
void SetSaveDeprecated(bool saveDeprecated)
Set if to save deprecated attributes.
Definition: config-store.cc:171
ns3::MakeEnumChecker
Ptr< const AttributeChecker > MakeEnumChecker(int v, std::string n, Ts... args)
Make an EnumChecker pre-configured with a set of allowed values by name.
Definition: enum.h:161
ns3::ConfigStore::m_mode
enum Mode m_mode
store mode
Definition: config-store.h:122
ns3::FileConfig::SetFilename
virtual void SetFilename(std::string filename)=0
Set the file name.
ns3::ConfigStore::m_fileFormat
enum FileFormat m_fileFormat
store format
Definition: config-store.h:123
ns3::ConfigStore::SetMode
void SetMode(enum Mode mode)
Set the mode of operation.
Definition: config-store.cc:153
ns3::RawTextConfigSave
A class to enable saving of configuration store in a raw text file.
Definition: raw-text-config.h:36
xml-config.h
ns3::ConfigStore::ConfigStore
ConfigStore()
Definition: config-store.cc:87
ns3::MakeBooleanAccessor
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: boolean.h:85
ns3::ObjectBase::ConstructSelf
void ConstructSelf(const AttributeConstructionList &attributes)
Complete construction of ObjectBase; invoked by derived classes.
Definition: object-base.cc:80
ns3::ConfigStore::Mode
Mode
for ConfigStore operation
Definition: config-store.h:66
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
ns3::EnumValue
Hold variables of type enum.
Definition: enum.h:55
ns3::ConfigStore::m_file
FileConfig * m_file
configuration file
Definition: config-store.h:126
ns3::ConfigStore::NONE
@ NONE
Definition: config-store.h:69
raw-text-config.h
ns3::ConfigStore::~ConfigStore
~ConfigStore()
Definition: config-store.cc:145
ns3::RawTextConfigLoad
A class to enable loading of configuration store from a raw text file.
Definition: raw-text-config.h:56
ns3::FileConfig::Global
virtual void Global(void)=0
Load or save the global values.
ns3::ConfigStore::FileFormat
FileFormat
store format
Definition: config-store.h:76
ns3::ConfigStore::m_saveDeprecated
bool m_saveDeprecated
save deprecated attributes
Definition: config-store.h:124
ns3::MakeBooleanChecker
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
ns3::ConfigStore::ConfigureDefaults
void ConfigureDefaults(void)
Configure the default values.
Definition: config-store.cc:185
ns3::FileConfig::Default
virtual void Default(void)=0
Load or save the default values.
ns3::ConfigStore::XML
@ XML
Definition: config-store.h:77
ns3::StringValue
Hold variables of type string.
Definition: string.h:41
ns3::ConfigStore::SAVE
@ SAVE
Definition: config-store.h:68
ns3::XmlConfigSave
A class to enable saving of configuration store in an XML file.
Definition: xml-config.h:37
ns3::ConfigStore::SetFilename
void SetFilename(std::string filename)
Set the filename.
Definition: config-store.cc:165
ns3::NoneFileConfig
A dummy class (does nothing)
Definition: file-config.h:69
ns3::ConfigStore::SetFileFormat
void SetFileFormat(enum FileFormat format)
Set the file format.
Definition: config-store.cc:159
ns3::FileConfig::Attributes
virtual void Attributes(void)=0
Load or save the attributes values.
ns3::MakeEnumAccessor
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: enum.h:203
ns3::ConfigStore::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition: config-store.cc:49
ns3::ConfigStore::RAW_TEXT
@ RAW_TEXT
Definition: config-store.h:78
config-store.h
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition: log-macros-enabled.h:244
ns3::MakeStringChecker
Ptr< const AttributeChecker > MakeStringChecker(void)
Definition: string.cc:30
ns3::ConfigStore::LOAD
@ LOAD
Definition: config-store.h:67
ns3::FileConfig::SetSaveDeprecated
void SetSaveDeprecated(bool saveDeprecated)
Set if to save deprecated attributes.
Definition: file-config.cc:30
ns3::ConfigStore::ConfigureAttributes
void ConfigureAttributes(void)
Configure the attribute values.
Definition: config-store.cc:178
ns3::operator<<
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:137
ns3::XmlConfigLoad
A class to enable loading of configuration store from an XML file.
Definition: xml-config.h:55
ns3::AttributeConstructionList
List of Attribute name, value and checker triples used to construct Objects.
Definition: attribute-construction-list.h:40
ns3::ObjectBase
Anchor the ns-3 type and attribute system.
Definition: object-base.h:120
ns3::MakeStringAccessor
Ptr< const AttributeAccessor > MakeStringAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: string.h:42
NS_ABORT_MSG
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:50