1*1a96fba6SXin Li // Copyright 2014 The Chromium OS Authors. All rights reserved. 2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be 3*1a96fba6SXin Li // found in the LICENSE file. 4*1a96fba6SXin Li 5*1a96fba6SXin Li // DBusParamWriter::Append(writer, ...) provides functionality opposite 6*1a96fba6SXin Li // to that of DBusParamReader. It writes each of the arguments to D-Bus message 7*1a96fba6SXin Li // writer and return true if successful. 8*1a96fba6SXin Li 9*1a96fba6SXin Li // DBusParamWriter::AppendDBusOutParams(writer, ...) is similar to Append() 10*1a96fba6SXin Li // but is used to send out the D-Bus OUT (pointer type) parameters in a D-Bus 11*1a96fba6SXin Li // method response message. This method skips any non-pointer parameters and 12*1a96fba6SXin Li // only appends the data for arguments that are pointers. 13*1a96fba6SXin Li 14*1a96fba6SXin Li #ifndef LIBBRILLO_BRILLO_DBUS_DBUS_PARAM_WRITER_H_ 15*1a96fba6SXin Li #define LIBBRILLO_BRILLO_DBUS_DBUS_PARAM_WRITER_H_ 16*1a96fba6SXin Li 17*1a96fba6SXin Li #include <brillo/dbus/data_serialization.h> 18*1a96fba6SXin Li #include <dbus/message.h> 19*1a96fba6SXin Li 20*1a96fba6SXin Li namespace brillo { 21*1a96fba6SXin Li namespace dbus_utils { 22*1a96fba6SXin Li 23*1a96fba6SXin Li class DBusParamWriter final { 24*1a96fba6SXin Li public: 25*1a96fba6SXin Li // Generic writer method that takes 1 or more arguments. It recursively calls 26*1a96fba6SXin Li // itself (each time with one fewer arguments) until no more is left. 27*1a96fba6SXin Li template <typename ParamType, typename... RestOfParams> Append(::dbus::MessageWriter * writer,const ParamType & param,const RestOfParams &...rest)28*1a96fba6SXin Li static void Append(::dbus::MessageWriter* writer, 29*1a96fba6SXin Li const ParamType& param, 30*1a96fba6SXin Li const RestOfParams&... rest) { 31*1a96fba6SXin Li // Append the current |param| to D-Bus, then call Append() with one 32*1a96fba6SXin Li // fewer arguments, until none is left and stand-alone version of 33*1a96fba6SXin Li // Append(dbus::MessageWriter*) is called to end the iteration. 34*1a96fba6SXin Li DBusType<ParamType>::Write(writer, param); 35*1a96fba6SXin Li Append(writer, rest...); 36*1a96fba6SXin Li } 37*1a96fba6SXin Li 38*1a96fba6SXin Li // The final overload of DBusParamWriter::Append() used when no more 39*1a96fba6SXin Li // parameters are remaining to be written. 40*1a96fba6SXin Li // Does nothing and finishes meta-recursion. Append(::dbus::MessageWriter *)41*1a96fba6SXin Li static void Append(::dbus::MessageWriter* /*writer*/) {} 42*1a96fba6SXin Li 43*1a96fba6SXin Li // Generic writer method that takes 1 or more arguments. It recursively calls 44*1a96fba6SXin Li // itself (each time with one fewer arguments) until no more is left. 45*1a96fba6SXin Li // Handles non-pointer parameter by just skipping over it. 46*1a96fba6SXin Li template <typename ParamType, typename... RestOfParams> AppendDBusOutParams(::dbus::MessageWriter * writer,const ParamType &,const RestOfParams &...rest)47*1a96fba6SXin Li static void AppendDBusOutParams(::dbus::MessageWriter* writer, 48*1a96fba6SXin Li const ParamType& /* param */, 49*1a96fba6SXin Li const RestOfParams&... rest) { 50*1a96fba6SXin Li // Skip the current |param| and call Append() with one fewer arguments, 51*1a96fba6SXin Li // until none is left and stand-alone version of 52*1a96fba6SXin Li // AppendDBusOutParams(dbus::MessageWriter*) is called to end the iteration. 53*1a96fba6SXin Li AppendDBusOutParams(writer, rest...); 54*1a96fba6SXin Li } 55*1a96fba6SXin Li 56*1a96fba6SXin Li // Generic writer method that takes 1 or more arguments. It recursively calls 57*1a96fba6SXin Li // itself (each time with one fewer arguments) until no more is left. 58*1a96fba6SXin Li // Handles only a parameter of pointer type and writes the data pointed to 59*1a96fba6SXin Li // to the output message buffer. 60*1a96fba6SXin Li template <typename ParamType, typename... RestOfParams> AppendDBusOutParams(::dbus::MessageWriter * writer,ParamType * param,const RestOfParams &...rest)61*1a96fba6SXin Li static void AppendDBusOutParams(::dbus::MessageWriter* writer, 62*1a96fba6SXin Li ParamType* param, 63*1a96fba6SXin Li const RestOfParams&... rest) { 64*1a96fba6SXin Li // Append the current |param| to D-Bus, then call Append() with one 65*1a96fba6SXin Li // fewer arguments, until none is left and stand-alone version of 66*1a96fba6SXin Li // Append(dbus::MessageWriter*) is called to end the iteration. 67*1a96fba6SXin Li DBusType<ParamType>::Write(writer, *param); 68*1a96fba6SXin Li AppendDBusOutParams(writer, rest...); 69*1a96fba6SXin Li } 70*1a96fba6SXin Li 71*1a96fba6SXin Li // The final overload of DBusParamWriter::AppendDBusOutParams() used when no 72*1a96fba6SXin Li // more parameters are remaining to be written. 73*1a96fba6SXin Li // Does nothing and finishes meta-recursion. AppendDBusOutParams(::dbus::MessageWriter *)74*1a96fba6SXin Li static void AppendDBusOutParams(::dbus::MessageWriter* /*writer*/) {} 75*1a96fba6SXin Li }; 76*1a96fba6SXin Li 77*1a96fba6SXin Li } // namespace dbus_utils 78*1a96fba6SXin Li } // namespace brillo 79*1a96fba6SXin Li 80*1a96fba6SXin Li #endif // LIBBRILLO_BRILLO_DBUS_DBUS_PARAM_WRITER_H_ 81