From 4cada31f808994b297439dff429638debc4a4ee9 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Tue, 15 Apr 2014 11:15:19 +0800 Subject: [PATCH] Do not rely on latest base/template_util. --- native_mate/object_template_builder.h | 6 +- native_mate/template_util.h | 150 ++++++++++++++++++++++++++ native_mate/wrappable.h | 6 +- native_mate_files.gypi | 1 + 4 files changed, 157 insertions(+), 6 deletions(-) create mode 100644 native_mate/template_util.h diff --git a/native_mate/object_template_builder.h b/native_mate/object_template_builder.h index a0fad1569b51..fb4be8324204 100644 --- a/native_mate/object_template_builder.h +++ b/native_mate/object_template_builder.h @@ -8,9 +8,9 @@ #include "base/bind.h" #include "base/callback.h" #include "base/strings/string_piece.h" -#include "base/template_util.h" #include "native_mate/converter.h" #include "native_mate/function_template.h" +#include "native_mate/template_util.h" #include "v8/include/v8.h" namespace mate { @@ -42,8 +42,8 @@ struct CallbackTraits > { // come from the the JavaScript "this" object the function was called on, not // from the first normal parameter. template -struct CallbackTraits::value>::type> { +struct CallbackTraits::value>::type> { static v8::Handle CreateTemplate(v8::Isolate* isolate, T callback) { return CreateFunctionTemplate(isolate, base::Bind(callback), diff --git a/native_mate/template_util.h b/native_mate/template_util.h new file mode 100644 index 000000000000..e1632a2d566f --- /dev/null +++ b/native_mate/template_util.h @@ -0,0 +1,150 @@ +// 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 NATIVE_MATE_TEMPLATE_UTIL_H_ +#define NATIVE_MATE_TEMPLATE_UTIL_H_ + +#include // For size_t. + +#include "build/build_config.h" + +namespace mate { + +// template definitions from tr1 + +template +struct integral_constant { + static const T value = v; + typedef T value_type; + typedef integral_constant type; +}; + +template const T integral_constant::value; + +typedef integral_constant true_type; +typedef integral_constant false_type; + +template struct is_pointer : false_type {}; +template struct is_pointer : true_type {}; + +// Member function pointer detection up to four params. Add more as needed +// below. This is built-in to C++ 11, and we can remove this when we switch. +template +struct is_member_function_pointer : false_type {}; + +template +struct is_member_function_pointer : true_type {}; +template +struct is_member_function_pointer : true_type {}; + +template +struct is_member_function_pointer : true_type {}; +template +struct is_member_function_pointer : true_type {}; + +template +struct is_member_function_pointer : true_type {}; +template +struct is_member_function_pointer : true_type {}; + +template +struct is_member_function_pointer : true_type {}; +template +struct is_member_function_pointer : true_type {}; + +template +struct is_member_function_pointer : true_type {}; +template +struct is_member_function_pointer : true_type {}; + + +template struct is_same : public false_type {}; +template struct is_same : true_type {}; + +template struct is_array : public false_type {}; +template struct is_array : public true_type {}; +template struct is_array : public true_type {}; + +template struct is_non_const_reference : false_type {}; +template struct is_non_const_reference : true_type {}; +template struct is_non_const_reference : false_type {}; + +template struct is_const : false_type {}; +template struct is_const : true_type {}; + +template struct is_void : false_type {}; +template <> struct is_void : true_type {}; + +namespace internal { + +// Types YesType and NoType are guaranteed such that sizeof(YesType) < +// sizeof(NoType). +typedef char YesType; + +struct NoType { + YesType dummy[2]; +}; + +// This class is an implementation detail for is_convertible, and you +// don't need to know how it works to use is_convertible. For those +// who care: we declare two different functions, one whose argument is +// of type To and one with a variadic argument list. We give them +// return types of different size, so we can use sizeof to trick the +// compiler into telling us which function it would have chosen if we +// had called it with an argument of type From. See Alexandrescu's +// _Modern C++ Design_ for more details on this sort of trick. + +struct ConvertHelper { + template + static YesType Test(To); + + template + static NoType Test(...); + + template + static From& Create(); +}; + +// Used to determine if a type is a struct/union/class. Inspired by Boost's +// is_class type_trait implementation. +struct IsClassHelper { + template + static YesType Test(void(C::*)(void)); + + template + static NoType Test(...); +}; + +} // namespace internal + +// Inherits from true_type if From is convertible to To, false_type otherwise. +// +// Note that if the type is convertible, this will be a true_type REGARDLESS +// of whether or not the conversion would emit a warning. +template +struct is_convertible + : integral_constant( + internal::ConvertHelper::Create())) == + sizeof(internal::YesType)> { +}; + +template +struct is_class + : integral_constant(0)) == + sizeof(internal::YesType)> { +}; + +template +struct enable_if {}; + +template +struct enable_if { typedef T type; }; + +} // namespace mate + +#endif // NATIVE_MATE_TEMPLATE_UTIL_H_ diff --git a/native_mate/wrappable.h b/native_mate/wrappable.h index 949a9ebebd9b..e4037c0bb5e7 100644 --- a/native_mate/wrappable.h +++ b/native_mate/wrappable.h @@ -5,9 +5,9 @@ #ifndef NATIVE_MATE_WRAPPABLE_H_ #define NATIVE_MATE_WRAPPABLE_H_ -#include "base/template_util.h" #include "native_mate/compat.h" #include "native_mate/converter.h" +#include "native_mate/template_util.h" namespace mate { @@ -67,8 +67,8 @@ class Wrappable { // This converter handles any subclass of Wrappable. template -struct Converter::value>::type> { +struct Converter::value>::type> { static v8::Handle ToV8(v8::Isolate* isolate, T* val) { return val->GetWrapper(isolate); } diff --git a/native_mate_files.gypi b/native_mate_files.gypi index 1496ab4f91e3..8d3952bdd8d2 100644 --- a/native_mate_files.gypi +++ b/native_mate_files.gypi @@ -14,6 +14,7 @@ 'native_mate/object_template_builder.cc', 'native_mate/object_template_builder.h', 'native_mate/scoped_persistent.h', + 'native_mate/template_util.h', 'native_mate/try_catch.cc', 'native_mate/try_catch.h', 'native_mate/wrappable.cc',