我目前正在尝试创建一组转换函数,通过一次调用,可以(尝试)将 JavaScript 对象(CefV8Value
)转换为其 C++ 对应对象,并支持指针。
下面是转换函数(最后是指针转换):
template<typename T>
T convert_v8value_to_cpp(const CefRefPtr<CefV8Value> &value) {};
// Explicit type conversion functions
#define V8VALUE_TO_CPP_CONVERSION(type) \
template<> type \
convert_v8value_to_cpp<type>(const CefRefPtr<CefV8Value> &value)
V8VALUE_TO_CPP_CONVERSION(CefRefPtr<CefV8Value>)
{
return value;
}
V8VALUE_TO_CPP_CONVERSION(bool)
{
return value->GetBoolValue();
}
V8VALUE_TO_CPP_CONVERSION(int)
{
return value->GetIntValue();
}
V8VALUE_TO_CPP_CONVERSION(std::string)
{
return value->GetStringValue().ToString();
}
V8VALUE_TO_CPP_CONVERSION(const char *)
{
return value->GetStringValue().ToString().c_str();
}
V8VALUE_TO_CPP_CONVERSION(std::wstring)
{
return value->GetStringValue().ToWString();
}
// HACKHACK: most VGUI functions take non-const wchar_t pointers, when they
// shouldn't
V8VALUE_TO_CPP_CONVERSION(wchar_t *)
{
return (wchar_t*)value->GetStringValue().ToWString().c_str();
}
V8VALUE_TO_CPP_CONVERSION(const wchar_t *)
{
return value->GetStringValue().ToWString().c_str();
}
V8VALUE_TO_CPP_CONVERSION(double)
{
return value->GetDoubleValue();
}
V8VALUE_TO_CPP_CONVERSION(float)
{
return value->GetDoubleValue();
}
//-----------------------------------------------------------------------------
// Purpose: converts a JS array to a C++ vector (of type T)
//-----------------------------------------------------------------------------
template<typename T>
std::vector<T> convert_v8value_to_cpp(const CefRefPtr<CefV8Value> &value)
{
std::vector<T> vec;
if(!value->IsArray())
return vec;
for(int i = 0; i < value->GetArrayLength(); ++i)
{
CefRefPtr<CefV8Value> element = value->GetValue(i);
vec.push_back(convert_v8value_to_cpp<T>(element));
}
return vec; // hopefully move semantics will optimise this and not copy-construct
}
//-----------------------------------------------------------------------------
// Purpose: converts a JS object to a C++ pointer (where T is a pointer type)
//-----------------------------------------------------------------------------
template<typename T>
typename std::enable_if<std::is_pointer<T>::value, T>::type
convert_v8value_to_cpp(const CefRefPtr<CefV8Value> &value)
{
if(!value->IsObject())
return NULL;
CefRefPtr<CefV8Value> pTypeId = value->GetValue("__v8bind_typeid__");
if(!pTypeId || !pTypeId->IsString())
return NULL;
CefRefPtr<CefV8Value> pPointerVal = value->GetValue("__v8bind_ptr__");
if(!pPointerVal || !pPointerVal->IsInt())
return NULL;
if(pTypeId->GetStringValue().ToString() == typeid(T).name())
return (T)pPointerVal->GetIntValue();
return NULL;
}
这是使用所述指针函数的代码:
WrapClass *pThis = convert_v8value_to_cpp<WrapClass*>(object);
Visual Studio抱怨说:
error C2668: 'convert_v8value_to_cpp' : ambiguous call to overloaded function
binding_test.cpp(106): could be 'Point *convert_v8value_to_cpp<WrapClass*>(const CefRefPtr<T> &)'
with
[
WrapClass=Point,
T=CefV8Value
]
binding_test.cpp(88): or 'std::vector<_Ty,_Ax> convert_v8value_to_cpp<WrapClass*>(const CefRefPtr<T> &)'
with
[
_Ty=Point *,
_Ax=std::allocator<Point *>,
WrapClass=Point,
T=CefV8Value
]
binding_test.cpp(31): or 'T convert_v8value_to_cpp<WrapClass*>(const CefRefPtr<CefV8Value> &)'
with
[
T=Point *,
WrapClass=Point
]
while trying to match the argument list '(CefRefPtr<T>)'
with
[
T=CefV8Value
]
我不明白调用是如何模棱两可的(除了WrapClass *
匹配 的第一个转换函数T
)。但是,它也表示可能的呼叫候选是std::vector
转换。这怎么可能?
提前谢谢了!