C++ SharedString API: allow assigning from const char *

There is already a constructor that takes a char*, so there should be an
assignment operator as well
This commit is contained in:
Olivier Goffart 2021-04-27 10:37:27 +02:00
parent 0083bd8d23
commit 3ab11b62e6
2 changed files with 17 additions and 3 deletions

View file

@ -36,7 +36,7 @@ struct SharedString
{ {
cbindgen_private::sixtyfps_shared_string_from_bytes(this, s.data(), s.size()); cbindgen_private::sixtyfps_shared_string_from_bytes(this, s.data(), s.size());
} }
/// Creates a new SharedString from the null-terminated string pointer \a. The underlying /// Creates a new SharedString from the null-terminated string pointer \a s. The underlying
/// string data is copied. It is assumed that the string is UTF-8 encoded. /// string data is copied. It is assumed that the string is UTF-8 encoded.
SharedString(const char *s) : SharedString(std::string_view(s)) { } SharedString(const char *s) : SharedString(std::string_view(s)) { }
/// Creates a new SharedString from \a other. /// Creates a new SharedString from \a other.
@ -55,13 +55,21 @@ struct SharedString
return *this; return *this;
} }
/// Assigns the string view \s to this string and returns a reference to this string. /// Assigns the string view \s to this string and returns a reference to this string.
/// The underlying string data is copied. /// The underlying string data is copied. It is assumed that the string is UTF-8 encoded.
SharedString &operator=(std::string_view s) SharedString &operator=(std::string_view s)
{ {
cbindgen_private::sixtyfps_shared_string_drop(this); cbindgen_private::sixtyfps_shared_string_drop(this);
cbindgen_private::sixtyfps_shared_string_from_bytes(this, s.data(), s.size()); cbindgen_private::sixtyfps_shared_string_from_bytes(this, s.data(), s.size());
return *this; return *this;
} }
/// Assigns null-terminated string pointer \a s to this string and returns a reference
/// to this string. The underlying string data is copied. It is assumed that the string
/// is UTF-8 encoded.
SharedString &operator=(const char *s)
{
return *this = std::string_view(s);
}
/// Move-assigns \a other to this SharedString instance. /// Move-assigns \a other to this SharedString instance.
SharedString &operator=(SharedString &&other) SharedString &operator=(SharedString &&other)
{ {

View file

@ -27,6 +27,12 @@ SCENARIO("SharedString API")
str = foo_view; str = foo_view;
REQUIRE(str == "Foo"); REQUIRE(str == "Foo");
} }
SECTION("Construct from char*")
{
str = "Bar";
REQUIRE(str == "Bar");
}
} }
TEST_CASE("Basic SharedVector API", "[vector]") TEST_CASE("Basic SharedVector API", "[vector]")