Avoid finding mean for an empty metrics.
[chromium-blink-merge.git] / gin / converter.h
blobe5c95fc1f9f1c4bff5bb4e413129006bd1ac8104
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef GIN_CONVERTER_H_
6 #define GIN_CONVERTER_H_
8 #include <string>
9 #include <vector>
11 #include "base/strings/string_piece.h"
12 #include "gin/gin_export.h"
13 #include "v8/include/v8.h"
15 namespace gin {
17 template<typename T, typename Enable = void>
18 struct Converter {};
20 template<>
21 struct GIN_EXPORT Converter<bool> {
22 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
23 bool val);
24 static bool FromV8(v8::Isolate* isolate,
25 v8::Handle<v8::Value> val,
26 bool* out);
29 template<>
30 struct GIN_EXPORT Converter<int32_t> {
31 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
32 int32_t val);
33 static bool FromV8(v8::Isolate* isolate,
34 v8::Handle<v8::Value> val,
35 int32_t* out);
38 template<>
39 struct GIN_EXPORT Converter<uint32_t> {
40 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
41 uint32_t val);
42 static bool FromV8(v8::Isolate* isolate,
43 v8::Handle<v8::Value> val,
44 uint32_t* out);
47 template<>
48 struct GIN_EXPORT Converter<int64_t> {
49 // Warning: JavaScript cannot represent 64 integers precisely.
50 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
51 int64_t val);
52 static bool FromV8(v8::Isolate* isolate,
53 v8::Handle<v8::Value> val,
54 int64_t* out);
57 template<>
58 struct GIN_EXPORT Converter<uint64_t> {
59 // Warning: JavaScript cannot represent 64 integers precisely.
60 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
61 uint64_t val);
62 static bool FromV8(v8::Isolate* isolate,
63 v8::Handle<v8::Value> val,
64 uint64_t* out);
67 template<>
68 struct GIN_EXPORT Converter<float> {
69 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
70 float val);
71 static bool FromV8(v8::Isolate* isolate,
72 v8::Handle<v8::Value> val,
73 float* out);
76 template<>
77 struct GIN_EXPORT Converter<double> {
78 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
79 double val);
80 static bool FromV8(v8::Isolate* isolate,
81 v8::Handle<v8::Value> val,
82 double* out);
85 template<>
86 struct GIN_EXPORT Converter<base::StringPiece> {
87 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
88 const base::StringPiece& val);
89 // No conversion out is possible because StringPiece does not contain storage.
92 template<>
93 struct GIN_EXPORT Converter<std::string> {
94 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
95 const std::string& val);
96 static bool FromV8(v8::Isolate* isolate,
97 v8::Handle<v8::Value> val,
98 std::string* out);
101 template<>
102 struct GIN_EXPORT Converter<v8::Handle<v8::Function> > {
103 static bool FromV8(v8::Isolate* isolate,
104 v8::Handle<v8::Value> val,
105 v8::Handle<v8::Function>* out);
108 template<>
109 struct GIN_EXPORT Converter<v8::Handle<v8::Object> > {
110 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
111 v8::Handle<v8::Object> val);
112 static bool FromV8(v8::Isolate* isolate,
113 v8::Handle<v8::Value> val,
114 v8::Handle<v8::Object>* out);
117 template<>
118 struct GIN_EXPORT Converter<v8::Handle<v8::ArrayBuffer> > {
119 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
120 v8::Handle<v8::ArrayBuffer> val);
121 static bool FromV8(v8::Isolate* isolate,
122 v8::Handle<v8::Value> val,
123 v8::Handle<v8::ArrayBuffer>* out);
126 template<>
127 struct GIN_EXPORT Converter<v8::Handle<v8::External> > {
128 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
129 v8::Handle<v8::External> val);
130 static bool FromV8(v8::Isolate* isolate,
131 v8::Handle<v8::Value> val,
132 v8::Handle<v8::External>* out);
135 template<>
136 struct GIN_EXPORT Converter<v8::Handle<v8::Value> > {
137 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
138 v8::Handle<v8::Value> val);
139 static bool FromV8(v8::Isolate* isolate,
140 v8::Handle<v8::Value> val,
141 v8::Handle<v8::Value>* out);
144 template<typename T>
145 struct Converter<std::vector<T> > {
146 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
147 const std::vector<T>& val) {
148 v8::Handle<v8::Array> result(
149 v8::Array::New(isolate, static_cast<int>(val.size())));
150 for (size_t i = 0; i < val.size(); ++i) {
151 result->Set(static_cast<int>(i), Converter<T>::ToV8(isolate, val[i]));
153 return result;
156 static bool FromV8(v8::Isolate* isolate,
157 v8::Handle<v8::Value> val,
158 std::vector<T>* out) {
159 if (!val->IsArray())
160 return false;
162 std::vector<T> result;
163 v8::Handle<v8::Array> array(v8::Handle<v8::Array>::Cast(val));
164 uint32_t length = array->Length();
165 for (uint32_t i = 0; i < length; ++i) {
166 T item;
167 if (!Converter<T>::FromV8(isolate, array->Get(i), &item))
168 return false;
169 result.push_back(item);
172 out->swap(result);
173 return true;
177 // Convenience functions that deduce T.
178 template<typename T>
179 v8::Handle<v8::Value> ConvertToV8(v8::Isolate* isolate, T input) {
180 return Converter<T>::ToV8(isolate, input);
183 GIN_EXPORT inline v8::Handle<v8::String> StringToV8(
184 v8::Isolate* isolate,
185 const base::StringPiece& input) {
186 return ConvertToV8(isolate, input).As<v8::String>();
189 GIN_EXPORT v8::Handle<v8::String> StringToSymbol(v8::Isolate* isolate,
190 const base::StringPiece& val);
192 template<typename T>
193 bool ConvertFromV8(v8::Isolate* isolate, v8::Handle<v8::Value> input,
194 T* result) {
195 return Converter<T>::FromV8(isolate, input, result);
198 GIN_EXPORT std::string V8ToString(v8::Handle<v8::Value> value);
200 } // namespace gin
202 #endif // GIN_CONVERTER_H_