Glib
view release on metacpan or search on metacpan
*/
g_type_class_ref (new_type); /* leak */
/* vfuncs cause a bit of a problem, because the normal mechanisms of
* GObject don't give us a predefined way to handle them. here we
* provide a way to override them in each child class as it is
* derived. */
install_overrides (new_type);
/* fin */
=for apidoc
=for arg name package name for new enum type
=for arg ... new enum's values; see description.
=for signature Glib::Type->register_enum ($name, ...)
Register and initialize a new Glib::Enum type with the provided "values".
This creates a type properly registered GLib so that it can be used for
property and signal parameter or return types created with
C<< Glib::Type->register >> or C<Glib::Object::Subclass>.
The list of values is used to create the "nicknames" that are used in general
Perl code; the actual numeric values used at the C level are automatically
assigned, starting with 1. If you need to specify a particular numeric value
for a nick, use an array reference containing the nickname and the numeric
value, instead. You may mix and match the two styles.
Glib::Type->register_enum ('MyFoo::Bar',
'value-one', # assigned 1
'value-two', # assigned 2
['value-three' => 15 ], # explicit 15
['value-four' => 35 ], # explicit 35
'value-five', # assigned 5
);
If you use the array-ref form, beware: the code performs no validation
for unique values.
=cut
void
g_type_register_enum (class, name, ...)
const char * name
PREINIT:
int i = 0;
char * ctype_name;
SV * sv;
SV ** av2sv;
GType type;
GEnumValue * values = NULL;
CODE:
if (items-2 < 1)
croak ("Usage: Glib::Type->register_enums (new_package, LIST)\n"
" no values supplied");
/*
* we create a value table on the fly, and we can't free it without
* causing problems. the value table is stored in the type
* registration information, which conceivably may be called more
* than once per program (which is why we don't use a class_finalize
* to destroy it). unfortunately, there doesn't appear to be a
* g_enum_register_dynamic().
* this means we will also leak the nickname strings, which must
* be duplicated to keep them alive (perl will reuse those strings).
*
* note also that we don't clean up very well when things go wrong.
* we build up the structure as we go, and an exception in the middle
* will leak everything done up to that point. we could clean it up,
* but it will make things uglier than they already are, and if
* your script can't register the enums properly, it probably won't
* live much longer.
*/
values = g_new0 (GEnumValue, items-1); /* leak (see above) */
for (i = 0; i < items-2; i++)
{
sv = (SV*)ST (i+2);
/* default to the i based numbering */
values[i].value = i + 1;
if (gperl_sv_is_array_ref (sv))
{
/* [ name => value ] syntax */
AV * av = (AV*)SvRV(sv);
/* value_name */
av2sv = av_fetch (av, 0, 0);
if (av2sv && gperl_sv_is_defined (*av2sv))
values[i].value_name = SvPV_nolen (*av2sv);
else
croak ("invalid enum name and value pair, no name provided");
/* custom value */
av2sv = av_fetch (av, 1, 0);
if (av2sv && gperl_sv_is_defined (*av2sv))
values[i].value = SvIV (*av2sv);
}
else if (gperl_sv_is_defined (sv))
{
/* name syntax */
values[i].value_name = SvPV_nolen (sv);
}
else
croak ("invalid type flag name");
/* make sure that the nickname stays alive as long as the
* type is registered. */
values[i].value_name = g_strdup (values[i].value_name);
/* let the nick and name match. there are few uses for the
* name, anyway. */
values[i].value_nick = values[i].value_name;
}
ctype_name = sanitize_package_name (name);
type = g_enum_register_static (ctype_name, values);
gperl_register_fundamental (type, name);
g_free (ctype_name);
=for apidoc
=for arg name package name of new flags type
=for arg ... flag values, see discussion.
=for signature Glib::Type->register_flags ($name, ...)
Register and initialize a new Glib::Flags type with the provided "values".
This creates a type properly registered GLib so that it can be used for
property and signal parameter or return types created with
C<< Glib::Type->register >> or C<Glib::Object::Subclass>.
The list of values is used to create the "nicknames" that are used in general
Perl code; the actual numeric values used at the C level are automatically
assigned, of the form 1<<i, starting with i = 0. If you need to specify a
particular numeric value for a nick, use an array reference containing the
nickname and the numeric value, instead. You may mix and match the two styles.
Glib::Type->register_flags ('MyFoo::Baz',
'value-one', # assigned 1<<0
'value-two', # assigned 1<<1
['value-three' => 1<<10 ], # explicit 1<<10
['value-four' => 0x0f ], # explicit 0x0f
'value-five', # assigned 1<<4
);
If you use the array-ref form, beware: the code performs no validation
for unique values.
=cut
void
g_type_register_flags (class, name, ...)
const char * name
PREINIT:
int i = 0;
char * ctype_name;
SV * sv;
SV ** av2sv;
GType type;
GFlagsValue * values = NULL;
CODE:
if (items-2 < 1)
croak ("Usage: Glib::Type->register_flags (new_package, LIST)\n"
" no values supplied");
/* see the notes about memory management in register_enums -- they
* all apply here. we can't combine the implementations because
* GEnumValue and GFlagsValue are not typedefed together. */
values = g_new0 (GFlagsValue, items-1);
for (i = 0; i < items-2; i++)
{
sv = (SV*)ST (i+2);
/* default to the i based numbering */
values[i].value = 1 << i;
if (gperl_sv_is_array_ref (sv))
{
/* [ name => value ] syntax */
AV * av = (AV*)SvRV(sv);
/* value_name */
av2sv = av_fetch (av, 0, 0);
if (av2sv && gperl_sv_is_defined (*av2sv))
values[i].value_name = SvPV_nolen (*av2sv);
else
croak ("invalid flag name and value pair, no name provided");
/* custom value */
av2sv = av_fetch (av, 1, 0);
if (av2sv && gperl_sv_is_defined (*av2sv))
values[i].value = SvIV (*av2sv);
}
else if (gperl_sv_is_defined (sv))
{
/* name syntax */
values[i].value_name = SvPV_nolen (sv);
}
else
croak ("invalid type flag name");
/* make sure that the nickname stays alive as long as the
* type is registered. */
values[i].value_name = g_strdup (values[i].value_name);
/* let the nick and name match. there are few uses for the
* name, anyway. */
values[i].value_nick = values[i].value_name;
}
ctype_name = sanitize_package_name (name);
type = g_flags_register_static (ctype_name, values);
gperl_register_fundamental (type, name);
g_free (ctype_name);
=for apidoc
List the ancestry of I<package>, as seen by the GLib type system. The
important difference is that GLib's type system implements only single
inheritance, whereas Perl's @ISA allows multiple inheritance.
This returns the package names of the ancestral types in reverse order, with
the root of the tree at the end of the list.
See also L<list_interfaces ()|/"list = Glib::Type-E<gt>B<list_interfaces> ($package)">.
=cut
void
list_ancestors (class, package)
gchar * package
PREINIT:
GType package_gtype;
GType parent_gtype;
const char * pkg;
PPCODE:
package_gtype = gperl_type_from_package (package);
XPUSHs (sv_2mortal (newSVpv (package, 0)));
if (!package_gtype)
croak ("%s is not registered with either GPerl or GLib",
package);
parent_gtype = g_type_parent (package_gtype);
while (parent_gtype)
{
pkg = gperl_package_from_type (parent_gtype);
if (!pkg)
croak("problem looking up parent package name, "
"gtype %lu", parent_gtype);
XPUSHs (sv_2mortal (newSVpv (pkg, 0)));
parent_gtype = g_type_parent (parent_gtype);
}
=for apidoc
List the GInterfaces implemented by the type associated with I<package>.
The interfaces are returned as package names.
=cut
void
list_interfaces (class, package)
gchar * package
( run in 1.396 second using v1.01-cache-2.11-cpan-39bf76dae61 )