diff options
author | Mike Kaganski <mike.kaganski@collabora.com> | 2020-07-06 02:08:33 +0300 |
---|---|---|
committer | Mike Kaganski <mike.kaganski@collabora.com> | 2020-07-07 12:21:27 +0200 |
commit | b7ed6de51422ca7bc8333e80ae6e9a4d57e07239 (patch) | |
tree | 7d55d33e38731db58d35e8d1fc77ec558f391914 /include/sax | |
parent | 125f3c4c930bd28a42c6819417b11b885e4586fc (diff) |
Use std::optional to allow optional inclusion of attributes
... instead of converting the O(U)String objects to char*.
Eventually this could allow to drop variants of *Element that take
XFastAttributeListRef.
Change-Id: Ib2748fcd93e655c55a176c00410fdcc7f052930d
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/98179
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
Diffstat (limited to 'include/sax')
-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 |