| 1 |
/* sysprof-document-bitset-index.c |
| 2 |
* |
| 3 |
* Copyright 2023 Christian Hergert <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include "sysprof-document-bitset-index-private.h" |
| 24 |
|
| 25 |
struct _SysprofDocumentBitsetIndex |
| 26 |
{ |
| 27 |
GObject parent_instance; |
| 28 |
GListModel *model; |
| 29 |
EggBitset *bitset; |
| 30 |
GType type; |
| 31 |
}; |
| 32 |
|
| 33 |
static GType |
| 34 |
sysprof_document_bitset_index_get_item_type (GListModel *model) |
| 35 |
{ |
| 36 |
SysprofDocumentBitsetIndex *self = SYSPROF_DOCUMENT_BITSET_INDEX (model); |
| 37 |
|
| 38 |
if (self->type) |
| 39 |
return self->type; |
| 40 |
|
| 41 |
if (self->model != NULL) |
| 42 |
return g_list_model_get_item_type (self->model); |
| 43 |
|
| 44 |
return G_TYPE_OBJECT; |
| 45 |
} |
| 46 |
|
| 47 |
static guint |
| 48 |
sysprof_document_bitset_index_get_n_items (GListModel *model) |
| 49 |
{ |
| 50 |
SysprofDocumentBitsetIndex *self = SYSPROF_DOCUMENT_BITSET_INDEX (model); |
| 51 |
|
| 52 |
if (self->bitset != NULL) |
| 53 |
return egg_bitset_get_size (self->bitset); |
| 54 |
|
| 55 |
return 0; |
| 56 |
} |
| 57 |
|
| 58 |
static gpointer |
| 59 |
sysprof_document_bitset_index_get_item (GListModel *model, |
| 60 |
guint position) |
| 61 |
{ |
| 62 |
SysprofDocumentBitsetIndex *self = SYSPROF_DOCUMENT_BITSET_INDEX (model); |
| 63 |
|
| 64 |
if (self->model == NULL || self->bitset == NULL) |
| 65 |
return NULL; |
| 66 |
|
| 67 |
if (position >= egg_bitset_get_size (self->bitset)) |
| 68 |
return NULL; |
| 69 |
|
| 70 |
return g_list_model_get_item (self->model, |
| 71 |
egg_bitset_get_nth (self->bitset, position)); |
| 72 |
} |
| 73 |
|
| 74 |
static void |
| 75 |
list_model_iface_init (GListModelInterface *iface) |
| 76 |
{ |
| 77 |
iface->get_item = sysprof_document_bitset_index_get_item; |
| 78 |
iface->get_item_type = sysprof_document_bitset_index_get_item_type; |
| 79 |
iface->get_n_items = sysprof_document_bitset_index_get_n_items; |
| 80 |
} |
| 81 |
|
| 82 |
G_DEFINE_FINAL_TYPE_WITH_CODE (SysprofDocumentBitsetIndex, sysprof_document_bitset_index, G_TYPE_OBJECT, |
| 83 |
G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init)) |
| 84 |
|
| 85 |
enum { |
| 86 |
PROP_0, |
| 87 |
PROP_N_ITEMS, |
| 88 |
N_PROPS |
| 89 |
}; |
| 90 |
|
| 91 |
static GParamSpec *properties[N_PROPS]; |
| 92 |
|
| 93 |
static void |
| 94 |
sysprof_document_bitset_index_get_property (GObject *object, |
| 95 |
guint prop_id, |
| 96 |
GValue *value, |
| 97 |
GParamSpec *pspec) |
| 98 |
{ |
| 99 |
SysprofDocumentBitsetIndex *self = SYSPROF_DOCUMENT_BITSET_INDEX (object); |
| 100 |
|
| 101 |
switch (prop_id) |
| 102 |
{ |
| 103 |
case PROP_N_ITEMS: |
| 104 |
g_value_set_uint (value, g_list_model_get_n_items (G_LIST_MODEL (self))); |
| 105 |
break; |
| 106 |
|
| 107 |
default: |
| 108 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
static void |
| 113 |
sysprof_document_bitset_index_dispose (GObject *object) |
| 114 |
{ |
| 115 |
SysprofDocumentBitsetIndex *self = (SysprofDocumentBitsetIndex *)object; |
| 116 |
|
| 117 |
g_clear_pointer (&self->bitset, egg_bitset_unref); |
| 118 |
g_clear_object (&self->model); |
| 119 |
|
| 120 |
G_OBJECT_CLASS (sysprof_document_bitset_index_parent_class)->dispose (object); |
| 121 |
} |
| 122 |
|
| 123 |
static void |
| 124 |
sysprof_document_bitset_index_class_init (SysprofDocumentBitsetIndexClass *klass) |
| 125 |
{ |
| 126 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 127 |
|
| 128 |
object_class->dispose = sysprof_document_bitset_index_dispose; |
| 129 |
object_class->get_property = sysprof_document_bitset_index_get_property; |
| 130 |
|
| 131 |
properties[PROP_N_ITEMS] = |
| 132 |
g_param_spec_uint ("n-items", NULL, NULL, |
| 133 |
0, G_MAXUINT, 0, |
| 134 |
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 135 |
|
| 136 |
g_object_class_install_properties (object_class, N_PROPS, properties); |
| 137 |
} |
| 138 |
|
| 139 |
static void |
| 140 |
sysprof_document_bitset_index_init (SysprofDocumentBitsetIndex *self) |
| 141 |
{ |
| 142 |
} |
| 143 |
|
| 144 |
GListModel * |
| 145 |
_sysprof_document_bitset_index_new_full (GListModel *model, |
| 146 |
EggBitset *bitset, |
| 147 |
GType type) |
| 148 |
{ |
| 149 |
SysprofDocumentBitsetIndex *self; |
| 150 |
|
| 151 |
g_return_val_if_fail (G_IS_LIST_MODEL (model), NULL); |
| 152 |
g_return_val_if_fail (bitset != NULL, NULL); |
| 153 |
|
| 154 |
self = g_object_new (SYSPROF_TYPE_DOCUMENT_BITSET_INDEX, NULL); |
| 155 |
self->model = g_object_ref (model); |
| 156 |
self->bitset = egg_bitset_ref (bitset); |
| 157 |
self->type = type; |
| 158 |
|
| 159 |
return G_LIST_MODEL (self); |
| 160 |
} |
| 161 |
|
| 162 |
GListModel * |
| 163 |
_sysprof_document_bitset_index_new (GListModel *model, |
| 164 |
EggBitset *bitset) |
| 165 |
{ |
| 166 |
return _sysprof_document_bitset_index_new_full (model, bitset, G_TYPE_INVALID); |
| 167 |
} |