39class ConstructionContext;
54 using std::runtime_error::runtime_error;
57#ifndef DOXYGEN_SHOULD_IGNORE_THIS
59inline std::string trim(
const std::string& str)
61 auto start = std::find_if_not(str.begin(), str.end(), ::isspace);
62 auto end = std::find_if_not(str.rbegin(), str.rend(), ::isspace).base();
63 return (start < end) ? std::string(start, end) : std::string();
102 if constexpr (std::is_same_v<T, bool>) {
105 std::transform(str.begin(), str.end(), str.begin(), ::tolower);
109 else if (str ==
"false") {
113 if (!(iss >> value)) {
114 throw std::invalid_argument(
"Failed to convert attribute value [" + iss.str() +
"] to the specified type");
167 template <
typename T>
170 static_assert(std::is_arithmetic_v<T>,
171 "getTextAs requires a numeric type. Use getText for string content.");
172 static_assert(!std::is_same_v<T, bool>,
"Do not use getTextAs with bool type. Simply assign true. (The presence of the node means true.)");
175 if (iss.str().empty()) {
179 using ValueType = std::conditional_t<
180 std::is_same_v<T, char>
181 || std::is_same_v<T, uint8_t>
182 || std::is_same_v<T, char16_t>
183 || std::is_same_v<T, char32_t>, int, T>;
184 ValueType value = ValueType(defaultValue);
186 if (!(iss >> value)) {
187 throw std::invalid_argument(
"Failed to convert text content [" + iss.str() +
"] to the specified type");
204 virtual std::shared_ptr<IXmlAttribute>
findAttribute(
const std::string& name)
const = 0;
State shared by all factories during one document construction.
Definition ConstructionContext.h:12
Interface for an XML attribute.
Definition XmlInterface.h:71
std::string getValueTrimmed() const
Gets the text content of the attribute with whitespace trimmed.
Definition XmlInterface.h:90
virtual std::string getValue() const =0
Gets the value of the attribute.
virtual std::shared_ptr< IXmlAttribute > nextAttribute() const =0
Advances to the next attribute.
T getValueAs() const
Gets the value of the attribute, converted to the specified type.
Definition XmlInterface.h:99
virtual std::string getName() const =0
Gets the name of the attribute.
Interface for an XML document.
Definition XmlInterface.h:238
virtual std::shared_ptr< IXmlElement > getRootElement() const =0
Gets the root element of the document.
virtual void loadFromString(const std::string &xmlContent)=0
Loads XML content from a string.
virtual void loadFromBuffer(const char *data, size_t size)=0
Loads XML content from buffer.
void loadFromString(const std::vector< char > &xmlContent)
Loads XML content from a vector of characters.
Definition XmlInterface.h:262
Interface for an XML element.
Definition XmlInterface.h:139
virtual XmlElementPtr getPreviousSibling(const std::string &tagName={}) const =0
Gets the previous sibling element.
virtual XmlElementPtr getParent() const =0
Gets the parent element.
virtual XmlElementPtr getNextSibling(const std::string &tagName={}) const =0
Gets the next sibling element.
virtual std::shared_ptr< IXmlAttribute > getFirstAttribute() const =0
Gets the first attribute.
virtual std::string getText() const =0
Gets the text content of the element.
std::string getTextTrimmed() const
Gets the text content of the element with whitespace trimmed.
Definition XmlInterface.h:158
T getTextAs(T defaultValue={}) const
Gets the text content of the element, converted to the specified type.
Definition XmlInterface.h:168
virtual XmlElementPtr getFirstChildElement(const std::string &tagName={}) const =0
Finds the first child element.
virtual std::shared_ptr< IXmlAttribute > findAttribute(const std::string &name) const =0
Finds the first attribute.
virtual std::string getTagName() const =0
Gets the tag name of the element.
Exception for load xml error.
Definition XmlInterface.h:52
std::tuple< const std::string_view, XmlElementPopulator< T > > XmlElementDescriptor
associates an xml node name with and XmlElementPopulator
Definition XmlInterface.h:131
std::vector< XmlElementDescriptor< T > > XmlElementArray
an array type for XmlElementDescriptor instances.
Definition XmlInterface.h:133
std::function< void(factory::ConstructionContext &, const XmlElementPtr &, const std::shared_ptr< T > &)> XmlElementPopulator
function type for populating a field from an IXmlElement
Definition XmlInterface.h:129
std::shared_ptr< IXmlElement > XmlElementPtr
shared pointer to IXmlElement
Definition XmlInterface.h:127
object model for musx file (enigmaxml)
Definition BaseClasses.h:38