d84b4b7bdfbfd06e70e82059d8a4ad021cf05992
[debian/amanda] / perl / amglue / constants.swg
1 /*
2  * Copyright (c) Zmanda, Inc.  All Rights Reserved.
3  *
4  * This library is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 2.1
6  * as published by the Free Software Foundation.
7  *
8  * This library 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 Lesser General Public
11  * License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this library; if not, write to the Free Software Foundation,
15  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
16  *
17  * Contact information: Zmanda Inc., 465 S Mathlida Ave, Suite 300
18  * Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19  */
20
21 /*
22  * This file contains SWIG macros to handle C constants, enums, and flags
23  */
24
25 %include "amglue/exports.swg"
26
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.
35  *
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, .. )
41  *
42  * Similarly, amglue_add_enum_tag_fns(FileType) would add the same
43  * EXPORTs, but a function
44  *  - FileType_to_string($enum) -> name
45  */
46
47 %define amglue_add_flag_tag_fns(TAG)
48 typedef int TAG;
49 amglue_export_tag(TAG, TAG ## _to_strings);
50 %perlcode %{
51 my %_ ## TAG ## _VALUES;
52 # Convert a flag value to a list of names for flags that are set.
53 sub TAG ## _to_strings {
54     my ($flags) = @_;
55     my @result = ();
56
57     for my $k (keys %_ ## TAG ## _VALUES) {
58         my $v = $_ ## TAG ## _VALUES{$k};
59
60         # is this a matching flag?
61         if (($v == 0 && $flags == 0) || ($v != 0 && ($flags & $v) == $v)) {
62             push @result, $k;
63         }
64     }
65
66     # by default, just return the number as a 1-element list
67     if (!@result) {
68         return ($flags);
69     }
70
71     return @result;
72 }
73 %}
74 %enddef
75
76 %define amglue_add_enum_tag_fns(TAG)
77 typedef int TAG;
78 amglue_export_tag(TAG, TAG ## _to_string);
79 %perlcode %{
80 my %_ ## TAG ## _VALUES;
81 # Convert an enum value to a single string
82 sub TAG ## _to_string {
83     my ($enumval) = @_;
84
85     for my $k (keys %_ ## TAG ## _VALUES) {
86         my $v = $_ ## TAG ## _VALUES{$k};
87
88         # is this a matching flag?
89         if ($enumval == $v) {
90             return $k;
91         }
92     }
93
94     # default, just return the number
95     return $enumval;
96 }
97 %}
98 %enddef
99
100 /* Add the given constant, assuming the constant name is the 
101  * short name
102  *
103  * @param CONSTNAME: the name of the constant, as used in C code
104  * @param TAG: the tag for this constant (enum name, etc.)
105  */
106 %define amglue_add_constant(CONSTNAME, TAG)
107 enum { CONSTNAME }; /* pass the constant to SWIG */
108 amglue_export_tag(TAG, $CONSTNAME);
109 %perlcode %{
110 $_ ## TAG ## _VALUES{`CONSTNAME`} = $CONSTNAME;
111 %}
112 %enddef
113
114 /* Add the given constant with a short name
115  *
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.)
119  */
120 %define amglue_add_constant_short(CONSTNAME, SHORTNAME, TAG)
121 enum { CONSTNAME }; /* pass the constant to SWIG */
122 amglue_export_tag(TAG, $CONSTNAME);
123 %perlcode %{
124 $_ ## TAG ## _VALUES{`SHORTNAME`} = $CONSTNAME;
125 %}
126 %enddef
127
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
131  *
132  * @param CONSTNAME: the name of the constant, as used in C code
133  * @param TAG: the tag for this constant (enum name, etc.)
134  */
135 %define amglue_add_constant_noshort(CONSTNAME, TAG)
136 enum { CONSTNAME }; /* pass the constant to SWIG */
137 amglue_export_tag(TAG, $CONSTNAME);
138 %enddef