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