String16 is moveable (noexcept)
String16 is still in use by AIDL compiler. Because String16 is not
noexcept-move-constructible, the C++ compiler will complain when it is
used with non-copyable types (such as ParcelFileDescriptor).
For example, when vector<Foo> is resized, copy-ctor of Foo is called,
which is not available.
parcelable Foo {
String s;
ParcelFileDescriptor[] pfds;
}
By providing noexcept move-ctor for String16, vector<Foo> can be resized
with no problem.
Btw, copy from StaticString16 is specialized for efficiency and move
from StaticString16 don't need to be different from copy.
Bug: 192136980
Test: libutils_test
Change-Id: I13744a2ceebf5781c3ef7f3a04237a6750b0db0a
diff --git a/libutils/String16_test.cpp b/libutils/String16_test.cpp
index 54662ac..917f1a3 100644
--- a/libutils/String16_test.cpp
+++ b/libutils/String16_test.cpp
@@ -64,6 +64,13 @@
EXPECT_STR16EQ(u"Verify me", another);
}
+TEST(String16Test, MoveAssign) {
+ String16 tmp("Verify me");
+ String16 another;
+ another = std::move(tmp);
+ EXPECT_STR16EQ(u"Verify me", another);
+}
+
TEST(String16Test, Size) {
String16 tmp("Verify me");
EXPECT_EQ(9U, tmp.size());
@@ -123,6 +130,10 @@
String16 another(std::move(tmp));
EXPECT_STR16EQ(u"Verify me", another);
EXPECT_TRUE(another.isStaticString());
+ // move/copy from StaticString16 is specialized (just copy the handle).
+ // no extra actions required.
+ EXPECT_STR16EQ(u"Verify me", tmp);
+ EXPECT_TRUE(tmp.isStaticString());
}
TEST(String16Test, StaticStringSize) {
@@ -174,10 +185,16 @@
EXPECT_STR16EQ(u"Verify me", another);
}
-TEST(String16Test, StringMoveFromStaticString) {
+TEST(String16Test, StringMoveAssignFromStaticString) {
StaticString16 tmp(u"Verify me");
- String16 another(std::move(tmp));
+ String16 another(u"nonstatic");
+ another = std::move(tmp);
EXPECT_STR16EQ(u"Verify me", another);
+ EXPECT_TRUE(another.isStaticString());
+ // move/copy from StaticString16 is specialized (just copy handle).
+ // no extra actions required.
+ EXPECT_STR16EQ(u"Verify me", tmp);
+ EXPECT_TRUE(tmp.isStaticString());
}
TEST(String16Test, EmptyStringIsStatic) {