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
|
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// DBusParamWriter::Append(writer, ...) provides functionality opposite
// to that of DBusParamReader. It writes each of the arguments to D-Bus message
// writer and return true if successful.
// DBusParamWriter::AppendDBusOutParams(writer, ...) is similar to Append()
// but is used to send out the D-Bus OUT (pointer type) parameters in a D-Bus
// method response message. This method skips any non-pointer parameters and
// only appends the data for arguments that are pointers.
#ifndef LIBCHROMEOS_BRILLO_DBUS_DBUS_PARAM_WRITER_H_
#define LIBCHROMEOS_BRILLO_DBUS_DBUS_PARAM_WRITER_H_
#include <brillo/dbus/data_serialization.h>
#include <dbus/message.h>
namespace brillo {
namespace dbus_utils {
class DBusParamWriter final {
public:
// Generic writer method that takes 1 or more arguments. It recursively calls
// itself (each time with one fewer arguments) until no more is left.
template<typename ParamType, typename... RestOfParams>
static void Append(dbus::MessageWriter* writer,
const ParamType& param,
const RestOfParams&... rest) {
// Append the current |param| to D-Bus, then call Append() with one
// fewer arguments, until none is left and stand-alone version of
// Append(dbus::MessageWriter*) is called to end the iteration.
DBusType<ParamType>::Write(writer, param);
Append(writer, rest...);
}
// The final overload of DBusParamWriter::Append() used when no more
// parameters are remaining to be written.
// Does nothing and finishes meta-recursion.
static void Append(dbus::MessageWriter* /*writer*/) {}
// Generic writer method that takes 1 or more arguments. It recursively calls
// itself (each time with one fewer arguments) until no more is left.
// Handles non-pointer parameter by just skipping over it.
template<typename ParamType, typename... RestOfParams>
static void AppendDBusOutParams(dbus::MessageWriter* writer,
const ParamType& param,
const RestOfParams&... rest) {
// Skip the current |param| and call Append() with one fewer arguments,
// until none is left and stand-alone version of
// AppendDBusOutParams(dbus::MessageWriter*) is called to end the iteration.
AppendDBusOutParams(writer, rest...);
}
// Generic writer method that takes 1 or more arguments. It recursively calls
// itself (each time with one fewer arguments) until no more is left.
// Handles only a parameter of pointer type and writes the data pointed to
// to the output message buffer.
template<typename ParamType, typename... RestOfParams>
static void AppendDBusOutParams(dbus::MessageWriter* writer,
ParamType* param,
const RestOfParams&... rest) {
// Append the current |param| to D-Bus, then call Append() with one
// fewer arguments, until none is left and stand-alone version of
// Append(dbus::MessageWriter*) is called to end the iteration.
DBusType<ParamType>::Write(writer, *param);
AppendDBusOutParams(writer, rest...);
}
// The final overload of DBusParamWriter::AppendDBusOutParams() used when no
// more parameters are remaining to be written.
// Does nothing and finishes meta-recursion.
static void AppendDBusOutParams(dbus::MessageWriter* /*writer*/) {}
};
} // namespace dbus_utils
} // namespace brillo
#endif // LIBCHROMEOS_BRILLO_DBUS_DBUS_PARAM_WRITER_H_
|