How do handle multiple versions of a method in my node
addon
when compiling across multiple versions of node. For ex: uv_inet_pton
and uv_inet_pton
of node
version 0.10*
is different than 0.12.*
. The return type is different in these versions. Can I handle this case by defining a Macro or a pre processor ?
The signature in 0.10 is:
typedef struct uv_err_s uv_err_t;
uv_err_t uv_inet_pton(int af, const char* src, void* dst);
struct uv_err_s {
/* read-only */
/* MY ADDITION:
uv_err_code is an enum here, and 0 means OK.
*/
uv_err_code code;
/* private */
int sys_errno_;
};
So a call to this function returns a structure which has a code
field, which specifies the error, which is 0 for success.
As for newer versions, the return type has changed:
UV_EXTERN int uv_inet_pton(int af, const char* src, void* dst);
So, using version information provided in newer and older versions, you can deduce which version you're compiling against. The links are for 0.10
and 1.0x
, but you can get similar information in 0.12
as well.
If it's OK for a function that returns int, 0 for success, other values for failure, you need to use the function provided below:
int my_inet_pton(int af, const char* src, void* dst)
{
#ifdef VERSION_0_10_CHECK
/* I'm a little rusty on enums, you might need to
* cast to int, i'm not sure */
return uv_inet_pton(af, src, dst).code;
#else
return uv_inet_pton(af, src, dst);
#endif
}
For other possible incompatibilities, try using version checking as little as possible in the main code, and write wrappers for the dirty work. This way, your main code will look nicer.