diff options
author | Luboš Luňák <l.lunak@suse.cz> | 2012-03-26 18:51:59 +0200 |
---|---|---|
committer | Luboš Luňák <l.lunak@suse.cz> | 2012-03-26 18:57:04 +0200 |
commit | 09517a98fe44eafcd19a8eecb0c14384799e2684 (patch) | |
tree | 6baaabfb42f1b0b2f8889bff7da1643d4cc5db89 /sal | |
parent | 62983d5ae66d510c3a6a3167eb41207a9d2777ec (diff) |
string literal overload for OString::operator=()
Diffstat (limited to 'sal')
-rw-r--r-- | sal/inc/rtl/string.hxx | 35 | ||||
-rw-r--r-- | sal/qa/rtl/strings/test_ostring_stringliterals.cxx | 50 | ||||
-rw-r--r-- | sal/qa/rtl/strings/test_oustring_stringliterals.cxx | 2 |
3 files changed, 87 insertions, 0 deletions
diff --git a/sal/inc/rtl/string.hxx b/sal/inc/rtl/string.hxx index 79b9a15ae94a..d899067754b4 100644 --- a/sal/inc/rtl/string.hxx +++ b/sal/inc/rtl/string.hxx @@ -57,6 +57,12 @@ namespace rtl #ifdef RTL_STRING_UNITTEST #undef rtl +// helper macros to make functions appear more readable +#define RTL_STRING_CONST_FUNCTION rtl_string_unittest_const_literal_function = true; +#define RTL_STRING_NON_CONST_FUNCTION rtl_string_unittest_non_const_literal_function = true; +#else +#define RTL_STRING_CONST_FUNCTION +#define RTL_STRING_NON_CONST_FUNCTION #endif /* ======================================================================= */ @@ -209,6 +215,7 @@ public: If there are any embedded \0's in the string literal, the result is undefined. Use the overload that explicitly accepts length. + @since LibreOffice 3.6 @param literal a string literal */ @@ -225,6 +232,7 @@ public: /** @overload New string from a non-const char array. + @since LibreOffice 3.6 @param value non-const char array */ @@ -299,6 +307,33 @@ public: } /** + @overload + This function accepts an ASCII string literal as its argument. + @since LibreOffice 3.6 + */ + template< int N > + OString& operator=( const char (&literal)[ N ] ) SAL_THROW(()) + { + rtl_string_newFromLiteral( &pData, literal, N - 1 ); + RTL_STRING_CONST_FUNCTION + return *this; + } + /** + @overload + This function accepts a non-const char array as its argument. + @since LibreOffice 3.6 + + @param value non-const char array + */ + template< int N > + OString& operator=( char (&value)[ N ] ) SAL_THROW(()) + { + rtl_string_newFromStr( &pData, value ); + RTL_STRING_NON_CONST_FUNCTION + return *this; + } + + /** Append a string to this string. @param str a OString. diff --git a/sal/qa/rtl/strings/test_ostring_stringliterals.cxx b/sal/qa/rtl/strings/test_ostring_stringliterals.cxx index 8398ae9e63e9..2176a5023a0c 100644 --- a/sal/qa/rtl/strings/test_ostring_stringliterals.cxx +++ b/sal/qa/rtl/strings/test_ostring_stringliterals.cxx @@ -29,6 +29,8 @@ // activate the extra needed ctor #define RTL_STRING_UNITTEST bool rtl_string_unittest_const_literal; +bool rtl_string_unittest_const_literal_function; +bool rtl_string_unittest_non_const_literal_function; #include "sal/config.h" #include "sal/precppunit.hxx" @@ -38,17 +40,33 @@ bool rtl_string_unittest_const_literal; #include "rtl/string.h" #include "rtl/string.hxx" +namespace rtlunittest { + +template< typename charT, typename traits > std::basic_ostream<charT, traits> & +operator <<( + std::basic_ostream<charT, traits> & stream, rtl::OString const & string) +{ + return stream << string.getStr(); + // best effort; potentially loses data due to embedded null characters +} + +} + namespace test { namespace ostring { class StringLiterals: public CppUnit::TestFixture { private: void checkCtors(); + void checkUsage(); + void checkNonConstUsage(); void testcall( const char str[] ); CPPUNIT_TEST_SUITE(StringLiterals); CPPUNIT_TEST(checkCtors); +CPPUNIT_TEST(checkUsage); +CPPUNIT_TEST(checkNonConstUsage); CPPUNIT_TEST_SUITE_END(); }; @@ -116,6 +134,38 @@ void test::ostring::StringLiterals::testcall( const char str[] ) #endif } +void test::ostring::StringLiterals::checkUsage() +{ +// simply check that all string literal based calls work as expected +// also check that they really use string literal overload and do not convert to OString + rtl::OString foo( "foo" ); + + rtl_string_unittest_const_literal = false; // start checking for OString conversions + rtl_string_unittest_non_const_literal_function = false; // and check for non-const variants + CPPUNIT_ASSERT_EQUAL( foo, rtl::OString() = "foo" ); + // if this is not true, some of the calls above converted to OString + CPPUNIT_ASSERT( rtl_string_unittest_const_literal == false ); + // if this is not true, some of the calls above used non-const variants + CPPUNIT_ASSERT( rtl_string_unittest_non_const_literal_function == false ); +} + +void test::ostring::StringLiterals::checkNonConstUsage() +{ +// check that (non-const) char[] overloads work and do not use const char[] overloads + rtl::OString foo( "foo" ); + char foo_c[] = "foo"; + + rtl_string_unittest_const_literal = false; // start checking for OString conversions + rtl_string_unittest_const_literal_function = false; // and check for const variants + sleep(10); + rtl::OString() = (const char*)"foo"; + CPPUNIT_ASSERT_EQUAL( foo, rtl::OString() = foo_c ); + // if this is not true, some of the calls above converted to OString + CPPUNIT_ASSERT( rtl_string_unittest_const_literal == false ); + // if this is not true, some of the calls above used const variants + CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function == false ); +} + #undef CONST_CTOR_USED }} // namespace diff --git a/sal/qa/rtl/strings/test_oustring_stringliterals.cxx b/sal/qa/rtl/strings/test_oustring_stringliterals.cxx index c90fe5e70a6d..fb0e21ebf363 100644 --- a/sal/qa/rtl/strings/test_oustring_stringliterals.cxx +++ b/sal/qa/rtl/strings/test_oustring_stringliterals.cxx @@ -29,6 +29,8 @@ // activate the extra needed ctor #define RTL_STRING_UNITTEST extern bool rtl_string_unittest_const_literal; +extern bool rtl_string_unittest_const_literal_function; +extern bool rtl_string_unittest_non_const_literal_function; #include "sal/config.h" #include "sal/precppunit.hxx" |