1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // This file is only meant to be included in autogenerated .cc files.
18 
19 #ifndef INCLUDE_PERFETTO_IPC_CODEGEN_HELPERS_H_
20 #define INCLUDE_PERFETTO_IPC_CODEGEN_HELPERS_H_
21 
22 // A templated protobuf message decoder. Returns nullptr in case of failure.
23 template <typename T>
_IPC_Decoder(const std::string & proto_data)24 ::std::unique_ptr<::perfetto::ipc::ProtoMessage> _IPC_Decoder(
25     const std::string& proto_data) {
26   ::std::unique_ptr<::perfetto::ipc::ProtoMessage> msg(new T());
27   if (msg->ParseFromString(proto_data))
28     return msg;
29   return nullptr;
30 }
31 
32 // Templated method dispatcher. Used to obtain a function pointer to a given
33 // IPC method (Method) of a given service (TSvc) that can be invoked by the
34 // host-side machinery starting from a generic Service pointer and a generic
35 // ProtoMessage request argument.
36 template <typename TSvc,    // Type of the actual Service subclass.
37           typename TReq,    // Type of the request argument.
38           typename TReply,  // Type of the reply argument.
39           void (TSvc::*Method)(const TReq&, ::perfetto::ipc::Deferred<TReply>)>
_IPC_Invoker(::perfetto::ipc::Service * s,const::perfetto::ipc::ProtoMessage & req,::perfetto::ipc::DeferredBase reply)40 void _IPC_Invoker(::perfetto::ipc::Service* s,
41                   const ::perfetto::ipc::ProtoMessage& req,
42                   ::perfetto::ipc::DeferredBase reply) {
43   (*static_cast<TSvc*>(s).*Method)(
44       static_cast<const TReq&>(req),
45       ::perfetto::ipc::Deferred<TReply>(::std::move(reply)));
46 }
47 
48 #endif  // INCLUDE_PERFETTO_IPC_CODEGEN_HELPERS_H_
49