Skip to content

Add enum helper #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion pyvips/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# basic defs and link to ffi


from pyvips import ffi, vips_lib, gobject_lib, _to_string, _to_bytes, Error
from pyvips import ffi, vips_lib, glib_lib, gobject_lib, _to_string, _to_bytes, Error


def leak_set(leak):
Expand Down Expand Up @@ -89,6 +89,22 @@ def type_map(gtype, fn):
return vips_lib.vips_type_map(gtype, cb, ffi.NULL, ffi.NULL)


def values_for_enum(gtype):
"""Get all values for a enum (gtype)."""

g_type_class = gobject_lib.g_type_class_ref(gtype)
g_enum_class = ffi.cast('GEnumClass *', g_type_class)

values = []

# -1 since we always have a "last" member.
for i in range(0, g_enum_class.n_values - 1):
value = _to_string(ffi.string(g_enum_class.values[i].value_nick))
values.append(value)

return values


__all__ = [
'leak_set',
'version',
Expand All @@ -100,4 +116,5 @@ def type_map(gtype, fn):
'type_name',
'type_map',
'type_from_name',
'values_for_enum'
]
24 changes: 22 additions & 2 deletions pyvips/decls.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,15 @@ def cdefs(features):
GType vips_interpretation_get_type (void);
GType vips_operation_flags_get_type (void);
GType vips_band_format_get_type (void);
GType vips_token_get_type (void);
GType vips_saveable_get_type (void);
GType vips_image_type_get_type (void);

typedef struct _GData GData;

typedef struct _GTypeClass GTypeClass;

typedef struct _GTypeInstance
{
typedef struct _GTypeInstance {
GTypeClass *g_class;
} GTypeInstance;

Expand All @@ -153,6 +155,24 @@ def cdefs(features):
unsigned int param_id;
} GParamSpec;

typedef struct _GEnumValue {
int value;

const char *value_name;
const char *value_nick;
} GEnumValue;

typedef struct _GEnumClass {
GTypeClass *g_type_class;

int minimum;
int maximum;
unsigned int n_values;
GEnumValue *values;
} GEnumClass;

void* g_type_class_ref (GType type);

void g_object_ref (void* object);
void g_object_unref (void* object);

Expand Down