1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef IPC_IPC_MESSAGE_TEMPLATES_IMPL_H_
6 #define IPC_IPC_MESSAGE_TEMPLATES_IMPL_H_
7
8 #include <tuple>
9
10 namespace IPC {
11
12 template <typename... Ts>
13 class ParamDeserializer : public MessageReplyDeserializer {
14 public:
ParamDeserializer(const std::tuple<Ts &...> & out)15 explicit ParamDeserializer(const std::tuple<Ts&...>& out) : out_(out) {}
16
SerializeOutputParameters(const IPC::Message & msg,base::PickleIterator iter)17 bool SerializeOutputParameters(const IPC::Message& msg,
18 base::PickleIterator iter) override {
19 return ReadParam(&msg, &iter, &out_);
20 }
21
22 std::tuple<Ts&...> out_;
23 };
24
25 template <typename Meta, typename... Ins>
MessageT(Routing routing,const Ins &...ins)26 MessageT<Meta, std::tuple<Ins...>, void>::MessageT(Routing routing,
27 const Ins&... ins)
28 : Message(routing.id, ID, PRIORITY_NORMAL) {
29 WriteParam(this, std::tie(ins...));
30 }
31
32 template <typename Meta, typename... Ins>
Read(const Message * msg,Param * p)33 bool MessageT<Meta, std::tuple<Ins...>, void>::Read(const Message* msg,
34 Param* p) {
35 base::PickleIterator iter(*msg);
36 return ReadParam(msg, &iter, p);
37 }
38
39 template <typename Meta, typename... Ins>
Log(std::string * name,const Message * msg,std::string * l)40 void MessageT<Meta, std::tuple<Ins...>, void>::Log(std::string* name,
41 const Message* msg,
42 std::string* l) {
43 if (name)
44 *name = Meta::kName;
45 if (!msg || !l)
46 return;
47 Param p;
48 if (Read(msg, &p))
49 LogParam(p, l);
50 }
51
52 template <typename Meta, typename... Ins, typename... Outs>
MessageT(Routing routing,const Ins &...ins,Outs * ...outs)53 MessageT<Meta, std::tuple<Ins...>, std::tuple<Outs...>>::MessageT(
54 Routing routing,
55 const Ins&... ins,
56 Outs*... outs)
57 : SyncMessage(
58 routing.id,
59 ID,
60 PRIORITY_NORMAL,
61 new ParamDeserializer<Outs...>(std::tie(*outs...))) {
62 WriteParam(this, std::tie(ins...));
63 }
64
65 template <typename Meta, typename... Ins, typename... Outs>
ReadSendParam(const Message * msg,SendParam * p)66 bool MessageT<Meta, std::tuple<Ins...>, std::tuple<Outs...>>::ReadSendParam(
67 const Message* msg,
68 SendParam* p) {
69 base::PickleIterator iter = SyncMessage::GetDataIterator(msg);
70 return ReadParam(msg, &iter, p);
71 }
72
73 template <typename Meta, typename... Ins, typename... Outs>
ReadReplyParam(const Message * msg,ReplyParam * p)74 bool MessageT<Meta, std::tuple<Ins...>, std::tuple<Outs...>>::ReadReplyParam(
75 const Message* msg,
76 ReplyParam* p) {
77 base::PickleIterator iter = SyncMessage::GetDataIterator(msg);
78 return ReadParam(msg, &iter, p);
79 }
80
81 template <typename Meta, typename... Ins, typename... Outs>
82 void MessageT<Meta,
83 std::tuple<Ins...>,
WriteReplyParams(Message * reply,const Outs &...outs)84 std::tuple<Outs...>>::WriteReplyParams(Message* reply,
85 const Outs&... outs) {
86 WriteParam(reply, std::tie(outs...));
87 }
88
89 template <typename Meta, typename... Ins, typename... Outs>
Log(std::string * name,const Message * msg,std::string * l)90 void MessageT<Meta, std::tuple<Ins...>, std::tuple<Outs...>>::Log(
91 std::string* name,
92 const Message* msg,
93 std::string* l) {
94 if (name)
95 *name = Meta::kName;
96 if (!msg || !l)
97 return;
98 if (msg->is_sync()) {
99 SendParam p;
100 if (ReadSendParam(msg, &p))
101 LogParam(p, l);
102 AddOutputParamsToLog(msg, l);
103 } else {
104 ReplyParam p;
105 if (ReadReplyParam(msg, &p))
106 LogParam(p, l);
107 }
108 }
109
110 } // namespace IPC
111
112 #endif // IPC_IPC_MESSAGE_TEMPLATES_IMPL_H_
113