enumcode.pl 5.94 KB
Newer Older
1 2
#!/usr/bin/perl -w

3
# GIMP - The GNU Image Manipulation Program
4
# Copyright (C) 1999-2003 Manish Singh <yosh@gimp.org>
5

6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 3 of the License, or
9 10 11 12 13 14 15 16
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUTFILE ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18 19

BEGIN {
20 21 22
    $srcdir   = $ENV{srcdir}  || '.';
    $destdir  = $ENV{destdir} || '.';
    $builddir = $ENV{builddir} || '.';
23 24 25 26 27 28 29 30 31 32 33 34
}

use lib $srcdir;

require 'enums.pl';
require 'util.pl';

*enums = \%Gimp::CodeGen::enums::enums;

*write_file = \&Gimp::CodeGen::util::write_file;
*FILE_EXT   = \$Gimp::CodeGen::util::FILE_EXT;

35
my $enumfile = "$builddir/libgimp/gimpenums.h$FILE_EXT";
36 37 38 39
open ENUMFILE, "> $enumfile" or die "Can't open $enumfile: $!\n";

print ENUMFILE <<'LGPL';
/* LIBGIMP - The GIMP Library
40
 * Copyright (C) 1995-2003 Peter Mattis and Spencer Kimball
41
 *
42
 * This library is free software: you can redistribute it and/or
43
 * modify it under the terms of the GNU Lesser General Public
44
 * License as published by the Free Software Foundation; either
45
 * version 3 of the License, or (at your option) any later version.
46 47 48 49
 *
 * This library 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
50
 * Lesser General Public License for more details.
51
 *
52
 * You should have received a copy of the GNU Lesser General Public
53
 * License along with this library.  If not, see
54
 * <https://www.gnu.org/licenses/>.
55 56 57 58 59 60 61 62 63 64 65
 */

/* NOTE: This file is autogenerated by enumcode.pl */

LGPL

my $guard = "__GIMP_ENUMS_H__";
print ENUMFILE <<HEADER;
#ifndef $guard
#define $guard

66
G_BEGIN_DECLS
67 68 69 70

HEADER

foreach (sort keys %enums) {
71 72
    if (! ($enums{$_}->{header} =~ /libgimp/) &&
	! $enums{$_}->{external}) {
73 74
        my $gtype = $func = $_;

75 76
	for ($gtype) { s/Gimp//; s/([A-Z][^A-Z]+)/\U$1\E_/g; s/_$// }
	for ($func) { s/Gimp//; s/([A-Z][^A-Z]+)/\L$1\E_/g; s/_$// }
77 78 79

	print ENUMFILE "\n#define GIMP_TYPE_$gtype (gimp_$func\_get_type ())\n\n";
	print ENUMFILE "GType gimp_$func\_get_type (void) G_GNUC_CONST;\n\n";
80
	print ENUMFILE "typedef enum\n{\n";
81

82
	my $enum = $enums{$_}; my $body = "";
83

84 85
	foreach $symbol (@{$enum->{symbols}}) {
	    my $sym = $symbol;
86
	    $body .= "  $sym";
87 88 89 90 91 92 93 94
	    $body .= " = $enum->{mapping}->{$symbol}" if !$enum->{contig};
	    $body .= ",\n";
	}

	$body =~ s/,\n$//s;
	$body .= "\n} ";
	$body .= "$_;\n\n";
	print ENUMFILE $body
95 96 97 98
    }
}

print ENUMFILE <<HEADER;
99

100
void           gimp_enums_init           (void);
101 102 103 104

const gchar ** gimp_enums_get_type_names (gint *n_type_names);


105
G_END_DECLS
106

107 108 109 110
#endif /* $guard */
HEADER

close ENUMFILE;
111
&write_file($enumfile, "$destdir/libgimp");
112

113
$enumfile = "$builddir/libgimp/gimpenums.c.tail$FILE_EXT";
114 115 116 117 118 119
open ENUMFILE, "> $enumfile" or die "Can't open $enumfile: $!\n";

print ENUMFILE <<CODE;

typedef GType (* GimpGetTypeFunc) (void);

120
static const GimpGetTypeFunc get_type_funcs[] =
121 122 123
{
CODE

124
my $first = 1;
125 126 127 128
foreach (sort keys %enums) {
    if (! ($_ =~ /GimpUnit/)) {
	my $enum = $enums{$_};
	my $func = $_;
129
	my $gegl_enum = ($func =~ /Gegl/);
130

131 132
	for ($func) { s/Gimp//; s/Gegl//; s/PDB/Pdb/;
		      s/([A-Z][^A-Z]+)/\L$1\E_/g; s/_$// }
133

134
	print ENUMFILE ",\n" unless $first;
135 136 137 138 139 140

	if ($gegl_enum) {
	    print ENUMFILE "  gegl_$func\_get_type";
	} else {
	    print ENUMFILE "  gimp_$func\_get_type";
	}
141 142

	$first = 0;
143 144
    }
}
145
print ENUMFILE "\n" unless $first;
146 147 148 149

print ENUMFILE <<CODE;
};

150
static const gchar * const type_names[] =
151 152 153
{
CODE

154
$first = 1;
155 156 157 158 159
foreach (sort keys %enums) {
    if (! ($_ =~ /GimpUnit/)) {
	my $enum = $enums{$_};
	my $gtype = $_;

160 161 162 163
	print ENUMFILE ",\n" unless $first;
	print ENUMFILE "  \"$gtype\"";

        $first = 0;
164 165
    }
}
166
print ENUMFILE "\n" unless $first;
167 168 169 170

print ENUMFILE <<CODE;
};

171 172
static gboolean enums_initialized = FALSE;

173 174
#if 0
/*  keep around as documentation how to do compat enums  */
175
GType gimp_convert_dither_type_compat_get_type (void);
176
#endif
177

178
/**
179
 * gimp_enums_init:
180 181
 *
 * This function makes sure all the enum types are registered
182 183 184 185
 * with the #GType system. This is intended for use by language
 * bindings that need the symbols early, before gimp_main is run.
 * It's not necessary for plug-ins to call this directly, because
 * the normal plug-in initialization code will handle it implicitly.
186
 *
187
 * Since: 2.4
188
 **/
189
void
190
gimp_enums_init (void)
191
{
192
  const GimpGetTypeFunc *funcs = get_type_funcs;
193
#if 0
194
  GQuark                 quark;
195
#endif
196
  gint                   i;
197

198 199 200
  if (enums_initialized)
    return;

201
  for (i = 0; i < G_N_ELEMENTS (get_type_funcs); i++, funcs++)
202
    {
203
      GType type = (*funcs) ();
204 205 206

      g_type_class_ref (type);
    }
207

208 209 210
#if 0
  /*  keep around as documentation how to do compat enums  */

211 212 213
  /*  keep compat enum code in sync with app/app.c (app_libs_init)  */
  quark = g_quark_from_static_string ("gimp-compat-enum");

214 215
  g_type_set_qdata (GIMP_TYPE_CONVERT_DITHER_TYPE, quark,
		    (gpointer) gimp_convert_dither_type_compat_get_type ());
216
#endif
217

218
  gimp_base_compat_enums_init ();
219

220
  enums_initialized = TRUE;
221 222
}

223 224 225 226 227 228 229 230 231
/**
 * gimp_enums_get_type_names:
 * \@n_type_names: return location for the number of names
 *
 * This function gives access to the list of enums registered by libgimp.
 * The returned array is static and must not be modified.
 *
 * Return value: an array with type names
 *
232
 * Since: 2.2
233
 **/
234 235 236 237 238 239 240
const gchar **
gimp_enums_get_type_names (gint *n_type_names)
{
  g_return_val_if_fail (n_type_names != NULL, NULL);

  *n_type_names = G_N_ELEMENTS (type_names);

241
  return (const gchar **) type_names;
242 243 244 245
}
CODE

close ENUMFILE;
246
&write_file($enumfile, "$destdir/libgimp");