Simplify those assertLayout checks

...recently introduced with 52a49f9e48
"static_assert that O[U]StringLiteral are layout compatible with rtl_[u]String"

Change-Id: Ie89e92aa4230329aa582c4211c2001117d2287b5
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/103138
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
This commit is contained in:
Stephan Bergmann 2020-09-22 09:26:09 +02:00
parent 0e66570eeb
commit 43003173fb
2 changed files with 11 additions and 16 deletions

View file

@ -90,6 +90,7 @@ public:
constexpr
#endif
explicit OStringLiteral(char const (&literal)[N]) {
assertLayout();
assert(literal[N - 1] == '\0');
//TODO: Use C++20 constexpr std::copy_n (P0202R3):
for (std::size_t i = 0; i != N; ++i) {
@ -104,6 +105,7 @@ public:
constexpr
#endif
explicit OStringLiteral(char8_t const (&literal)[N]) {
assertLayout();
assert(literal[N - 1] == '\0');
//TODO: Use C++20 constexpr std::copy_n (P0202R3):
for (std::size_t i = 0; i != N; ++i) {
@ -116,10 +118,10 @@ public:
constexpr char const * getStr() const SAL_RETURNS_NONNULL { return buffer; }
// offsetof needs a complete type, so do not have these static_asserts as class template
// members, but postpone their instantiation to the later non-member static_assert that calls
// detail_assertLayout:
static constexpr bool detail_assertLayout() {
private:
static constexpr void assertLayout() {
// These static_asserts verifying the layout compatibility with rtl_String cannot be class
// member declarations, as offsetof requires a complete type, so defer them to here:
static_assert(offsetof(OStringLiteral, refCount) == offsetof(rtl_String, refCount));
static_assert(
std::is_same_v<decltype(refCount), decltype(rtl_String::refCount)>);
@ -130,17 +132,13 @@ public:
std::is_same_v<
std::remove_extent_t<decltype(buffer)>,
std::remove_extent_t<decltype(rtl_String::buffer)>>);
return true;
}
private:
// Same layout as rtl_String (include/rtl/string.h):
oslInterlockedCount refCount = 0x40000000; // SAL_STRING_STATIC_FLAG (sal/rtl/strimp.hxx)
sal_Int32 length = N - 1;
char buffer[N] = {}; //TODO: drop initialization for C++20 (P1331R2)
};
static_assert(OStringLiteral<1>::detail_assertLayout());
#endif
/* ======================================================================= */

View file

@ -87,6 +87,7 @@ public:
constexpr
#endif
OUStringLiteral(char16_t const (&literal)[N]) {
assertLayout();
assert(literal[N - 1] == '\0');
//TODO: Use C++20 constexpr std::copy_n (P0202R3):
for (std::size_t i = 0; i != N; ++i) {
@ -100,10 +101,10 @@ public:
constexpr operator std::u16string_view() const { return {buffer, sal_uInt32(length)}; }
// offsetof needs a complete type, so do not have these static_asserts as class template
// members, but postpone their instantiation to the later non-member static_assert that calls
// detail_assertLayout:
static constexpr bool detail_assertLayout() {
private:
static constexpr void assertLayout() {
// These static_asserts verifying the layout compatibility with rtl_uString cannot be class
// member declarations, as offsetof requires a complete type, so defer them to here:
static_assert(offsetof(OUStringLiteral, refCount) == offsetof(rtl_uString, refCount));
static_assert(std::is_same_v<decltype(refCount), decltype(rtl_uString::refCount)>);
static_assert(offsetof(OUStringLiteral, length) == offsetof(rtl_uString, length));
@ -113,18 +114,14 @@ public:
std::is_same_v<
std::remove_extent_t<decltype(buffer)>,
std::remove_extent_t<decltype(rtl_uString::buffer)>>);
return true;
}
private:
// Same layout as rtl_uString (include/rtl/ustring.h):
oslInterlockedCount refCount = 0x40000000; // SAL_STRING_STATIC_FLAG (sal/rtl/strimp.hxx)
sal_Int32 length = N - 1;
sal_Unicode buffer[N] = {}; //TODO: drop initialization for C++20 (P1331R2)
};
static_assert(OUStringLiteral<1>::detail_assertLayout());
#if defined RTL_STRING_UNITTEST
namespace libreoffice_internal {
template<std::size_t N> struct ExceptConstCharArrayDetector<OUStringLiteral<N>> {};