Blame gobject/gclosure.h

Packit ae235b
/* GObject - GLib Type, Object, Parameter and Signal Library
Packit ae235b
 * Copyright (C) 2000-2001 Red Hat, Inc.
Packit ae235b
 * Copyright (C) 2005 Imendio AB
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
#ifndef __G_CLOSURE_H__
Packit ae235b
#define __G_CLOSURE_H__
Packit ae235b
Packit ae235b
#if !defined (__GLIB_GOBJECT_H_INSIDE__) && !defined (GOBJECT_COMPILATION)
Packit ae235b
#error "Only <glib-object.h> can be included directly."
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include        <gobject/gtype.h>
Packit ae235b
Packit ae235b
G_BEGIN_DECLS
Packit ae235b
Packit ae235b
/* --- defines --- */
Packit ae235b
/**
Packit ae235b
 * G_CLOSURE_NEEDS_MARSHAL:
Packit ae235b
 * @closure: a #GClosure
Packit ae235b
 * 
Packit ae235b
 * Check if the closure still needs a marshaller. See g_closure_set_marshal().
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if a #GClosureMarshal marshaller has not yet been set on 
Packit ae235b
 * @closure.
Packit ae235b
 */
Packit ae235b
#define	G_CLOSURE_NEEDS_MARSHAL(closure) (((GClosure*) (closure))->marshal == NULL)
Packit ae235b
/**
Packit ae235b
 * G_CLOSURE_N_NOTIFIERS:
Packit ae235b
 * @cl: a #GClosure
Packit ae235b
 * 
Packit ae235b
 * Get the total number of notifiers connected with the closure @cl. 
Packit ae235b
 * The count includes the meta marshaller, the finalize and invalidate notifiers 
Packit ae235b
 * and the marshal guards. Note that each guard counts as two notifiers. 
Packit ae235b
 * See g_closure_set_meta_marshal(), g_closure_add_finalize_notifier(),
Packit ae235b
 * g_closure_add_invalidate_notifier() and g_closure_add_marshal_guards().
Packit ae235b
 *
Packit ae235b
 * Returns: number of notifiers
Packit ae235b
 */
Packit ae235b
#define	G_CLOSURE_N_NOTIFIERS(cl)	 (((cl)->n_guards << 1L) + \
Packit ae235b
                                          (cl)->n_fnotifiers + (cl)->n_inotifiers)
Packit ae235b
/**
Packit ae235b
 * G_CCLOSURE_SWAP_DATA:
Packit ae235b
 * @cclosure: a #GCClosure
Packit ae235b
 * 
Packit ae235b
 * Checks whether the user data of the #GCClosure should be passed as the
Packit ae235b
 * first parameter to the callback. See g_cclosure_new_swap().
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if data has to be swapped.
Packit ae235b
 */
Packit ae235b
#define	G_CCLOSURE_SWAP_DATA(cclosure)	 (((GClosure*) (cclosure))->derivative_flag)
Packit ae235b
/**
Packit ae235b
 * G_CALLBACK:
Packit ae235b
 * @f: a function pointer.
Packit ae235b
 * 
Packit ae235b
 * Cast a function pointer to a #GCallback.
Packit ae235b
 */
Packit ae235b
#define	G_CALLBACK(f)			 ((GCallback) (f))
Packit ae235b
Packit ae235b
Packit ae235b
/* -- typedefs --- */
Packit ae235b
typedef struct _GClosure		 GClosure;
Packit ae235b
typedef struct _GClosureNotifyData	 GClosureNotifyData;
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GCallback:
Packit ae235b
 * 
Packit ae235b
 * The type used for callback functions in structure definitions and function 
Packit ae235b
 * signatures. This doesn't mean that all callback functions must take no 
Packit ae235b
 * parameters and return void. The required signature of a callback function 
Packit ae235b
 * is determined by the context in which is used (e.g. the signal to which it 
Packit ae235b
 * is connected). Use G_CALLBACK() to cast the callback function to a #GCallback. 
Packit ae235b
 */
Packit ae235b
typedef void  (*GCallback)              (void);
Packit ae235b
/**
Packit ae235b
 * GClosureNotify:
Packit ae235b
 * @data: data specified when registering the notification callback
Packit ae235b
 * @closure: the #GClosure on which the notification is emitted
Packit ae235b
 * 
Packit ae235b
 * The type used for the various notification callbacks which can be registered
Packit ae235b
 * on closures.
Packit ae235b
 */
Packit ae235b
typedef void  (*GClosureNotify)		(gpointer	 data,
Packit ae235b
					 GClosure	*closure);
Packit ae235b
/**
Packit ae235b
 * GClosureMarshal:
Packit ae235b
 * @closure: the #GClosure to which the marshaller belongs
Packit ae235b
 * @return_value: (nullable): a #GValue to store the return
Packit ae235b
 *  value. May be %NULL if the callback of @closure doesn't return a
Packit ae235b
 *  value.
Packit ae235b
 * @n_param_values: the length of the @param_values array
Packit ae235b
 * @param_values: (array length=n_param_values): an array of
Packit ae235b
 *  #GValues holding the arguments on which to invoke the
Packit ae235b
 *  callback of @closure
Packit ae235b
 * @invocation_hint: (nullable): the invocation hint given as the
Packit ae235b
 *  last argument to g_closure_invoke()
Packit ae235b
 * @marshal_data: (nullable): additional data specified when
Packit ae235b
 *  registering the marshaller, see g_closure_set_marshal() and
Packit ae235b
 *  g_closure_set_meta_marshal()
Packit ae235b
 * 
Packit ae235b
 * The type used for marshaller functions.
Packit ae235b
 */
Packit ae235b
typedef void  (*GClosureMarshal)	(GClosure	*closure,
Packit ae235b
					 GValue         *return_value,
Packit ae235b
					 guint           n_param_values,
Packit ae235b
					 const GValue   *param_values,
Packit ae235b
					 gpointer        invocation_hint,
Packit ae235b
					 gpointer	 marshal_data);
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GVaClosureMarshal:
Packit ae235b
 * @closure: the #GClosure to which the marshaller belongs
Packit ae235b
 * @return_value: (nullable): a #GValue to store the return
Packit ae235b
 *  value. May be %NULL if the callback of @closure doesn't return a
Packit ae235b
 *  value.
Packit ae235b
 * @instance: (type GObject.TypeInstance): the instance on which the closure is
Packit ae235b
 *  invoked.
Packit ae235b
 * @args: va_list of arguments to be passed to the closure.
Packit ae235b
 * @marshal_data: (nullable): additional data specified when
Packit ae235b
 *  registering the marshaller, see g_closure_set_marshal() and
Packit ae235b
 *  g_closure_set_meta_marshal()
Packit ae235b
 * @n_params: the length of the @param_types array
Packit ae235b
 * @param_types: (array length=n_params): the #GType of each argument from
Packit ae235b
 *  @args.
Packit ae235b
 *
Packit ae235b
 * This is the signature of va_list marshaller functions, an optional
Packit ae235b
 * marshaller that can be used in some situations to avoid
Packit ae235b
 * marshalling the signal argument into GValues.
Packit ae235b
 */
Packit ae235b
typedef void (* GVaClosureMarshal) (GClosure *closure,
Packit ae235b
				    GValue   *return_value,
Packit ae235b
				    gpointer  instance,
Packit ae235b
				    va_list   args,
Packit ae235b
				    gpointer  marshal_data,
Packit ae235b
				    int       n_params,
Packit ae235b
				    GType    *param_types);
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GCClosure:
Packit ae235b
 * @closure: the #GClosure
Packit ae235b
 * @callback: the callback function
Packit ae235b
 * 
Packit ae235b
 * A #GCClosure is a specialization of #GClosure for C function callbacks.
Packit ae235b
 */
Packit ae235b
typedef struct _GCClosure		 GCClosure;
Packit ae235b
Packit ae235b
Packit ae235b
/* --- structures --- */
Packit ae235b
struct _GClosureNotifyData
Packit ae235b
{
Packit ae235b
  gpointer       data;
Packit ae235b
  GClosureNotify notify;
Packit ae235b
};
Packit ae235b
/**
Packit ae235b
 * GClosure:
Packit ae235b
 * @in_marshal: Indicates whether the closure is currently being invoked with 
Packit ae235b
 *  g_closure_invoke()
Packit ae235b
 * @is_invalid: Indicates whether the closure has been invalidated by 
Packit ae235b
 *  g_closure_invalidate()
Packit ae235b
 * 
Packit ae235b
 * A #GClosure represents a callback supplied by the programmer.
Packit ae235b
 */
Packit ae235b
struct _GClosure
Packit ae235b
{
Packit ae235b
  /*< private >*/
Packit ae235b
  volatile      	guint	 ref_count : 15;
Packit ae235b
  /* meta_marshal is not used anymore but must be zero for historical reasons
Packit ae235b
     as it was exposed in the G_CLOSURE_N_NOTIFIERS macro */
Packit ae235b
  volatile       	guint	 meta_marshal_nouse : 1;
Packit ae235b
  volatile       	guint	 n_guards : 1;
Packit ae235b
  volatile       	guint	 n_fnotifiers : 2;	/* finalization notifiers */
Packit ae235b
  volatile       	guint	 n_inotifiers : 8;	/* invalidation notifiers */
Packit ae235b
  volatile       	guint	 in_inotify : 1;
Packit ae235b
  volatile       	guint	 floating : 1;
Packit ae235b
  /*< protected >*/
Packit ae235b
  volatile         	guint	 derivative_flag : 1;
Packit ae235b
  /*< public >*/
Packit ae235b
  volatile       	guint	 in_marshal : 1;
Packit ae235b
  volatile       	guint	 is_invalid : 1;
Packit ae235b
Packit ae235b
  /*< private >*/	void   (*marshal)  (GClosure       *closure,
Packit ae235b
					    GValue /*out*/ *return_value,
Packit ae235b
					    guint           n_param_values,
Packit ae235b
					    const GValue   *param_values,
Packit ae235b
					    gpointer        invocation_hint,
Packit ae235b
					    gpointer	    marshal_data);
Packit ae235b
  /*< protected >*/	gpointer data;
Packit ae235b
Packit ae235b
  /*< private >*/	GClosureNotifyData *notifiers;
Packit ae235b
Packit ae235b
  /* invariants/constrains:
Packit ae235b
   * - ->marshal and ->data are _invalid_ as soon as ->is_invalid==TRUE
Packit ae235b
   * - invocation of all inotifiers occours prior to fnotifiers
Packit ae235b
   * - order of inotifiers is random
Packit ae235b
   *   inotifiers may _not_ free/invalidate parameter values (e.g. ->data)
Packit ae235b
   * - order of fnotifiers is random
Packit ae235b
   * - each notifier may only be removed before or during its invocation
Packit ae235b
   * - reference counting may only happen prior to fnotify invocation
Packit ae235b
   *   (in that sense, fnotifiers are really finalization handlers)
Packit ae235b
   */
Packit ae235b
};
Packit ae235b
/* closure for C function calls, callback() is the user function
Packit ae235b
 */
Packit ae235b
struct _GCClosure
Packit ae235b
{
Packit ae235b
  GClosure	closure;
Packit ae235b
  gpointer	callback;
Packit ae235b
};
Packit ae235b
Packit ae235b
Packit ae235b
/* --- prototypes --- */
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
GClosure* g_cclosure_new			(GCallback	callback_func,
Packit ae235b
						 gpointer	user_data,
Packit ae235b
						 GClosureNotify destroy_data);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
GClosure* g_cclosure_new_swap			(GCallback	callback_func,
Packit ae235b
						 gpointer	user_data,
Packit ae235b
						 GClosureNotify destroy_data);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
GClosure* g_signal_type_cclosure_new		(GType          itype,
Packit ae235b
						 guint          struct_offset);
Packit ae235b
Packit ae235b
Packit ae235b
/* --- prototypes --- */
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
GClosure* g_closure_ref				(GClosure	*closure);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
void	  g_closure_sink			(GClosure	*closure);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
void	  g_closure_unref			(GClosure	*closure);
Packit ae235b
/* intimidating */
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
GClosure* g_closure_new_simple			(guint		 sizeof_closure,
Packit ae235b
						 gpointer	 data);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
void	  g_closure_add_finalize_notifier	(GClosure       *closure,
Packit ae235b
						 gpointer	 notify_data,
Packit ae235b
						 GClosureNotify	 notify_func);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
void	  g_closure_remove_finalize_notifier	(GClosure       *closure,
Packit ae235b
						 gpointer	 notify_data,
Packit ae235b
						 GClosureNotify	 notify_func);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
void	  g_closure_add_invalidate_notifier	(GClosure       *closure,
Packit ae235b
						 gpointer	 notify_data,
Packit ae235b
						 GClosureNotify	 notify_func);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
void	  g_closure_remove_invalidate_notifier	(GClosure       *closure,
Packit ae235b
						 gpointer	 notify_data,
Packit ae235b
						 GClosureNotify	 notify_func);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
void	  g_closure_add_marshal_guards		(GClosure	*closure,
Packit ae235b
						 gpointer        pre_marshal_data,
Packit ae235b
						 GClosureNotify	 pre_marshal_notify,
Packit ae235b
						 gpointer        post_marshal_data,
Packit ae235b
						 GClosureNotify	 post_marshal_notify);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
void	  g_closure_set_marshal			(GClosure	*closure,
Packit ae235b
						 GClosureMarshal marshal);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
void	  g_closure_set_meta_marshal		(GClosure       *closure,
Packit ae235b
						 gpointer	 marshal_data,
Packit ae235b
						 GClosureMarshal meta_marshal);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
void	  g_closure_invalidate			(GClosure	*closure);
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
void	  g_closure_invoke			(GClosure 	*closure,
Packit ae235b
						 GValue	/*out*/	*return_value,
Packit ae235b
						 guint		 n_param_values,
Packit ae235b
						 const GValue	*param_values,
Packit ae235b
						 gpointer	 invocation_hint);
Packit ae235b
Packit ae235b
/* FIXME:
Packit ae235b
   OK:  data_object::destroy		-> closure_invalidate();
Packit ae235b
   MIS:	closure_invalidate()		-> disconnect(closure);
Packit ae235b
   MIS:	disconnect(closure)		-> (unlink) closure_unref();
Packit ae235b
   OK:	closure_finalize()		-> g_free (data_string);
Packit ae235b
Packit ae235b
   random remarks:
Packit ae235b
   - need marshaller repo with decent aliasing to base types
Packit ae235b
   - provide marshaller collection, virtually covering anything out there
Packit ae235b
*/
Packit ae235b
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
void g_cclosure_marshal_generic (GClosure     *closure,
Packit ae235b
                                 GValue       *return_gvalue,
Packit ae235b
                                 guint         n_param_values,
Packit ae235b
                                 const GValue *param_values,
Packit ae235b
                                 gpointer      invocation_hint,
Packit ae235b
                                 gpointer      marshal_data);
Packit ae235b
Packit ae235b
GLIB_AVAILABLE_IN_ALL
Packit ae235b
void g_cclosure_marshal_generic_va (GClosure *closure,
Packit ae235b
				    GValue   *return_value,
Packit ae235b
				    gpointer  instance,
Packit ae235b
				    va_list   args_list,
Packit ae235b
				    gpointer  marshal_data,
Packit ae235b
				    int       n_params,
Packit ae235b
				    GType    *param_types);
Packit ae235b
Packit ae235b
Packit ae235b
G_END_DECLS
Packit ae235b
Packit ae235b
#endif /* __G_CLOSURE_H__ */