// Copyright (c) 2011 Google 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:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * 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.
// * Neither the name of Google Inc. nor the name Chromium Embedded
// Framework nor the names of its contributors may be used to endorse
// or promote products derived from this software without specific prior
// written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 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 THE COPYRIGHT
// OWNER OR 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.
// Do not include this header file directly. Use base/cef_bind.h instead.
#ifndef CEF_INCLUDE_BASE_INTERNAL_CEF_BIND_INTERNAL_H_
#define CEF_INCLUDE_BASE_INTERNAL_CEF_BIND_INTERNAL_H_
#include "include/base/cef_bind_helpers.h"
#include "include/base/cef_build.h"
#include "include/base/cef_template_util.h"
#include "include/base/cef_weak_ptr.h"
#include "include/base/internal/cef_callback_internal.h"
#include "include/base/internal/cef_raw_scoped_refptr_mismatch_checker.h"
#if defined(OS_WIN)
#include "include/base/internal/cef_bind_internal_win.h"
#endif
namespace base {
namespace cef_internal {
// See base/callback.h for user documentation.
//
//
// CONCEPTS:
// Runnable -- A type (really a type class) that has a single Run() method
// and a RunType typedef that corresponds to the type of Run().
// A Runnable can declare that it should treated like a method
// call by including a typedef named IsMethod. The value of
// this typedef is NOT inspected, only the existence. When a
// Runnable declares itself a method, Bind() will enforce special
// refcounting + WeakPtr handling semantics for the first
// parameter which is expected to be an object.
// Functor -- A copyable type representing something that should be called.
// All function pointers, Callback<>, and Runnables are functors
// even if the invocation syntax differs.
// RunType -- A function type (as opposed to function _pointer_ type) for
// a Run() function. Usually just a convenience typedef.
// (Bound)ArgsType -- A function type that is being (ab)used to store the
// types of set of arguments. The "return" type is always
// void here. We use this hack so that we do not need
// a new type name for each arity of type. (eg.,
// BindState1, BindState2). This makes forward
// declarations and friending much much easier.
//
// Types:
// RunnableAdapter<> -- Wraps the various "function" pointer types into an
// object that adheres to the Runnable interface.
// There are |3*ARITY| RunnableAdapter types.
// FunctionTraits<> -- Type traits that unwrap a function signature into a
// a set of easier to use typedefs. Used mainly for
// compile time asserts.
// There are |ARITY| FunctionTraits types.
// ForceVoidReturn<> -- Helper class for translating function signatures to
// equivalent forms with a "void" return type.
// There are |ARITY| ForceVoidReturn types.
// FunctorTraits<> -- Type traits used determine the correct RunType and
// RunnableType for a Functor. This is where function
// signature adapters are applied.
// There are |ARITY| ForceVoidReturn types.
// MakeRunnable<> -- Takes a Functor and returns an object in the Runnable
// type class that represents the underlying Functor.
// There are |O(1)| MakeRunnable types.
// InvokeHelper<> -- Take a Runnable + arguments and actully invokes it.
// Handle the differing syntaxes needed for WeakPtr<> support,
// and for ignoring return values. This is separate from
// Invoker to avoid creating multiple version of Invoker<>
// which grows at O(n^2) with the arity.
// There are |k*ARITY| InvokeHelper types.
// Invoker<> -- Unwraps the curried parameters and executes the Runnable.
// There are |(ARITY^2 + ARITY)/2| Invoketypes.
// BindState<> -- Stores the curried parameters, and is the main entry point
// into the Bind() system, doing most of the type resolution.
// There are ARITY BindState types.
// RunnableAdapter<>
//
// The RunnableAdapter<> templates provide a uniform interface for invoking
// a function pointer, method pointer, or const method pointer. The adapter
// exposes a Run() method with an appropriate signature. Using this wrapper
// allows for writing code that supports all three pointer types without
// undue repetition. Without it, a lot of code would need to be repeated 3
// times.
//
// For method pointers and const method pointers the first argument to Run()
// is considered to be the received of the method. This is similar to STL's
// mem_fun().
//
// This class also exposes a RunType typedef that is the function type of the
// Run() function.
//
// If and only if the wrapper contains a method or const method pointer, an
// IsMethod typedef is exposed. The existence of this typedef (NOT the value)
// marks that the wrapper should be considered a method wrapper.
template <typename Functor>
class RunnableAdapter;
// Function: Arity 0.
template <typename R>
class RunnableAdapter<R(*)()> {
public:
typedef R (RunType)();
explicit RunnableAdapter(R(*function)())
: function_(function) {
}
R Run() {
return function_();
}
private:
R (*function_)();
};
// Method: Arity 0.
template <typename R, typename T>
class RunnableAdapter<R(T::*)()> {
public:
typedef R (RunType)(T*);
typedef true_type IsMethod;
explicit RunnableAdapter(R(T::*method)())
: method_(method) {
}
R Run(T* object) {
return (object->*method_)();
}
private:
R (T::*method_)();
};
// Const Method: Arity 0.
template <typename R, typename T>
class RunnableAdapter<R(T::*)() const> {
public:
typedef R (RunType)(const T*);
typedef true_type IsMethod;
explicit RunnableAdapter(R(T::*method)() const)
: method_(method) {
}
R Run(const T* object) {
return (object->*method_)();
}
private:
R (T::*method_)() const;
};
// Function: Arity 1.
template <typename R, typename A1>
class RunnableAdapter<R(*)(A1)> {
public:
typedef R (RunType)(A1);
explicit RunnableAdapter(R(*function)(A1))
: function_(function) {
}
R Run(typename CallbackParamTraits<A1>::ForwardType a1) {
return function_(CallbackForward(a1));
}
private:
R (*function_)(A1);
};
// Method: Arity 1.
template <typename R, typename T, typename A1>
class RunnableAdapter<R(T::*)(A1)> {
public:
typedef R (RunType)(T*, A1);
typedef true_t
- 1
- 2
- 3
- 4
- 5
- 6
前往页