[perl-Glib] Turn gperl_hv_take_sv into a function



commit f9f4fbf80293ff6ad338f1a009159b8465755490
Author: Torsten Schönfeld <kaffeetisch gmx de>
Date:   Sun Jul 12 20:46:39 2009 +0200

    Turn gperl_hv_take_sv into a function
    
    Rename the original gperl_hv_take_sv macro to gperl_hv_take_sv_s and
    turn it into a convenience wrapper for the new gperl_hv_take_sv function.

 GError.xs     |   16 ++++++++--------
 GParamSpec.xs |   12 ++++++------
 GSignal.xs    |   30 +++++++++++++++---------------
 GType.xs      |   12 ++++++------
 Glib.xs       |   14 ++++++++++++++
 gperl.h       |   15 ++++++---------
 6 files changed, 55 insertions(+), 44 deletions(-)
---
diff --git a/GError.xs b/GError.xs
index dbc11fb..c7e6f8e 100644
--- a/GError.xs
+++ b/GError.xs
@@ -173,21 +173,21 @@ gperl_sv_from_gerror (GError * error)
 	info = error_info_from_domain (error->domain);
 
 	hv = newHV ();
-	gperl_hv_take_sv (hv, "domain",
-	                  newSVGChar (g_quark_to_string (error->domain)));
-	gperl_hv_take_sv (hv, "code", newSViv (error->code));
+	gperl_hv_take_sv_s (hv, "domain",
+	                    newSVGChar (g_quark_to_string (error->domain)));
+	gperl_hv_take_sv_s (hv, "code", newSViv (error->code));
 	if (info)
-		gperl_hv_take_sv (hv, "value",
-		                  gperl_convert_back_enum (info->error_enum,
-		                                           error->code));
-	gperl_hv_take_sv (hv, "message", newSVGChar (error->message));
+		gperl_hv_take_sv_s (hv, "value",
+		                    gperl_convert_back_enum (info->error_enum,
+		                                             error->code));
+	gperl_hv_take_sv_s (hv, "message", newSVGChar (error->message));
 
 	/* WARNING: using evil undocumented voodoo.  mess() is the function
 	 * that die(), warn(), and croak() use to format messages, and it's
 	 * what knows how to find the code location.  don't want to do that
 	 * ourselves, since that's blacker magic, so we'll call this and 
 	 * hope the perl API doesn't change.  */
-	gperl_hv_take_sv (hv, "location", newSVsv (mess ("")));
+	gperl_hv_take_sv_s (hv, "location", newSVsv (mess ("")));
 
 	package = info ? info->package : "Glib::Error";
 
diff --git a/GParamSpec.xs b/GParamSpec.xs
index 6c3782c..5c7e200 100644
--- a/GParamSpec.xs
+++ b/GParamSpec.xs
@@ -161,23 +161,23 @@ newSVGParamSpec (GParamSpec * pspec)
 	 * paramspec list returned from Glib::Object::list_properties())
 	 * we store a few select keys in the hash directly.
 	 */
-	gperl_hv_take_sv (property, "name",
-	                  newSVpv (g_param_spec_get_name (pspec), 0));
+	gperl_hv_take_sv_s (property, "name",
+	                    newSVpv (g_param_spec_get_name (pspec), 0));
 
 	/* map type names to package names, if possible */
 	pv = gperl_package_from_type (pspec->value_type);
 	if (!pv) pv = g_type_name (pspec->value_type);
-	gperl_hv_take_sv (property, "type", newSVpv (pv, 0));
+	gperl_hv_take_sv_s (property, "type", newSVpv (pv, 0));
 
 	pv = gperl_package_from_type (pspec->owner_type);
 	if (!pv)
 		pv = g_type_name (pspec->owner_type);
 	if (pv)
-		gperl_hv_take_sv (property, "owner_type", newSVpv (pv, 0));
+		gperl_hv_take_sv_s (property, "owner_type", newSVpv (pv, 0));
 
 	pv = g_param_spec_get_blurb (pspec);
-	if (pv) gperl_hv_take_sv (property, "descr", newSVpv (pv, 0));
-	gperl_hv_take_sv (property, "flags", newSVGParamFlags (pspec->flags));
+	if (pv) gperl_hv_take_sv_s (property, "descr", newSVpv (pv, 0));
+	gperl_hv_take_sv_s (property, "flags", newSVGParamFlags (pspec->flags));
 
 	/* wrap it, bless it, ship it. */
 	sv = newRV_noinc ((SV*)property);
diff --git a/GSignal.xs b/GSignal.xs
index 9ebd795..f2bca07 100644
--- a/GSignal.xs
+++ b/GSignal.xs
@@ -89,12 +89,12 @@ SV *
 newSVGSignalInvocationHint (GSignalInvocationHint * ihint)
 {
 	HV * hv = newHV ();
-	gperl_hv_take_sv (hv, "signal_name",
-	                  newSVGChar (g_signal_name (ihint->signal_id)));
-	gperl_hv_take_sv (hv, "detail",
-	                  newSVGChar (g_quark_to_string (ihint->detail)));
-	gperl_hv_take_sv (hv, "run_type",
-	                  newSVGSignalFlags (ihint->run_type));
+	gperl_hv_take_sv_s (hv, "signal_name",
+	                    newSVGChar (g_signal_name (ihint->signal_id)));
+	gperl_hv_take_sv_s (hv, "detail",
+	                    newSVGChar (g_quark_to_string (ihint->detail)));
+	gperl_hv_take_sv_s (hv, "run_type",
+	                    newSVGSignalFlags (ihint->run_type));
 	return newRV_noinc ((SV*)hv);
 }
 
@@ -115,20 +115,20 @@ newSVGSignalQuery (GSignalQuery * query)
 		return &PL_sv_undef;
 
 	hv = newHV ();
-	gperl_hv_take_sv (hv, "signal_id", newSViv (query->signal_id));
-	gperl_hv_take_sv (hv, "signal_name",
-	                  newSVpv (query->signal_name, 0));
+	gperl_hv_take_sv_s (hv, "signal_id", newSViv (query->signal_id));
+	gperl_hv_take_sv_s (hv, "signal_name",
+	                    newSVpv (query->signal_name, 0));
 	GET_NAME (pkgname, query->itype);
 	if (pkgname)
-		gperl_hv_take_sv (hv, "itype", newSVpv (pkgname, 0));
-	gperl_hv_take_sv (hv, "signal_flags",
-	                  newSVGSignalFlags (query->signal_flags));
+		gperl_hv_take_sv_s (hv, "itype", newSVpv (pkgname, 0));
+	gperl_hv_take_sv_s (hv, "signal_flags",
+	                    newSVGSignalFlags (query->signal_flags));
 	if (query->return_type != G_TYPE_NONE) {
 		GType t = query->return_type & ~G_SIGNAL_TYPE_STATIC_SCOPE;
 		GET_NAME (pkgname, t);
 		if (pkgname)
-			gperl_hv_take_sv (hv, "return_type",
-			                  newSVpv (pkgname, 0));
+			gperl_hv_take_sv_s (hv, "return_type",
+			                    newSVpv (pkgname, 0));
 	}
 	av = newAV ();
 	for (j = 0; j < query->n_params; j++) {
@@ -136,7 +136,7 @@ newSVGSignalQuery (GSignalQuery * query)
 		GET_NAME (pkgname, t);
 		av_push (av, newSVpv (pkgname, 0));
 	}
-	gperl_hv_take_sv (hv, "param_types", newRV_noinc ((SV*)av));
+	gperl_hv_take_sv_s (hv, "param_types", newRV_noinc ((SV*)av));
 	/* n_params is inferred by the length of the av in param_types */
 
 	return newRV_noinc ((SV*)hv);
diff --git a/GType.xs b/GType.xs
index 6fd7c30..f47522c 100644
--- a/GType.xs
+++ b/GType.xs
@@ -2696,18 +2696,18 @@ list_values (class, const char * package)
 		GEnumValue * v = gperl_type_enum_get_values (type);
 		for ( ; v && v->value_nick && v->value_name ; v++) {
 			HV * hv = newHV ();
-			gperl_hv_take_sv (hv, "value", newSViv (v->value));
-			gperl_hv_take_sv (hv, "nick", newSVpv (v->value_nick, 0));
-			gperl_hv_take_sv (hv, "name", newSVpv (v->value_name, 0));
+			gperl_hv_take_sv_s (hv, "value", newSViv (v->value));
+			gperl_hv_take_sv_s (hv, "nick", newSVpv (v->value_nick, 0));
+			gperl_hv_take_sv_s (hv, "name", newSVpv (v->value_name, 0));
 			XPUSHs (sv_2mortal (newRV_noinc ((SV*)hv)));
 		}
 	} else if (G_TYPE_IS_FLAGS (type)) {
 		GFlagsValue * v = gperl_type_flags_get_values (type);
 		for ( ; v && v->value_nick && v->value_name ; v++) {
 			HV * hv = newHV ();
-			gperl_hv_take_sv (hv, "value", newSVuv (v->value));
-			gperl_hv_take_sv (hv, "nick", newSVpv (v->value_nick, 0));
-			gperl_hv_take_sv (hv, "name", newSVpv (v->value_name, 0));
+			gperl_hv_take_sv_s (hv, "value", newSVuv (v->value));
+			gperl_hv_take_sv_s (hv, "nick", newSVpv (v->value_nick, 0));
+			gperl_hv_take_sv_s (hv, "name", newSVpv (v->value_name, 0));
 			XPUSHs (sv_2mortal (newRV_noinc ((SV*)hv)));
 		}
 	} else {
diff --git a/Glib.xs b/Glib.xs
index d16a2ac..fdbbda0 100644
--- a/Glib.xs
+++ b/Glib.xs
@@ -337,6 +337,20 @@ gperl_sv_is_defined (SV *sv)
 	return FALSE;
 }
 
+=item void gperl_hv_take_sv (HV *hv, const char *key, size_t key_length, SV *sv)
+
+Tries to store I<sv> in I<hv>.  Decreases I<sv>'s reference count if something
+goes wrong.
+
+=cut
+void
+gperl_hv_take_sv (HV *hv, const char *key, size_t key_length, SV *sv)
+{
+	if (!hv_store (hv, key, key_length, sv, 0)) {
+		sv_free (sv);
+	}
+}
+
 =back
 
 =cut
diff --git a/gperl.h b/gperl.h
index 4389748..7df0f87 100644
--- a/gperl.h
+++ b/gperl.h
@@ -412,15 +412,12 @@ gboolean gperl_sv_is_defined (SV *sv);
 #define gperl_sv_is_hash_ref(sv) \
 	(gperl_sv_is_defined (sv) && SvROK (sv) && SvTYPE (SvRV(sv)) == SVt_PVHV)
 
-/* try to store an SV in an HV.  decrease the SV's reference count if something
- * went wrong.  key must be a string literal. */
-#define gperl_hv_take_sv(hv, key, sv) \
-	G_STMT_START { \
-		SV *tmp = sv; \
-		if (!hv_store (hv, key, sizeof(key) - 1, tmp, 0)) { \
-			sv_free (tmp); \
-		} \
-	} G_STMT_END
+void gperl_hv_take_sv (HV *hv, const char *key, size_t key_length, SV *sv);
+
+/* helper wrapper for static string literals.  concatenating with "" enforces
+ * the restriction. */
+#define gperl_hv_take_sv_s(hv, key, sv) \
+	gperl_hv_take_sv (hv, "" key "", sizeof(key) - 1, sv)
 
 /* internal trickery */
 gpointer gperl_type_class (GType type);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]