1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /*
4 * GThumb
5 *
6 * Copyright (C) 2010 Free Software Foundation, Inc.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22
23 #include <config.h>
24 #include "gvaluehash.h"
25 #include "glib-utils.h"
26
27
28 struct _GValueHash {
29 int ref;
30 GHashTable *table;
31 };
32
33
34 static void
_g_value_free(gpointer data)35 _g_value_free (gpointer data)
36 {
37 GValue *value = data;
38
39 g_value_reset (value);
40 g_free (value);
41 }
42
43
44 GValueHash *
g_value_hash_new(void)45 g_value_hash_new (void)
46 {
47 GValueHash *self;
48
49 self = g_new0 (GValueHash, 1);
50 self->ref = 1;
51 self->table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, _g_value_free);
52
53 return self;
54 }
55
56
57 void
g_value_hash_ref(GValueHash * self)58 g_value_hash_ref (GValueHash *self)
59 {
60 self->ref++;
61 }
62
63
64 void
g_value_hash_unref(GValueHash * self)65 g_value_hash_unref (GValueHash *self)
66 {
67 self->ref--;
68 if (self->ref > 0)
69 return;
70 g_hash_table_destroy (self->table);
71 g_free (self);
72 }
73
74
75 void
g_value_hash_set_boolean(GValueHash * self,const char * key,gboolean b_value)76 g_value_hash_set_boolean (GValueHash *self,
77 const char *key,
78 gboolean b_value)
79 {
80 GValue *priv_value;
81
82 priv_value = g_new0 (GValue, 1);
83 g_value_init (priv_value, G_TYPE_BOOLEAN);
84 g_value_set_boolean (priv_value, b_value);
85 g_hash_table_insert (self->table, g_strdup (key), priv_value);
86 }
87
88
89 void
g_value_hash_set_float(GValueHash * self,const char * key,float f_value)90 g_value_hash_set_float (GValueHash *self,
91 const char *key,
92 float f_value)
93 {
94 GValue *priv_value;
95
96 priv_value = g_new0 (GValue, 1);
97 g_value_init (priv_value, G_TYPE_FLOAT);
98 g_value_set_float (priv_value, f_value);
99 g_hash_table_insert (self->table, g_strdup (key), priv_value);
100 }
101
102
103 void
g_value_hash_set_int(GValueHash * self,const char * key,int i_value)104 g_value_hash_set_int (GValueHash *self,
105 const char *key,
106 int i_value)
107 {
108 GValue *priv_value;
109
110 priv_value = g_new0 (GValue, 1);
111 g_value_init (priv_value, G_TYPE_INT);
112 g_value_set_int (priv_value, i_value);
113 g_hash_table_insert (self->table, g_strdup (key), priv_value);
114 }
115
116
117 void
g_value_hash_set_string(GValueHash * self,const char * key,const char * s_value)118 g_value_hash_set_string (GValueHash *self,
119 const char *key,
120 const char *s_value)
121 {
122 GValue *priv_value;
123
124 priv_value = g_new0 (GValue, 1);
125 g_value_init (priv_value, G_TYPE_STRING);
126 g_value_set_string (priv_value, s_value);
127 g_hash_table_insert (self->table, g_strdup (key), priv_value);
128 }
129
130
131 void
g_value_hash_set_stringv(GValueHash * self,const char * key,char ** value)132 g_value_hash_set_stringv (GValueHash *self,
133 const char *key,
134 char **value)
135 {
136 GValue *priv_value;
137
138 priv_value = g_new0 (GValue, 1);
139 g_value_init (priv_value, G_TYPE_STRV);
140 g_value_set_boxed (priv_value, value);
141 g_hash_table_insert (self->table, g_strdup (key), priv_value);
142 }
143
144
145 void
g_value_hash_set_string_list(GValueHash * self,const char * key,GList * l_value)146 g_value_hash_set_string_list (GValueHash *self,
147 const char *key,
148 GList *l_value)
149 {
150 GValue *priv_value;
151
152 priv_value = g_new0 (GValue, 1);
153 g_value_init (priv_value, G_TYPE_STRING_LIST);
154 g_value_set_boxed (priv_value, l_value);
155 g_hash_table_insert (self->table, g_strdup (key), priv_value);
156 }
157
158
159 void
g_value_hash_set_value(GValueHash * self,const char * key,GValue * value)160 g_value_hash_set_value (GValueHash *self,
161 const char *key,
162 GValue *value)
163 {
164 GValue *priv_value;
165
166 priv_value = g_new0 (GValue, 1);
167 g_value_copy (value, priv_value);
168 g_hash_table_insert (self->table, g_strdup (key), priv_value);
169 }
170
171
172 GValue *
g_value_hash_get_value(GValueHash * self,const char * key)173 g_value_hash_get_value (GValueHash *self,
174 const char *key)
175 {
176 return g_hash_table_lookup (self->table, key);
177 }
178
179
180 gboolean
g_value_hash_is_set(GValueHash * self,const char * key)181 g_value_hash_is_set (GValueHash *self,
182 const char *key)
183 {
184 return (g_hash_table_lookup (self->table, key) != NULL);
185 }
186
187
188 void
g_value_hash_unset(GValueHash * self,const char * key)189 g_value_hash_unset (GValueHash *self,
190 const char *key)
191 {
192 g_hash_table_remove (self->table, key);
193 }
194
195
196 void
g_value_hash_clear(GValueHash * self)197 g_value_hash_clear (GValueHash *self)
198 {
199 g_hash_table_remove_all (self->table);
200 }
201