Allow implicit string_wrapper conversions
Add comparison operators to allow string_wrappers
to be implicity converted to strings for comparisons to
cut down on casting and/or str member access.
Do not allow std::string to be impliclitly converted
to object_path to avoid a temporary string copy.
Change-Id: I2e45a4fc3ee07d1df55f6fab2cfa35da205c150e
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
diff --git a/sdbusplus/message/native_types.hpp b/sdbusplus/message/native_types.hpp
index c0daa31..ce50f8e 100644
--- a/sdbusplus/message/native_types.hpp
+++ b/sdbusplus/message/native_types.hpp
@@ -34,6 +34,17 @@
bool operator==(const string_wrapper<T>& r) const { return str == r.str; }
bool operator<(const string_wrapper<T>& r) const { return str < r.str; }
+ bool operator==(const std::string& r) const { return str == r; }
+ bool operator<(const std::string& r) const { return str < r; }
+
+ friend bool operator==(const std::string& l, const string_wrapper& r)
+ {
+ return l == r.str;
+ }
+ friend bool operator<(const std::string& l, const string_wrapper& r)
+ {
+ return l < r.str;
+ }
};
/** Typename for sdbus OBJECT_PATH types. */
diff --git a/test/message/native_types.cpp b/test/message/native_types.cpp
index df1cb96..cbe33eb 100644
--- a/test/message/native_types.cpp
+++ b/test/message/native_types.cpp
@@ -4,6 +4,14 @@
#include <map>
#include <unordered_map>
+void check_string_compares(const sdbusplus::message::signature& sig,
+ const std::string& str)
+{
+ assert(sig == str);
+ assert(str == sig);
+ return;
+}
+
int main()
{
std::string s1 = sdbusplus::message::object_path("/asdf/");
@@ -12,6 +20,8 @@
std::string s2 = sdbusplus::message::signature("iii");
sdbusplus::message::signature sig = s2;
+ check_string_compares(sig, s2);
+
std::vector<sdbusplus::message::signature> v =
{ sdbusplus::message::signature("iii") };