Helperclassforconvertingagiventypetoastringrepresentation.
*/ template< typename T > struct ToStringHelper
{ /// Return length of the string representation of the given object. // static std::size_t length( const T& ); /// Add 8-bit representation of the given object to the given buffer and return position right after the added data. // char* operator()( char* buffer, const T& ) const SAL_RETURNS_NONNULL; /// Add Unicode representation of the given object to the given buffer and return position right after the added data. // sal_Unicode* operator()( sal_Unicode* buffer, const T& ) const SAL_RETURNS_NONNULL;
};
/// If true, T can be used in concatenation resulting in O(U)String. template<typename C, typename T, class Enable = void> constexpr bool allowStringConcat = false; template<typename C, typename T> constexpr bool allowStringConcat<C, T, std::enable_if_t<std::is_invocable_v<ToStringHelper<T>, C*, T>>> = true;
// Output a character specified number of times. If the number is less than 1, do nothing. template <typename C> struct RepeatedChar_t
{
C c;
sal_Int32 n;
constexpr RepeatedChar_t(C ch, sal_Int32 num) : c(ch), n(std::max(num, sal_Int32(0))) {}
};
Ifyougetabuilderrorrelatedtothisclass,youmostprobablyneedtoexplicitlyconverttheresultofastring concatenationtoO(U)String.
*/ template <typename C, typename T1, typename T2, std::enable_if_t<allowStringConcat<C, T1> && allowStringConcat<C, T2>, int> = 0 > struct StringConcat
{ public:
StringConcat( const T1& left_, const T2& right_ ) : left( left_ ), right( right_ ) {}
std::size_t length() const { return ToStringHelper< T1 >::length( left ) + ToStringHelper< T2 >::length( right ); }
C* addData( C* buffer ) const SAL_RETURNS_NONNULL { return ToStringHelper< T2 >()( ToStringHelper< T1 >()( buffer, left ), right ); } // NOTE here could be functions that would forward to the "real" temporary O(U)String. Note however that e.g. getStr() // is not so simple, as the O(U)String temporary must live long enough (i.e. can't be created here in a function, a wrapper // temporary object containing it must be returned instead). private: const T1& left; const T2& right;
};
// char[N] and const char[N] need to be done explicitly, otherwise the compiler likes to treat them the same way for some reason template< typename T, std::size_t N >
[[nodiscard]] inline
OStringConcat< T, constchar[ N ] > operator+( const T& left, constchar (&right)[ N ] )
{ return OStringConcat< T, constchar[ N ] >( left, right );
}
template< typename T, std::size_t N >
[[nodiscard]] inline
OStringConcat< constchar[ N ], T > operator+( constchar (&left)[ N ], const T& right )
{ return OStringConcat< constchar[ N ], T >( left, right );
}
template< typename T, std::size_t N >
[[nodiscard]] inline
OStringConcat< T, char[ N ] > operator+( const T& left, char (&right)[ N ] )
{ return OStringConcat< T, char[ N ] >( left, right );
}
template< typename T, std::size_t N >
[[nodiscard]] inline
OStringConcat< char[ N ], T > operator+( char (&left)[ N ], const T& right )
{ return OStringConcat< char[ N ], T >( left, right );
}
#ifdef RTL_STRING_UNITTEST_CONCAT // Special overload to catch the remaining invalid combinations. The helper struct must // be used to make this operator+ overload a worse choice than all the existing overloads above. struct StringConcatInvalid
{ template< typename T >
StringConcatInvalid( const T& ) {}
}; template< typename T > inline intoperator+( const StringConcatInvalid&, const T& )
{
rtl_string_unittest_invalid_concat = true; return0; // doesn't matter
} #endif
// Lightweight alternative to O(U)String when a (temporary) object is needed to hold // an O(U)StringConcat result that can then be used as a std::(u16)string_view: template <typename C> class StringConcatenation { public: template <class Concat> explicit StringConcatenation(Concat const& c):
length_(c.length()),
buffer_(new C[length_])
{ autoconst end = c.addData(buffer_.get());
assert(end == buffer_.get() + length_); (void)end;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.