Merge pull request #1 from deepak1556/converter_patch
added std::set converter
This commit is contained in:
commit
047a8de934
1 changed files with 35 additions and 0 deletions
|
@ -7,6 +7,7 @@
|
|||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <set>
|
||||
|
||||
#include "base/strings/string_piece.h"
|
||||
#include "native_mate/compat.h"
|
||||
|
@ -197,6 +198,40 @@ struct Converter<std::vector<T> > {
|
|||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct Converter<std::set<T> > {
|
||||
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const std::set<T>& val) {
|
||||
v8::Handle<v8::Array> result(
|
||||
MATE_ARRAY_NEW(isolate, static_cast<int>(val.size())));
|
||||
typename std::set<T>::const_iterator it;
|
||||
int i;
|
||||
for (i = 0, it = val.begin(); it != val.end(); ++it, ++i)
|
||||
result->Set(i, Converter<T>::ToV8(isolate, *it));
|
||||
return result;
|
||||
}
|
||||
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Handle<v8::Value> val,
|
||||
std::set<T>* out) {
|
||||
if (!val->IsArray())
|
||||
return false;
|
||||
|
||||
std::set<T> result;
|
||||
v8::Handle<v8::Array> array(v8::Handle<v8::Array>::Cast(val));
|
||||
uint32_t length = array->Length();
|
||||
for (uint32_t i = 0; i < length; ++i) {
|
||||
T item;
|
||||
if (!Converter<T>::FromV8(isolate, array->Get(i), &item))
|
||||
return false;
|
||||
result.push_back(item);
|
||||
}
|
||||
|
||||
out->swap(result);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
// Convenience functions that deduce T.
|
||||
template<typename T>
|
||||
v8::Handle<v8::Value> ConvertToV8(v8::Isolate* isolate,
|
||||
|
|
Loading…
Reference in a new issue