summaryrefslogtreecommitdiff
path: root/chromium/ppapi/proxy/ppapi_message_utils.h
blob: 3fdb51913da34d77087fb02f7fdf772f62a199af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef PPAPI_PROXY_PPAPI_MESSAGE_UTILS_H_
#define PPAPI_PROXY_PPAPI_MESSAGE_UTILS_H_

#include "base/pickle.h"
#include "base/tuple.h"
#include "ipc/ipc_message.h"
#include "ipc/ipc_message_utils.h"

namespace ppapi {

namespace internal {

// TupleTypeMatch* check whether a tuple type contains elements of the specified
// types. They are used to make sure the output parameters of UnpackMessage()
// match the corresponding message type.
template <class TupleType, class A>
struct TupleTypeMatch1 {
  static const bool kValue = false;
};
template <class A>
struct TupleTypeMatch1<std::tuple<A>, A> {
  static const bool kValue = true;
};

template <class TupleType, class A, class B>
struct TupleTypeMatch2 {
  static const bool kValue = false;
};
template <class A, class B>
struct TupleTypeMatch2<std::tuple<A, B>, A, B> {
  static const bool kValue = true;
};

template <class TupleType, class A, class B, class C>
struct TupleTypeMatch3 {
  static const bool kValue = false;
};
template <class A, class B, class C>
struct TupleTypeMatch3<std::tuple<A, B, C>, A, B, C> {
  static const bool kValue = true;
};

template <class TupleType, class A, class B, class C, class D>
struct TupleTypeMatch4 {
  static const bool kValue = false;
};
template <class A, class B, class C, class D>
struct TupleTypeMatch4<std::tuple<A, B, C, D>, A, B, C, D> {
  static const bool kValue = true;
};

template <class TupleType, class A, class B, class C, class D, class E>
struct TupleTypeMatch5 {
  static const bool kValue = false;
};
template <class A, class B, class C, class D, class E>
struct TupleTypeMatch5<std::tuple<A, B, C, D, E>, A, B, C, D, E> {
  static const bool kValue = true;
};

}  // namespace internal

template <class MsgClass, class A>
bool UnpackMessage(const IPC::Message& msg, A* a) {
  static_assert(
      (internal::TupleTypeMatch1<typename MsgClass::Param, A>::kValue),
      "tuple types should match");

  base::PickleIterator iter(msg);
  return IPC::ReadParam(&msg, &iter, a);
}

template <class MsgClass, class A, class B>
bool UnpackMessage(const IPC::Message& msg, A* a, B* b) {
  static_assert(
      (internal::TupleTypeMatch2<typename MsgClass::Param, A, B>::kValue),
      "tuple types should match");

  base::PickleIterator iter(msg);
  return IPC::ReadParam(&msg, &iter, a) && IPC::ReadParam(&msg, &iter, b);
}

template <class MsgClass, class A, class B, class C>
bool UnpackMessage(const IPC::Message& msg, A* a, B* b, C* c) {
  static_assert(
      (internal::TupleTypeMatch3<typename MsgClass::Param, A, B, C>::kValue),
      "tuple types should match");

  base::PickleIterator iter(msg);
  return IPC::ReadParam(&msg, &iter, a) &&
         IPC::ReadParam(&msg, &iter, b) &&
         IPC::ReadParam(&msg, &iter, c);
}

template <class MsgClass, class A, class B, class C, class D>
bool UnpackMessage(const IPC::Message& msg, A* a, B* b, C* c, D* d) {
  static_assert(
      (internal::TupleTypeMatch4<typename MsgClass::Param, A, B, C, D>::kValue),
      "tuple types should match");

  base::PickleIterator iter(msg);
  return IPC::ReadParam(&msg, &iter, a) &&
         IPC::ReadParam(&msg, &iter, b) &&
         IPC::ReadParam(&msg, &iter, c) &&
         IPC::ReadParam(&msg, &iter, d);
}

template <class MsgClass, class A, class B, class C, class D, class E>
bool UnpackMessage(const IPC::Message& msg, A* a, B* b, C* c, D* d, E* e) {
  static_assert(
      (internal::TupleTypeMatch5<
           typename MsgClass::Param, A, B, C, D, E>::kValue),
      "tuple types should match");

  base::PickleIterator iter(msg);
  return IPC::ReadParam(&msg, &iter, a) &&
         IPC::ReadParam(&msg, &iter, b) &&
         IPC::ReadParam(&msg, &iter, c) &&
         IPC::ReadParam(&msg, &iter, d) &&
         IPC::ReadParam(&msg, &iter, e);
}

}  // namespace ppapi

#endif  // PPAPI_PROXY_PPAPI_MESSAGE_UTILS_H_