diff --git a/src/libsysprof-analyze/meson.build b/src/libsysprof-analyze/meson.build index 91b65de2..782685ee 100644 --- a/src/libsysprof-analyze/meson.build +++ b/src/libsysprof-analyze/meson.build @@ -21,6 +21,7 @@ libsysprof_analyze_public_sources = [ ] libsysprof_analyze_private_sources = [ + 'sysprof-document-bitset-index.c', 'sysprof-mapped-file.c', 'sysprof-memory-map.c', 'sysprof-mount-device.c', diff --git a/src/libsysprof-analyze/sysprof-document-bitset-index-private.h b/src/libsysprof-analyze/sysprof-document-bitset-index-private.h new file mode 100644 index 00000000..b541828c --- /dev/null +++ b/src/libsysprof-analyze/sysprof-document-bitset-index-private.h @@ -0,0 +1,35 @@ +/* sysprof-document-bitset-index.h + * + * Copyright 2023 Christian Hergert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#pragma once + +#include +#include + +G_BEGIN_DECLS + +#define SYSPROF_TYPE_DOCUMENT_BITSET_INDEX (sysprof_document_bitset_index_get_type()) + +G_DECLARE_FINAL_TYPE (SysprofDocumentBitsetIndex, sysprof_document_bitset_index, SYSPROF, DOCUMENT_BITSET_INDEX, GObject) + +GListModel *_sysprof_document_bitset_index_new (GListModel *model, + GtkBitset *bitset); + +G_END_DECLS diff --git a/src/libsysprof-analyze/sysprof-document-bitset-index.c b/src/libsysprof-analyze/sysprof-document-bitset-index.c new file mode 100644 index 00000000..8f4b9496 --- /dev/null +++ b/src/libsysprof-analyze/sysprof-document-bitset-index.c @@ -0,0 +1,119 @@ +/* sysprof-document-bitset-index.c + * + * Copyright 2023 Christian Hergert + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include "config.h" + +#include "sysprof-document-bitset-index-private.h" + +struct _SysprofDocumentBitsetIndex +{ + GObject parent_instance; + GListModel *model; + GtkBitset *bitset; +}; + +static GType +sysprof_document_bitset_index_get_item_type (GListModel *model) +{ + SysprofDocumentBitsetIndex *self = SYSPROF_DOCUMENT_BITSET_INDEX (model); + + if (self->model != NULL) + return g_list_model_get_item_type (self->model); + + return G_TYPE_OBJECT; +} + +static guint +sysprof_document_bitset_index_get_n_items (GListModel *model) +{ + SysprofDocumentBitsetIndex *self = SYSPROF_DOCUMENT_BITSET_INDEX (model); + + if (self->bitset != NULL) + return gtk_bitset_get_size (self->bitset); + + return 0; +} + +static gpointer +sysprof_document_bitset_index_get_item (GListModel *model, + guint position) +{ + SysprofDocumentBitsetIndex *self = SYSPROF_DOCUMENT_BITSET_INDEX (model); + + if (self->model == NULL || self->bitset == NULL) + return NULL; + + if (position >= gtk_bitset_get_size (self->bitset)) + return NULL; + + return g_list_model_get_item (self->model, + gtk_bitset_get_nth (self->bitset, position)); +} + +static void +list_model_iface_init (GListModelInterface *iface) +{ + iface->get_item = sysprof_document_bitset_index_get_item; + iface->get_item_type = sysprof_document_bitset_index_get_item_type; + iface->get_n_items = sysprof_document_bitset_index_get_n_items; +} + +G_DEFINE_FINAL_TYPE_WITH_CODE (SysprofDocumentBitsetIndex, sysprof_document_bitset_index, G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init)) + +static void +sysprof_document_bitset_index_dispose (GObject *object) +{ + SysprofDocumentBitsetIndex *self = (SysprofDocumentBitsetIndex *)object; + + g_clear_pointer (&self->bitset, gtk_bitset_unref); + g_clear_object (&self->model); + + G_OBJECT_CLASS (sysprof_document_bitset_index_parent_class)->dispose (object); +} + +static void +sysprof_document_bitset_index_class_init (SysprofDocumentBitsetIndexClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + + object_class->dispose = sysprof_document_bitset_index_dispose; +} + +static void +sysprof_document_bitset_index_init (SysprofDocumentBitsetIndex *self) +{ +} + +GListModel * +_sysprof_document_bitset_index_new (GListModel *model, + GtkBitset *bitset) +{ + SysprofDocumentBitsetIndex *self; + + g_return_val_if_fail (G_IS_LIST_MODEL (model), NULL); + g_return_val_if_fail (bitset != NULL, NULL); + + self = g_object_new (SYSPROF_TYPE_DOCUMENT_BITSET_INDEX, NULL); + self->model = g_object_ref (model); + self->bitset = gtk_bitset_ref (bitset); + + return G_LIST_MODEL (self); +}