Implicit const char* cast removed, operator<< added to hidl_string.
hidl_string no longer will provide an implicit cast to const char* as it
interfers with other expected behaviors of the class. It now emulated
std::string in requiring a call to .c_str() to get the same behavior.
Bug: 36532780
Test: Build the tree for marlin, bullhead, bat
Change-Id: I6ba76522ef65aa211bc156144990ad8b7495b051
diff --git a/base/HidlSupport.cpp b/base/HidlSupport.cpp
index 91c9717..5a07554 100644
--- a/base/HidlSupport.cpp
+++ b/base/HidlSupport.cpp
@@ -257,8 +257,9 @@
return std::string(mBuffer, mSize);
}
-hidl_string::operator const char *() const {
- return mBuffer;
+std::ostream& operator<<(std::ostream& os, const hidl_string& str) {
+ os << str.c_str();
+ return os;
}
void hidl_string::copyFrom(const char *data, size_t size) {
diff --git a/base/include/hidl/HidlSupport.h b/base/include/hidl/HidlSupport.h
index a8eae8c..0137eed 100644
--- a/base/include/hidl/HidlSupport.h
+++ b/base/include/hidl/HidlSupport.h
@@ -157,9 +157,6 @@
hidl_string &operator=(hidl_string &&other);
// cast to std::string.
operator std::string() const;
- // cast to C-style string. Caller is responsible
- // to maintain this hidl_string alive.
- operator const char *() const;
void clear();
@@ -203,6 +200,10 @@
#undef HIDL_STRING_OPERATOR
+// Send our content to the output stream
+std::ostream& operator<<(std::ostream& os, const hidl_string& str);
+
+
// hidl_memory is a structure that can be used to transfer
// pieces of shared memory between processes. The assumption
// of this object is that the memory remains accessible as
diff --git a/test_main.cpp b/test_main.cpp
index 658a7eb..4be2eb9 100644
--- a/test_main.cpp
+++ b/test_main.cpp
@@ -62,7 +62,7 @@
hidl_string s2("s2"); // copy constructor from cstr
EXPECT_STREQ(s2.c_str(), "s2");
hidl_string s2a(nullptr); // copy constructor from null cstr
- EXPECT_STREQ("", s2a);
+ EXPECT_STREQ("", s2a.c_str());
s2a = nullptr; // = from nullptr cstr
EXPECT_STREQ(s2a.c_str(), "");
hidl_string s3 = hidl_string("s3"); // move =
@@ -72,34 +72,44 @@
hidl_string s5(hidl_string(hidl_string("s5"))); // move constructor
EXPECT_STREQ(s5.c_str(), "s5");
hidl_string s6(std::string("s6")); // copy constructor from std::string
- EXPECT_STREQ(s6, "s6");
+ EXPECT_STREQ(s6.c_str(), "s6");
hidl_string s7 = std::string("s7"); // copy = from std::string
- EXPECT_STREQ(s7, "s7");
+ EXPECT_STREQ(s7.c_str(), "s7");
hidl_string s8(s7); // copy constructor
- EXPECT_STREQ(s8, "s7");
+ EXPECT_STREQ(s8.c_str(), "s7");
hidl_string s9 = s8; // copy =
- EXPECT_STREQ(s9, "s7");
+ EXPECT_STREQ(s9.c_str(), "s7");
char myCString[20] = "myCString";
s.setToExternal(&myCString[0], strlen(myCString));
- EXPECT_STREQ(s, "myCString");
+ EXPECT_STREQ(s.c_str(), "myCString");
myCString[2] = 'D';
- EXPECT_STREQ(s, "myDString");
+ EXPECT_STREQ(s.c_str(), "myDString");
s.clear(); // should not affect myCString
EXPECT_STREQ(myCString, "myDString");
// casts
s = "great";
std::string myString = s;
- const char *anotherCString = s;
+ const char *anotherCString = s.c_str();
EXPECT_EQ(myString, "great");
EXPECT_STREQ(anotherCString, "great");
const hidl_string t = "not so great";
std::string myTString = t;
- const char * anotherTCString = t;
+ const char * anotherTCString = t.c_str();
EXPECT_EQ(myTString, "not so great");
EXPECT_STREQ(anotherTCString, "not so great");
+ // Assignment from hidl_string to std::string
+ std::string tgt;
+ hidl_string src("some stuff");
+ tgt = src;
+ EXPECT_STREQ(tgt.c_str(), "some stuff");
+
+ // Stream output operator
+ hidl_string msg("hidl_string works with operator<<");
+ std::cout << msg;
+
// Comparisons
const char * cstr1 = "abc";
std::string string1(cstr1);
diff --git a/transport/ServiceManagement.cpp b/transport/ServiceManagement.cpp
index 5ba3ec3..23349c3 100644
--- a/transport/ServiceManagement.cpp
+++ b/transport/ServiceManagement.cpp
@@ -196,7 +196,7 @@
continue;
}
- IBase *interface = (*generator)(name);
+ IBase *interface = (*generator)(name.c_str());
if (interface == nullptr) {
dlclose(handle);