I'm working on a libcurl binding for NodeJS, and to get info from a curl handler, using curl_easy_getinfo, I've created a template function, the issue, is that the result of the call can be a null pointer.
To make sure this was correctly addressed, I've created a template specialization that handle the edge case:
template<typename ResultType, typename v8MappingType>
v8::Handle<v8::Value> Curl::GetInfoTmpl( const Curl &obj, int infoId )
{
v8::HandleScope scope;
ResultType result;
CURLINFO info = (CURLINFO) infoId;
CURLcode code = curl_easy_getinfo( obj.curl, info, &result );
if ( code != CURLE_OK )
return Curl::Raise( "curl_easy_getinfo failed!", curl_easy_strerror( code ) );
return scope.Close( v8MappingType::New( result ) );
}
//The null pointer is only possible when the value returned is a c string
template<> //workaround for null pointer, aka, hack.
v8::Handle<v8::Value> Curl::GetInfoTmpl<char*,v8::String>( const Curl &obj, int infoId )
{
//duplicated code
v8::HandleScope scope;
char *result;
CURLINFO info = (CURLINFO) infoId;
CURLcode code = curl_easy_getinfo( obj.curl, info, &result );
if ( !result ) { //null pointer
return v8::String::New( "" );
}
if ( code != CURLE_OK )
return Curl::Raise( "curl_easy_getinfo failed!", curl_easy_strerror( code ) );
return scope.Close( v8::String::New( result ) );
}
Is this the correct way to handle that, or are there better ways to do so?
The full code is on GitHub.