mirror of
https://github.com/Kitware/CMake.git
synced 2026-04-29 18:51:05 -05:00
cmXMLWriter: Add RAII helpers to allow DOM-like syntax
Use RAII for cmXMLWriter::StartElement/EndElement to make nesting automatic.
This commit is contained in:
committed by
Brad King
parent
0e362b23e1
commit
8401b6ac4e
@@ -133,4 +133,56 @@ private:
|
||||
bool IsContent;
|
||||
};
|
||||
|
||||
class cmXMLElement; // IWYU pragma: keep
|
||||
|
||||
class cmXMLDocument
|
||||
{
|
||||
public:
|
||||
cmXMLDocument(cmXMLWriter& xml)
|
||||
: xmlwr(xml)
|
||||
{
|
||||
xmlwr.StartDocument();
|
||||
}
|
||||
~cmXMLDocument() { xmlwr.EndDocument(); }
|
||||
private:
|
||||
friend class cmXMLElement;
|
||||
cmXMLWriter& xmlwr;
|
||||
};
|
||||
|
||||
class cmXMLElement
|
||||
{
|
||||
public:
|
||||
cmXMLElement(cmXMLWriter& xml, const char* tag)
|
||||
: xmlwr(xml)
|
||||
{
|
||||
xmlwr.StartElement(tag);
|
||||
}
|
||||
cmXMLElement(cmXMLElement const& par, const char* tag)
|
||||
: xmlwr(par.xmlwr)
|
||||
{
|
||||
xmlwr.StartElement(tag);
|
||||
}
|
||||
cmXMLElement(cmXMLDocument const& doc, const char* tag)
|
||||
: xmlwr(doc.xmlwr)
|
||||
{
|
||||
xmlwr.StartElement(tag);
|
||||
}
|
||||
~cmXMLElement() { xmlwr.EndElement(); }
|
||||
|
||||
template <typename T>
|
||||
cmXMLElement& Attribute(const char* name, T const& value)
|
||||
{
|
||||
xmlwr.Attribute(name, value);
|
||||
return *this;
|
||||
}
|
||||
template <typename T>
|
||||
void Content(T const& content)
|
||||
{
|
||||
xmlwr.Content(content);
|
||||
}
|
||||
|
||||
private:
|
||||
cmXMLWriter& xmlwr;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user