Add type_id templates
C++ templates to assist in type conversions between C++
types and dbus types. These templates provide a compile-time
conversion between built-in types and a C-string of dbus types.
Example:
uint32_t a = 0;
const char* b = "a string";
auto t = sdbusplus::message::types::type_id(a, b);
This results in a tuple equal to std::make_tuple('u','s', '\0')', which
can be converted to a C-string("us") by tuple_to_array.
In a subsequent commit this interface will be used to do a
compile-time transformation from something like:
msg->append(a, b)
To:
sd_bus_message_append(msg, "us", a, b);
The type_id template can be extended to support tuples, STL
containers, and user-defined types as needed.
Change-Id: I913501b51ffc24bcf4219f4297a1e0f8ebed97b5
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
diff --git a/test/message/types.cpp b/test/message/types.cpp
new file mode 100644
index 0000000..4172cb6
--- /dev/null
+++ b/test/message/types.cpp
@@ -0,0 +1,33 @@
+#include <cassert>
+#include <sdbusplus/message/types.hpp>
+#include <sdbusplus/utility/tuple_to_array.hpp>
+
+template <typename ...Args>
+auto dbus_string(Args&& ... args)
+{
+ return std::string(
+ sdbusplus::utility::tuple_to_array(
+ sdbusplus::message::types::type_id<Args...>()).data());
+}
+
+int main()
+{
+
+ // Check a few simple types.
+ assert(dbus_string(1) == "i");
+ assert(dbus_string(1.0) == "d");
+
+ // Check a multiple parameter call.
+ assert(dbus_string(false, true) == "bb");
+ assert(dbus_string(1, 2, 3, true, 1.0) == "iiibd");
+
+ // Check an l-value and r-value reference.
+ {
+ std::string a = "a";
+ std::string b = "b";
+ const char* c = "c";
+
+ assert(dbus_string(a, std::move(b), c) == "sss");
+ }
+ return 0;
+}