message: allow obj-path and sig in STL containers
Allow object_path and signatures to be used in std::vector,
std::map, and std::unordered_map.
Change-Id: Ieb592aa518bfae08da393632a1dd0ed0dd684c7a
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
diff --git a/sdbusplus/message/native_types.hpp b/sdbusplus/message/native_types.hpp
index c3941ca..c0daa31 100644
--- a/sdbusplus/message/native_types.hpp
+++ b/sdbusplus/message/native_types.hpp
@@ -31,6 +31,9 @@
operator std::string() const { return str; }
operator const std::string&() const { return str; }
operator std::string&&() { return std::move(str); }
+
+ bool operator==(const string_wrapper<T>& r) const { return str == r.str; }
+ bool operator<(const string_wrapper<T>& r) const { return str < r.str; }
};
/** Typename for sdbus OBJECT_PATH types. */
@@ -47,3 +50,21 @@
} // namespace message
} // namespace sdbusplus
+
+namespace std
+{
+
+/** Overload of std::hash for details::string_wrappers */
+template <typename T>
+struct hash<sdbusplus::message::details::string_wrapper<T>>
+{
+ using argument_type = sdbusplus::message::details::string_wrapper<T>;
+ using result_type = std::size_t;
+
+ result_type operator()(argument_type const& s) const
+ {
+ return hash<std::string>()(s.str);
+ }
+};
+
+} // namespace std
diff --git a/test/message/native_types.cpp b/test/message/native_types.cpp
index 1ee3dee..df1cb96 100644
--- a/test/message/native_types.cpp
+++ b/test/message/native_types.cpp
@@ -1,5 +1,8 @@
#include <cassert>
#include <sdbusplus/message.hpp>
+#include <vector>
+#include <map>
+#include <unordered_map>
int main()
{
@@ -9,5 +12,14 @@
std::string s2 = sdbusplus::message::signature("iii");
sdbusplus::message::signature sig = s2;
+ std::vector<sdbusplus::message::signature> v =
+ { sdbusplus::message::signature("iii") };
+
+ std::map<sdbusplus::message::signature, int> m =
+ { { sdbusplus::message::signature("iii"), 1 } };
+
+ std::unordered_map<sdbusplus::message::signature, int> u =
+ { { sdbusplus::message::signature("iii"), 1 } };
+
return 0;
}