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
|
/*
* Copyright (C) 2007 James Livingston <doclivingston@gmail.com>
*
* 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 2 of the License, or
* (at your option) any later version.
*
* The Rhythmbox authors hereby grant permission for non-GPL compatible
* GStreamer plugins to be used and distributed together with GStreamer
* and Rhythmbox. This permission is above and beyond the permissions granted
* by the GPL license by which Rhythmbox is covered. If you modify this code
* you may extend this exception to your version of the code, but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "config.h"
#include "rb-string-value-map.h"
#include "rb-util.h"
/**
* SECTION:rb-string-value-map
* @short_description: specialized hash table for storing string to GValue mappings
*
* Simplifies the use of string:GValue maps with respect to copying of the values
* inserted into the map. Except for rb_string_value_map_peek, the caller retains
* ownership of values passed in, and assumes ownership of all values returned.
*/
static void rb_string_value_map_finalize (GObject *obj);
struct RBStringValueMapPrivate {
GHashTable *map;
};
G_DEFINE_TYPE (RBStringValueMap, rb_string_value_map, G_TYPE_OBJECT)
static void
rb_string_value_map_class_init (RBStringValueMapClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = rb_string_value_map_finalize;
g_type_class_add_private (klass, sizeof (RBStringValueMapPrivate));
}
static void
rb_string_value_map_init (RBStringValueMap *map)
{
map->priv = G_TYPE_INSTANCE_GET_PRIVATE (map, RB_TYPE_STRING_VALUE_MAP, RBStringValueMapPrivate);
map->priv->map = g_hash_table_new_full (g_str_hash,
g_str_equal,
(GDestroyNotify)g_free,
(GDestroyNotify)rb_value_free);
}
static void
rb_string_value_map_finalize (GObject *obj)
{
RBStringValueMap *map = RB_STRING_VALUE_MAP (obj);
g_hash_table_destroy (map->priv->map);
G_OBJECT_CLASS(rb_string_value_map_parent_class)->finalize (obj);
}
/**
* rb_string_value_map_new:
*
* Creates a new #RBStringValueMap
*
* Return value: new empty #RBStringValueMap
*/
RBStringValueMap*
rb_string_value_map_new (void)
{
return g_object_new (RB_TYPE_STRING_VALUE_MAP, NULL);
}
/**
* rb_string_value_map_set:
* @map: a #RBStringValueMap
* @key: key to set
* @value: value to store
*
* Inserts a value into the map. The value is copied.
*/
void
rb_string_value_map_set (RBStringValueMap *map,
const char *key,
const GValue *value)
{
GValue *val;
val = g_slice_new0 (GValue);
g_value_init (val, G_VALUE_TYPE (value));
g_value_copy (value, val);
g_hash_table_insert (map->priv->map, g_strdup(key), val);
}
/**
* rb_string_value_map_remove:
* @map: a #RBStringValueMap
* @key: key to remove
*
* Removes a value from the map.
*
* Return value: %TRUE if the value was found and removed
*/
gboolean
rb_string_value_map_remove (RBStringValueMap *map,
const char *key)
{
return g_hash_table_remove (map->priv->map, key);
}
/**
* rb_string_value_map_size:
* @map: a #RBStringValueMap
*
* Returns the number of entries in the map.
*
* Return value: number of entries
*/
guint
rb_string_value_map_size (RBStringValueMap *map)
{
return g_hash_table_size (map->priv->map);
}
/**
* rb_string_value_map_get:
* @map: a #RBStringValueMap
* @key: key to retrieve
* @out: returns a copy of the value in the map
*
* Locates and copies the value associated with the key.
*
* Return value: %TRUE if the value was found
*/
gboolean
rb_string_value_map_get (RBStringValueMap *map,
const char *key,
GValue *out)
{
GValue *val;
val = g_hash_table_lookup (map->priv->map, key);
if (val == NULL)
return FALSE;
g_value_init (out, G_VALUE_TYPE (val));
g_value_copy (val, out);
return TRUE;
}
/**
* rb_string_value_map_peek:
* @map: a #RBStringValueMap
* @key: key to retrieve
*
* Locates the value associated with the key. This returns the
* GValue stored in the map, so it cannot be modified.
*
* Return value: the GValue associated with the key
*/
const GValue*
rb_string_value_map_peek (RBStringValueMap *map,
const char *key)
{
return g_hash_table_lookup (map->priv->map, key);
}
/**
* rb_string_value_map_steal_hashtable:
* @map: a #RBStringValueMap
*
* Extracts and returns the underlying hash table from the map,
* and creates a new empty map.
*
* Return value: (transfer full): #GHashTable from the map
*/
GHashTable*
rb_string_value_map_steal_hashtable (RBStringValueMap *map)
{
GHashTable *old;
old = map->priv->map;
map->priv->map = g_hash_table_new_full (g_str_hash,
g_str_equal,
(GDestroyNotify)g_free,
(GDestroyNotify)rb_value_free);
return old;
}
|