Imported Upstream version 3.2.0
[debian/amanda] / perl / amglue / constants.swg
index 0fa198519d9715880d9ee5e7fd144e543884d055..f194ef2e64f38ae35496ada1238d9304a88df616 100644 (file)
@@ -1,20 +1,20 @@
 /*
- * Copyright (c) Zmanda, Inc.  All Rights Reserved.
+ * Copyright (c) 2007, 2008, 2009, 2010 Zmanda, Inc.  All Rights Reserved.
  *
- * This library is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License version 2.1
- * as published by the Free Software Foundation.
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 as published
+ * by the Free Software Foundation.
  *
- * This library is distributed in the hope that it will be useful, but
+ * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
- * License for more details.
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
  *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this library; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  *
- * Contact information: Zmanda Inc., 505 N Mathlida Ave, Suite 120
+ * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
  * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
  */
 
 
 %include "amglue/exports.swg"
 
+/*
+ * Internal support for iterating over lists of symbols; users will define
+ * the list as e.g.,
+ *
+ *   #define FOR_ALL_WHATEVER(APPLY) APPLY(SYMBOL1) APPLY(SYMBOL2) ..
+ *
+ * and then passes FOR_ALL_WHATEVER as the FORALL_FN argument to one of the
+ * macros below.  This uses the "X Macro" trick; see
+ * http://www.drdobbs.com/blog/archives/2010/06/the_x_macro.html
+ *
+ * NOTE: SWIG's %define macros add C-style comments when they are expanded, so
+ * this method must use #define, not %define.  Also note that this always expands
+ * to a single line, making the result very ugly.
+ */
+
+#define _amglue_apply_null(V) V
+#define _amglue_apply_leading_scalar(V) $V
+#define _amglue_apply_trailing_comma(V) V,
+#define _amglue_apply_C_scalar(V) C<$V>
+#define _amglue_forall_join_whitespace(FORALL_FN) FORALL_FN(_amglue_apply_null)
+#define _amglue_forall_scalars(FORALL_FN) FORALL_FN(_amglue_apply_leading_scalar)
+#define _amglue_forall_terminated_by_comma(FORALL_FN) FORALL_FN(_amglue_apply_trailing_comma)
+
 /* Rather than try to use glib's flag/enum architecture, which is only used
  * for a few constants (mostly in property.h), .swg files define constants using
  * these macros.  A typical definition would look like:
@@ -111,6 +134,21 @@ $_ ## TAG ## _VALUES{`CONSTNAME`} = $CONSTNAME;
 %}
 %enddef
 
+/* Add a bunch of constants all at once; this is more efficient with
+ * a large list of constants
+ *
+ * @param FORALL_FN: the FORALL_FN listing all of the constants (see above)
+ * @param TAG: the tag for these constants
+ */
+#define amglue_add_constants(FORALL_FN, TAG) \
+enum { _amglue_forall_terminated_by_comma(FORALL_FN) }; \
+amglue_export_tag(TAG, _amglue_forall_scalars(FORALL_FN)) \
+%perlcode %{ \
+    foreach (qw( _amglue_forall_join_whitespace(FORALL_FN))) { \
+       $_ ## TAG ## _VALUES{$_} = $$_; \
+    } \
+%}
+
 /* Add the given constant with a short name
  *
  * @param CONSTNAME: the name of the constant, as used in C code
@@ -136,3 +174,12 @@ $_ ## TAG ## _VALUES{`SHORTNAME`} = $CONSTNAME;
 enum { CONSTNAME }; /* pass the constant to SWIG */
 amglue_export_tag(TAG, $CONSTNAME);
 %enddef
+
+/* Return the symbols alone, separated by whitespace.  Note that this will
+ * being with whitespace, too.  Be careful if using it in POD.
+ *
+ * @param FORALL_FN: the forall function to use (see above)
+ */
+#define amglue_constants_list(FORALL_FN) \
+    FORALL_FN(_amglue_apply_C_scalar)
+