예제 #1
0
GType
gsk_load_type_introspective (const char  *type_name,
			     gpointer     unused,
			     GError     **error)
{
  static gboolean self_inited = FALSE;
  static GModule *self_module = NULL;
  guint index = 0;
  GType type;
  GString *func_name;
  gpointer symbol;

  (void) unused;

  type = g_type_from_name (type_name);
  if (type != G_TYPE_INVALID)
    return type;

  /* Transform `GObject' into `g_object_get_type',
   * which should be a function that returns a GType,
   * if we're lucky...
   */
  func_name = g_string_new ("");
  while (type_name[index] != '\0')
    {
      if ('A' <= type_name[index] && type_name[index] <= 'Z')
	{
	  if (index > 0)
	    g_string_append_c (func_name, '_');
	  g_string_append_c (func_name, g_ascii_tolower (type_name[index]));
	}
      else
	g_string_append_c (func_name, type_name[index]);
      ++index;
    }
  g_string_append (func_name, "_get_type");

  if (!self_inited)
    {
      self_inited = TRUE;
      self_module = g_module_open (NULL, G_MODULE_BIND_LAZY);
      if (self_module == NULL)
	{
	  g_set_error (error,
		       GSK_G_ERROR_DOMAIN,
		       GSK_ERROR_UNKNOWN,
		       "g_module_open: %s",
		       g_module_error ());
	  goto DONE;
	}
    }
  if (g_module_symbol (self_module, func_name->str, &symbol))
    {
      GType (*func) () = (GType (*)()) symbol;
      const char *name;
      GTypeClass *klass;

      type = (*func) ();
      name = g_type_name (type);
      if (name == NULL)
	{
	  g_set_error (error,
		       GSK_G_ERROR_DOMAIN,
		       GSK_ERROR_UNKNOWN,
		       "called %s, didn't get a valid GType",
		       func_name->str);
	  type = G_TYPE_INVALID;
	  goto DONE;
	}
      if (strcmp (name, type_name) != 0)
	{
	  g_set_error (error,
		       GSK_G_ERROR_DOMAIN,
		       GSK_ERROR_UNKNOWN,
		       "called %s: got %s instead of %s",
		       func_name->str,
		       name,
		       type_name);
	  type = G_TYPE_INVALID;
	  goto DONE;
	}

      /* Sometimes the registrations in the class_init are vital. */
      klass = g_type_class_ref (type);
      g_type_class_unref (klass);
    }
  else
    {
      g_set_error (error,
		   GSK_G_ERROR_DOMAIN,
		   GSK_ERROR_UNKNOWN,
		   "couldn't find symbol %s: %s",
		   func_name->str,
		   g_module_error ());
    }

DONE:
  g_string_free (func_name, TRUE);
  return type;
}
예제 #2
0
#include "Type.h"
const GType GType::ACOUSTIC = GType("ACOUSTIC");
const GType GType::ELECTRIC = GType("ELECTRIC");

std::ostream& operator<<(std::ostream& os, const GType &r)
{
    os << r.toString();
    return os;
}