chromium/ipc/ipc_message_templates.h

// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef IPC_IPC_MESSAGE_TEMPLATES_H_
#define IPC_IPC_MESSAGE_TEMPLATES_H_

#include <stdint.h>

#include <tuple>
#include <type_traits>
#include <utility>

#include "base/check.h"
#include "base/notreached.h"
#include "base/trace_event/trace_event.h"
#include "base/tuple.h"
#include "build/build_config.h"
#include "ipc/ipc_message.h"
#include "ipc/ipc_message_utils.h"
#include "ipc/ipc_sync_message.h"

namespace IPC {

template <typename Tuple, size_t... Ns>
auto TupleForwardImpl(Tuple&& tuple, std::index_sequence<Ns...>) -> decltype(
    std::forward_as_tuple(std::get<Ns>(std::forward<Tuple>(tuple))...)) {}

// Transforms std::tuple contents to the forwarding form.
// Example:
//   std::tuple<int, int&, const int&, int&&>&&
//     -> std::tuple<int&&, int&, const int&, int&&>.
//   const std::tuple<int, const int&, int&&>&
//     -> std::tuple<const int&, int&, const int&, int&>.
//
// TupleForward(std::make_tuple(a, b, c)) is equivalent to
// std::forward_as_tuple(a, b, c).
template <typename Tuple>
auto TupleForward(Tuple&& tuple) -> decltype(TupleForwardImpl(
    std::forward<Tuple>(tuple),
    std::make_index_sequence<std::tuple_size<std::decay_t<Tuple>>::value>())) {}

// This function is for all the async IPCs that don't pass an extra parameter
// using IPC_BEGIN_MESSAGE_MAP_WITH_PARAM.
template <typename ObjT, typename Method, typename P, typename Tuple>
void DispatchToMethod(ObjT* obj, Method method, P*, Tuple&& tuple) {}

template <typename ObjT,
          typename Method,
          typename P,
          typename Tuple,
          size_t... Ns>
void DispatchToMethodImpl(ObjT* obj,
                          Method method,
                          P* parameter,
                          Tuple&& tuple,
                          std::index_sequence<Ns...>) {}

// The following function is for async IPCs which have a dispatcher with an
// extra parameter specified using IPC_BEGIN_MESSAGE_MAP_WITH_PARAM.
template <typename ObjT, typename P, typename... Args, typename Tuple>
std::enable_if_t<sizeof...(Args) == std::tuple_size<std::decay_t<Tuple>>::value>
DispatchToMethod(ObjT* obj,
                 void (ObjT::*method)(P*, Args...),
                 P* parameter,
                 Tuple&& tuple) {}

enum class MessageKind {};

// Routing is a helper struct so MessageT's private common constructor has a
// different type signature than the public "int32_t routing_id" one.
struct Routing {};

// We want to restrict MessageT's constructors so that a routing_id is always
// provided for ROUTED messages and never provided for CONTROL messages, so
// use the SFINAE technique from N4387's "Implementation Hint" section.
#define IPC_MESSAGET_SFINAE(x)

// MessageT is the common template used for all user-defined message types.
// It's intended to be used via the macros defined in ipc_message_macros.h.
template <typename Meta,
          typename InTuple = typename Meta::InTuple,
          typename OutTuple = typename Meta::OutTuple>
class MessageT;

// Asynchronous message partial specialization.
MessageT<Meta, std::tuple<Ins...>, void>;

// Synchronous message partial specialization.
MessageT<Meta, std::tuple<Ins...>, std::tuple<Outs...>>;

}  // namespace IPC

#if defined(IPC_MESSAGE_IMPL)
#include "ipc/ipc_message_templates_impl.h"
#endif

#endif  // IPC_IPC_MESSAGE_TEMPLATES_H_