diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2021-11-12 12:20:18 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2021-11-12 18:37:38 +0100 |
commit | a4d4286a6ae5ad7f57db4f34380ed170b92ced06 (patch) | |
tree | 3d354a2a00b2fea1003d08566f7e4ab003c4c954 /include/rtl | |
parent | 16be302fededaf2cc6e82701bda6dcba6e3fa4d3 (diff) |
introduce OUStringConstExpr
so we can declare compile-time constant arrays and structs
containing OUStringLiteral
Change-Id: I51fd743f2e461a36bb4a0a17c6ff107f86096895
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/125045
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'include/rtl')
-rw-r--r-- | include/rtl/ustring.hxx | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/include/rtl/ustring.hxx b/include/rtl/ustring.hxx index 1a5ff2d97d6c..6cf92e1d3599 100644 --- a/include/rtl/ustring.hxx +++ b/include/rtl/ustring.hxx @@ -84,6 +84,7 @@ template<std::size_t N> class SAL_WARN_UNUSED OUStringLiteral { static_assert(N != 0); static_assert(N - 1 <= std::numeric_limits<sal_Int32>::max(), "literal too long"); friend class OUString; + friend class OUStringConstExpr; public: #if HAVE_CPP_CONSTEVAL @@ -138,6 +139,34 @@ template<std::size_t N> struct ExceptCharArrayDetector<OUStringLiteral<N>> {}; } #endif +/** + This is intended to be used when declaring compile-time-constant structs or arrays + that can be initialised from named OUStringLiteral e.g. + + constexpr OUStringLiteral AAA = u"aaa"; + constexpr OUStringLiteral BBB = u"bbb"; + constexpr OUStringConstExpr FOO[] { AAA, BBB }; +*/ +class OUString; +class OUStringConstExpr +{ +public: + template<std::size_t N> constexpr OUStringConstExpr(OUStringLiteral<N> const & literal): + pData(const_cast<rtl_uString *>(&literal.str)) {} + + // prevent mis-use + template<std::size_t N> constexpr OUStringConstExpr(OUStringLiteral<N> && literal) + = delete; + + // no destructor necessary because we know we are pointing at a compile-time + // constant OUStringLiteral, which bypasses ref-counting. + + inline operator OUString() const; + +private: + rtl_uString* pData; +}; + /// @endcond #endif @@ -3269,6 +3298,11 @@ private: }; #if defined LIBO_INTERNAL_ONLY +// Can only define this after we define OUString +inline OUStringConstExpr::operator OUString() const { return OUString::unacquired(&pData); } +#endif + +#if defined LIBO_INTERNAL_ONLY // Prevent the operator ==/!= overloads with 'sal_Unicode const *' parameter from // being selected for nonsensical code like // @@ -3328,6 +3362,7 @@ inline std::basic_ostream<charT, traits> & operator <<( return stream << OUString( std::move(concat) ); } + /// @endcond #endif |