Patrick Williams | 51103a4 | 2016-07-20 17:43:38 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <tuple> |
| 4 | #include <string> |
| 5 | #include <vector> |
Patrick Williams | bbe0e43 | 2016-09-10 08:57:03 -0500 | [diff] [blame] | 6 | #include <map> |
Patrick Williams | 7559678 | 2016-09-11 21:14:10 -0500 | [diff] [blame] | 7 | #include <mapbox/variant.hpp> |
Patrick Williams | 90b4f3c | 2016-09-09 13:54:56 -0500 | [diff] [blame] | 8 | #include <systemd/sd-bus.h> |
Patrick Williams | 51103a4 | 2016-07-20 17:43:38 -0500 | [diff] [blame] | 9 | |
| 10 | #include <sdbusplus/utility/type_traits.hpp> |
Patrick Williams | 4498298 | 2017-01-06 16:02:37 -0600 | [diff] [blame] | 11 | #include <sdbusplus/message/native_types.hpp> |
Patrick Williams | 51103a4 | 2016-07-20 17:43:38 -0500 | [diff] [blame] | 12 | |
| 13 | namespace sdbusplus |
| 14 | { |
| 15 | |
| 16 | namespace message |
| 17 | { |
| 18 | |
Patrick Williams | 9fa8552 | 2017-02-01 23:06:33 -0600 | [diff] [blame] | 19 | namespace variant_ns = mapbox::util; |
| 20 | |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 21 | template <typename... Args> using variant = variant_ns::variant<Args...>; |
Patrick Williams | 7559678 | 2016-09-11 21:14:10 -0500 | [diff] [blame] | 22 | |
Patrick Williams | 51103a4 | 2016-07-20 17:43:38 -0500 | [diff] [blame] | 23 | namespace types |
| 24 | { |
| 25 | |
| 26 | /** @fn type_id() |
| 27 | * @brief Get a tuple containing the dbus type character(s) for a |
| 28 | * sequence of C++ types. |
| 29 | * |
| 30 | * @tparam ...Args - Types to get the type_id sequence for. |
| 31 | * @returns A tuple of characters representing the dbus types for ...Args. |
| 32 | * |
| 33 | * The design uses a tuple because a tuple can, at compile-time, be easily |
| 34 | * appended to, concatenated, and converted to an array (representing a |
| 35 | * C-string). There are other options to create a string of characters but |
| 36 | * they require runtime processing and memory allocation. By performing all |
| 37 | * options at compile-time the use of type-deduced dbus strings is equal to |
| 38 | * the cost of hard-coded type string constants. |
| 39 | */ |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 40 | template <typename... Args> constexpr auto type_id(); |
Patrick Williams | bbe0e43 | 2016-09-10 08:57:03 -0500 | [diff] [blame] | 41 | /** @fn type_id_nonull() |
| 42 | * @brief A non-null-terminated version of type_id. |
| 43 | * |
| 44 | * This is useful when type-ids may need to be concatenated. |
| 45 | */ |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 46 | template <typename... Args> constexpr auto type_id_nonull(); |
Patrick Williams | 51103a4 | 2016-07-20 17:43:38 -0500 | [diff] [blame] | 47 | |
| 48 | namespace details |
| 49 | { |
| 50 | |
Patrick Williams | 930460c | 2016-07-20 17:52:55 -0500 | [diff] [blame] | 51 | /** @brief Convert some C++ types to others for 'type_id' conversion purposes. |
| 52 | * |
| 53 | * Similar C++ types have the same dbus type-id, so 'downcast' those to limit |
| 54 | * duplication in type_id template specializations. |
| 55 | * |
| 56 | * 1. Remove references. |
| 57 | * 2. Remove 'const' and 'volatile'. |
| 58 | * 3. Convert 'char[N]' to 'char*'. |
| 59 | */ |
| 60 | template <typename T> struct type_id_downcast |
| 61 | { |
| 62 | using type = typename utility::array_to_ptr_t< |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 63 | char, std::remove_cv_t<std::remove_reference_t<T>>>; |
Patrick Williams | 930460c | 2016-07-20 17:52:55 -0500 | [diff] [blame] | 64 | }; |
| 65 | |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 66 | template <typename T> |
| 67 | using type_id_downcast_t = typename type_id_downcast<T>::type; |
Patrick Williams | 930460c | 2016-07-20 17:52:55 -0500 | [diff] [blame] | 68 | |
Patrick Williams | 51103a4 | 2016-07-20 17:43:38 -0500 | [diff] [blame] | 69 | /** @struct undefined_type_id |
| 70 | * @brief Special type indicating no dbus-type_id is defined for a C++ type. |
| 71 | */ |
| 72 | struct undefined_type_id |
| 73 | { |
| 74 | /** An empty tuple indicating no type-characters. */ |
| 75 | // We want this to be tuple so that we can use operations like |
| 76 | // tuple_cat on it without cascading compile failures. There is a |
| 77 | // static_assert in type_id_single to ensure this is never used, but to |
| 78 | // keep the compile failures as clean as possible. |
| 79 | static constexpr auto value = std::make_tuple(); |
| 80 | }; |
| 81 | |
| 82 | /** @struct tuple_type_id |
| 83 | * @brief Special type indicating a tuple of dbus-type_id's. |
| 84 | * |
| 85 | * @tparam C1 - The first dbus type character character. |
| 86 | * @tparam ...C - The remaining sequence of dbus type characters. |
| 87 | * |
| 88 | * A tuple_type_id must be one or more characters. The C1 template param |
| 89 | * ensures at least one is present. |
| 90 | */ |
| 91 | template <char C1, char... C> struct tuple_type_id |
| 92 | { |
| 93 | |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 94 | /* This version check is required because a fix for auto is in 5.2+. |
| 95 | * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66421 |
| 96 | */ |
| 97 | /** A tuple containing the type-characters. */ |
Patrick Williams | 51103a4 | 2016-07-20 17:43:38 -0500 | [diff] [blame] | 98 | #if (__GNUC__ > 5) || (__GNUC__ == 5 && (__GNUC_MINOR__ >= 2)) |
| 99 | static constexpr auto value = std::make_tuple(C1, C...); |
| 100 | #else |
| 101 | static constexpr decltype(std::make_tuple(C1, C...)) value = |
| 102 | std::make_tuple(C1, C...); |
| 103 | #endif |
| 104 | }; |
| 105 | |
| 106 | /** @fn type_id_single() |
| 107 | * @brief Get a tuple containing the dbus type character(s) for a C++ type. |
| 108 | * |
| 109 | * @tparam T - The type to get the dbus type character(s) for. |
| 110 | */ |
Brad Bishop | f7c2307 | 2018-02-14 23:45:30 -0500 | [diff] [blame^] | 111 | template <typename T> constexpr auto type_id_single(); |
Patrick Williams | 51103a4 | 2016-07-20 17:43:38 -0500 | [diff] [blame] | 112 | |
| 113 | /** @fn type_id_multiple() |
| 114 | * @brief Get a tuple containing the dbus type characters for a sequence of |
| 115 | * C++ types. |
| 116 | * |
| 117 | * @tparam T - The first type to get the dbus type character(s) for. |
| 118 | * @tparam ...Args - The remaining types. |
| 119 | */ |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 120 | template <typename T, typename... Args> constexpr auto type_id_multiple(); |
Patrick Williams | 51103a4 | 2016-07-20 17:43:38 -0500 | [diff] [blame] | 121 | |
| 122 | /** @struct type_id |
| 123 | * @brief Defined dbus type tuple for a C++ type. |
| 124 | * |
| 125 | * @tparam T - C++ type. |
| 126 | * |
| 127 | * Struct must have a 'value' tuple containing the dbus type. The default |
Patrick Williams | c8235ce | 2016-09-11 21:17:07 -0500 | [diff] [blame] | 128 | * value is an empty tuple, which is used to indicate an unsupported type. |
Patrick Williams | 51103a4 | 2016-07-20 17:43:38 -0500 | [diff] [blame] | 129 | */ |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 130 | template <typename T> struct type_id : public undefined_type_id |
| 131 | { |
| 132 | }; |
| 133 | // Specializations for built-in types. |
| 134 | template <> struct type_id<bool> : tuple_type_id<SD_BUS_TYPE_BOOLEAN> |
| 135 | { |
| 136 | }; |
| 137 | template <> struct type_id<uint8_t> : tuple_type_id<SD_BUS_TYPE_BYTE> |
| 138 | { |
| 139 | }; |
Patrick Williams | 51103a4 | 2016-07-20 17:43:38 -0500 | [diff] [blame] | 140 | // int8_t isn't supported by dbus. |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 141 | template <> struct type_id<uint16_t> : tuple_type_id<SD_BUS_TYPE_UINT16> |
| 142 | { |
| 143 | }; |
| 144 | template <> struct type_id<int16_t> : tuple_type_id<SD_BUS_TYPE_INT16> |
| 145 | { |
| 146 | }; |
| 147 | template <> struct type_id<uint32_t> : tuple_type_id<SD_BUS_TYPE_UINT32> |
| 148 | { |
| 149 | }; |
| 150 | template <> struct type_id<int32_t> : tuple_type_id<SD_BUS_TYPE_INT32> |
| 151 | { |
| 152 | }; |
| 153 | template <> struct type_id<uint64_t> : tuple_type_id<SD_BUS_TYPE_UINT64> |
| 154 | { |
| 155 | }; |
| 156 | template <> struct type_id<int64_t> : tuple_type_id<SD_BUS_TYPE_INT64> |
| 157 | { |
| 158 | }; |
Patrick Williams | 51103a4 | 2016-07-20 17:43:38 -0500 | [diff] [blame] | 159 | // float isn't supported by dbus. |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 160 | template <> struct type_id<double> : tuple_type_id<SD_BUS_TYPE_DOUBLE> |
| 161 | { |
| 162 | }; |
| 163 | template <> struct type_id<const char*> : tuple_type_id<SD_BUS_TYPE_STRING> |
| 164 | { |
| 165 | }; |
| 166 | template <> struct type_id<char*> : tuple_type_id<SD_BUS_TYPE_STRING> |
| 167 | { |
| 168 | }; |
| 169 | template <> struct type_id<std::string> : tuple_type_id<SD_BUS_TYPE_STRING> |
| 170 | { |
| 171 | }; |
| 172 | template <> struct type_id<object_path> : tuple_type_id<SD_BUS_TYPE_OBJECT_PATH> |
| 173 | { |
| 174 | }; |
| 175 | template <> struct type_id<signature> : tuple_type_id<SD_BUS_TYPE_SIGNATURE> |
| 176 | { |
| 177 | }; |
Patrick Williams | 51103a4 | 2016-07-20 17:43:38 -0500 | [diff] [blame] | 178 | |
Patrick Williams | 930460c | 2016-07-20 17:52:55 -0500 | [diff] [blame] | 179 | template <typename T> struct type_id<std::vector<T>> |
| 180 | { |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 181 | static constexpr auto value = |
| 182 | std::tuple_cat(tuple_type_id<SD_BUS_TYPE_ARRAY>::value, |
| 183 | type_id<type_id_downcast_t<T>>::value); |
Patrick Williams | 930460c | 2016-07-20 17:52:55 -0500 | [diff] [blame] | 184 | }; |
| 185 | |
Patrick Williams | bbe0e43 | 2016-09-10 08:57:03 -0500 | [diff] [blame] | 186 | template <typename T1, typename T2> struct type_id<std::pair<T1, T2>> |
| 187 | { |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 188 | static constexpr auto value = |
| 189 | std::tuple_cat(tuple_type_id<SD_BUS_TYPE_DICT_ENTRY_BEGIN>::value, |
| 190 | type_id<type_id_downcast_t<T1>>::value, |
| 191 | type_id<type_id_downcast_t<T2>>::value, |
| 192 | tuple_type_id<SD_BUS_TYPE_DICT_ENTRY_END>::value); |
Patrick Williams | bbe0e43 | 2016-09-10 08:57:03 -0500 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | template <typename T1, typename T2> struct type_id<std::map<T1, T2>> |
| 196 | { |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 197 | static constexpr auto value = |
| 198 | std::tuple_cat(tuple_type_id<SD_BUS_TYPE_ARRAY>::value, |
| 199 | type_id<typename std::map<T1, T2>::value_type>::value); |
Patrick Williams | bbe0e43 | 2016-09-10 08:57:03 -0500 | [diff] [blame] | 200 | }; |
| 201 | |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 202 | template <typename... Args> struct type_id<std::tuple<Args...>> |
Patrick Williams | aa2f48a | 2016-09-10 14:23:34 -0500 | [diff] [blame] | 203 | { |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 204 | static constexpr auto value = |
| 205 | std::tuple_cat(tuple_type_id<SD_BUS_TYPE_STRUCT_BEGIN>::value, |
| 206 | type_id<type_id_downcast_t<Args>>::value..., |
| 207 | tuple_type_id<SD_BUS_TYPE_STRUCT_END>::value); |
Patrick Williams | aa2f48a | 2016-09-10 14:23:34 -0500 | [diff] [blame] | 208 | }; |
| 209 | |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 210 | template <typename... Args> |
| 211 | struct type_id<variant<Args...>> : tuple_type_id<SD_BUS_TYPE_VARIANT> |
| 212 | { |
| 213 | }; |
Patrick Williams | 7559678 | 2016-09-11 21:14:10 -0500 | [diff] [blame] | 214 | |
Brad Bishop | f7c2307 | 2018-02-14 23:45:30 -0500 | [diff] [blame^] | 215 | template <typename T> constexpr auto type_id_single() |
Patrick Williams | 51103a4 | 2016-07-20 17:43:38 -0500 | [diff] [blame] | 216 | { |
| 217 | static_assert(!std::is_base_of<undefined_type_id, type_id<T>>::value, |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 218 | "No dbus type conversion provided for type."); |
Patrick Williams | 51103a4 | 2016-07-20 17:43:38 -0500 | [diff] [blame] | 219 | return type_id<T>::value; |
| 220 | } |
| 221 | |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 222 | template <typename T, typename... Args> constexpr auto type_id_multiple() |
Patrick Williams | 51103a4 | 2016-07-20 17:43:38 -0500 | [diff] [blame] | 223 | { |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 224 | return std::tuple_cat(type_id_single<T>(), type_id_single<Args>()...); |
Patrick Williams | 51103a4 | 2016-07-20 17:43:38 -0500 | [diff] [blame] | 225 | } |
| 226 | |
Patrick Williams | 51103a4 | 2016-07-20 17:43:38 -0500 | [diff] [blame] | 227 | } // namespace details |
| 228 | |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 229 | template <typename... Args> constexpr auto type_id() |
Patrick Williams | 51103a4 | 2016-07-20 17:43:38 -0500 | [diff] [blame] | 230 | { |
| 231 | return std::tuple_cat( |
| 232 | details::type_id_multiple<details::type_id_downcast_t<Args>...>(), |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 233 | std::make_tuple('\0') /* null terminator for C-string */); |
Patrick Williams | 51103a4 | 2016-07-20 17:43:38 -0500 | [diff] [blame] | 234 | } |
| 235 | |
Andrew Geissler | 072da3e | 2018-01-18 07:21:42 -0800 | [diff] [blame] | 236 | template <typename... Args> constexpr auto type_id_nonull() |
Patrick Williams | bbe0e43 | 2016-09-10 08:57:03 -0500 | [diff] [blame] | 237 | { |
| 238 | return details::type_id_multiple<details::type_id_downcast_t<Args>...>(); |
| 239 | } |
| 240 | |
Patrick Williams | 51103a4 | 2016-07-20 17:43:38 -0500 | [diff] [blame] | 241 | } // namespace types |
| 242 | |
| 243 | } // namespace message |
| 244 | |
| 245 | } // namespace sdbusplus |