blob: 3fdb51913da34d77087fb02f7fdf772f62a199af [file] [log] [blame]
[email protected]58786932012-10-13 10:16:081// Copyright (c) 2012 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 PPAPI_PROXY_PPAPI_MESSAGE_UTILS_H_
6#define PPAPI_PROXY_PPAPI_MESSAGE_UTILS_H_
7
[email protected]58786932012-10-13 10:16:088#include "base/pickle.h"
9#include "base/tuple.h"
10#include "ipc/ipc_message.h"
11#include "ipc/ipc_message_utils.h"
12
13namespace ppapi {
14
15namespace internal {
16
17// TupleTypeMatch* check whether a tuple type contains elements of the specified
18// types. They are used to make sure the output parameters of UnpackMessage()
19// match the corresponding message type.
20template <class TupleType, class A>
21struct TupleTypeMatch1 {
22 static const bool kValue = false;
23};
24template <class A>
tzik9ca302192016-02-11 10:24:4525struct TupleTypeMatch1<std::tuple<A>, A> {
[email protected]58786932012-10-13 10:16:0826 static const bool kValue = true;
27};
28
29template <class TupleType, class A, class B>
30struct TupleTypeMatch2 {
31 static const bool kValue = false;
32};
33template <class A, class B>
tzik9ca302192016-02-11 10:24:4534struct TupleTypeMatch2<std::tuple<A, B>, A, B> {
[email protected]58786932012-10-13 10:16:0835 static const bool kValue = true;
36};
37
38template <class TupleType, class A, class B, class C>
39struct TupleTypeMatch3 {
40 static const bool kValue = false;
41};
42template <class A, class B, class C>
tzik9ca302192016-02-11 10:24:4543struct TupleTypeMatch3<std::tuple<A, B, C>, A, B, C> {
[email protected]58786932012-10-13 10:16:0844 static const bool kValue = true;
45};
46
47template <class TupleType, class A, class B, class C, class D>
48struct TupleTypeMatch4 {
49 static const bool kValue = false;
50};
51template <class A, class B, class C, class D>
tzik9ca302192016-02-11 10:24:4552struct TupleTypeMatch4<std::tuple<A, B, C, D>, A, B, C, D> {
[email protected]58786932012-10-13 10:16:0853 static const bool kValue = true;
54};
55
56template <class TupleType, class A, class B, class C, class D, class E>
57struct TupleTypeMatch5 {
58 static const bool kValue = false;
59};
60template <class A, class B, class C, class D, class E>
tzik9ca302192016-02-11 10:24:4561struct TupleTypeMatch5<std::tuple<A, B, C, D, E>, A, B, C, D, E> {
[email protected]58786932012-10-13 10:16:0862 static const bool kValue = true;
63};
64
65} // namespace internal
66
67template <class MsgClass, class A>
68bool UnpackMessage(const IPC::Message& msg, A* a) {
mostynbd349f6d62015-01-06 00:42:0369 static_assert(
[email protected]58786932012-10-13 10:16:0870 (internal::TupleTypeMatch1<typename MsgClass::Param, A>::kValue),
mostynbd349f6d62015-01-06 00:42:0371 "tuple types should match");
[email protected]58786932012-10-13 10:16:0872
brettw05cfd8ddb2015-06-02 07:02:4773 base::PickleIterator iter(msg);
[email protected]58786932012-10-13 10:16:0874 return IPC::ReadParam(&msg, &iter, a);
75}
76
77template <class MsgClass, class A, class B>
78bool UnpackMessage(const IPC::Message& msg, A* a, B* b) {
mostynbd349f6d62015-01-06 00:42:0379 static_assert(
[email protected]58786932012-10-13 10:16:0880 (internal::TupleTypeMatch2<typename MsgClass::Param, A, B>::kValue),
mostynbd349f6d62015-01-06 00:42:0381 "tuple types should match");
[email protected]58786932012-10-13 10:16:0882
brettw05cfd8ddb2015-06-02 07:02:4783 base::PickleIterator iter(msg);
[email protected]58786932012-10-13 10:16:0884 return IPC::ReadParam(&msg, &iter, a) && IPC::ReadParam(&msg, &iter, b);
85}
86
87template <class MsgClass, class A, class B, class C>
88bool UnpackMessage(const IPC::Message& msg, A* a, B* b, C* c) {
mostynbd349f6d62015-01-06 00:42:0389 static_assert(
[email protected]58786932012-10-13 10:16:0890 (internal::TupleTypeMatch3<typename MsgClass::Param, A, B, C>::kValue),
mostynbd349f6d62015-01-06 00:42:0391 "tuple types should match");
[email protected]58786932012-10-13 10:16:0892
brettw05cfd8ddb2015-06-02 07:02:4793 base::PickleIterator iter(msg);
[email protected]58786932012-10-13 10:16:0894 return IPC::ReadParam(&msg, &iter, a) &&
95 IPC::ReadParam(&msg, &iter, b) &&
96 IPC::ReadParam(&msg, &iter, c);
97}
98
99template <class MsgClass, class A, class B, class C, class D>
100bool UnpackMessage(const IPC::Message& msg, A* a, B* b, C* c, D* d) {
mostynbd349f6d62015-01-06 00:42:03101 static_assert(
[email protected]58786932012-10-13 10:16:08102 (internal::TupleTypeMatch4<typename MsgClass::Param, A, B, C, D>::kValue),
mostynbd349f6d62015-01-06 00:42:03103 "tuple types should match");
[email protected]58786932012-10-13 10:16:08104
brettw05cfd8ddb2015-06-02 07:02:47105 base::PickleIterator iter(msg);
[email protected]58786932012-10-13 10:16:08106 return IPC::ReadParam(&msg, &iter, a) &&
107 IPC::ReadParam(&msg, &iter, b) &&
108 IPC::ReadParam(&msg, &iter, c) &&
109 IPC::ReadParam(&msg, &iter, d);
110}
111
112template <class MsgClass, class A, class B, class C, class D, class E>
113bool UnpackMessage(const IPC::Message& msg, A* a, B* b, C* c, D* d, E* e) {
mostynbd349f6d62015-01-06 00:42:03114 static_assert(
[email protected]58786932012-10-13 10:16:08115 (internal::TupleTypeMatch5<
116 typename MsgClass::Param, A, B, C, D, E>::kValue),
mostynbd349f6d62015-01-06 00:42:03117 "tuple types should match");
[email protected]58786932012-10-13 10:16:08118
brettw05cfd8ddb2015-06-02 07:02:47119 base::PickleIterator iter(msg);
[email protected]58786932012-10-13 10:16:08120 return IPC::ReadParam(&msg, &iter, a) &&
121 IPC::ReadParam(&msg, &iter, b) &&
122 IPC::ReadParam(&msg, &iter, c) &&
123 IPC::ReadParam(&msg, &iter, d) &&
124 IPC::ReadParam(&msg, &iter, e);
125}
126
127} // namespace ppapi
128
129#endif // PPAPI_PROXY_PPAPI_MESSAGE_UTILS_H_