feat: support enums in D-Bus serialization and signatures (#416)

This commit is contained in:
Marcel Hellwig
2024-03-07 17:49:05 +01:00
committed by GitHub
parent 30d9f1d462
commit 8a9cfc1f19
4 changed files with 70 additions and 6 deletions
+22 -2
View File
@@ -29,9 +29,10 @@
#include <sdbus-c++/TypeTraits.h>
#include <sdbus-c++/Error.h>
#include <string>
#include <vector>
#include <array>
#include <string>
#include <utility>
#include <vector>
#if __cplusplus >= 202002L
#include <span>
#endif
@@ -100,6 +101,8 @@ namespace sdbus {
template <typename _Element, std::size_t _Extent>
Message& operator<<(const std::span<_Element, _Extent>& items);
#endif
template <typename _Enum, typename = std::enable_if_t<std::is_enum_v<_Enum>>>
Message& operator<<(const _Enum& item);
template <typename _Key, typename _Value, typename _Compare, typename _Allocator>
Message& operator<<(const std::map<_Key, _Value, _Compare, _Allocator>& items);
template <typename _Key, typename _Value, typename _Hash, typename _KeyEqual, typename _Allocator>
@@ -132,6 +135,8 @@ namespace sdbus {
template <typename _Element, std::size_t _Extent>
Message& operator>>(std::span<_Element, _Extent>& items);
#endif
template <typename _Enum, typename = std::enable_if_t<std::is_enum_v<_Enum>>>
Message& operator>>(_Enum& item);
template <typename _Key, typename _Value, typename _Compare, typename _Allocator>
Message& operator>>(std::map<_Key, _Value, _Compare, _Allocator>& items);
template <typename _Key, typename _Value, typename _Hash, typename _KeyEqual, typename _Allocator>
@@ -332,6 +337,12 @@ namespace sdbus {
}
#endif
template <typename _Enum, typename>
inline Message& Message::operator<<(const _Enum &item)
{
return operator<<(static_cast<std::underlying_type_t<_Enum>>(item));
}
template <typename _Array>
inline void Message::serializeArray(const _Array& items)
{
@@ -457,6 +468,15 @@ namespace sdbus {
}
#endif
template <typename _Enum, typename>
inline Message& Message::operator>>(_Enum& item)
{
std::underlying_type_t<_Enum> val;
*this >> val;
item = static_cast<_Enum>(val);
return *this;
}
template <typename _Array>
inline void Message::deserializeArray(_Array& items)
{
+8 -3
View File
@@ -98,7 +98,7 @@ namespace sdbus {
inline constexpr dont_expect_reply_t dont_expect_reply{};
// Template specializations for getting D-Bus signatures from C++ types
template <typename _T>
template <typename _T, typename _Enable = void>
struct signature_of
{
static constexpr bool is_valid = false;
@@ -120,7 +120,7 @@ namespace sdbus {
template <typename _T>
struct signature_of<_T&>
: public signature_of<_T>
: public signature_of<_T>
{};
template <>
@@ -405,6 +405,12 @@ namespace sdbus {
};
#endif
template <typename _Enum>
struct signature_of<_Enum, typename std::enable_if_t<std::is_enum_v<_Enum>>>
: public signature_of<std::underlying_type_t<_Enum>>
{};
template <typename _Key, typename _Value, typename _Compare, typename _Allocator>
struct signature_of<std::map<_Key, _Value, _Compare, _Allocator>>
{
@@ -429,7 +435,6 @@ namespace sdbus {
}
};
// Function traits implementation inspired by (c) kennytm,
// https://github.com/kennytm/utils/blob/master/traits.hpp
template <typename _Type>