Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NativeValueTraits_h | 5 #ifndef NativeValueTraits_h |
| 6 #define NativeValueTraits_h | 6 #define NativeValueTraits_h |
| 7 | 7 |
| 8 #include <type_traits> | |
| 9 #include "bindings/core/v8/IDLTypesBase.h" | |
| 8 #include "v8/include/v8.h" | 10 #include "v8/include/v8.h" |
| 9 #include "wtf/Allocator.h" | 11 #include "wtf/Allocator.h" |
| 10 | 12 |
| 11 namespace blink { | 13 namespace blink { |
| 12 | 14 |
| 13 class ExceptionState; | 15 class ExceptionState; |
| 14 | 16 |
| 15 template <typename T, typename... Arguments> | 17 // NativeValueTraitsBase is supposed to be inherited by NativeValueTraits |
| 16 struct NativeValueTraits { | 18 // classes. They serve as a way to hold the ImplType typedef without requiring |
| 17 STATIC_ONLY(NativeValueTraits); | 19 // all NativeValueTraits specializations to declare it. |
| 18 static T nativeValue(v8::Isolate*, | 20 // |
| 19 v8::Local<v8::Value>, | 21 // The primary template below is used by NativeValueTraits specializations with |
| 20 ExceptionState&, | 22 // types that do not inherit from IDLBase, in which case it is assumed the type |
| 21 Arguments... args); | 23 // of the specialization is also |ImplType|. The NativeValueTraitsBase |
| 24 // specialization is used for IDLBase-based types, which are supposed to have | |
| 25 // their own |ImplType| typedefs. | |
| 26 template <typename T, typename SFINAEHelper = void> | |
| 27 struct NativeValueTraitsBase { | |
| 28 using ImplType = T; | |
| 29 STATIC_ONLY(NativeValueTraitsBase); | |
| 30 }; | |
| 31 | |
| 32 template <typename T> | |
| 33 struct NativeValueTraitsBase< | |
| 34 T, | |
| 35 typename std::enable_if<std::is_base_of<IDLBase, T>::value>::type> { | |
| 36 using ImplType = typename T::ImplType; | |
| 37 STATIC_ONLY(NativeValueTraitsBase); | |
| 38 }; | |
| 39 | |
| 40 // Primary template for NativeValueTraits. It is not supposed to be used | |
| 41 // directly: there needs to be a specialization for each type which represents | |
| 42 // a JavaScript type that will be converted to a C++ representation. | |
| 43 // Its main goal is to provide a standard interface for converting JS types | |
| 44 // into C++ ones. | |
| 45 // | |
| 46 // Example: | |
| 47 // template <> | |
| 48 // struct NativeValueTraits<IDLLong> : public NativeValueTraitsBase<IDLLong> { | |
| 49 // static inline int32_t nativeValue(v8::Isolate* isolate, | |
| 50 // v8::Local<v8::Value> value, | |
| 51 // ExceptionState& exceptionState) { | |
| 52 // return toInt32(isolate, value, exceptionState, NormalConversion); | |
| 53 // } | |
| 54 // } | |
| 55 // | |
| 56 // The SFINAEHelper template parameter can be used by template specializations | |
| 57 // on T that need to perform type introspection on it. | |
| 58 // | |
| 59 // Example: | |
| 60 // struct S { | |
| 61 // static const int i = 42; | |
| 62 // }; | |
| 63 // /* This specialization is chosen when T has an integral member called i */ | |
| 64 // template <typename T> | |
| 65 // struct NativeValueTraits<T, | |
| 66 // typename std::enable_if<std::is_integral<decltype(T::i)>::value>::type> | |
| 67 // : public NativeValueTraitsBase<T> { | |
| 68 // static inline bool nativeValue(v8::Isolate* isolate, | |
|
haraken
2017/03/06 12:56:33
Why are we returning bool while the type is an int
| |
| 69 // v8::Local<v8::Value> value, | |
| 70 // ExceptionState& exceptionState) { | |
| 71 // return true; | |
| 72 // } | |
| 73 // }; | |
| 74 // | |
| 75 // Note that there exist some specializations (particularly in V8Binding.h) for | |
| 76 // which T actually represents the final C++ type that a JavaScript value | |
| 77 // should be converted to. Introducing new specializations of this kind is | |
| 78 // discouraged. | |
| 79 template <typename T, typename SFINAEHelper = void> | |
| 80 struct NativeValueTraits : public NativeValueTraitsBase<T> { | |
| 81 // This declaration serves only as a blueprint for specializations: the | |
| 82 // return type can change, but all specializations are expected to provide a | |
| 83 // nativeValue() method that takes the 3 arguments below. | |
|
haraken
2017/03/06 12:56:33
Just to confirm: If someone forgets to specialize
Raphael Kubo da Costa (rakuco)
2017/03/06 14:29:17
Correct. If there's no specialization for a given
| |
| 84 static inline typename NativeValueTraitsBase<T>::ImplType | |
|
Yuki
2017/03/06 11:39:00
nit: inline keyword here doesn't make much sense.
| |
| 85 nativeValue(v8::Isolate*, v8::Local<v8::Value>, ExceptionState&); | |
| 22 }; | 86 }; |
| 23 | 87 |
| 24 } // namespace blink | 88 } // namespace blink |
| 25 | 89 |
| 26 #endif // NativeValueTraits_h | 90 #endif // NativeValueTraits_h |
| OLD | NEW |