Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

Commit

Permalink
Implement support for GError type. This allows us to do something like:
Browse files Browse the repository at this point in the history
try {
	$pixbuf = GdkPixbuf::new_from_file($image_file);
} catch (PhpGtkGErrorException $e) {
	echo $e->message, $e->code,"\n";
}
  • Loading branch information
Andrei Zmievski committed Mar 1, 2005
1 parent f05c825 commit e8caaff
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 10 deletions.
18 changes: 16 additions & 2 deletions generator/arg_types.php
Expand Up @@ -147,7 +147,7 @@ function write_from_prop($name, $type)
class None_Arg extends Arg_Type {
function write_return($type, $owns_return, $info)
{
return "\tRETVAL_NULL();";
$this->post_code[] = "\tRETVAL_NULL();";
}
}
/* }}} */
Expand Down Expand Up @@ -829,6 +829,19 @@ function write_return($type, $owns_return, $info)
}
/* }}} */

/* {{{ GError_Arg */
class GError_Arg extends Arg_Type {
function write_param($type, $name, $default, $null_ok, $info)
{
$info->var_list->add('GError', '*' . $name . ' = NULL');
$info->arg_list[] = '&' . $name;
$info->post_code[] = " if (phpg_handle_gerror(&$name TSRMLS_CC)) {\n" .
" return;\n" .
" }\n";
}
}
/* }}} */

/* {{{ Arg_Matcher */
class Arg_Matcher {
var $arg_types = array();
Expand Down Expand Up @@ -978,7 +991,8 @@ function get($type)
#$matcher->register_boxed('GtkStyle', 'gtk_style');

$matcher->register_object('GObject', 'G_TYPE_OBJECT');
$matcher->register('GType', new GType_Arg());
$matcher->register('GType', new GType_Arg);
$matcher->register('GError**', new GError_Arg);

/* }}} */

Expand Down
9 changes: 3 additions & 6 deletions generator/templates.php
Expand Up @@ -51,8 +51,7 @@ class Templates {
const constructor_body = "
static PHP_METHOD(%(class), %(name))
{
%(var_list)
GObject *wrapped_obj;
%(var_list)\tGObject *wrapped_obj;
if (!php_gtk_parse_args(ZEND_NUM_ARGS(), \"%(specs)\"%(parse_list))) {
PHPG_THROW_CONSTRUCT_EXCEPTION(%(class));
Expand All @@ -69,8 +68,7 @@ class Templates {
const static_constructor_body = "
static PHP_METHOD(%(class), %(name))
{
%(var_list)
GObject *wrapped_obj;
%(var_list)\tGObject *wrapped_obj;
if (!php_gtk_parse_args(ZEND_NUM_ARGS(), \"%(specs)\"%(parse_list))) {
PHPG_THROW_CONSTRUCT_EXCEPTION(%(class));
Expand Down Expand Up @@ -107,8 +105,7 @@ class Templates {
const boxed_static_constructor_body = "
static PHP_METHOD(%(class), %(name))
{
%(var_list)
%(class) *wrapped_obj = NULL;
%(var_list)\t%(class) *wrapped_obj = NULL;
if (!php_gtk_parse_args(ZEND_NUM_ARGS(), \"%(specs)\"%(parse_list))) {
PHPG_THROW_CONSTRUCT_EXCEPTION(%(class));
Expand Down
7 changes: 5 additions & 2 deletions main/php_gtk.h
Expand Up @@ -172,6 +172,7 @@ extern GQuark phpg_class_key;
extern PHP_GTK_API zend_class_entry *phpg_generic_exception;
extern PHP_GTK_API zend_class_entry *phpg_construct_exception;
extern PHP_GTK_API zend_class_entry *phpg_type_exception;
extern PHP_GTK_API zend_class_entry *phpg_gerror_exception;

extern PHP_GTK_API zend_object_handlers php_gtk_handlers;

Expand Down Expand Up @@ -274,13 +275,15 @@ PHP_GTK_API zval* php_gtk_simple_signal_callback(GtkObject *o, gpointer data, zv
return retval; \
} while (0)

void phpg_register_exceptions();
zend_bool phpg_handle_gerror(GError **error TSRMLS_DC);
PHP_GTK_API zval* phpg_throw_gerror_exception(const char *domain, long code, const char *message TSRMLS_DC);

PHP_GTK_API PHP_FUNCTION(no_constructor);
PHP_GTK_API PHP_FUNCTION(no_direct_constructor);

extern char *php_gtk_zval_type_name(zval *arg);

void phpg_register_exceptions();

PHP_GTK_API void phpg_register_enum(GType gtype, const char *strip_prefix, zend_class_entry *ce);
PHP_GTK_API void phpg_register_flags(GType gtype, const char *strip_prefix, zend_class_entry *ce);

Expand Down
27 changes: 27 additions & 0 deletions main/phpg_exceptions.c
Expand Up @@ -25,6 +25,7 @@
PHP_GTK_API zend_class_entry *phpg_generic_exception = NULL;
PHP_GTK_API zend_class_entry *phpg_construct_exception = NULL;
PHP_GTK_API zend_class_entry *phpg_type_exception = NULL;
PHP_GTK_API zend_class_entry *phpg_gerror_exception = NULL;

void phpg_register_exceptions()
{
Expand All @@ -45,6 +46,32 @@ void phpg_register_exceptions()
phpg_type_exception = zend_register_internal_class_ex(&ce, zend_exception_get_default(), NULL TSRMLS_CC);
phpg_type_exception->ce_flags |= ZEND_ACC_FINAL;
phpg_type_exception->constructor->common.fn_flags |= ZEND_ACC_PROTECTED;

INIT_CLASS_ENTRY(ce, "PhpGtkGErrorException", NULL);
phpg_gerror_exception = zend_register_internal_class_ex(&ce, zend_exception_get_default(), NULL TSRMLS_CC);
phpg_gerror_exception->ce_flags |= ZEND_ACC_FINAL;
phpg_gerror_exception->constructor->common.fn_flags |= ZEND_ACC_PROTECTED;
zend_declare_property_string(phpg_gerror_exception, "domain", sizeof("domain")-1, "", ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_string(phpg_gerror_exception, "message", sizeof("message")-1, "", ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_string(phpg_gerror_exception, "code", sizeof("code")-1, "", ZEND_ACC_PUBLIC TSRMLS_CC);
}

PHP_GTK_API zval* phpg_throw_gerror_exception(const char *domain, long code, const char *message TSRMLS_DC)
{
zval *exc;

MAKE_STD_ZVAL(exc);
object_init_ex(exc, phpg_gerror_exception);
zend_update_property_string(phpg_gerror_exception, exc, "domain", sizeof("domain")-1, (char *)domain TSRMLS_CC);
zend_update_property_long(phpg_gerror_exception, exc, "code", sizeof("code")-1, code TSRMLS_CC);

if (message) {
zend_update_property_string(phpg_gerror_exception, exc, "message", sizeof("message")-1, (char *)message TSRMLS_CC);
}

zend_throw_exception_internal(exc TSRMLS_CC);

return exc;
}

#endif /* HAVE_PHP_GTK */
13 changes: 13 additions & 0 deletions main/phpg_gobject.c
Expand Up @@ -528,6 +528,19 @@ PHP_GTK_API void phpg_get_properties_helper(zval *object, HashTable *ht TSRMLS_D
}
/* }}} */

zend_bool phpg_handle_gerror(GError **error TSRMLS_DC)
{
if (error == NULL || *error == NULL)
return FALSE;

phpg_throw_gerror_exception(g_quark_to_string((*error)->domain),
(*error)->code,
(*error)->message TSRMLS_CC);
g_clear_error(error);

return TRUE;
}

/*
* GObject PHP class definition
*/
Expand Down

0 comments on commit e8caaff

Please sign in to comment.