Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(118)

Unified Diff: base/prebind.h.pump

Issue 6109007: Unified callback system. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/base
Patch Set: more bits of cleanup Created 9 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/prebind.h.pump
diff --git a/base/prebind.h.pump b/base/prebind.h.pump
new file mode 100644
index 0000000000000000000000000000000000000000..eef9f8b8b117dca3d4c9b77fecb6b3237f091931
--- /dev/null
+++ b/base/prebind.h.pump
@@ -0,0 +1,225 @@
+$$ This is a pump file for generating file templates. Pump is a python
+$$ script that is part of the Google Test suite of utilities. Description
+$$ can be found here:
+$$
+$$ http://code.google.com/p/googletest/wiki/PumpManual
+$$
+
+$var MAX_ARITY = 6
+
+// Copyright (c) 2011 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 BASE_PREBIND_H_
+#define BASE_PREBIND_H_
+#pragma once
+
+#include "base/prebind_helpers.h"
+#include "base/template_util.h"
+#include "base/uber_callback.h"
+
+namespace base {
+namespace internal {
+
+// The method by which a function is invoked is determined by 3 different
+// dimensions:
+//
+// 1) The type of function (normal, method, const-method)
+// 2) The arity of the function
+// 3) The number of bound parameters.
+//
+// The FunctionTraitsN classes unwrap the function signature type to
+// specialize based on the first two dimensions. The N in FunctionTraitsN
+// specifies the 3rd dimension. We could have specified the unbound parameters
+// via template parameters, but this method looked cleaner.
+//
+// The FunctionTraitsN contains a static DoInvoke() function that is the key to
+// implementing type erasure in the Callback() classes. DoInvoke() is a static
+// function with a fixed signature that is indepenent of StorageType; its first
+// argument is a pointer to the non-templated common baseclass of StorageType.
+// This lets us store pointer to DoInvoke() in a function pointer that has
+// knowledge of the specific StorageType, and thus no knowledge of the bound
+// function and bound parameter types.
+//
+// As long as we ensure that DoInvoke() is only used with pointers there were
+// upcasted from the correct StorageType, we can be sure that execution is
+// safe.
+
+$range PREBOUND 0..MAX_ARITY
+$for PREBOUND [[
+
+template <typename StorageType, typename Sig>
+struct FunctionTraits$(PREBOUND);
+
+$range ARITY 0..MAX_ARITY
+$for ARITY [[
+
+$var UNBOUND = ARITY - PREBOUND
+$if UNBOUND >= 0 [[
+
+$$ Variables for function traits generation.
+$range ARG 1..ARITY
+$range BOUND_ARG 1..PREBOUND
+$range UNBOUND_ARG (ARITY - UNBOUND + 1)..ARITY
+
+$$ Variables for method traits generation. We are always short one arity since
+$$ the first bound parameter is the object.
+$var M_ARITY = ARITY - 1
+$range M_ARG 1..M_ARITY
+$range M_BOUND_ARG 2..PREBOUND
+$range M_UNBOUND_ARG (M_ARITY - UNBOUND + 1)..M_ARITY
+
+// Function: Arity $(ARITY) -> $(UNBOUND).
+template <typename StorageType, typename R[[]]
+$if ARITY > 0 [[,]][[]]
+$for ARG , [[typename X$(ARG)]]>
+struct FunctionTraits$(PREBOUND)<StorageType, R(*)($for ARG , [[X$(ARG)]])> {
+$if ARITY > 0 [[
+
+ COMPILE_ASSERT(
+ !($for ARG || [[ is_non_const_reference<X$(ARG)>::value ]]),
+ do_not_prebind_functions_with_nonconst_ref);
+
+]]
+
+ typedef base::false_type ShouldRef;
+
+ static R DoInvoke(internal::InvokerStorageBase* base[[]]
+$if UNBOUND != 0 [[, ]][[]]
+$for UNBOUND_ARG , [[const X$(UNBOUND_ARG)& x$(UNBOUND_ARG)]]) {
+ StorageType* invoker = static_cast<StorageType*>(base);
+ return invoker->f_($for BOUND_ARG , [[Unwrap(invoker->p$(BOUND_ARG)_)]][[]]
+$$ Add comma if there are both boudn and unbound args.
+$if UNBOUND > 0 [[$if PREBOUND > 0 [[, ]]]][[]]
+$for UNBOUND_ARG , [[x$(UNBOUND_ARG)]]);
+ }
+};
+
+$if PREBOUND > 0 [[
+
+// Method: Arity $(M_ARITY) -> $(UNBOUND).
+template <typename StorageType, typename R, typename T[[]]
+$if M_ARITY > 0[[, ]] $for M_ARG , [[typename X$(M_ARG)]]>
+struct FunctionTraits$(PREBOUND)<StorageType, R(T::*)($for M_ARG , [[X$(M_ARG)]])> {
+ typedef base::true_type ShouldRef;
+ static R DoInvoke(internal::InvokerStorageBase* base[[]]
+$if UNBOUND > 0 [[, ]][[]]
+$for M_UNBOUND_ARG , [[const X$(M_UNBOUND_ARG)& x$(M_UNBOUND_ARG)]]) {
+ StorageType* invoker = static_cast<StorageType*>(base);
+ return (Unwrap(invoker->p1_)->*invoker->f_)([[]]
+$for M_BOUND_ARG , [[Unwrap(invoker->p$(M_BOUND_ARG)_)]][[]]
+$if UNBOUND > 0 [[$if PREBOUND > 1 [[, ]]]][[]]
+$for M_UNBOUND_ARG , [[x$(M_UNBOUND_ARG)]]);
+ }
+};
+
+// Const Method: Arity $(M_ARITY) -> $(UNBOUND).
+template <typename StorageType, typename R, typename T[[]]
+$if M_ARITY > 0[[, ]] $for M_ARG , [[typename X$(M_ARG)]]>
+struct FunctionTraits$(PREBOUND)<StorageType, R(T::*)($for M_ARG , [[X$(M_ARG)]]) const> {
+ typedef base::true_type ShouldRef;
+ static R DoInvoke(internal::InvokerStorageBase* base[[]]
+$if UNBOUND > 0 [[, ]]
+[[]] $for M_UNBOUND_ARG , [[const X$(M_UNBOUND_ARG)& x$(M_UNBOUND_ARG)]]) {
+ StorageType* invoker = static_cast<StorageType*>(base);
+ return (Unwrap(invoker->p1_)->*invoker->f_)([[]]
+$for M_BOUND_ARG , [[Unwrap(invoker->p$(M_BOUND_ARG)_)]][[]]
+$if UNBOUND > 0 [[$if PREBOUND > 1 [[, ]]]][[]]
+$for M_UNBOUND_ARG , [[x$(M_UNBOUND_ARG)]]);
+ }
+};
+
+]] $$ if PREBOUND
+
+]] $$ if UNBOUND
+]] $$ for ARITY
+]] $$ for PREBOUND
+
+
+// These are the actual storage classes for the invokers.
+
+$for PREBOUND [[
+$range BOUND_ARG 1..PREBOUND
+
+template <typename Sig[[]]
+$if PREBOUND > 0 [[, ]]
+$for BOUND_ARG , [[typename P$(BOUND_ARG)]]>
+class InvokerStorage$(PREBOUND) : public internal::InvokerStorageBase {
+ public:
+ typedef InvokerStorage$(PREBOUND) StorageType;
+ typedef FunctionTraits$(PREBOUND)<StorageType, Sig> FunctionTraits;
+ typedef typename FunctionTraits::ShouldRef ShouldRef;
+
+$if PREBOUND > 0 [[
+
+ COMPILE_ASSERT(!ShouldRef::value || !is_array<P1>::value,
+ first_bound_argument_to_method_cannot_be_array);
+
+]]
+
+ InvokerStorage$(PREBOUND)(Sig f
+$if PREBOUND > 0 [[, ]]
+$for BOUND_ARG , [[const P$(BOUND_ARG)& p$(BOUND_ARG)]])
+ : f_(f)[[]]
+$if PREBOUND == 0 [[
+ {
+
+]] $else [[
+, $for BOUND_ARG , [[p$(BOUND_ARG)_(static_cast<typename PrebindType<P$(BOUND_ARG)>::StorageType>(p$(BOUND_ARG)))]] {
+ MaybeRefcount<ShouldRef, P1>::AddRef(p1_);
+
+]]
+ }
+
+ virtual ~InvokerStorage$(PREBOUND)() {
+$if PREBOUND > 0 [[
+
+ MaybeRefcount<ShouldRef, P1>::Release(p1_);
+
+]]
+ }
+
+ Sig f_;
+
+$for BOUND_ARG [[
+ typename PrebindType<P$(BOUND_ARG)>::StorageType p$(BOUND_ARG)_;
+
+]]
+};
+
+]] $$ for PREBOUND
+
+} // namespace internal
+
+$for PREBOUND [[
+$range BOUND_ARG 1..PREBOUND
+
+$if PREBOUND == 0 [[
+
+template <typename Sig>
+internal::InvokerStorageHolder<internal::InvokerStorage0<Sig> >
+Prebind(Sig f) {
+ return internal::MakeInvokerStorageHolder(
+ new internal::InvokerStorage0<Sig>(f));
+}
+
+]] $else [[
+
+template <typename Sig, $for BOUND_ARG , [[typename P$(BOUND_ARG)]]>
+internal::InvokerStorageHolder<internal::InvokerStorage$(PREBOUND)<Sig,
+$for BOUND_ARG , [[P$(BOUND_ARG)]]> >
+Prebind(Sig f, $for BOUND_ARG , [[const P$(BOUND_ARG)& p$(BOUND_ARG)]]) {
+ return internal::MakeInvokerStorageHolder(
+ new internal::InvokerStorage$(PREBOUND)<Sig, [[]]
+$for BOUND_ARG , [[P$(BOUND_ARG)]]>(
+ f, $for BOUND_ARG , [[p$(BOUND_ARG)]]));
+}
+
+]]
+
+]] $$ for PREBOUND
+
+} // namespace base
+
+#endif // BASE_PREBIND_H_

Powered by Google App Engine
This is Rietveld 408576698