23
|
1 /* Libvisual - The audio visualisation framework.
|
|
2 *
|
|
3 * Copyright (C) 2004, 2005 Dennis Smit <ds@nerds-incorporated.org>
|
|
4 *
|
|
5 * Authors: Dennis Smit <ds@nerds-incorporated.org>
|
|
6 *
|
|
7 * $Id:
|
|
8 *
|
|
9 * This program is free software; you can redistribute it and/or modify
|
|
10 * it under the terms of the GNU Lesser General Public License as
|
|
11 * published by the Free Software Foundation; either version 2.1
|
|
12 * of the License, or (at your option) any later version.
|
|
13 *
|
|
14 * This program is distributed in the hope that it will be useful,
|
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 * GNU Lesser General Public License for more details.
|
|
18 *
|
|
19 * You should have received a copy of the GNU Lesser General Public License
|
|
20 * along with this program; if not, write to the Free Software
|
|
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
22 */
|
|
23
|
|
24 #include <stdio.h>
|
|
25 #include <stdlib.h>
|
|
26 #include <unistd.h>
|
|
27 #include <string.h>
|
|
28
|
|
29 #include "lv_log.h"
|
|
30 #include "lv_object.h"
|
|
31
|
|
32
|
|
33 /**
|
|
34 * @defgroup VisObject VisObject
|
|
35 * @{
|
|
36 */
|
|
37
|
|
38 /**
|
|
39 * This function is a global VisListDestroyerFunc handler that unrefs VisObjects.
|
|
40 *
|
|
41 * @param data Pointer to the VisObject that needs to be unrefed
|
|
42 */
|
|
43 void visual_object_list_destroyer (void *data)
|
|
44 {
|
|
45 if (data == NULL)
|
|
46 return;
|
|
47
|
|
48 visual_object_unref (VISUAL_OBJECT (data));
|
|
49 }
|
|
50
|
|
51 /**
|
|
52 * Creates a new VisObject structure.
|
|
53 *
|
|
54 * @return A newly allocated VisObject, or NULL on failure.
|
|
55 */
|
|
56 VisObject *visual_object_new ()
|
|
57 {
|
|
58 VisObject *object;
|
|
59
|
|
60 object = visual_mem_new0 (VisObject, 1);
|
|
61
|
|
62 object->allocated = TRUE;
|
|
63
|
|
64 visual_object_ref (object);
|
|
65
|
|
66 return object;
|
|
67 }
|
|
68
|
|
69 /**
|
|
70 * Frees the VisObject. This does not destroy the object itself but only releases the memory it's using.
|
|
71 *
|
|
72 * @param object Pointer to a VisObject that needs to be freed.
|
|
73 *
|
|
74 * @return VISUAL_OK on succes, -VISUAL_ERROR_OBJECT_NULL, -VISUAL_ERROR_OBJECT_NOT_ALLOCATED or error values
|
|
75 * returned by visual_mem_free on failure.
|
|
76 */
|
|
77 int visual_object_free (VisObject *object)
|
|
78 {
|
|
79 visual_log_return_val_if_fail (object != NULL, -VISUAL_ERROR_OBJECT_NULL);
|
|
80 visual_log_return_val_if_fail (object->allocated == TRUE, -VISUAL_ERROR_OBJECT_NOT_ALLOCATED);
|
|
81
|
|
82 return visual_mem_free (object);
|
|
83 }
|
|
84
|
|
85 /**
|
|
86 * Destroys the VisObject. This does destruct the VisObject
|
|
87 * by using the dtor function if it's set and also frees the memory
|
|
88 * it's using. It's valid to pass non allocated VisObjects,
|
|
89 * the function will recognize this by a flag that is set in the VisObject.
|
|
90 *
|
|
91 * @param object Pointer to a VisObject that needs to be destroyed.
|
|
92 *
|
|
93 * @return VISUAL_OK on succes, -VISUAL_ERROR_OBJECT_NULL or error values returned byvisual_object_free on failure.
|
|
94 */
|
|
95 int visual_object_destroy (VisObject *object)
|
|
96 {
|
|
97 visual_log_return_val_if_fail (object != NULL, -VISUAL_ERROR_OBJECT_NULL);
|
|
98
|
|
99 if (object->dtor != NULL)
|
|
100 object->dtor (object);
|
|
101
|
|
102 if (object->allocated == TRUE)
|
|
103 return visual_object_free (object);
|
|
104
|
|
105 return VISUAL_OK;
|
|
106 }
|
|
107
|
|
108 /**
|
|
109 * Initializes a VisObject for usage. This also ups the refcount by one, so this function really is for initial object
|
|
110 * creation.
|
|
111 *
|
|
112 * @param object Pointer to a VisObject that is initialized.
|
|
113 * @param allocated Flag to indicate if the VisObject itself is an allocated piece of memory.
|
|
114 * @param dtor The destructor function, that is used to destroy the VisObject when it loses all references or when it's
|
|
115 * being destroyed.
|
|
116 *
|
|
117 * @return VISUAL_OK on succes, -VISUAL_ERROR_OBJECT_NULL on failure.
|
|
118 */
|
|
119 int visual_object_initialize (VisObject *object, int allocated, VisObjectDtorFunc dtor)
|
|
120 {
|
|
121 visual_log_return_val_if_fail (object != NULL, -VISUAL_ERROR_OBJECT_NULL);
|
|
122
|
|
123 object->allocated = allocated;
|
|
124 object->dtor = dtor;
|
|
125
|
|
126 visual_object_ref (object);
|
|
127
|
|
128 return VISUAL_OK;
|
|
129 }
|
|
130
|
|
131 /**
|
|
132 * Increases the reference counter for a VisObject.
|
|
133 *
|
|
134 * @param object Pointer to a VisObject in which the reference count is increased.
|
|
135 *
|
|
136 * @return VISUAL_OK on succes, -VISUAL_ERROR_OBJECT_NULL on failure.
|
|
137 */
|
|
138 int visual_object_ref (VisObject *object)
|
|
139 {
|
|
140 visual_log_return_val_if_fail (object != NULL, -VISUAL_ERROR_OBJECT_NULL);
|
|
141
|
|
142 object->refcount++;
|
|
143
|
|
144 return VISUAL_OK;
|
|
145 }
|
|
146
|
|
147 /**
|
|
148 * Decreases the reference counter for a VisObject. If the reference counter hits zero it will
|
|
149 * destruct the object using visual_object_destroy.
|
|
150 *
|
|
151 * @see visual_object_destroy
|
|
152 *
|
|
153 * @param object Pointer to a VisObject in which the reference count is decreased.
|
|
154 *
|
|
155 * @return VISUAL_OK on succes, -VISUAL_ERROR_OBJECT_NULL or error values returned by
|
|
156 * visual_object_destroy on failure.
|
|
157 */
|
|
158 int visual_object_unref (VisObject *object)
|
|
159 {
|
|
160 visual_log_return_val_if_fail (object != NULL, -VISUAL_ERROR_OBJECT_NULL);
|
|
161
|
|
162 object->refcount--;
|
|
163
|
|
164 /* No reference left, start dtoring of this VisObject */
|
|
165 if (object->refcount <= 0) {
|
|
166 object->refcount = 0;
|
|
167
|
|
168 return visual_object_destroy (object);
|
|
169 }
|
|
170 return VISUAL_OK;
|
|
171 }
|
|
172
|
|
173 /**
|
|
174 * Sets the private data pointer to a VisObject.
|
|
175 *
|
|
176 * @param object Pointer to a VisObject to which the private data is set.
|
|
177 * @param priv Pointer to the private data that is set to the VisObject.
|
|
178 *
|
|
179 * @return VISUAL_OK on succes, -VISUAL_ERROR_OBJECT_NULL on failure.
|
|
180 */
|
|
181 int visual_object_set_private (VisObject *object, void *priv)
|
|
182 {
|
|
183 visual_log_return_val_if_fail (object != NULL, -VISUAL_ERROR_OBJECT_NULL);
|
|
184
|
|
185 object->priv = priv;
|
|
186
|
|
187 return VISUAL_OK;
|
|
188 }
|
|
189
|
|
190 /**
|
|
191 * Retrieves the private data from a VisObject.
|
|
192 *
|
|
193 * @param object Pointer to a VisObject from which the private data is requested.
|
|
194 *
|
|
195 * @return Pointer to the private data or NULL.
|
|
196 */
|
|
197 void *visual_object_get_private (VisObject *object)
|
|
198 {
|
|
199 visual_log_return_val_if_fail (object != NULL, NULL);
|
|
200
|
|
201 return object->priv;
|
|
202 }
|
|
203
|
|
204 /**
|
|
205 * @}
|
|
206 */
|
|
207
|