Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Replace vir*DefineXML functions with a macro

Reduce the amount of code by using yet another macro. Doesn't work with
virInterfaceDefineXML() because it has a third, unused flag argument.
Consistency is nice.

If any other 3 arg definexml's are introduced, could add a
VERT_FUN_DEFINEXML3() macro later.
  • Loading branch information...
commit 6d602120342b603c3b0f42803fd37a197f0d8f4c 1 parent 5cd2984
@msantos authored
View
26 c_src/vert.h
@@ -258,6 +258,32 @@ vert_##fun(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) \
atom_ok, list); \
}
+#define VERT_FUN_DEFINEXML(fun, type, tag) \
+ ERL_NIF_TERM \
+vert_##fun(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) \
+{ \
+ VERT_RESOURCE *vp = NULL; \
+ ErlNifBinary xml = {0}; \
+ \
+ VERT_RESOURCE *rp = NULL; \
+ \
+ VERT_GET_RESOURCE(0, vp, VERT_RES_CONNECT); \
+ VERT_GET_IOLIST(1, xml); \
+ \
+ VERT_BIN_APPEND_NULL(xml); \
+ \
+ VERT_RES_ALLOC(rp, type, vp->res); \
+ \
+ rp->res = fun(vp->res, (const char *)xml.data); \
+ \
+ if (rp->res == NULL) { \
+ enif_release_resource(rp); \
+ return verterr(env); \
+ } \
+ \
+ return vert_make_resource(env, rp, tag); \
+}
+
#define VERT_FUN_UNSUPPORTED(fun) \
ERL_NIF_TERM \
vert_##fun(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) \
View
32 c_src/vert_domain.c
@@ -34,12 +34,14 @@
#include "vert_domain.h"
+VERT_FUN_INT_RES(virDomainDestroy, VERT_RES_DOMAIN)
VERT_FUN_INT_RES(virDomainGetMaxVcpus, VERT_RES_DOMAIN)
+VERT_FUN_INT_RES(virDomainResume, VERT_RES_DOMAIN)
VERT_FUN_INT_RES(virDomainShutdown, VERT_RES_DOMAIN)
VERT_FUN_INT_RES(virDomainSuspend, VERT_RES_DOMAIN)
-VERT_FUN_INT_RES(virDomainResume, VERT_RES_DOMAIN)
VERT_FUN_INT_RES(virDomainUndefine, VERT_RES_DOMAIN)
-VERT_FUN_INT_RES(virDomainDestroy, VERT_RES_DOMAIN)
+
+VERT_FUN_DEFINEXML(virDomainDefineXML, VERT_RES_DOMAIN, atom_domain)
#if HAVE_VIRDOMAINCREATEWITHFLAGS
ERL_NIF_TERM
@@ -526,32 +528,6 @@ vert_virDomainRestore(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
}
ERL_NIF_TERM
-vert_virDomainDefineXML(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
-{
- VERT_RESOURCE *vp = NULL;
- ErlNifBinary cfg = {0};
-
- VERT_RESOURCE *dp = NULL;
-
-
- VERT_GET_RESOURCE(0, vp, VERT_RES_CONNECT);
- VERT_GET_IOLIST(1, cfg);
-
- VERT_BIN_APPEND_NULL(cfg);
-
- VERT_RES_ALLOC(dp, VERT_RES_DOMAIN, vp->res);
-
- dp->res = virDomainDefineXML(vp->res, (const char *)cfg.data);
-
- if (dp->res == NULL) {
- enif_release_resource(dp);
- return verterr(env);
- }
-
- return vert_make_resource(env, dp, atom_domain);
-}
-
- ERL_NIF_TERM
vert_virDomainSetAutostart(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
return vert_domain_int_res_int(env, argv, virDomainSetAutostart);
View
2  c_src/vert_interface.c
@@ -36,8 +36,8 @@
VERT_FUN_INT_RES(virInterfaceUndefine, VERT_RES_INTERFACE)
-VERT_FUN_CCHARP_RES(virInterfaceGetName, VERT_RES_INTERFACE)
VERT_FUN_CCHARP_RES(virInterfaceGetMACString, VERT_RES_INTERFACE)
+VERT_FUN_CCHARP_RES(virInterfaceGetName, VERT_RES_INTERFACE)
ERL_NIF_TERM
View
32 c_src/vert_network.c
@@ -34,11 +34,12 @@
#include "vert_network.h"
-VERT_FUN_INT_RES(virNetworkIsPersistent, VERT_RES_NETWORK)
-VERT_FUN_INT_RES(virNetworkUndefine, VERT_RES_NETWORK)
VERT_FUN_INT_RES(virNetworkCreate, VERT_RES_NETWORK)
VERT_FUN_INT_RES(virNetworkDestroy, VERT_RES_NETWORK)
+VERT_FUN_INT_RES(virNetworkIsPersistent, VERT_RES_NETWORK)
+VERT_FUN_INT_RES(virNetworkUndefine, VERT_RES_NETWORK)
+VERT_FUN_DEFINEXML(virNetworkDefineXML, VERT_RES_NETWORK, atom_network)
ERL_NIF_TERM
vert_virNetworkLookupByName(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
@@ -173,33 +174,6 @@ vert_virNetworkGetXMLDesc(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
enif_make_string(env, desc, ERL_NIF_LATIN1));
}
- ERL_NIF_TERM
-vert_virNetworkDefineXML(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
-{
- VERT_RESOURCE *vp = NULL;
- ErlNifBinary cfg;
-
- VERT_RESOURCE *np = NULL;
-
-
- VERT_GET_RESOURCE(0, vp, VERT_RES_CONNECT);
- VERT_GET_IOLIST(1, cfg);
-
- VERT_BIN_APPEND_NULL(cfg);
-
- VERT_RES_ALLOC(np, VERT_RES_NETWORK, vp->res);
-
- np->res = virNetworkDefineXML(vp->res, (char *)cfg.data);
-
- if (np->res == NULL) {
- enif_release_resource(np);
- return verterr(env);
- }
-
- return vert_make_resource(env, np, atom_network);
-}
-
-
/*
* Internal functions
*/
View
27 c_src/vert_nwfilter.c
@@ -35,32 +35,7 @@
#ifdef HAVE_NWFILTER
VERT_FUN_INT_RES(virNWFilterUndefine, VERT_RES_NWFILTER)
-
- ERL_NIF_TERM
-vert_virNWFilterDefineXML(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
-{
- VERT_RESOURCE *vp = NULL;
- ErlNifBinary xml = {0};
-
- VERT_RESOURCE *np = NULL;
-
-
- VERT_GET_RESOURCE(0, vp, VERT_RES_CONNECT);
- VERT_GET_IOLIST(1, xml);
-
- VERT_BIN_APPEND_NULL(xml);
-
- VERT_RES_ALLOC(np, VERT_RES_NWFILTER, vp->res);
-
- np->res = virNWFilterDefineXML(vp->res, (const char *)xml.data);
-
- if (np->res == NULL) {
- enif_release_resource(np);
- return verterr(env);
- }
-
- return vert_make_resource(env, np, atom_nwfilter);
-}
+VERT_FUN_DEFINEXML(virNWFilterDefineXML, VERT_RES_NWFILTER, atom_nwfilter);
ERL_NIF_TERM
vert_virNWFilterGetName(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
Please sign in to comment.
Something went wrong with that request. Please try again.