/* * Copyright (C) 2010-2016 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #pragma once #include "ArgumentCoders.h" #include namespace IPC { class Connection; // Dispatch functions with no reply arguments. template void callMemberFunctionImpl(C* object, MF function, ArgsTuple&& args, std::index_sequence) { (object->*function)(std::get(std::forward(args))...); } template::value>> void callMemberFunction(ArgsTuple&& args, C* object, MF function) { callMemberFunctionImpl(object, function, std::forward(args), ArgsIndicies()); } // Dispatch functions with reply arguments. template void callMemberFunctionImpl(C* object, MF function, ArgsTuple&& args, ReplyArgsTuple& replyArgs, std::index_sequence, std::index_sequence) { (object->*function)(std::get(std::forward(args))..., std::get(replyArgs)...); } template ::value>, typename ReplyArgsTuple, typename ReplyArgsIndicies = std::make_index_sequence::value>> void callMemberFunction(ArgsTuple&& args, ReplyArgsTuple& replyArgs, C* object, MF function) { callMemberFunctionImpl(object, function, std::forward(args), replyArgs, ArgsIndicies(), ReplyArgsIndicies()); } // Dispatch functions with delayed reply arguments. template void callMemberFunctionImpl(C* object, MF function, Ref&& delayedReply, ArgsTuple&& args, std::index_sequence) { (object->*function)(std::get(args)..., WTFMove(delayedReply)); } template::value>> void callMemberFunction(ArgsTuple&& args, Ref&& delayedReply, C* object, MF function) { callMemberFunctionImpl(object, function, WTFMove(delayedReply), std::forward(args), ArgsIndicies()); } // Dispatch functions with connection parameter with no reply arguments. template void callMemberFunctionImpl(C* object, MF function, Connection& connection, ArgsTuple&& args, std::index_sequence) { (object->*function)(connection, std::get(std::forward(args))...); } template::value>> void callMemberFunction(Connection& connection, ArgsTuple&& args, C* object, MF function) { callMemberFunctionImpl(object, function, connection, std::forward(args), ArgsIndicies()); } // Dispatch functions with connection parameter with reply arguments. template void callMemberFunctionImpl(C* object, MF function, Connection& connection, ArgsTuple&& args, ReplyArgsTuple& replyArgs, std::index_sequence, std::index_sequence) { (object->*function)(connection, std::get(std::forward(args))..., std::get(replyArgs)...); } template ::value>, typename ReplyArgsTuple, typename ReplyArgsIndicies = std::make_index_sequence::value>> void callMemberFunction(Connection& connection, ArgsTuple&& args, ReplyArgsTuple& replyArgs, C* object, MF function) { callMemberFunctionImpl(object, function, connection, std::forward(args), replyArgs, ArgsIndicies(), ReplyArgsIndicies()); } // Main dispatch functions template struct CodingType { typedef std::remove_const_t> Type; }; template struct CodingType> { typedef std::tuple::Type...> Type; }; template void handleMessage(Decoder& decoder, C* object, MF function) { typename CodingType::Type arguments; if (!decoder.decode(arguments)) { ASSERT(decoder.isInvalid()); return; } callMemberFunction(WTFMove(arguments), object, function); } template void handleMessage(Decoder& decoder, Encoder& replyEncoder, C* object, MF function) { typename CodingType::Type arguments; if (!decoder.decode(arguments)) { ASSERT(decoder.isInvalid()); return; } typename CodingType::Type replyArguments; callMemberFunction(WTFMove(arguments), replyArguments, object, function); replyEncoder << replyArguments; } template void handleMessage(Connection& connection, Decoder& decoder, Encoder& replyEncoder, C* object, MF function) { typename CodingType::Type arguments; if (!decoder.decode(arguments)) { ASSERT(decoder.isInvalid()); return; } typename CodingType::Type replyArguments; callMemberFunction(connection, WTFMove(arguments), replyArguments, object, function); replyEncoder << replyArguments; } template void handleMessage(Connection& connection, Decoder& decoder, C* object, MF function) { typename CodingType::Type arguments; if (!decoder.decode(arguments)) { ASSERT(decoder.isInvalid()); return; } callMemberFunction(connection, WTFMove(arguments), object, function); } template void handleMessageDelayed(Connection& connection, Decoder& decoder, std::unique_ptr& replyEncoder, C* object, MF function) { typename CodingType::Type arguments; if (!decoder.decode(arguments)) { ASSERT(decoder.isInvalid()); return; } Ref delayedReply = adoptRef(*new typename T::DelayedReply(connection, WTFMove(replyEncoder))); callMemberFunction(WTFMove(arguments), WTFMove(delayedReply), object, function); } } // namespace IPC