From 74361d859354e4416285cd803b1b0075be1fe514 Mon Sep 17 00:00:00 2001 From: B Stack Date: Mon, 5 Apr 2021 11:02:07 -0400 Subject: add upstream 11.9 --- zenXml/zenxml/cvrt_struc.h | 35 ++++----- zenXml/zenxml/cvrt_text.h | 32 ++++---- zenXml/zenxml/dom.h | 78 ++++++++----------- zenXml/zenxml/parser.h | 6 +- zenXml/zenxml/xml.h | 181 +++++++++++++++++++++------------------------ 5 files changed, 153 insertions(+), 179 deletions(-) (limited to 'zenXml/zenxml') diff --git a/zenXml/zenxml/cvrt_struc.h b/zenXml/zenxml/cvrt_struc.h index 9d484f75..a437604c 100644 --- a/zenXml/zenxml/cvrt_struc.h +++ b/zenXml/zenxml/cvrt_struc.h @@ -84,17 +84,17 @@ public: //Conversion from arbitrary types to an XML element enum class ValueType { - STL_CONTAINER, - STL_PAIR, - OTHER, + stlContainer, + stlPair, + other, }; template using GetValueType = std::integral_constant::value != TEXT_TYPE_OTHER ? ValueType::OTHER : //some string classes are also STL containers, so check this first - IsStlContainer::value ? ValueType::STL_CONTAINER : - IsStlPair ::value ? ValueType::STL_PAIR : - ValueType::OTHER>; + GetTextType ::value != TextType::other ? ValueType::other : //some string classes are also STL containers, so check this first + IsStlContainer::value ? ValueType::stlContainer : + IsStlPair ::value ? ValueType::stlPair : + ValueType::other>; template @@ -109,7 +109,7 @@ struct ConvertElement; //partial specialization: handle conversion for all STL-container types! template -struct ConvertElement +struct ConvertElement { void writeStruc(const T& value, XmlElement& output) const { @@ -121,19 +121,20 @@ struct ConvertElement } bool readStruc(const XmlElement& input, T& value) const { - bool success = true; value.clear(); - auto itPair = input.getChildren("Item"); - for (auto it = itPair.first; it != itPair.second; ++it) + bool success = true; + const auto itPair = input.getChildren("Item"); + + std::for_each(itPair.first, itPair.second, [&](const XmlElement& xmlChild) { - typename T::value_type childVal; //MSVC 2010 bug: cannot put this into a lambda body - if (zen::readStruc(*it, childVal)) - value.insert(value.end(), childVal); + typename T::value_type childVal; + if (zen::readStruc(xmlChild, childVal)) + value.insert(value.end(), std::move(childVal)); else success = false; //should we support insertion of partially-loaded struct?? - } + }); return success; } }; @@ -141,7 +142,7 @@ struct ConvertElement //partial specialization: handle conversion for std::pair template -struct ConvertElement +struct ConvertElement { void writeStruc(const T& value, XmlElement& output) const { @@ -169,7 +170,7 @@ struct ConvertElement //partial specialization: not a pure structured type, try text conversion (thereby respect user specializations of writeText()/readText()) template -struct ConvertElement +struct ConvertElement { void writeStruc(const T& value, XmlElement& output) const { diff --git a/zenXml/zenxml/cvrt_text.h b/zenXml/zenxml/cvrt_text.h index 058ffa30..c06a62e0 100644 --- a/zenXml/zenxml/cvrt_text.h +++ b/zenXml/zenxml/cvrt_text.h @@ -118,22 +118,22 @@ public: //Conversion from arbitrary types to text (for use with XML elements and attributes) -enum TextType +enum class TextType { - TEXT_TYPE_BOOL, - TEXT_TYPE_NUMBER, - TEXT_TYPE_CHRONO, - TEXT_TYPE_STRING, - TEXT_TYPE_OTHER, + boolean, + number, + chrono, + string, + other, }; template struct GetTextType : std::integral_constant ? TEXT_TYPE_BOOL : - IsStringLikeV ? TEXT_TYPE_STRING : //string before number to correctly handle char/wchar_t -> this was an issue with Loki only! - IsArithmeticV ? TEXT_TYPE_NUMBER : // - IsChronoDuration::value ? TEXT_TYPE_CHRONO : - TEXT_TYPE_OTHER> {}; + std::is_same_v ? TextType::boolean : + IsStringLikeV ? TextType::string : //string before number to correctly handle char/wchar_t -> this was an issue with Loki only! + IsArithmeticV ? TextType::number : // + IsChronoDuration::value ? TextType::chrono : + TextType::other> {}; //###################################################################################### @@ -148,7 +148,7 @@ struct ConvertText; //partial specialization: type bool template -struct ConvertText +struct ConvertText { void writeText(bool value, std::string& output) const { @@ -169,7 +169,7 @@ struct ConvertText //partial specialization: handle conversion for all built-in arithmetic types! template -struct ConvertText +struct ConvertText { void writeText(const T& value, std::string& output) const { @@ -183,7 +183,7 @@ struct ConvertText }; template -struct ConvertText +struct ConvertText { void writeText(const T& value, std::string& output) const { @@ -198,7 +198,7 @@ struct ConvertText //partial specialization: handle conversion for all string-like types! template -struct ConvertText +struct ConvertText { void writeText(const T& value, std::string& output) const { @@ -214,7 +214,7 @@ struct ConvertText //partial specialization: unknown type template -struct ConvertText +struct ConvertText { //########################################################################################################################################### static_assert(sizeof(T) == -1); diff --git a/zenXml/zenxml/dom.h b/zenXml/zenxml/dom.h index e77509bf..8d5bc0f5 100644 --- a/zenXml/zenxml/dom.h +++ b/zenXml/zenxml/dom.h @@ -24,8 +24,7 @@ public: XmlElement() {} //Construct an empty XML element - template - explicit XmlElement(const String& name, XmlElement* parent = nullptr) : name_(utfTo(name)), parent_(parent) {} + explicit XmlElement(std::string name, XmlElement* parent = nullptr) : name_(std::move(name)), parent_(parent) {} ///Retrieve the name of this XML element. /** @@ -52,52 +51,46 @@ public: ///Retrieve an attribute by name. /** - \tparam String Arbitrary string-like type: e.g. std::string, wchar_t*, char[], wchar_t, wxString, MyStringClass, ... \tparam T String-convertible user data type: e.g. any string class, all built-in arithmetic numbers \param name The name of the attribute to retrieve. \param value The value of the attribute converted to T. \return "true" if value was retrieved successfully. */ - template - bool getAttribute(const String& name, T& value) const + template + bool getAttribute(const std::string& name, T& value) const { - auto it = attributesSorted_.find(utfTo(name)); + auto it = attributesSorted_.find(name); return it == attributesSorted_.end() ? false : readText(it->second->value, value); } + bool hasAttribute(const std::string& name) const { return attributesSorted_.contains(name); } + ///Create or update an XML attribute. /** - \tparam String Arbitrary string-like type: e.g. std::string, wchar_t*, char[], wchar_t, wxString, MyStringClass, ... \tparam T String-convertible user data type: e.g. any string-like type, all built-in arithmetic numbers \param name The name of the attribute to create or update. \param value The value to set. */ - template - void setAttribute(const String& name, const T& value) + template + void setAttribute(std::string name, const T& value) { - std::string attrName = utfTo(name); - std::string attrValue; writeText(value, attrValue); - auto it = attributesSorted_.find(attrName); + auto it = attributesSorted_.find(name); if (it != attributesSorted_.end()) it->second->value = std::move(attrValue); else { - auto itBack = attributes_.insert(attributes_.end(), {attrName, std::move(attrValue)}); - attributesSorted_.emplace(std::move(attrName), itBack); + auto itBack = attributes_.insert(attributes_.end(), {name, std::move(attrValue)}); + attributesSorted_.emplace(std::move(name), itBack); } } ///Remove the attribute with the given name. - /** - \tparam String Arbitrary string-like type: e.g. std::string, wchar_t*, char[], wchar_t, wxString, MyStringClass, ... - */ - template - void removeAttribute(const String& name) + void removeAttribute(const std::string& name) { - auto it = attributesSorted_.find(utfTo(name)); + auto it = attributesSorted_.find(name); if (it != attributesSorted_.end()) { attributes_.erase(it->second); @@ -107,42 +100,36 @@ public: ///Create a new child element and return a reference to it. /** - \tparam String Arbitrary string-like type: e.g. std::string, wchar_t*, char[], wchar_t, wxString, MyStringClass, ... \param name The name of the child element to be created. */ - template - XmlElement& addChild(const String& name) + XmlElement& addChild(std::string name) { - std::string elemName = utfTo(name); - childElements_.emplace_back(elemName, this); + childElements_.emplace_back(name, this); XmlElement& newElement = childElements_.back(); - childElementsSorted_.emplace(std::move(elemName), &newElement); + childElementsSorted_.emplace(std::move(name), &newElement); return newElement; } ///Retrieve a child element with the given name. /** - \tparam String Arbitrary string-like type: e.g. std::string, wchar_t*, char[], wchar_t, wxString, MyStringClass, ... \param name The name of the child element to be retrieved. \return A pointer to the child element or nullptr if none was found. */ - template - const XmlElement* getChild(const String& name) const + const XmlElement* getChild(const std::string& name) const { - auto it = childElementsSorted_.find(utfTo(name)); + auto it = childElementsSorted_.find(name); return it == childElementsSorted_.end() ? nullptr : it->second; } ///\sa getChild - template - XmlElement* getChild(const String& name) + XmlElement* getChild(const std::string& name) { return const_cast(static_cast(this)->getChild(name)); } - template < class IterTy, //underlying iterator type - class T, //target object type - class AccessPolicy > //access policy: see AccessPtrMap + template //access policy: see AccessPtrMap class PtrIter : private AccessPolicy //get rid of shared_ptr indirection { public: @@ -175,19 +162,17 @@ public: ///Access all child elements with the given name via STL iterators. /** \code - auto iterPair = elem.getChildren("Item"); + auto itPair = elem.getChildren("Item"); std::for_each(iterPair.first, iterPair.second, [](const XmlElement& child) { ... }); \endcode \param name The name of the child elements to be retrieved. \return A pair of STL begin/end iterators to access the child elements sequentially. */ - template - std::pair getChildren(const String& name) const { return childElementsSorted_.equal_range(utfTo(name)); } + std::pair getChildren(const std::string& name) const { return childElementsSorted_.equal_range(name); } ///\sa getChildren - template - std::pair getChildren(const String& name) { return childElementsSorted_.equal_range(utfTo(name)); } + std::pair getChildren(const std::string& name) { return childElementsSorted_.equal_range(name); } struct AccessListElement { @@ -201,8 +186,8 @@ public: ///Access all child elements sequentially via STL iterators. /** \code - auto iterPair = elem.getChildren(); - std::for_each(iterPair.first, iterPair.second, + auto itPair = elem.getChildren(); + std::for_each(itPair.first, itPair.second, [](const XmlElement& child) { ... }); \endcode \return A pair of STL begin/end iterators to access all child elements sequentially. @@ -227,8 +212,8 @@ public: /* -> disabled documentation extraction \brief Get all attributes associated with the element. \code - auto iterPair = elem.getAttributes(); - for (auto it = iterPair.first; it != iterPair.second; ++it) + auto itPair = elem.getAttributes(); + for (auto it = itPair.first; it != itPair.second; ++it) std::cout << "name: " << it->name << " value: " << it->value << '\n'; \endcode \return A pair of STL begin/end iterators to access all attributes sequentially as a list of name/value pairs of std::string. */ @@ -286,11 +271,9 @@ public: //Setup an empty XML document /** - \tparam String Arbitrary string-like type: e.g. std::string, wchar_t*, char[], wchar_t, wxString, MyStringClass, ... \param rootName The name of the XML document's root element. */ - template - XmlDoc(String rootName) : root_(rootName) {} + explicit XmlDoc(std::string rootName) : root_(std::move(rootName)) {} ///Get a const reference to the document's root element. const XmlElement& root() const { return root_; } @@ -334,7 +317,6 @@ private: XmlElement root_{"Root"}; }; - } #endif //DOM_H_82085720723894567204564256 diff --git a/zenXml/zenxml/parser.h b/zenXml/zenxml/parser.h index a4800ab3..14ccfd60 100644 --- a/zenXml/zenxml/parser.h +++ b/zenXml/zenxml/parser.h @@ -206,13 +206,13 @@ void serialize(const XmlElement& element, std::string& stream, for (auto it = attr.first; it != attr.second; ++it) stream += ' ' + normalizeName(it->name) + "=\"" + normalizeAttribValue(it->value) + '"'; - auto iterPair = element.getChildren(); - if (iterPair.first != iterPair.second) //structured element + auto itPair = element.getChildren(); + if (itPair.first != itPair.second) //structured element { //no support for mixed-mode content stream += '>' + lineBreak; - std::for_each(iterPair.first, iterPair.second, + std::for_each(itPair.first, itPair.second, [&](const XmlElement& el) { serialize(el, stream, lineBreak, indent, indentLevel + 1); }); for (size_t i = 0; i < indentLevel; ++i) diff --git a/zenXml/zenxml/xml.h b/zenXml/zenxml/xml.h index ffd00ea0..7e50221a 100644 --- a/zenXml/zenxml/xml.h +++ b/zenXml/zenxml/xml.h @@ -126,25 +126,28 @@ public: \endverbatim */ - XmlOut(XmlDoc& doc) : ref_(&doc.root()) {} - ///Construct an output proxy for a single XML element + explicit XmlOut(XmlDoc& doc) : ref_(&doc.root()) {} + + ///Retrieve a handle to an XML child element for writing /** - \sa XmlOut(XmlDoc& doc) + The child element will be created if it is not yet existing. + \param name The name of the child element */ - XmlOut(XmlElement& element) : ref_(&element) {} + XmlOut operator[](std::string name) const + { + XmlElement* child = ref_->getChild(name); + return XmlOut(child ? *child : ref_->addChild(std::move(name))); + } ///Retrieve a handle to an XML child element for writing /** - The child element will be created if it is not yet existing. + The child element will be added, allowing for multiple elements with the same name. \tparam String Arbitrary string-like type: e.g. std::string, wchar_t*, char[], wchar_t, wxString, MyStringClass, ... \param name The name of the child element */ - template - XmlOut operator[](const String& name) const + XmlOut addChild(std::string name) const { - const std::string utf8name = utfTo(name); - XmlElement* child = ref_->getChild(utf8name); - return child ? *child : ref_->addChild(utf8name); + return XmlOut(ref_->addChild(std::move(name))); } ///Write user data to the underlying XML element @@ -176,17 +179,19 @@ public: \endverbatim - \tparam String Arbitrary string-like type: e.g. std::string, wchar_t*, char[], wchar_t, wxString, MyStringClass, ... \tparam T String-convertible user data type: e.g. any string-like type, all built-in arithmetic numbers \sa XmlElement::setAttribute() */ - template - void attribute(const String& name, const T& value) { ref_->setAttribute(name, value); } - - ///Return a reference to the underlying Xml element - XmlElement& ref() { return *ref_; } + template + void attribute(std::string name, const T& value) { ref_->setAttribute(std::move(name), value); } private: + ///Construct an output proxy for a single XML element + /** + \sa XmlOut(XmlDoc& doc) + */ + explicit XmlOut(XmlElement& element) : ref_(&element) {} + XmlElement* ref_; //always bound! }; @@ -208,37 +213,28 @@ public: in["elem3"](value3); // \endcode */ - XmlIn(const XmlDoc& doc) { refList_.push_back(&doc.root()); } - ///Construct an input proxy for a single XML element, may be nullptr - /** - \sa XmlIn(const XmlDoc& doc) - */ - XmlIn(const XmlElement* element) { refList_.push_back(element); } - ///Construct an input proxy for a single XML element - /** - \sa XmlIn(const XmlDoc& doc) - */ - XmlIn(const XmlElement& element) { refList_.push_back(&element); } + explicit XmlIn(const XmlDoc& doc) : nodeNameFormatted_('<' + doc.root().getName() + '>') + { + refList_.push_back(&doc.root()); + } ///Retrieve a handle to an XML child element for reading /** It is \b not an error if the child element does not exist, but only later if a conversion to user data is attempted. - \tparam String Arbitrary string-like type: e.g. std::string, wchar_t*, char[], wchar_t, wxString, MyStringClass, ... \param name The name of the child element */ - template - XmlIn operator[](const String& name) const + XmlIn operator[](const std::string& name) const { std::vector childList; - if (refIndex_ < refList_.size()) + if (const XmlElement* elem = get()) { - auto iterPair = refList_[refIndex_]->getChildren(name); - std::for_each(iterPair.first, iterPair.second, - [&](const XmlElement& child) { childList.push_back(&child); }); + auto itPair = elem->getChildren(name); + std::for_each(itPair.first, itPair.second, [&](const XmlElement& child) + { childList.push_back(&child); }); } - return XmlIn(childList, childList.empty() ? getChildNameFormatted(name) : std::string(), log_); + return XmlIn(childList, getChildNameFormatted(name), log_); } ///Refer to next sibling element with the same name @@ -264,6 +260,18 @@ public: */ void next() { ++refIndex_; } + ///Test whether the underlying XML element exists + /** + \code + XmlIn in(doc); + XmlIn child = in["elem1"]; + if (child) + ... + \endcode + Use member pointer as implicit conversion to bool (C++ Templates - Vandevoorde/Josuttis; chapter 20) + */ + explicit operator bool() const { return get() != nullptr; } + ///Read user data from the underlying XML element /** This conversion requires a specialization of zen::readText() or zen::readStruc() for type T. @@ -273,18 +281,25 @@ public: template bool operator()(T& value) const { - if (refIndex_ < refList_.size()) + if (const XmlElement* elem = get()) { - const bool success = readStruc(*refList_[refIndex_], value); - if (!success) - log_.ref().notifyConversionError(getNameFormatted()); - return success; + if (readStruc(*elem, value)) + return true; + + log_.ref().notifyConversionError(getNameFormatted()); } else - { log_.ref().notifyMissingElement(getNameFormatted()); - return false; - } + + return false; + } + + bool hasAttribute(const std::string& name) const + { + if (const XmlElement* elem = get()) + if (elem->hasAttribute(name)) + return true; + return false; } ///Read user data from an XML attribute @@ -305,37 +320,21 @@ public: \returns "true" if the attribute was found and the conversion to the output value was successful. \sa XmlElement::getAttribute() */ - template - bool attribute(const String& name, T& value) const + template + bool attribute(const std::string& name, T& value) const { - if (refIndex_ < refList_.size()) + if (const XmlElement* elem = get()) { - const bool success = refList_[refIndex_]->getAttribute(name, value); - if (!success) - log_.ref().notifyMissingAttribute(getNameFormatted(), utfTo(name)); - return success; + if (elem->getAttribute(name, value)) + return true; + + log_.ref().notifyMissingAttribute(getNameFormatted(), name); } else - { log_.ref().notifyMissingElement(getNameFormatted()); - return false; - } - } - ///Return a pointer to the underlying Xml element, may be nullptr - const XmlElement* get() const { return refIndex_ < refList_.size() ? refList_[refIndex_] : nullptr; } - - ///Test whether the underlying XML element exists - /** - \code - XmlIn in(doc); - XmlIn child = in["elem1"]; - if (child) - ... - \endcode - Use member pointer as implicit conversion to bool (C++ Templates - Vandevoorde/Josuttis; chapter 20) - */ - explicit operator bool() const { return get() != nullptr; } + return false; + } ///Notifies errors while mapping the XML to user data /** @@ -357,56 +356,47 @@ public: However be aware that the chain of connected proxy instances will be broken once you call XmlIn::get() to retrieve the underlying pointer. Errors that occur when working with this pointer are not logged by the original set of related instances. */ - bool haveErrors() const { return !log_.ref().elementList().empty(); } ///Get a list of XML element and attribute names which failed to convert to user data. /** - \tparam String Arbitrary string class: e.g. std::string, std::wstring, wxString, MyStringClass, ... \returns A list of XML element and attribute names, empty list if no errors occured. */ - template - std::vector getErrorsAs() const + std::vector getErrors() const { - std::vector output; + std::vector output; for (const std::string& str : log_.ref().elementList()) - output.push_back(utfTo(str)); + output.push_back(utfTo(str)); return output; } private: - XmlIn(const std::vector& siblingList, const std::string& elementNameFmt, const SharedRef& sharedlog) : - refList_(siblingList), formattedName_(elementNameFmt), log_(sharedlog) - { assert((!siblingList.empty() && elementNameFmt.empty()) || (siblingList.empty() && !elementNameFmt.empty())); } + XmlIn(const std::vector& siblingList, + const std::string& nodeNameFormatted, + const SharedRef& sharedlog) : refList_(siblingList), nodeNameFormatted_(nodeNameFormatted), log_(sharedlog) {} - static std::string getNameFormatted(const XmlElement& elem) //" " - { - return (elem.parent() ? getNameFormatted(*elem.parent()) + ' ' : std::string()) + '<' + elem.getName() + '>'; - } + ///Return a pointer to the underlying Xml element, may be nullptr + const XmlElement* get() const { return refIndex_ < refList_.size() ? refList_[refIndex_] : nullptr; } - std::string getNameFormatted() const + std::string getNameFormatted() const //" " { - if (refIndex_ < refList_.size()) - { - assert(formattedName_.empty()); - return getNameFormatted(*refList_[refIndex_]); - } + if (refIndex_ == 0 && refList_.size() <= 1) + return nodeNameFormatted_; else - return formattedName_; + return nodeNameFormatted_ + '[' + numberTo(refIndex_ + 1) + ']'; } std::string getChildNameFormatted(const std::string& childName) const { - std::string parentName = getNameFormatted(); - return (parentName.empty() ? std::string() : (parentName + ' ')) + '<' + childName + '>'; + return getNameFormatted() + " <" + childName + '>'; } class ErrorLog { public: - void notifyConversionError (const std::string& displayName) { insert(displayName); } - void notifyMissingElement (const std::string& displayName) { insert(displayName); } + void notifyConversionError (const std::string& displayName) { insert(displayName); } + void notifyMissingElement (const std::string& displayName) { insert(displayName); } void notifyMissingAttribute(const std::string& displayName, const std::string& attribName) { insert(displayName + " @" + attribName); } const std::vector& elementList() const { return failedElements; } @@ -424,7 +414,7 @@ private: std::vector refList_; //all sibling elements with same name (all pointers bound!) size_t refIndex_ = 0; //this sibling's index in refList_ - std::string formattedName_; //contains full and formatted element name if (and only if) refList_ is empty + std::string nodeNameFormatted_; mutable SharedRef log_ = makeSharedRef(); }; @@ -438,10 +428,11 @@ private: inline void checkXmlMappingErrors(const XmlIn& xmlInput, const Zstring& filePath) //throw FileError { - if (xmlInput.haveErrors()) + if (const std::vector& errors = xmlInput.getErrors(); + !errors.empty()) { std::wstring msg = _("The following XML elements could not be read:") + L'\n'; - for (const std::wstring& elem : xmlInput.getErrorsAs()) + for (const std::wstring& elem : errors) msg += L'\n' + elem; throw FileError(replaceCpy(_("Configuration file %x is incomplete. The missing elements will be set to their default values."), L"%x", fmtPath(filePath)) + L"\n\n" + msg); -- cgit