hidl_string: add operator<
This allows us to std::sort vecs of hidl_strings, etc..
Test: libhidl_test
Change-Id: I6e9a7669bceb09d62791fe8c88cfd1977ea07194
diff --git a/base/HidlSupport.cpp b/base/HidlSupport.cpp
index d657793..ef82775 100644
--- a/base/HidlSupport.cpp
+++ b/base/HidlSupport.cpp
@@ -90,6 +90,10 @@
return *this;
}
+bool hidl_string::operator< (const hidl_string &rhs) const {
+ return strcmp(mBuffer, rhs.mBuffer) < 0;
+}
+
hidl_string::operator std::string() const {
return std::string(mBuffer, mSize);
}
diff --git a/base/include/hidl/HidlSupport.h b/base/include/hidl/HidlSupport.h
index 8d43bfd..e201a1e 100644
--- a/base/include/hidl/HidlSupport.h
+++ b/base/include/hidl/HidlSupport.h
@@ -154,6 +154,8 @@
// to maintain this hidl_string alive.
operator const char *() const;
+ bool operator< (const hidl_string &rhs) const;
+
void clear();
// Reference an external char array. Ownership is _not_ transferred.
diff --git a/test_main.cpp b/test_main.cpp
index 1967614..6e9bf25 100644
--- a/test_main.cpp
+++ b/test_main.cpp
@@ -81,6 +81,9 @@
s.clear(); // should not affect myCString
EXPECT_STREQ(myCString, "myDString");
+ // operator <
+ EXPECT_LT("ab", "bcd");
+
// casts
s = "great";
std::string myString = s;