1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
/*
* Copyright © 2012 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Thomas Voß <thomas.voss@canonical.com>
*/
#ifndef CORE_DBUS_CODEC_H_
#define CORE_DBUS_CODEC_H_
#include <core/dbus/message.h>
#include <core/dbus/helper/type_mapper.h>
namespace core
{
namespace dbus
{
/**
* @brief A templated class that allows for defining encoding/decoding of arbitrary types to the DBus type system.
* @tparam T The type for which encoding and decoding should be specified.
*/
template<typename T>
struct Codec
{
/**
* @brief Encodes an argument to a DBus message
* @param out Output writer into the outgoing message.
* @param arg The argument to encode.
* @throw std::runtime_error in case of errors.
*/
static void encode_argument(Message::Writer& out, const T& arg);
/**
* @brief Decodes an argument from a DBus message.
* @param in Input iterator into the incoming message.
* @param arg The argument to decode to.
* @throw std::runtime_error in case of errors.
*/
static void decode_argument(Message::Reader& in, T& arg);
};
/**
* @brief Template specialization for void argument types.
*/
template<>
struct Codec<void>
{
/**
* @brief Does nothing
*/
inline static void encode_argument(Message::Writer&)
{
}
/**
* @brief Does nothing
*/
inline static void decode_argument(Message::Reader&)
{
}
};
/**
* @brief Template specialization for byte argument types.
*/
template<>
struct Codec<std::int8_t>
{
inline static void encode_argument(Message::Writer& out, const std::int8_t& value)
{
out.push_byte(value);
}
inline static void decode_argument(Message::Reader& in, std::int8_t& value)
{
value = in.pop_byte();
}
};
/**
* @brief Template specialization for boolean argument types.
*/
template<>
struct Codec<bool>
{
inline static void encode_argument(Message::Writer& out, bool value)
{
out.push_boolean(value);
}
inline static void decode_argument(Message::Reader& in, bool& value)
{
value = in.pop_boolean();
}
};
/**
* @brief Template specialization for int16 argument types.
*/
template<>
struct Codec<std::int16_t>
{
inline static void encode_argument(Message::Writer& out, std::int16_t value)
{
out.push_int16(value);
}
inline static void decode_argument(Message::Reader& in, std::int16_t& value)
{
value = in.pop_int16();
}
};
/**
* @brief Template specialization for uint16 argument types.
*/
template<>
struct Codec<std::uint16_t>
{
inline static void encode_argument(Message::Writer& out, std::uint16_t value)
{
out.push_uint16(value);
}
inline static void decode_argument(Message::Reader& in, std::uint16_t& value)
{
value = in.pop_uint16();
}
};
/**
* @brief Template specialization for int32 argument types.
*/
template<>
struct Codec<std::int32_t>
{
inline static void encode_argument(Message::Writer& out, std::int32_t value)
{
out.push_int32(value);
}
inline static void decode_argument(Message::Reader& in, std::int32_t& value)
{
value = in.pop_int32();
}
};
/**
* @brief Template specialization for uint32 argument types.
*/
template<>
struct Codec<std::uint32_t>
{
inline static void encode_argument(Message::Writer& out, std::uint32_t value)
{
out.push_uint32(value);
}
inline static void decode_argument(Message::Reader& in, std::uint32_t& value)
{
value = in.pop_uint32();
}
};
/**
* @brief Template specialization for int64 argument types.
*/
template<>
struct Codec<std::int64_t>
{
inline static void encode_argument(Message::Writer& out, std::int64_t value)
{
out.push_int64(value);
}
inline static void decode_argument(Message::Reader& in, std::int64_t& value)
{
value = in.pop_int64();
}
};
/**
* @brief Template specialization for uint64 argument types.
*/
template<>
struct Codec<std::uint64_t>
{
inline static void encode_argument(Message::Writer& out, std::uint64_t value)
{
out.push_uint64(value);
}
inline static void decode_argument(Message::Reader& in, std::uint64_t& value)
{
value = in.pop_uint64();
}
};
/**
* @brief Template specialization for floating point argument types.
*/
template<>
struct Codec<double>
{
inline static void encode_argument(Message::Writer& out, double value)
{
out.push_floating_point(value);
}
inline static void decode_argument(Message::Reader& in, double& value)
{
value = in.pop_floating_point();
}
};
/**
* @brief Template specialization for floating point argument types.
*/
template<>
struct Codec<float>
{
inline static void encode_argument(Message::Writer& out, float value)
{
out.push_floating_point(value);
}
inline static void decode_argument(Message::Reader& in, float& value)
{
value = in.pop_floating_point();
}
};
/**
* @brief Template specialization for object path argument types.
*/
template<>
struct Codec<types::ObjectPath>
{
inline static void encode_argument(Message::Writer& out, const types::ObjectPath& value)
{
out.push_object_path(value);
}
inline static void decode_argument(Message::Reader& in, types::ObjectPath& value)
{
value = in.pop_object_path();
}
};
/**
* @brief Template specialization for object path argument types.
*/
template<>
struct Codec<types::Signature>
{
inline static void encode_argument(Message::Writer& out, const types::Signature& value)
{
out.push_signature(value);
}
inline static void decode_argument(Message::Reader& in, types::Signature& value)
{
value = in.pop_signature();
}
};
/**
* @brief Template specialization for object path argument types.
*/
template<>
struct Codec<types::UnixFd>
{
inline static void encode_argument(Message::Writer& out, const types::UnixFd& value)
{
out.push_unix_fd(value);
}
inline static void decode_argument(Message::Reader& in, types::UnixFd& value)
{
value = in.pop_unix_fd();
}
};
/**
* @brief Template specialization for any argument types.
*/
template<>
struct Codec<types::Any>
{
inline static void encode_argument(Message::Writer& out, const types::Any& value)
{
(void) out;
(void) value;
}
inline static void decode_argument(Message::Reader& in, types::Any& value)
{
value = types::Any{in};
}
};
/**
* @brief Helper function that encodes the supplied argument relying on a Codec template specialization.
* @tparam T Type of the argument that should be encoded.
* @param out Output iterator to write to.
* @param arg Argument to encode.
* @throw std::runtime_error as thrown by Codec<T>::encode_argument.
*/
template<typename T>
inline void encode_argument(Message::Writer& out, const T& arg)
{
Codec<typename std::decay<T>::type>::encode_argument(out, arg);
}
/**
* @brief Special overload to end compile-time recursion.
*/
inline void encode_message(Message::Writer&) {}
/**
* @brief Compile-time recursion to encode a parameter pack into a DBus message.
* @tparam Arg The first argument to encode.
* @tparam Args The remaining arguments to encode.
* @throw std::runtime_error as propagated by Codec<Arg>::encode_argument.
*/
template<typename Arg, typename... Args>
inline void encode_message(Message::Writer& out, const Arg& arg, Args... params)
{
encode_argument(out, arg);
encode_message(out, params...);
}
/**
* @brief Helper function that decodes the supplied argument relying on a Codec template specialization.
* @tparam T Type of the argument that should be decoded.
* @param out Input iterator to read from.
* @param arg Argument to decode.
* @throw std::runtime_error as thrown by Codec<T>::decode_argument.
*/
template<typename T>
inline void decode_argument(Message::Reader& out, T& arg)
{
Codec<T>::decode_argument(out, arg);
}
/**
* @brief Helper function that decodes an argument relying on a Codec template specialization, returning a copy of the argument.
* @tparam T Type of the argument that should be decoded.
* @param out Input iterator to read from.
* @return An instance of T, filled with values decoded from the underlying message.
* @throw std::runtime_error as thrown by Codec<T>::decode_argument.
*/
template<typename T>
inline T decode_argument(Message::Reader& out)
{
T arg;
Codec<T>::decode_argument(out, arg);
return arg;
}
/**
* @brief Special overload to end compile-time recursion.
*/
inline void decode_message(Message::Reader&) {}
/**
* @brief Compile-time recursion to decode a parameter pack from a DBus message.
* @tparam Arg The first argument to decode.
* @tparam Args The remaining arguments to decode.
* @throw std::runtime_error as propagated by Codec<Arg>::decode_argument.
*/
template<typename Arg, typename... Args>
inline void decode_message(Message::Reader& out, Arg& arg, Args& ... params)
{
decode_argument(out, arg); decode_message(out, params...);
}
}
}
#endif // CORE_DBUS_CODEC_H_
|