A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
config-store.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@cutebugs.net>
7 */
8
9#include "config-store.h"
10
11#include "raw-text-config.h"
12
13#include "ns3/abort.h"
14#include "ns3/attribute-construction-list.h"
15#include "ns3/boolean.h"
16#include "ns3/enum.h"
17#include "ns3/log.h"
18#include "ns3/simulator.h"
19#include "ns3/string.h"
20#ifdef HAVE_LIBXML2
21#include "xml-config.h"
22#endif
23
24#include <cstdlib>
25#include <fstream>
26#include <iostream>
27#include <string>
28
29namespace ns3
30{
31
32NS_LOG_COMPONENT_DEFINE("ConfigStore");
33
35
36TypeId
38{
39 static TypeId tid =
40 TypeId("ns3::ConfigStore")
42 .SetGroupName("ConfigStore")
43 .AddAttribute("Mode",
44 "Configuration mode",
48 "None",
50 "Load",
52 "Save"))
53 .AddAttribute("Filename",
54 "The file where the configuration should be saved to or loaded from.",
55 StringValue(""),
58 .AddAttribute(
59 "FileFormat",
60 "Type of file format",
64 .AddAttribute(
65 "SaveDeprecated",
66 "Save DEPRECATED attributes",
67 BooleanValue(true),
71 "OBSOLETE since ns-3.43 as it is no longer needed; deprecated attributes are saved "
72 "only if their value differs from their respective original initial value");
73 return tid;
74}
75
78{
79 return GetTypeId();
80}
81
83{
84 NS_LOG_FUNCTION(this);
86
87#ifdef HAVE_LIBXML2
89 {
91 {
92 m_file = new XmlConfigSave();
93 }
94 else if (m_mode == ConfigStore::LOAD)
95 {
96 m_file = new XmlConfigLoad();
97 }
98 else
99 {
100 m_file = new NoneFileConfig();
101 }
102 }
103#else
105 {
107 {
109 "ConfigStore tried to read or write an XML file but XML is not supported.");
110 }
111 else
112 {
113 m_file = new NoneFileConfig();
114 }
115 }
116#endif /* HAVE_LIBXML2 */
117
119 {
121 {
123 }
124 else if (m_mode == ConfigStore::LOAD)
125 {
127 }
128 else
129 {
130 m_file = new NoneFileConfig();
131 }
132 }
134 NS_LOG_FUNCTION(this << ": format: " << m_fileFormat << ", mode: " << m_mode
135 << ", file name: " << m_filename);
136}
137
139{
140 NS_LOG_FUNCTION(this);
141 delete m_file;
142 m_file = nullptr;
143}
144
145void
147{
148 NS_LOG_FUNCTION(this << mode);
149 m_mode = mode;
150}
151
152void
154{
155 NS_LOG_FUNCTION(this << format);
156 m_fileFormat = format;
157}
158
159void
160ConfigStore::SetFilename(std::string filename)
161{
162 NS_LOG_FUNCTION(this << filename);
163 m_filename = filename;
164}
165
166void
168{
169 NS_LOG_FUNCTION(this << saveDeprecated);
170 m_saveDeprecated = saveDeprecated;
171}
172
173void
179
180void
187
188std::ostream&
189operator<<(std::ostream& os, ConfigStore::Mode& mode)
190{
191 switch (mode)
192 {
194 os << "LOAD";
195 break;
197 os << "SAVE";
198 break;
200 os << "NONE";
201 break;
202 default:
203 os << "UNKNOWN";
204 }
205 return os;
206}
207
208std::ostream&
209operator<<(std::ostream& os, ConfigStore::FileFormat& format)
210{
211 switch (format)
212 {
213 case ConfigStore::XML:
214 os << "XML";
215 break;
217 os << "RAW_TEXT";
218 break;
219 }
220 return os;
221}
222
223} // namespace ns3
List of Attribute name, value and checker triples used to construct Objects.
std::string m_filename
store file name
void SetFilename(std::string filename)
Set the filename.
FileFormat m_fileFormat
store format
FileConfig * m_file
configuration file
static TypeId GetTypeId()
Get the type ID.
Mode
for ConfigStore operation
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
void SetMode(Mode mode)
Set the mode of operation.
bool m_saveDeprecated
save deprecated attributes
void ConfigureAttributes()
Configure the attribute values.
Mode m_mode
store mode
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
Hold variables of type enum.
Definition enum.h:52
virtual void Attributes()=0
Load or save the attributes values.
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:50
Anchor the ns-3 type and attribute system.
void ConstructSelf(const AttributeConstructionList &attributes)
Complete construction of ObjectBase; invoked by derived classes.
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:45
a unique identifier for an interface.
Definition type-id.h:49
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
@ OBSOLETE
Attribute or trace source is not used anymore; simulation fails.
A class to enable loading of configuration store from an XML file.
Definition xml-config.h:49
A class to enable saving of configuration store in an XML file.
Definition xml-config.h:30
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition abort.h:38
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#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:35
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition boolean.cc:113
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition angles.cc:148
Ptr< const AttributeChecker > MakeEnumChecker(T v, std::string n, Ts... args)
Make an EnumChecker pre-configured with a set of allowed values by name.
Definition enum.h:179
Ptr< const AttributeChecker > MakeStringChecker()
Definition string.cc:19
Ptr< const AttributeAccessor > MakeStringAccessor(T1 a1)
Definition string.h:46
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition boolean.h:70
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Definition enum.h:221