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 // Note that there exist some specializations (particularly in V8Binding.h) for |
| 57 // which T actually represents the final C++ type that a JavaScript value |
| 58 // should be converted to. Introducing new specializations of this kind is |
| 59 // discouraged. |
| 60 template <typename T> |
| 61 struct NativeValueTraits : public NativeValueTraitsBase<T> { |
| 62 // This declaration serves only as a blueprint for specializations: the |
| 63 // return type can change, but all specializations are expected to provide a |
| 64 // nativeValue() method that takes the 3 arguments below. |
| 65 static inline typename NativeValueTraitsBase<T>::ImplType |
| 66 nativeValue(v8::Isolate*, v8::Local<v8::Value>, ExceptionState&); |
22 }; | 67 }; |
23 | 68 |
24 } // namespace blink | 69 } // namespace blink |
25 | 70 |
26 #endif // NativeValueTraits_h | 71 #endif // NativeValueTraits_h |
OLD | NEW |