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 // NS_DEPRECATED_3_43
65 .AddAttribute(
66 "SaveDeprecated",
67 "Save DEPRECATED attributes",
68 BooleanValue(true),
72 "OBSOLETE since ns-3.43 as it is no longer needed; deprecated attributes are saved "
73 "only if their value differs from their respective original initial value");
74 return tid;
75}
76
79{
80 return GetTypeId();
81}
82
84{
85 NS_LOG_FUNCTION(this);
87
88#ifdef HAVE_LIBXML2
90 {
92 {
93 m_file = new XmlConfigSave();
94 }
95 else if (m_mode == ConfigStore::LOAD)
96 {
97 m_file = new XmlConfigLoad();
98 }
99 else
100 {
101 m_file = new NoneFileConfig();
102 }
103 }
104#else
106 {
108 {
110 "ConfigStore tried to read or write an XML file but XML is not supported.");
111 }
112 else
113 {
114 m_file = new NoneFileConfig();
115 }
116 }
117#endif /* HAVE_LIBXML2 */
118
120 {
122 {
124 }
125 else if (m_mode == ConfigStore::LOAD)
126 {
128 }
129 else
130 {
131 m_file = new NoneFileConfig();
132 }
133 }
135 NS_LOG_FUNCTION(this << ": format: " << m_fileFormat << ", mode: " << m_mode
136 << ", file name: " << m_filename);
137}
138
140{
141 NS_LOG_FUNCTION(this);
142 delete m_file;
143 m_file = nullptr;
144}
145
146void
148{
149 NS_LOG_FUNCTION(this << mode);
150 m_mode = mode;
151}
152
153void
155{
156 NS_LOG_FUNCTION(this << format);
157 m_fileFormat = format;
158}
159
160void
161ConfigStore::SetFilename(std::string filename)
162{
163 NS_LOG_FUNCTION(this << filename);
164 m_filename = filename;
165}
166
167void
169{
170 NS_LOG_FUNCTION(this << saveDeprecated);
171 m_saveDeprecated = saveDeprecated;
172}
173
174void
180
181void
188
189std::ostream&
190operator<<(std::ostream& os, ConfigStore::Mode& mode)
191{
192 switch (mode)
193 {
195 os << "LOAD";
196 break;
198 os << "SAVE";
199 break;
201 os << "NONE";
202 break;
203 default:
204 os << "UNKNOWN";
205 }
206 return os;
207}
208
209std::ostream&
210operator<<(std::ostream& os, ConfigStore::FileFormat& format)
211{
212 switch (format)
213 {
214 case ConfigStore::XML:
215 os << "XML";
216 break;
218 os << "RAW_TEXT";
219 break;
220 }
221 return os;
222}
223
224} // namespace ns3
List of Attribute name, value and checker triples used to construct Objects.
AttributeValue implementation for Boolean.
Definition boolean.h:26
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
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition boolean.cc:113
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:70
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:221
Ptr< const AttributeChecker > MakeStringChecker()
Definition string.cc:19
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:46
#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.
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