diff --git a/native_mate/converter.h b/native_mate/converter.h index e2fe3e885cf..2093ce0ea45 100644 --- a/native_mate/converter.h +++ b/native_mate/converter.h @@ -7,6 +7,7 @@ #include #include +#include #include "base/strings/string_piece.h" #include "native_mate/compat.h" @@ -197,6 +198,40 @@ struct Converter > { } }; +template +struct Converter > { + static v8::Handle ToV8(v8::Isolate* isolate, + const std::set& val) { + v8::Handle result( + MATE_ARRAY_NEW(isolate, static_cast(val.size()))); + typename std::set::const_iterator it; + int i; + for (i = 0, it = val.begin(); it != val.end(); ++it, ++i) + result->Set(i, Converter::ToV8(isolate, *it)); + return result; + } + + static bool FromV8(v8::Isolate* isolate, + v8::Handle val, + std::set* out) { + if (!val->IsArray()) + return false; + + std::set result; + v8::Handle array(v8::Handle::Cast(val)); + uint32_t length = array->Length(); + for (uint32_t i = 0; i < length; ++i) { + T item; + if (!Converter::FromV8(isolate, array->Get(i), &item)) + return false; + result.push_back(item); + } + + out->swap(result); + return true; + } +}; + // Convenience functions that deduce T. template v8::Handle ConvertToV8(v8::Isolate* isolate,