1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * bean-extension.c
 * This file is part of libbean
 *
 * Copyright (C) 2010 Steve Frécinaux
 *
 * libbean is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * libbean 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA.
 */

#include "config.h"

#include "bean-extension.h"
#include "bean-introspection.h"

/**
 * SECTION:bean-extension
 * @short_description: Proxy for extensions.
 * @see_also: #BeanExtensionSet
 *
 * #BeanExtension is a proxy class used to access actual extensions
 * implemented using various languages.  As such, the application writer will
 * use #BeanExtension instances to call methods on extension provided by
 * loaded plugins.
 *
 * To properly use the proxy instances, you will need GObject-introspection
 * data for the #GType you want to use as an extension point.
 * For instance, if you wish to use #BeanActivatable, you will need to
 * put the following code excerpt in the engine initialization code, in order
 * to load the required "Bean" typelib:
 *
 * |[
 * GIRepository *repository = gi_repository_dup_default ();
 * gi_repository_require (repository, "Bean", "2.0", 0, NULL);
 * ]|
 *
 * You should proceed the same way for any namespace which provides types
 * you want to use as extension points. GObject-introspection data is required
 * for all the supported languages, even for C.
 *
 * #BeanExtension does not provide any way to access the underlying object.
 * The main reason is that some loaders may not rely on proper GObject
 * inheritance for the definition of extensions, and hence it would not be
 * possible for libbean to provide a functional GObject instance at all.
 * Another reason is that it makes reference counting issues easier to deal
 * with.
 *
 * See bean_extension_call() for more information.
 **/
GType
bean_extension_get_type (void)
{
  return G_TYPE_OBJECT;
}

static
G_DEFINE_QUARK (bean-extension-type, extension_type)

static GICallableInfo *
get_method_info (BeanExtension *exten,
                 const gchar   *method_name,
                 GType         *gtype)
{
  guint i;
  GType exten_type;
  GType *interfaces;
  GICallableInfo *method_info;

  /* Must prioritize the initial GType */
  exten_type = bean_extension_get_extension_type (exten);
  method_info = bean_gi_get_method_info (exten_type, method_name);

  if (method_info != NULL)
    {
      if (gtype != NULL)
        *gtype = exten_type;

      return method_info;
    }

  interfaces = g_type_interfaces (G_TYPE_FROM_INSTANCE (exten), NULL);

  for (i = 0; interfaces[i] != G_TYPE_INVALID; ++i)
    {
      method_info = bean_gi_get_method_info (interfaces[i], method_name);

      if (method_info != NULL)
        {
          if (gtype != NULL)
            *gtype = interfaces[i];

          break;
        }
    }

  if (method_info == NULL)
    g_warning ("Could not find the GType for method '%s'", method_name);

  g_free (interfaces);
  return method_info;
}

/**
 * bean_extension_get_extension_type:
 * @exten: A #BeanExtension.
 *
 * Get the #GType of the extension proxied by @exten.
 *
 * Return value: The #GType proxied by @exten.
 *
 * Deprecated: 1.2.
 */
GType
bean_extension_get_extension_type (BeanExtension *exten)
{
  return GPOINTER_TO_SIZE (g_object_get_qdata (G_OBJECT (exten),
                                               extension_type_quark ()));
}

/**
 * bean_extension_call:
 * @exten: A #BeanExtension.
 * @method_name: the name of the method that should be called.
 * @...: arguments for the method.
 *
 * Call a method of the object behind @extension.
 *
 * The arguments provided to this functions should be of the same type as
 * those defined in the #GInterface or #GObjectClass used as a base for the
 * proxied extension. They should be provided in the same order, and if its
 * return type is not void, then a pointer to a variable of that type should
 * be passed as the last argument.
 *
 * For instance, if the method prototype is:
 * |[ gint (*my_method) (MyClass *instance, const gchar *str, SomeObject *obj); ]|
 * you should call bean_extension_call() this way:
 * |[ bean_extension_call (extension, "my_method", "some_str", obj, &gint_var); ]|
 *
 * This function will not do anything if the introspection data for the proxied
 * object's class has not been loaded previously through gi_repository_require().
 *
 * Return value: %TRUE on successful call.
 *
 * Deprecated: 1.2: Use the object directly instead.
 */
gboolean
bean_extension_call (BeanExtension *exten,
                     const gchar   *method_name,
                     ...)
{
  va_list args;
  gboolean result;

  g_return_val_if_fail (BEAN_IS_EXTENSION (exten), FALSE);
  g_return_val_if_fail (method_name != NULL, FALSE);

  va_start (args, method_name);
  result = bean_extension_call_valist (exten, method_name, args);
  va_end (args);

  return result;
}

/**
 * bean_extension_call_valist:
 * @exten: A #BeanExtension.
 * @method_name: the name of the method that should be called.
 * @args: the arguments for the method.
 *
 * Call a method of the object behind @extension, using @args as arguments.
 *
 * See bean_extension_call() for more information.
 *
 * Return value: %TRUE on successful call.
 *
 * Deprecated: 1.2: Use the object directly instead.
 */
gboolean
bean_extension_call_valist (BeanExtension *exten,
                            const gchar   *method_name,
                            va_list        args)
{
  GICallableInfo *callable_info;
  GITypeInfo retval_info;
  GIArgument *gargs;
  GIArgument retval;
  gpointer retval_ptr;
  gboolean ret;
  gint n_args;

  g_return_val_if_fail (BEAN_IS_EXTENSION (exten), FALSE);
  g_return_val_if_fail (method_name != NULL, FALSE);

  callable_info = get_method_info (exten, method_name, NULL);

  /* Already warned */
  if (callable_info == NULL)
    return FALSE;

  n_args = gi_callable_info_get_n_args (callable_info);
  g_return_val_if_fail (n_args >= 0, FALSE);
  gargs = g_newa (GIArgument, n_args);
  bean_gi_valist_to_arguments (callable_info, args, gargs, &retval_ptr);

  ret = bean_extension_callv (exten, method_name, gargs, &retval);

  if (retval_ptr != NULL)
    {
      gi_callable_info_load_return_type (callable_info, &retval_info);
      bean_gi_argument_to_pointer (&retval_info, &retval, retval_ptr);
      gi_base_info_clear (&retval_info);
    }

  gi_base_info_unref ((GIBaseInfo *) callable_info);

  return ret;
}

/**
 * bean_extension_callv:
 * @exten: A #BeanExtension.
 * @method_name: the name of the method that should be called.
 * @args: the arguments for the method.
 * @return_value: the return falue for the method.
 *
 * Call a method of the object behind @extension, using @args as arguments.
 *
 * See bean_extension_call() for more information.
 *
 * Return value: %TRUE on successful call.
 *
 * Deprecated: 1.2: Use the object directly instead.
 */
gboolean
bean_extension_callv (BeanExtension *exten,
                      const gchar   *method_name,
                      GIArgument    *args,
                      GIArgument    *return_value)
{
  GICallableInfo *method_info;
  GType gtype;
  gboolean success;

  g_return_val_if_fail (BEAN_IS_EXTENSION (exten), FALSE);
  g_return_val_if_fail (method_name != NULL, FALSE);

  method_info = get_method_info (exten, method_name, &gtype);

  /* Already warned */
  if (method_info == NULL)
    return FALSE;

  success = bean_gi_method_call (G_OBJECT (exten), method_info, gtype,
                                 method_name, args, return_value);

  gi_base_info_unref (method_info);
  return success;
}