diff options
Diffstat (limited to 'include/sax/fshelper.hxx')
-rw-r--r-- | include/sax/fshelper.hxx | 41 |
1 files changed, 31 insertions, 10 deletions
diff --git a/include/sax/fshelper.hxx b/include/sax/fshelper.hxx index 235181bf9846..e4b5072345e2 100644 --- a/include/sax/fshelper.hxx +++ b/include/sax/fshelper.hxx @@ -25,6 +25,7 @@ #include <com/sun/star/uno/Sequence.hxx> #include <rtl/ustring.hxx> #include <sax/saxdllapi.h> +#include <optional> #include <memory> #include <utility> @@ -58,16 +59,21 @@ public: startElement(elementTokenId, std::forward<Args>(args)...); } template<typename... Args> - void startElement(sal_Int32 elementTokenId, sal_Int32 attribute, const OString& value, Args &&... args) + void startElement(sal_Int32 elementTokenId, sal_Int32 attribute, + const std::optional<OString>& value, Args&&... args) { - pushAttributeValue(attribute, value); + if (value) + pushAttributeValue(attribute, *value); startElement(elementTokenId, std::forward<Args>(args)...); } template<typename... Args> - void startElement(sal_Int32 elementTokenId, sal_Int32 attribute, const OUString& value, Args&&... args) + void startElement(sal_Int32 elementTokenId, sal_Int32 attribute, + const std::optional<OUString>& value, Args&&... args) { - // The temporary created by toUtf8() must stay alive until startElement() ends using it - startElement(elementTokenId, attribute, value.toUtf8(), std::forward<Args>(args)...); + std::optional<OString> opt; + if (value) + opt = value->toUtf8(); + startElement(elementTokenId, attribute, opt, std::forward<Args>(args)...); } void startElement(sal_Int32 elementTokenId); @@ -87,16 +93,21 @@ public: singleElement(elementTokenId, std::forward<Args>(args)...); } template<typename... Args> - void singleElement(sal_Int32 elementTokenId, sal_Int32 attribute, const OString& value, Args &&... args) + void singleElement(sal_Int32 elementTokenId, sal_Int32 attribute, + const std::optional<OString>& value, Args&&... args) { - pushAttributeValue(attribute, value); + if (value) + pushAttributeValue(attribute, *value); singleElement(elementTokenId, std::forward<Args>(args)...); } template<typename... Args> - void singleElement(sal_Int32 elementTokenId, sal_Int32 attribute, const OUString& value, Args&&... args) + void singleElement(sal_Int32 elementTokenId, sal_Int32 attribute, + const std::optional<OUString>& value, Args&&... args) { - // The temporary created by toUtf8() must stay alive until singleElement() ends using it - singleElement(elementTokenId, attribute, value.toUtf8(), std::forward<Args>(args)...); + std::optional<OString> opt; + if (value) + opt = value->toUtf8(); + singleElement(elementTokenId, attribute, opt, std::forward<Args>(args)...); } void singleElement(sal_Int32 elementTokenId); @@ -150,6 +161,16 @@ private: typedef std::shared_ptr< FastSerializerHelper > FSHelperPtr; +// Helpers to make intention to pass optional attributes to *Element finctions explicit, instead of +// using `(condition) ? value.toUtf8().getStr() : nullptr` syntax. +inline const char* UseIf(const char* s, bool bUse) { return bUse ? s : nullptr; } +// OString, OUString +template<class TString> +std::optional<TString> UseIf(const TString& s, bool bUse) +{ + return bUse ? std::optional<TString>(s) : std::optional<TString>(); +} + } #endif // INCLUDED_SAX_FSHELPER_HXX |