Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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_H_ | |
| 6 #define IPC_IPC_MESSAGE_TEMPLATES_H_ | |
| 7 | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include <type_traits> | |
| 11 | |
| 12 #include "base/tuple.h" | |
| 13 #include "ipc/ipc_message.h" | |
| 14 #include "ipc/ipc_message_utils.h" | |
| 15 | |
| 16 namespace IPC { | |
| 17 | |
| 18 // This function is for all the async IPCs that don't pass an extra parameter | |
| 19 // using IPC_BEGIN_MESSAGE_MAP_WITH_PARAM. | |
| 20 template <typename ObjT, typename Method, typename P, typename Tuple> | |
| 21 void DispatchToMethod(ObjT* obj, Method method, P*, const Tuple& tuple) { | |
| 22 base::DispatchToMethod(obj, method, tuple); | |
| 23 } | |
| 24 | |
| 25 template <typename ObjT, | |
| 26 typename Method, | |
| 27 typename P, | |
| 28 typename Tuple, | |
| 29 size_t... Ns> | |
| 30 void DispatchToMethodImpl(ObjT* obj, | |
| 31 Method method, | |
| 32 P* parameter, | |
| 33 const Tuple& tuple, | |
| 34 base::IndexSequence<Ns...>) { | |
| 35 // TODO(mdempsky): Apply UnwrapTraits like base::DispatchToMethod? | |
| 36 (obj->*method)(parameter, base::get<Ns>(tuple)...); | |
| 37 } | |
| 38 | |
| 39 // The following function is for async IPCs which have a dispatcher with an | |
| 40 // extra parameter specified using IPC_BEGIN_MESSAGE_MAP_WITH_PARAM. | |
| 41 template <typename ObjT, typename P, typename... Args, typename... Ts> | |
| 42 typename std::enable_if<sizeof...(Args) == sizeof...(Ts)>::type | |
| 43 DispatchToMethod(ObjT* obj, | |
| 44 void (ObjT::*method)(P*, Args...), | |
| 45 P* parameter, | |
| 46 const base::Tuple<Ts...>& tuple) { | |
| 47 DispatchToMethodImpl(obj, method, parameter, tuple, | |
| 48 base::MakeIndexSequence<sizeof...(Ts)>()); | |
| 49 } | |
| 50 | |
| 51 template <uint32_t Id, const char* Name, typename InTuple, typename OutTuple> | |
| 52 class CommonAsyncMessage; | |
| 53 | |
| 54 template <uint32_t Id, const char* Name, typename... Ins> | |
|
danakj
2015/12/19 00:15:06
There's a real lack of comments in here to explain
mdempsky
2015/12/19 02:14:48
I'll add some. (To be fair, I preserved *both* of
| |
| 55 class CommonAsyncMessage<Id, Name, base::Tuple<Ins...>, base::Tuple<>> | |
| 56 : public Message { | |
| 57 public: | |
| 58 using Schema = MessageSchema<base::Tuple<Ins...>>; | |
| 59 using Param = typename Schema::Param; | |
| 60 enum { ID = Id }; | |
| 61 | |
| 62 CommonAsyncMessage(int32_t routing_id, const Ins&... ins); | |
| 63 | |
| 64 static bool Read(const Message* msg, Param* p); | |
| 65 static void Log(std::string* name, const Message* msg, std::string* l); | |
| 66 | |
| 67 template <class T, class S, class P, class Method> | |
| 68 static bool Dispatch(const Message* msg, | |
| 69 T* obj, | |
| 70 S* sender, | |
| 71 P* parameter, | |
| 72 Method func) { | |
| 73 Param p; | |
| 74 if (Read(msg, &p)) { | |
| 75 DispatchToMethod(obj, func, parameter, p); | |
| 76 return true; | |
| 77 } | |
| 78 return false; | |
| 79 } | |
| 80 }; | |
| 81 | |
| 82 template <uint32_t Id, const char* Name, typename InTuple, typename OutTuple> | |
| 83 class CommonSyncMessage; | |
| 84 | |
| 85 template <uint32_t Id, const char* Name, typename... Ins, typename... Outs> | |
| 86 class CommonSyncMessage<Id, Name, base::Tuple<Ins...>, base::Tuple<Outs...>> | |
| 87 : public SyncMessage { | |
| 88 public: | |
| 89 using Schema = SyncMessageSchema< | |
| 90 base::Tuple<Ins...>, | |
| 91 typename base::TupleTypes<base::Tuple<Outs...>>::RefTuple>; | |
| 92 using ReplyParam = typename Schema::ReplyParam; | |
| 93 using SendParam = typename Schema::SendParam; | |
| 94 enum { ID = Id }; | |
| 95 | |
| 96 CommonSyncMessage(int32_t routing_id, const Ins&... ins, Outs*... outs); | |
| 97 | |
| 98 static bool ReadSendParam(const Message* msg, SendParam* p); | |
| 99 static bool ReadReplyParam( | |
| 100 const Message* msg, | |
| 101 typename base::TupleTypes<ReplyParam>::ValueTuple* p); | |
| 102 static void Log(std::string* name, const Message* msg, std::string* l); | |
| 103 | |
| 104 template <class T, class S, class P, class Method> | |
| 105 static bool Dispatch(const Message* msg, | |
| 106 T* obj, | |
| 107 S* sender, | |
| 108 P* parameter, | |
| 109 Method func) { | |
| 110 SendParam send_params; | |
| 111 bool ok = ReadSendParam(msg, &send_params); | |
| 112 return Schema::DispatchWithSendParams(ok, send_params, msg, obj, sender, | |
| 113 func); | |
| 114 } | |
| 115 | |
| 116 template <class T, class P, class Method> | |
| 117 static bool DispatchDelayReply(const Message* msg, | |
| 118 T* obj, | |
| 119 P* parameter, | |
| 120 Method func) { | |
| 121 SendParam send_params; | |
| 122 bool ok = ReadSendParam(msg, &send_params); | |
| 123 return Schema::DispatchDelayReplyWithSendParams(ok, send_params, msg, obj, | |
| 124 func); | |
| 125 } | |
| 126 | |
| 127 template <typename... Args> | |
| 128 static void WriteReplyParams(Message* reply, Args... args) { | |
| 129 Schema::WriteReplyParams(reply, args...); | |
| 130 } | |
| 131 }; | |
| 132 | |
| 133 template <uint32_t Id, const char* Name, typename InTuple, typename OutTuple> | |
| 134 class AsyncControlMessage; | |
| 135 | |
| 136 template <uint32_t Id, const char* Name, typename... Ins> | |
| 137 class AsyncControlMessage<Id, Name, base::Tuple<Ins...>, base::Tuple<>> | |
| 138 : public CommonAsyncMessage<Id, Name, base::Tuple<Ins...>, base::Tuple<>> { | |
| 139 public: | |
| 140 AsyncControlMessage(const Ins&... ins) | |
| 141 : CommonAsyncMessage<Id, Name, base::Tuple<Ins...>, base::Tuple<>>( | |
| 142 MSG_ROUTING_CONTROL, | |
| 143 ins...) {} | |
| 144 }; | |
| 145 | |
| 146 template <uint32_t Id, const char* Name, typename InTuple, typename OutTuple> | |
| 147 class AsyncRoutedMessage; | |
| 148 | |
| 149 template <uint32_t Id, const char* Name, typename... Ins> | |
| 150 class AsyncRoutedMessage<Id, Name, base::Tuple<Ins...>, base::Tuple<>> | |
| 151 : public CommonAsyncMessage<Id, Name, base::Tuple<Ins...>, base::Tuple<>> { | |
| 152 public: | |
| 153 AsyncRoutedMessage(int32_t routing_id, const Ins&... ins) | |
| 154 : CommonAsyncMessage<Id, Name, base::Tuple<Ins...>, base::Tuple<>>( | |
| 155 routing_id, | |
| 156 ins...) {} | |
| 157 }; | |
| 158 | |
| 159 template <uint32_t Id, const char* Name, typename InTuple, typename OutTuple> | |
| 160 class SyncControlMessage; | |
| 161 | |
| 162 template <uint32_t Id, const char* Name, typename... Ins, typename... Outs> | |
| 163 class SyncControlMessage<Id, Name, base::Tuple<Ins...>, base::Tuple<Outs...>> | |
| 164 : public CommonSyncMessage<Id, | |
| 165 Name, | |
| 166 base::Tuple<Ins...>, | |
| 167 base::Tuple<Outs...>> { | |
| 168 public: | |
| 169 SyncControlMessage(const Ins&... ins, Outs*... outs) | |
| 170 : CommonSyncMessage<Id, Name, base::Tuple<Ins...>, base::Tuple<Outs...>>( | |
| 171 MSG_ROUTING_CONTROL, | |
| 172 ins..., | |
| 173 outs...) {} | |
| 174 }; | |
| 175 | |
| 176 template <uint32_t Id, const char* Name, typename InTuple, typename OutTuple> | |
| 177 class SyncRoutedMessage; | |
| 178 | |
| 179 template <uint32_t Id, const char* Name, typename... Ins, typename... Outs> | |
| 180 class SyncRoutedMessage<Id, Name, base::Tuple<Ins...>, base::Tuple<Outs...>> | |
| 181 : public CommonSyncMessage<Id, | |
| 182 Name, | |
| 183 base::Tuple<Ins...>, | |
| 184 base::Tuple<Outs...>> { | |
| 185 public: | |
| 186 SyncRoutedMessage(int32_t routing_id, const Ins&... ins, Outs*... outs) | |
| 187 : CommonSyncMessage<Id, Name, base::Tuple<Ins...>, base::Tuple<Outs...>>( | |
| 188 routing_id, | |
| 189 ins..., | |
| 190 outs...) {} | |
| 191 }; | |
| 192 | |
| 193 } // namespace IPC | |
| 194 | |
| 195 #if defined(IPC_MESSAGE_IMPL) | |
| 196 #include "ipc/ipc_message_templates_impl.h" | |
| 197 #endif | |
| 198 | |
| 199 #endif // IPC_IPC_MESSAGE_TEMPLATES_H_ | |
| OLD | NEW |