2 * Copyright (c) 2007,2008,2009 Zmanda, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 * Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
22 * This file contains SWIG macros to handle C constants, enums, and flags
25 %include "amglue/exports.swg"
27 /* Rather than try to use glib's flag/enum architecture, which is only used
28 * for a few constants (mostly in property.h), .swg files define constants using
29 * these macros. A typical definition would look like:
30 * amglue_add_flag_tag_fns(Permissions);
31 * amglue_add_constant(PERM_READ, Permissions);
32 * amglue_add_constant(PERM_WRITE, Permissions);
33 * note that the values of the constants do not appear here, although the header
34 * file in which they are defined must be included in the %{ .. %} block.
36 * The above would result in:
37 * - typedef int Permissions;
38 * - $PERM_READ and $PERM_WRITE in @EXPORT_OK
39 * - $PERM_READ and $PERM_WRITE in %EXPORT_TAGS{'Permissions'}
40 * - Permissions_to_strings($flags) -> ( name, name, .. )
42 * Similarly, amglue_add_enum_tag_fns(FileType) would add the same
43 * EXPORTs, but a function
44 * - FileType_to_string($enum) -> name
47 %define amglue_add_flag_tag_fns(TAG)
49 amglue_export_tag(TAG, TAG ## _to_strings);
51 my %_ ## TAG ## _VALUES;
52 # Convert a flag value to a list of names for flags that are set.
53 sub TAG ## _to_strings {
57 for my $k (keys %_ ## TAG ## _VALUES) {
58 my $v = $_ ## TAG ## _VALUES{$k};
60 # is this a matching flag?
61 if (($v == 0 && $flags == 0) || ($v != 0 && ($flags & $v) == $v)) {
66 # by default, just return the number as a 1-element list
76 %define amglue_add_enum_tag_fns(TAG)
78 amglue_export_tag(TAG, TAG ## _to_string);
80 my %_ ## TAG ## _VALUES;
81 # Convert an enum value to a single string
82 sub TAG ## _to_string {
85 for my $k (keys %_ ## TAG ## _VALUES) {
86 my $v = $_ ## TAG ## _VALUES{$k};
88 # is this a matching flag?
94 # default, just return the number
100 /* Add the given constant, assuming the constant name is the
103 * @param CONSTNAME: the name of the constant, as used in C code
104 * @param TAG: the tag for this constant (enum name, etc.)
106 %define amglue_add_constant(CONSTNAME, TAG)
107 enum { CONSTNAME }; /* pass the constant to SWIG */
108 amglue_export_tag(TAG, $CONSTNAME);
110 $_ ## TAG ## _VALUES{`CONSTNAME`} = $CONSTNAME;
114 /* Add the given constant with a short name
116 * @param CONSTNAME: the name of the constant, as used in C code
117 * @param SHORTNAME: the name to be shown by TAG_to_string(s) (a string)
118 * @param TAG: the tag for this constant (enum name, etc.)
120 %define amglue_add_constant_short(CONSTNAME, SHORTNAME, TAG)
121 enum { CONSTNAME }; /* pass the constant to SWIG */
122 amglue_export_tag(TAG, $CONSTNAME);
124 $_ ## TAG ## _VALUES{`SHORTNAME`} = $CONSTNAME;
128 /* Add the given constant. No shortname is supplied, so the constant
129 * will not be used for conversion to strings. Use this function for
130 * bit combinations and other metadata, e.g., FOO_MASK or FOO_MAX
132 * @param CONSTNAME: the name of the constant, as used in C code
133 * @param TAG: the tag for this constant (enum name, etc.)
135 %define amglue_add_constant_noshort(CONSTNAME, TAG)
136 enum { CONSTNAME }; /* pass the constant to SWIG */
137 amglue_export_tag(TAG, $CONSTNAME);