Permalink
Browse files

Macrofy *LookupBy(Name|UUID|UUIDString)

  • Loading branch information...
1 parent e7c8e91 commit 68f83b8789bfe18b125054d1eb01c628639c9a0d @msantos committed Aug 26, 2012
Showing with 64 additions and 269 deletions.
  1. +53 −0 c_src/vert.h
  2. +3 −76 c_src/vert_domain.c
  3. +2 −48 c_src/vert_interface.c
  4. +3 −67 c_src/vert_network.c
  5. +3 −78 c_src/vert_nwfilter.c
View
@@ -345,6 +345,59 @@ vert_##fun(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) \
return term; \
}
+/* *LookupByName and LookupByUUIDString */
+#define VERT_FUN_LOOKUPBYNAME(fun, type, tag) \
+ ERL_NIF_TERM \
+vert_##fun(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) \
+{ \
+ VERT_RESOURCE *vp = NULL; \
+ ErlNifBinary name = {0}; \
+ \
+ VERT_RESOURCE *rp = NULL; \
+ \
+ VERT_GET_RESOURCE(0, vp, VERT_RES_CONNECT); \
+ VERT_GET_IOLIST(1, name); \
+ \
+ VERT_BIN_APPEND_NULL(name); \
+ \
+ VERT_RES_ALLOC(rp, type, vp->res); \
+ \
+ rp->res = fun(vp->res, (char *)name.data); \
+ \
+ if (rp->res == NULL) { \
+ enif_release_resource(rp); \
+ return verterr(env); \
+ } \
+ \
+ return vert_make_resource(env, rp, tag); \
+}
+
+#define VERT_FUN_LOOKUPBYUUID(fun, type, tag) \
+ ERL_NIF_TERM \
+vert_##fun(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) \
+{ \
+ VERT_RESOURCE *vp = NULL; \
+ ErlNifBinary uuid = {0}; \
+ \
+ VERT_RESOURCE *rp = NULL; \
+ \
+ VERT_GET_RESOURCE(0, vp, VERT_RES_CONNECT); \
+ VERT_GET_IOLIST(1, uuid); \
+ \
+ VERT_BIN_APPEND_NULL(uuid); \
+ \
+ VERT_RES_ALLOC(rp, type, vp->res); \
+ \
+ rp->res = fun(vp->res, uuid.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
@@ -46,6 +46,9 @@ VERT_FUN_GETNAME(virDomainGetName, VERT_RES_DOMAIN)
VERT_FUN_GETUUID(virDomainGetUUID, VERT_RES_DOMAIN)
VERT_FUN_GETUUIDSTRING(virDomainGetUUIDString, VERT_RES_DOMAIN)
VERT_FUN_GETXMLDESC(virDomainGetXMLDesc, VERT_RES_DOMAIN)
+VERT_FUN_LOOKUPBYNAME(virDomainLookupByName, VERT_RES_DOMAIN, atom_domain)
+VERT_FUN_LOOKUPBYNAME(virDomainLookupByUUIDString, VERT_RES_DOMAIN, atom_domain)
+VERT_FUN_LOOKUPBYUUID(virDomainLookupByUUID, VERT_RES_DOMAIN, atom_domain)
#if HAVE_VIRDOMAINCREATEWITHFLAGS
ERL_NIF_TERM
@@ -82,82 +85,6 @@ vert_virDomainLookupByID(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
}
ERL_NIF_TERM
-vert_virDomainLookupByName(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
-{
-
- VERT_RESOURCE *vp = NULL;
- VERT_RESOURCE *dp = NULL;
- ErlNifBinary buf = {0};
-
-
- VERT_GET_RESOURCE(0, vp, VERT_RES_CONNECT);
- VERT_GET_IOLIST(1, buf);
-
- VERT_BIN_APPEND_NULL(buf);
-
- VERT_RES_ALLOC(dp, VERT_RES_DOMAIN, vp->res);
-
- dp->res = virDomainLookupByName(vp->res, (char *)buf.data);
-
- if (dp->res == NULL) {
- enif_release_resource(dp);
- return verterr(env);
- }
-
- return vert_make_resource(env, dp, atom_domain);
-}
-
- ERL_NIF_TERM
-vert_virDomainLookupByUUID(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
-{
- VERT_RESOURCE *vp = NULL;
- VERT_RESOURCE *dp = NULL;
- ErlNifBinary buf = {0};
-
-
- VERT_GET_RESOURCE(0, vp, VERT_RES_CONNECT);
- VERT_GET_IOLIST(1, buf);
-
- VERT_BIN_APPEND_NULL(buf);
-
- VERT_RES_ALLOC(dp, VERT_RES_DOMAIN, vp->res);
-
- dp->res = virDomainLookupByUUID(vp->res, buf.data);
-
- if (dp->res == NULL) {
- enif_release_resource(dp);
- return verterr(env);
- }
-
- return vert_make_resource(env, dp, atom_domain);
-}
-
- ERL_NIF_TERM
-vert_virDomainLookupByUUIDString(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
-{
- VERT_RESOURCE *vp = NULL;
- VERT_RESOURCE *dp = NULL;
- ErlNifBinary buf = {0};
-
-
- VERT_GET_RESOURCE(0, vp, VERT_RES_CONNECT);
- VERT_GET_IOLIST(1, buf);
-
- VERT_BIN_APPEND_NULL(buf);
-
- VERT_RES_ALLOC(dp, VERT_RES_DOMAIN, vp->res);
-
- dp->res = virDomainLookupByUUIDString(vp->res, (char *)buf.data);
-
- if (dp->res == NULL) {
- enif_release_resource(dp);
- return verterr(env);
- }
-
- return vert_make_resource(env, dp, atom_domain);
-}
-
- ERL_NIF_TERM
vert_virDomainGetAutostart(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
{
VERT_RESOURCE *dp = NULL;
View
@@ -39,19 +39,8 @@ VERT_FUN_INT_RES(virInterfaceUndefine, VERT_RES_INTERFACE)
VERT_FUN_GETNAME(virInterfaceGetMACString, VERT_RES_INTERFACE)
VERT_FUN_GETNAME(virInterfaceGetName, VERT_RES_INTERFACE)
VERT_FUN_GETXMLDESC(virInterfaceGetXMLDesc, VERT_RES_INTERFACE)
-
-
- ERL_NIF_TERM
-vert_virInterfaceLookupByName(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
-{
- return vert_interface_res_res_ccharp(env, argv, virInterfaceLookupByName);
-}
-
- ERL_NIF_TERM
-vert_virInterfaceLookupByMACString(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
-{
- return vert_interface_res_res_ccharp(env, argv, virInterfaceLookupByMACString);
-}
+VERT_FUN_LOOKUPBYNAME(virInterfaceLookupByName, VERT_RES_INTERFACE, atom_interface)
+VERT_FUN_LOOKUPBYNAME(virInterfaceLookupByMACString, VERT_RES_INTERFACE, atom_interface)
ERL_NIF_TERM
vert_virInterfaceDefineXML(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
@@ -96,41 +85,6 @@ vert_virInterfaceDestroy(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
* Internal functions
*/
ERL_NIF_TERM
-vert_interface_res_res_ccharp(
- ErlNifEnv *env,
- const ERL_NIF_TERM argv[],
- virInterfacePtr (*fp)(virConnectPtr, const char *))
-{
- VERT_RESOURCE *vp = NULL;
- const char name[256];
-
- VERT_RESOURCE *ifp = NULL;
-
-
- VERT_GET_RESOURCE(0, vp, VERT_RES_CONNECT);
-
- if (enif_get_string(env, argv[1], (char *)name, sizeof(name), ERL_NIF_LATIN1) < 1)
- return error_tuple(env, atom_badarg);
-
- ifp = enif_alloc_resource(NIF_VERT_RESOURCE, sizeof(VERT_RESOURCE));
-
- if (ifp == NULL)
- return atom_enomem;
-
- ifp->type = VERT_RES_INTERFACE;
- ifp->conn = vp->res;
-
- ifp->res = fp(vp->res, name);
-
- if (ifp->res == NULL) {
- enif_release_resource(ifp);
- return verterr(env);
- }
-
- return vert_make_resource(env, ifp, atom_interface);
-}
-
- ERL_NIF_TERM
vert_interface_int_res_uint(
ErlNifEnv *env,
const ERL_NIF_TERM argv[],
View
@@ -45,42 +45,9 @@ VERT_FUN_GETNAME(virNetworkGetName, VERT_RES_NETWORK)
VERT_FUN_GETUUID(virNetworkGetUUID, VERT_RES_NETWORK)
VERT_FUN_GETUUIDSTRING(virNetworkGetUUIDString, VERT_RES_NETWORK)
VERT_FUN_GETXMLDESC(virNetworkGetXMLDesc, VERT_RES_NETWORK)
-
- ERL_NIF_TERM
-vert_virNetworkLookupByName(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
-{
- return vert_network_res_res_ccharp(env, argv, virNetworkLookupByName);
-}
-
- ERL_NIF_TERM
-vert_virNetworkLookupByUUID(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
-{
- VERT_RESOURCE *vp = NULL;
- ErlNifBinary buf = {0};
-
- VERT_RESOURCE *np = NULL;
-
-
- VERT_GET_RESOURCE(0, vp, VERT_RES_CONNECT);
- VERT_GET_IOLIST(1, buf);
-
- VERT_RES_ALLOC(np, VERT_RES_NETWORK, vp->res);
-
- np->res = virNetworkLookupByUUID(vp->res, buf.data);
-
- if (np->res == NULL) {
- enif_release_resource(np);
- return verterr(env);
- }
-
- return vert_make_resource(env, np, atom_network);
-}
-
- ERL_NIF_TERM
-vert_virNetworkLookupByUUIDString(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
-{
- return vert_network_res_res_ccharp(env, argv, virNetworkLookupByUUIDString);
-}
+VERT_FUN_LOOKUPBYNAME(virNetworkLookupByName, VERT_RES_NETWORK, atom_network)
+VERT_FUN_LOOKUPBYNAME(virNetworkLookupByUUIDString, VERT_RES_NETWORK, atom_network)
+VERT_FUN_LOOKUPBYUUID(virNetworkLookupByUUID, VERT_RES_NETWORK, atom_network)
ERL_NIF_TERM
vert_virNetworkGetAutostart(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
@@ -117,34 +84,3 @@ vert_virNetworkGetBridgeName(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]
atom_ok,
term);
}
-
-/*
- * Internal functions
- */
- ERL_NIF_TERM
-vert_network_res_res_ccharp(
- ErlNifEnv *env,
- const ERL_NIF_TERM argv[],
- virNetworkPtr (*fp)(virConnectPtr, const char *)
- )
-{
- VERT_RESOURCE *vp = NULL;
- ErlNifBinary buf = {0};
-
- VERT_RESOURCE *np = NULL;
-
-
- VERT_GET_RESOURCE(0, vp, VERT_RES_CONNECT);
- VERT_GET_IOLIST(1, buf);
-
- VERT_RES_ALLOC(np, VERT_RES_NETWORK, vp->res);
-
- np->res = fp(vp->res, (char *)buf.data);
-
- if (np->res == NULL) {
- enif_release_resource(np);
- return verterr(env);
- }
-
- return vert_make_resource(env, np, atom_network);
-}
View
@@ -40,84 +40,9 @@ VERT_FUN_GETNAME(virNWFilterGetName, VERT_RES_NWFILTER)
VERT_FUN_GETUUID(virNWFilterGetUUID, VERT_RES_NWFILTER)
VERT_FUN_GETUUIDSTRING(virNWFilterGetUUIDString, VERT_RES_NWFILTER)
VERT_FUN_GETXMLDESC(virNWFilterGetXMLDesc, VERT_RES_NWFILTER)
-
- ERL_NIF_TERM
-vert_virNWFilterLookupByName(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
-{
- VERT_RESOURCE *vp = NULL;
- ErlNifBinary name = {0};
-
- VERT_RESOURCE *np = NULL;
-
-
- VERT_GET_RESOURCE(0, vp, VERT_RES_CONNECT);
- VERT_GET_IOLIST(1, name);
-
- VERT_BIN_APPEND_NULL(name);
-
- VERT_RES_ALLOC(np, VERT_RES_NWFILTER, vp->res);
-
- np->res = virNWFilterLookupByName(vp->res, (char *)name.data);
-
- if (np->res == NULL) {
- enif_release_resource(np);
- return verterr(env);
- }
-
- return vert_make_resource(env, np, atom_nwfilter);
-}
-
- ERL_NIF_TERM
-vert_virNWFilterLookupByUUID(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
-{
- VERT_RESOURCE *vp = NULL;
- ErlNifBinary uuid = {0};
-
- VERT_RESOURCE *np = NULL;
-
-
- VERT_GET_RESOURCE(0, vp, VERT_RES_CONNECT);
- VERT_GET_IOLIST(1, uuid);
-
- VERT_BIN_APPEND_NULL(uuid);
-
- VERT_RES_ALLOC(np, VERT_RES_NWFILTER, vp->res);
-
- np->res = virNWFilterLookupByUUID(vp->res, uuid.data);
-
- if (np->res == NULL) {
- enif_release_resource(np);
- return verterr(env);
- }
-
- return vert_make_resource(env, np, atom_nwfilter);
-}
-
- ERL_NIF_TERM
-vert_virNWFilterLookupByUUIDString(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
-{
- VERT_RESOURCE *vp = NULL;
- ErlNifBinary uuid = {0};
-
- VERT_RESOURCE *np = NULL;
-
-
- VERT_GET_RESOURCE(0, vp, VERT_RES_CONNECT);
- VERT_GET_IOLIST(1, uuid);
-
- VERT_BIN_APPEND_NULL(uuid);
-
- VERT_RES_ALLOC(np, VERT_RES_NWFILTER, vp->res);
-
- np->res = virNWFilterLookupByUUIDString(vp->res, (char *)uuid.data);
-
- if (np->res == NULL) {
- enif_release_resource(np);
- return verterr(env);
- }
-
- return vert_make_resource(env, np, atom_nwfilter);
-}
+VERT_FUN_LOOKUPBYNAME(virNWFilterLookupByName, VERT_RES_NWFILTER, atom_nwfilter)
+VERT_FUN_LOOKUPBYNAME(virNWFilterLookupByUUIDString, VERT_RES_NWFILTER, atom_nwfilter)
+VERT_FUN_LOOKUPBYUUID(virNWFilterLookupByUUID, VERT_RES_NWFILTER, atom_nwfilter)
#else
VERT_FUN_UNSUPPORTED(virNWFilterUndefine)
VERT_FUN_UNSUPPORTED(virNWFilterGetXMLDesc)

0 comments on commit 68f83b8

Please sign in to comment.