girepository: Use G_DECLARE_FINAL_TYPE for GIRepository

Might as well move to a modern way of declaring GObjects. This means
that `GIRepository` is no longer derivable, but it would be a bit
unexpected if anyone was deriving from it.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>

Helps: #3155
This commit is contained in:
Philip Withnall 2024-01-25 23:09:16 +00:00
parent e301782865
commit db0f69fbb8
2 changed files with 51 additions and 73 deletions

View File

@ -93,8 +93,10 @@ gtype_interface_cache_free (gpointer data)
g_free (cache); g_free (cache);
} }
struct _GIRepositoryPrivate struct _GIRepository
{ {
GObject parent;
GHashTable *typelibs; /* (string) namespace -> GITypelib */ GHashTable *typelibs; /* (string) namespace -> GITypelib */
GHashTable *lazy_typelibs; /* (string) namespace-version -> GITypelib */ GHashTable *lazy_typelibs; /* (string) namespace-version -> GITypelib */
GHashTable *info_by_gtype; /* GType -> GIBaseInfo */ GHashTable *info_by_gtype; /* GType -> GIBaseInfo */
@ -106,7 +108,7 @@ struct _GIRepositoryPrivate
size_t cached_n_shared_libraries; /* length of @cached_shared_libraries, not including NULL terminator */ size_t cached_n_shared_libraries; /* length of @cached_shared_libraries, not including NULL terminator */
}; };
G_DEFINE_TYPE_WITH_CODE (GIRepository, gi_repository, G_TYPE_OBJECT, G_ADD_PRIVATE (GIRepository)); G_DEFINE_TYPE (GIRepository, gi_repository, G_TYPE_OBJECT);
#ifdef G_PLATFORM_WIN32 #ifdef G_PLATFORM_WIN32
@ -146,28 +148,27 @@ DllMain (HINSTANCE hinstDLL,
static void static void
gi_repository_init (GIRepository *repository) gi_repository_init (GIRepository *repository)
{ {
repository->priv = gi_repository_get_instance_private (repository); repository->typelibs
repository->priv->typelibs
= g_hash_table_new_full (g_str_hash, g_str_equal, = g_hash_table_new_full (g_str_hash, g_str_equal,
(GDestroyNotify) g_free, (GDestroyNotify) g_free,
(GDestroyNotify) gi_typelib_free); (GDestroyNotify) gi_typelib_free);
repository->priv->lazy_typelibs repository->lazy_typelibs
= g_hash_table_new_full (g_str_hash, g_str_equal, = g_hash_table_new_full (g_str_hash, g_str_equal,
(GDestroyNotify) g_free, (GDestroyNotify) g_free,
(GDestroyNotify) NULL); (GDestroyNotify) NULL);
repository->priv->info_by_gtype repository->info_by_gtype
= g_hash_table_new_full (g_direct_hash, g_direct_equal, = g_hash_table_new_full (g_direct_hash, g_direct_equal,
(GDestroyNotify) NULL, (GDestroyNotify) NULL,
(GDestroyNotify) gi_base_info_unref); (GDestroyNotify) gi_base_info_unref);
repository->priv->info_by_error_domain repository->info_by_error_domain
= g_hash_table_new_full (g_direct_hash, g_direct_equal, = g_hash_table_new_full (g_direct_hash, g_direct_equal,
(GDestroyNotify) NULL, (GDestroyNotify) NULL,
(GDestroyNotify) gi_base_info_unref); (GDestroyNotify) gi_base_info_unref);
repository->priv->interfaces_for_gtype repository->interfaces_for_gtype
= g_hash_table_new_full (g_direct_hash, g_direct_equal, = g_hash_table_new_full (g_direct_hash, g_direct_equal,
(GDestroyNotify) NULL, (GDestroyNotify) NULL,
(GDestroyNotify) gtype_interface_cache_free); (GDestroyNotify) gtype_interface_cache_free);
repository->priv->unknown_gtypes = g_hash_table_new (NULL, NULL); repository->unknown_gtypes = g_hash_table_new (NULL, NULL);
} }
static void static void
@ -175,14 +176,14 @@ gi_repository_finalize (GObject *object)
{ {
GIRepository *repository = GI_REPOSITORY (object); GIRepository *repository = GI_REPOSITORY (object);
g_hash_table_destroy (repository->priv->typelibs); g_hash_table_destroy (repository->typelibs);
g_hash_table_destroy (repository->priv->lazy_typelibs); g_hash_table_destroy (repository->lazy_typelibs);
g_hash_table_destroy (repository->priv->info_by_gtype); g_hash_table_destroy (repository->info_by_gtype);
g_hash_table_destroy (repository->priv->info_by_error_domain); g_hash_table_destroy (repository->info_by_error_domain);
g_hash_table_destroy (repository->priv->interfaces_for_gtype); g_hash_table_destroy (repository->interfaces_for_gtype);
g_hash_table_destroy (repository->priv->unknown_gtypes); g_hash_table_destroy (repository->unknown_gtypes);
g_clear_pointer (&repository->priv->cached_shared_libraries, g_strfreev); g_clear_pointer (&repository->cached_shared_libraries, g_strfreev);
(* G_OBJECT_CLASS (gi_repository_parent_class)->finalize) (G_OBJECT (repository)); (* G_OBJECT_CLASS (gi_repository_parent_class)->finalize) (G_OBJECT (repository));
} }
@ -374,10 +375,10 @@ get_registered_status (GIRepository *repository,
repository = get_repository (repository); repository = get_repository (repository);
if (lazy_status) if (lazy_status)
*lazy_status = FALSE; *lazy_status = FALSE;
typelib = g_hash_table_lookup (repository->priv->typelibs, namespace); typelib = g_hash_table_lookup (repository->typelibs, namespace);
if (typelib) if (typelib)
return check_version_conflict (typelib, namespace, version, version_conflict); return check_version_conflict (typelib, namespace, version, version_conflict);
typelib = g_hash_table_lookup (repository->priv->lazy_typelibs, namespace); typelib = g_hash_table_lookup (repository->lazy_typelibs, namespace);
if (!typelib) if (!typelib)
return NULL; return NULL;
if (lazy_status) if (lazy_status)
@ -453,9 +454,9 @@ register_internal (GIRepository *repository,
if (lazy) if (lazy)
{ {
g_assert (!g_hash_table_lookup (repository->priv->lazy_typelibs, g_assert (!g_hash_table_lookup (repository->lazy_typelibs,
namespace)); namespace));
g_hash_table_insert (repository->priv->lazy_typelibs, g_hash_table_insert (repository->lazy_typelibs,
build_typelib_key (namespace, source), (void *)typelib); build_typelib_key (namespace, source), (void *)typelib);
} }
else else
@ -468,20 +469,20 @@ register_internal (GIRepository *repository,
return NULL; return NULL;
/* Check if we are transitioning from lazily loaded state */ /* Check if we are transitioning from lazily loaded state */
if (g_hash_table_lookup_extended (repository->priv->lazy_typelibs, if (g_hash_table_lookup_extended (repository->lazy_typelibs,
namespace, namespace,
(gpointer)&key, &value)) (gpointer)&key, &value))
g_hash_table_remove (repository->priv->lazy_typelibs, key); g_hash_table_remove (repository->lazy_typelibs, key);
else else
key = build_typelib_key (namespace, source); key = build_typelib_key (namespace, source);
g_hash_table_insert (repository->priv->typelibs, g_hash_table_insert (repository->typelibs,
g_steal_pointer (&key), g_steal_pointer (&key),
(void *)typelib); (void *)typelib);
} }
/* These types might be resolved now, clear the cache */ /* These types might be resolved now, clear the cache */
g_hash_table_remove_all (repository->priv->unknown_gtypes); g_hash_table_remove_all (repository->unknown_gtypes);
return namespace; return namespace;
} }
@ -887,13 +888,13 @@ gi_repository_find_by_gtype (GIRepository *repository,
repository = get_repository (repository); repository = get_repository (repository);
cached = g_hash_table_lookup (repository->priv->info_by_gtype, cached = g_hash_table_lookup (repository->info_by_gtype,
(gpointer)gtype); (gpointer)gtype);
if (cached != NULL) if (cached != NULL)
return gi_base_info_ref (cached); return gi_base_info_ref (cached);
if (g_hash_table_contains (repository->priv->unknown_gtypes, (gpointer)gtype)) if (g_hash_table_contains (repository->unknown_gtypes, (gpointer)gtype))
return NULL; return NULL;
data.gtype_name = g_type_name (gtype); data.gtype_name = g_type_name (gtype);
@ -906,9 +907,9 @@ gi_repository_find_by_gtype (GIRepository *repository,
* target type does not have this typelib's C prefix. Use this * target type does not have this typelib's C prefix. Use this
* assumption as our first attempt at locating the DirEntry. * assumption as our first attempt at locating the DirEntry.
*/ */
entry = find_by_gtype (repository->priv->typelibs, &data, TRUE); entry = find_by_gtype (repository->typelibs, &data, TRUE);
if (entry == NULL) if (entry == NULL)
entry = find_by_gtype (repository->priv->lazy_typelibs, &data, TRUE); entry = find_by_gtype (repository->lazy_typelibs, &data, TRUE);
/* Not ever class library necessarily specifies a correct c_prefix, /* Not ever class library necessarily specifies a correct c_prefix,
* so take a second pass. This time we will try a global lookup, * so take a second pass. This time we will try a global lookup,
@ -916,9 +917,9 @@ gi_repository_find_by_gtype (GIRepository *repository,
* See http://bugzilla.gnome.org/show_bug.cgi?id=564016 * See http://bugzilla.gnome.org/show_bug.cgi?id=564016
*/ */
if (entry == NULL) if (entry == NULL)
entry = find_by_gtype (repository->priv->typelibs, &data, FALSE); entry = find_by_gtype (repository->typelibs, &data, FALSE);
if (entry == NULL) if (entry == NULL)
entry = find_by_gtype (repository->priv->lazy_typelibs, &data, FALSE); entry = find_by_gtype (repository->lazy_typelibs, &data, FALSE);
if (entry != NULL) if (entry != NULL)
{ {
@ -926,14 +927,14 @@ gi_repository_find_by_gtype (GIRepository *repository,
repository, repository,
NULL, data.result_typelib, entry->offset); NULL, data.result_typelib, entry->offset);
g_hash_table_insert (repository->priv->info_by_gtype, g_hash_table_insert (repository->info_by_gtype,
(gpointer) gtype, (gpointer) gtype,
gi_base_info_ref (cached)); gi_base_info_ref (cached));
return cached; return cached;
} }
else else
{ {
g_hash_table_add (repository->priv->unknown_gtypes, (gpointer) gtype); g_hash_table_add (repository->unknown_gtypes, (gpointer) gtype);
return NULL; return NULL;
} }
} }
@ -1027,7 +1028,7 @@ gi_repository_find_by_error_domain (GIRepository *repository,
repository = get_repository (repository); repository = get_repository (repository);
cached = g_hash_table_lookup (repository->priv->info_by_error_domain, cached = g_hash_table_lookup (repository->info_by_error_domain,
GUINT_TO_POINTER (domain)); GUINT_TO_POINTER (domain));
if (cached != NULL) if (cached != NULL)
@ -1038,9 +1039,9 @@ gi_repository_find_by_error_domain (GIRepository *repository,
data.result_typelib = NULL; data.result_typelib = NULL;
data.result = NULL; data.result = NULL;
g_hash_table_foreach (repository->priv->typelibs, find_by_error_domain_foreach, &data); g_hash_table_foreach (repository->typelibs, find_by_error_domain_foreach, &data);
if (data.result == NULL) if (data.result == NULL)
g_hash_table_foreach (repository->priv->lazy_typelibs, find_by_error_domain_foreach, &data); g_hash_table_foreach (repository->lazy_typelibs, find_by_error_domain_foreach, &data);
if (data.result != NULL) if (data.result != NULL)
{ {
@ -1048,7 +1049,7 @@ gi_repository_find_by_error_domain (GIRepository *repository,
repository, repository,
NULL, data.result_typelib, data.result->offset); NULL, data.result_typelib, data.result->offset);
g_hash_table_insert (repository->priv->info_by_error_domain, g_hash_table_insert (repository->info_by_error_domain,
GUINT_TO_POINTER (domain), GUINT_TO_POINTER (domain),
gi_base_info_ref ((GIBaseInfo *) cached)); gi_base_info_ref ((GIBaseInfo *) cached));
return cached; return cached;
@ -1091,7 +1092,7 @@ gi_repository_get_object_gtype_interfaces (GIRepository *repository,
repository = get_repository (repository); repository = get_repository (repository);
cache = g_hash_table_lookup (repository->priv->interfaces_for_gtype, cache = g_hash_table_lookup (repository->interfaces_for_gtype,
(void *) gtype); (void *) gtype);
if (cache == NULL) if (cache == NULL)
{ {
@ -1127,7 +1128,7 @@ gi_repository_get_object_gtype_interfaces (GIRepository *repository,
cache->interfaces[i] = iter->data; cache->interfaces[i] = iter->data;
g_list_free (interface_infos); g_list_free (interface_infos);
g_hash_table_insert (repository->priv->interfaces_for_gtype, (gpointer) gtype, g_hash_table_insert (repository->interfaces_for_gtype, (gpointer) gtype,
cache); cache);
g_free (interfaces); g_free (interfaces);
@ -1167,8 +1168,8 @@ gi_repository_get_loaded_namespaces (GIRepository *repository)
repository = get_repository (repository); repository = get_repository (repository);
g_hash_table_foreach (repository->priv->typelibs, collect_namespaces, &list); g_hash_table_foreach (repository->typelibs, collect_namespaces, &list);
g_hash_table_foreach (repository->priv->lazy_typelibs, collect_namespaces, &list); g_hash_table_foreach (repository->lazy_typelibs, collect_namespaces, &list);
names = g_malloc0 (sizeof (char *) * (g_list_length (list) + 1)); names = g_malloc0 (sizeof (char *) * (g_list_length (list) + 1));
i = 0; i = 0;
@ -1264,21 +1265,21 @@ gi_repository_get_shared_libraries (GIRepository *repository,
} }
/* Populate the cache. */ /* Populate the cache. */
if (repository->priv->cached_shared_libraries == NULL) if (repository->cached_shared_libraries == NULL)
{ {
const char *comma_separated = gi_typelib_get_string (typelib, header->shared_library); const char *comma_separated = gi_typelib_get_string (typelib, header->shared_library);
if (comma_separated != NULL && *comma_separated != '\0') if (comma_separated != NULL && *comma_separated != '\0')
{ {
repository->priv->cached_shared_libraries = g_strsplit (comma_separated, ",", -1); repository->cached_shared_libraries = g_strsplit (comma_separated, ",", -1);
repository->priv->cached_n_shared_libraries = g_strv_length (repository->priv->cached_shared_libraries); repository->cached_n_shared_libraries = g_strv_length (repository->cached_shared_libraries);
} }
} }
if (out_n_elements != NULL) if (out_n_elements != NULL)
*out_n_elements = repository->priv->cached_n_shared_libraries; *out_n_elements = repository->cached_n_shared_libraries;
return (const char * const *) repository->priv->cached_shared_libraries; return (const char * const *) repository->cached_shared_libraries;
} }
/** /**
@ -1346,10 +1347,10 @@ gi_repository_get_typelib_path (GIRepository *repository,
repository = get_repository (repository); repository = get_repository (repository);
if (!g_hash_table_lookup_extended (repository->priv->typelibs, namespace, if (!g_hash_table_lookup_extended (repository->typelibs, namespace,
&orig_key, &value)) &orig_key, &value))
{ {
if (!g_hash_table_lookup_extended (repository->priv->lazy_typelibs, namespace, if (!g_hash_table_lookup_extended (repository->lazy_typelibs, namespace,
&orig_key, &value)) &orig_key, &value))
return NULL; return NULL;

View File

@ -58,28 +58,8 @@
G_BEGIN_DECLS G_BEGIN_DECLS
#define GI_TYPE_REPOSITORY (gi_repository_get_type ()) #define GI_TYPE_REPOSITORY (gi_repository_get_type ())
#define GI_REPOSITORY(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GI_TYPE_REPOSITORY, GIRepository)) GI_AVAILABLE_IN_ALL
#define GI_REPOSITORY_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GI_TYPE_REPOSITORY, GIRepositoryClass)) G_DECLARE_FINAL_TYPE (GIRepository, gi_repository, GI, REPOSITORY, GObject)
#define GI_IS_REPOSITORY(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GI_TYPE_REPOSITORY))
#define GI_IS_REPOSITORY_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GI_TYPE_REPOSITORY))
#define GI_REPOSITORY_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GI_TYPE_REPOSITORY, GIRepositoryClass))
typedef struct _GIRepository GIRepository;
typedef struct _GIRepositoryClass GIRepositoryClass;
typedef struct _GIRepositoryPrivate GIRepositoryPrivate;
struct _GIRepository
{
/*< private >*/
GObject parent;
GIRepositoryPrivate *priv;
};
struct _GIRepositoryClass
{
/*< private >*/
GObjectClass parent;
};
/** /**
* GIRepositoryLoadFlags: * GIRepositoryLoadFlags:
@ -96,9 +76,6 @@ typedef enum
/* Repository */ /* Repository */
GI_AVAILABLE_IN_ALL
GType gi_repository_get_type (void) G_GNUC_CONST;
GI_AVAILABLE_IN_ALL GI_AVAILABLE_IN_ALL
GIRepository *gi_repository_get_default (void); GIRepository *gi_repository_get_default (void);