14#include "ns3/config.h"
15#include "ns3/fatal-error.h"
16#include "ns3/global-value.h"
18#include "ns3/string.h"
20#include <libxml/encoding.h>
21#include <libxml/xmlreader.h>
22#include <libxml/xmlwriter.h>
46 m_writer = xmlNewTextWriterFilename(filename.c_str(), 0);
51 rc = xmlTextWriterSetIndent(
m_writer, 1);
59 rc = xmlTextWriterStartDocument(
m_writer,
nullptr,
"utf-8",
nullptr);
67 rc = xmlTextWriterStartElement(
m_writer, BAD_CAST
"ns3");
86 rc = xmlTextWriterEndDocument(
m_writer);
108 void StartVisitTypeId(std::string name)
override
113 void DoVisitAttribute(std::string name, std::string defaultValue)
override
115 TypeId tid = TypeId::LookupByName(m_typeid);
117 std::string originalInitialValue;
118 std::string valueTypeName;
122 if (tmp.
name == name)
126 valueTypeName = tmp.
checker->GetValueTypeName();
130 if (valueTypeName ==
"ns3::CallbackValue")
132 NS_LOG_WARN(
"Global attribute " << m_typeid <<
"::" << name
133 <<
" was not saved because it is a CallbackValue");
136 if (supportLevel == TypeId::SupportLevel::OBSOLETE)
138 NS_LOG_WARN(
"Global attribute " << m_typeid <<
"::" << name
139 <<
" was not saved because it is OBSOLETE");
142 if (supportLevel == TypeId::SupportLevel::DEPRECATED &&
143 defaultValue == originalInitialValue)
146 << m_typeid <<
"::" << name
147 <<
" was not saved because it is DEPRECATED and its value has not "
148 "changed from the original initial value");
153 rc = xmlTextWriterStartElement(m_writer, BAD_CAST
"default");
158 std::string fullname = m_typeid +
"::" + name;
159 rc = xmlTextWriterWriteAttribute(m_writer, BAD_CAST
"name", BAD_CAST fullname.c_str());
164 rc = xmlTextWriterWriteAttribute(m_writer,
166 BAD_CAST defaultValue.c_str());
171 rc = xmlTextWriterEndElement(m_writer);
179 std::string m_typeid;
182 XmlDefaultIterator iterator = XmlDefaultIterator(
m_writer);
198 void DoVisitAttribute(
Ptr<Object> object, std::string name)
override
201 TypeId tid =
object->GetInstanceTypeId();
203 auto [found, inTid, attr] = TypeId::FindAttribute(tid, name);
207 if (attr.checker && attr.checker->GetValueTypeName() ==
"ns3::CallbackValue")
210 <<
" was not saved because it is a CallbackValue");
213 auto supportLevel = attr.supportLevel;
214 if (supportLevel == TypeId::SupportLevel::OBSOLETE)
217 <<
" was not saved because it is OBSOLETE");
221 std::string originalInitialValue =
222 attr.originalInitialValue->SerializeToString(attr.checker);
223 object->GetAttribute(name, str,
true);
225 if (supportLevel == TypeId::SupportLevel::DEPRECATED &&
226 str.Get() == originalInitialValue)
230 <<
" was not saved because it is DEPRECATED and its value has not "
231 "changed from the original initial value");
237 rc = xmlTextWriterStartElement(m_writer, BAD_CAST
"value");
242 rc = xmlTextWriterWriteAttribute(m_writer,
244 BAD_CAST GetCurrentPath().c_str());
250 xmlTextWriterWriteAttribute(m_writer, BAD_CAST
"value", BAD_CAST str.Get().c_str());
255 rc = xmlTextWriterEndElement(m_writer);
265 XmlTextAttributeIterator iter = XmlTextAttributeIterator(
m_writer);
276 (*i)->GetValue(value);
278 rc = xmlTextWriterStartElement(
m_writer, BAD_CAST
"global");
284 xmlTextWriterWriteAttribute(
m_writer, BAD_CAST
"name", BAD_CAST(*i)->GetName().c_str());
289 rc = xmlTextWriterWriteAttribute(
m_writer, BAD_CAST
"value", BAD_CAST value.Get().c_str());
294 rc = xmlTextWriterEndElement(
m_writer);
322 xmlTextReaderPtr reader = xmlNewTextReaderFilename(
m_filename.c_str());
323 if (reader ==
nullptr)
328 rc = xmlTextReaderRead(reader);
331 const xmlChar* type = xmlTextReaderConstName(reader);
336 if (std::string((
char*)type) ==
"default")
338 xmlChar* name = xmlTextReaderGetAttribute(reader, BAD_CAST
"name");
343 xmlChar* value = xmlTextReaderGetAttribute(reader, BAD_CAST
"value");
344 if (value ==
nullptr)
348 NS_LOG_DEBUG(
"default=" << (
char*)name <<
", value=" << value);
353 rc = xmlTextReaderRead(reader);
355 xmlFreeTextReader(reader);
361 xmlTextReaderPtr reader = xmlNewTextReaderFilename(
m_filename.c_str());
362 if (reader ==
nullptr)
367 rc = xmlTextReaderRead(reader);
370 const xmlChar* type = xmlTextReaderConstName(reader);
375 if (std::string((
char*)type) ==
"global")
377 xmlChar* name = xmlTextReaderGetAttribute(reader, BAD_CAST
"name");
382 xmlChar* value = xmlTextReaderGetAttribute(reader, BAD_CAST
"value");
383 if (value ==
nullptr)
387 NS_LOG_DEBUG(
"global=" << (
char*)name <<
", value=" << value);
392 rc = xmlTextReaderRead(reader);
394 xmlFreeTextReader(reader);
400 xmlTextReaderPtr reader = xmlNewTextReaderFilename(
m_filename.c_str());
401 if (reader ==
nullptr)
406 rc = xmlTextReaderRead(reader);
409 const xmlChar* type = xmlTextReaderConstName(reader);
414 if (std::string((
char*)type) ==
"value")
416 xmlChar* path = xmlTextReaderGetAttribute(reader, BAD_CAST
"path");
421 xmlChar* value = xmlTextReaderGetAttribute(reader, BAD_CAST
"value");
422 if (value ==
nullptr)
426 NS_LOG_DEBUG(
"path=" << (
char*)path <<
", value=" << (
char*)value);
431 rc = xmlTextReaderRead(reader);
433 xmlFreeTextReader(reader);
Iterator to iterate on the default values of attributes of an ns3::Object.
Iterator to iterate on the values of attributes of an ns3::Object.
static Iterator Begin()
The Begin iterator.
static Iterator End()
The End iterator.
Smart pointer class similar to boost::intrusive_ptr.
Hold variables of type string.
a unique identifier for an interface.
std::size_t GetAttributeN() const
Get the number of attributes.
TypeId::AttributeInformation GetAttribute(std::size_t i) const
Get Attribute information by index.
SupportLevel
The level of support or deprecation for attributes or trace sources.
void Global() override
Load or save the global values.
void SetFilename(std::string filename) override
Set the file name.
void Default() override
Load or save the default values.
std::string m_filename
the file name
void Attributes() override
Load or save the attributes values.
~XmlConfigLoad() override
void Global() override
Load or save the global values.
void Attributes() override
Load or save the attributes values.
~XmlConfigSave() override
xmlTextWriterPtr m_writer
XML writer.
void SetFilename(std::string filename) override
Set the file name.
void Default() override
Load or save the default values.
void SetGlobal(std::string name, const AttributeValue &value)
void SetDefault(std::string name, const AttributeValue &value)
void Set(std::string path, const AttributeValue &value)
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
xmlTextWriter * xmlTextWriterPtr