]> khamidou — GitHub mirrors - electron.git/commitdiff
Support converting v8 value to arbitrary vector or map.
authorCheng Zhao <zcbenz@gmail.com>
Tue, 7 Jan 2014 12:23:46 +0000 (20:23 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Tue, 7 Jan 2014 12:29:17 +0000 (20:29 +0800)
common/v8/native_type_conversions.h

index 05bc883a901a6adcbf44b0e733d66811032fd3c9..2beed8befc3d7e04b93f4589b1ef5376470b762a 100644 (file)
@@ -17,7 +17,6 @@
 #include "common/swap_or_assign.h"
 #include "common/v8/scoped_persistent.h"
 #include "common/v8/v8_value_converter.h"
-#include "content/public/renderer/v8_value_converter.h"
 #include "ui/gfx/point.h"
 #include "ui/gfx/rect.h"
 #include "ui/gfx/size.h"
@@ -73,8 +72,9 @@ struct FromV8Value {
         converter->FromV8Value(value_, v8::Context::GetCurrent()));
   }
 
-  operator std::vector<std::string>() {
-    std::vector<std::string> array;
+  template<class T>
+  operator std::vector<T>() {
+    std::vector<T> array;
     v8::Handle<v8::Array> v8_array = v8::Handle<v8::Array>::Cast(value_);
     for (uint32_t i = 0; i < v8_array->Length(); ++i)
       array.push_back(FromV8Value(v8_array->Get(i)));
@@ -82,14 +82,15 @@ struct FromV8Value {
     return array;
   }
 
-  operator std::map<std::string, std::string>() {
-    std::map<std::string, std::string> dict;
+  template<class K, class V>
+  operator std::map<K, V>() {
+    std::map<K, V> dict;
     v8::Handle<v8::Object> v8_dict = value_->ToObject();
     v8::Handle<v8::Array> v8_keys = v8_dict->GetOwnPropertyNames();
     for (uint32_t i = 0; i < v8_keys->Length(); ++i) {
       v8::Handle<v8::Value> v8_key = v8_keys->Get(i);
-      std::string key = FromV8Value(v8_key);
-      dict[key] = std::string(FromV8Value(v8_dict->Get(v8_key)));
+      K key = FromV8Value(v8_key);
+      dict[key] = V(FromV8Value(v8_dict->Get(v8_key)));
     }
 
     return dict;
@@ -156,11 +157,11 @@ inline v8::Handle<v8::Value> ToV8Value(void* whatever) {
   return v8::Undefined();
 }
 
-inline
-v8::Handle<v8::Value> ToV8Value(const std::vector<base::FilePath>& paths) {
-  v8::Handle<v8::Array> result = v8::Array::New(paths.size());
-  for (size_t i = 0; i < paths.size(); ++i)
-    result->Set(i, ToV8Value(paths[i]));
+template<class T> inline
+v8::Handle<v8::Value> ToV8Value(const std::vector<T>& arr) {
+  v8::Handle<v8::Array> result = v8::Array::New(arr.size());
+  for (size_t i = 0; i < arr.size(); ++i)
+    result->Set(i, ToV8Value(arr[i]));
   return result;
 }