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/config-store-config.h"
17#include "ns3/enum.h"
18#include "ns3/log.h"
19#include "ns3/simulator.h"
20#include "ns3/string.h"
21#ifdef HAVE_LIBXML2
22#include "xml-config.h"
23#endif
24
25#include <cstdlib>
26#include <fstream>
27#include <iostream>
28#include <string>
29#include <unistd.h>
30
31namespace ns3
32{
33
34NS_LOG_COMPONENT_DEFINE("ConfigStore");
35
37
38TypeId
40{
41 static TypeId tid =
42 TypeId("ns3::ConfigStore")
44 .SetGroupName("ConfigStore")
45 .AddAttribute("Mode",
46 "Configuration mode",
50 "None",
52 "Load",
54 "Save"))
55 .AddAttribute("Filename",
56 "The file where the configuration should be saved to or loaded from.",
57 StringValue(""),
60 .AddAttribute(
61 "FileFormat",
62 "Type of file format",
66 .AddAttribute(
67 "SaveDeprecated",
68 "Save DEPRECATED attributes",
69 BooleanValue(true),
73 "OBSOLETE since ns-3.43 as it is no longer needed; deprecated attributes are saved "
74 "only if their value differs from their respective original initial value");
75 return tid;
76}
77
80{
81 return GetTypeId();
82}
83
85{
86 NS_LOG_FUNCTION(this);
88
89#ifdef HAVE_LIBXML2
91 {
93 {
94 m_file = new XmlConfigSave();
95 }
96 else if (m_mode == ConfigStore::LOAD)
97 {
98 m_file = new XmlConfigLoad();
99 }
100 else
101 {
102 m_file = new NoneFileConfig();
103 }
104 }
105#else
107 {
109 {
111 "ConfigStore tried to read or write an XML file but XML is not supported.");
112 }
113 else
114 {
115 m_file = new NoneFileConfig();
116 }
117 }
118#endif /* HAVE_LIBXML2 */
119
121 {
123 {
125 }
126 else if (m_mode == ConfigStore::LOAD)
127 {
129 }
130 else
131 {
132 m_file = new NoneFileConfig();
133 }
134 }
136 NS_LOG_FUNCTION(this << ": format: " << m_fileFormat << ", mode: " << m_mode
137 << ", file name: " << m_filename);
138}
139
141{
142 NS_LOG_FUNCTION(this);
143 delete m_file;
144 m_file = nullptr;
145}
146
147void
149{
150 NS_LOG_FUNCTION(this << mode);
151 m_mode = mode;
152}
153
154void
156{
157 NS_LOG_FUNCTION(this << format);
158 m_fileFormat = format;
159}
160
161void
162ConfigStore::SetFilename(std::string filename)
163{
164 NS_LOG_FUNCTION(this << filename);
165 m_filename = filename;
166}
167
168void
170{
171 NS_LOG_FUNCTION(this << saveDeprecated);
172 m_saveDeprecated = saveDeprecated;
173}
174
175void
181
182void
189
190std::ostream&
191operator<<(std::ostream& os, ConfigStore::Mode& mode)
192{
193 switch (mode)
194 {
196 os << "LOAD";
197 break;
199 os << "SAVE";
200 break;
202 os << "NONE";
203 break;
204 default:
205 os << "UNKNOWN";
206 }
207 return os;
208}
209
210std::ostream&
211operator<<(std::ostream& os, ConfigStore::FileFormat& format)
212{
213 switch (format)
214 {
215 case ConfigStore::XML:
216 os << "XML";
217 break;
219 os << "RAW_TEXT";
220 break;
221 }
222 return os;
223}
224
225} // 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:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
@ OBSOLETE
Attribute or trace source is not used anymore; simulation fails.
Definition type-id.h:65
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