1 /*
2  * goc-utils.c :
3  *
4  * Copyright (C) 2008 Jean Brefort (jean.brefort@normalesup.org)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) version 3.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
19  * USA
20  */
21 
22 #include <goffice/goffice-config.h>
23 #include <goffice/canvas/goc-utils.h>
24 
25 /**
26  * SECTION:goc-utils
27  *
28  * Only one structure is currently available: #GocPoints.
29  **/
30 
31 /**
32  * GocPoints:
33  * @points: The embedded points.
34  *
35  * A boxed type used to hold a list of #GocPoint instances.
36  **/
37 
38 /**
39  * goc_points_new:
40  * @n: the number of #GocPoint instances.
41  *
42  * Creates a new #GocPoints instances with @n points with nul initial
43  * coordinates. The coordinates can be changed using direct access:
44  *
45  * <programlisting>
46  *      GocPoints points = goc_points_new (1);
47  *      points->points[0].x = my_x;
48  *      points->points[0].y = my_y;
49  * </programlisting>
50  *
51  * Returns: the newly created #GocPoints with an initial references count of 1.
52  **/
53 
54 GocPoints *
goc_points_new(unsigned n)55 goc_points_new (unsigned n)
56 {
57 	GocPoints *points = g_new (GocPoints, 1);
58 	points->n = n;
59 	points->refs = 1;
60 	points->points = g_new0 (GocPoint, n);
61 	return points;
62 }
63 
64 /**
65  * goc_points_ref :
66  * @points: #GocPoints
67  *
68  * Increases the references count of @points by 1.
69  * Returns: the referenced #GocPoints.
70  **/
71 GocPoints *
goc_points_ref(GocPoints * points)72 goc_points_ref (GocPoints *points)
73 {
74 	points->refs++;
75 	return points;
76 }
77 
78 /**
79  * goc_points_unref:
80  * @points: #GocPoints
81  *
82  * Decreases the references count of @points by 1, and destroys it if the
83  * references count becomes 0.
84  **/
85 void
goc_points_unref(GocPoints * points)86 goc_points_unref (GocPoints *points)
87 {
88 	points->refs--;
89 	if (points->refs == 0) {
90 		g_free (points->points);
91 		points->points = NULL;
92 		g_free (points);
93 	}
94 }
95 
96 GType
goc_points_get_type(void)97 goc_points_get_type (void)
98 {
99     static GType type_points = 0;
100 
101     if (!type_points)
102 	type_points = g_boxed_type_register_static
103 	    ("GocPoints",
104 	     (GBoxedCopyFunc) goc_points_ref,
105 	     (GBoxedFreeFunc) goc_points_unref);
106 
107     return type_points;
108 }
109 
110 
111 /**
112  * GocIntArray:
113  * @vals: The embedded values.
114  *
115  * FocIntArray::n is the size of the array.
116  * A boxed type used to hold an array of integers.
117  * Since: 0.8.2
118  **/
119 
120 /**
121  * goc_int_array_new:
122  * @n: the number of integers in the array.
123  *
124  * Creates a new #GocIntArray instances with @n values initialized to 0.
125  * The values can be changed using direct access:
126  *
127  * <programlisting>
128  *      GocIntArray array = goc_int_array_new (2);
129  *      array->vals[0] = my_first_int;
130  *      array->vals[1] = my_second_int;
131  * </programlisting>
132  *
133  * Returns: the newly created #GocIntArray with an initial references count of 1.
134  * Since: 0.8.2
135  **/
136 
137 GocIntArray *
goc_int_array_new(unsigned n)138 goc_int_array_new (unsigned n)
139 {
140 	GocIntArray *array = g_new (GocIntArray, 1);
141 	array->n = n;
142 	array->refs = 1;
143 	array->vals = g_new0 (int, n);
144 	return array;
145 }
146 
147 /**
148  * goc_int_array_ref :
149  * @array: #GocIntArray
150  *
151  * Increases the references count of @array by 1.
152  * Returns: the referenced #GocIntArray.
153  * Since: 0.8.2
154  **/
155 GocIntArray *
goc_int_array_ref(GocIntArray * array)156 goc_int_array_ref (GocIntArray *array)
157 {
158 	array->refs++;
159 	return array;
160 }
161 
162 /**
163  * goc_int_array_unref:
164  * @array: #GocIntArray
165  *
166  * Decreases the references count of @array by 1, and destroys it if the
167  * references count becomes 0.
168  * Since: 0.8.2
169  **/
170 void
goc_int_array_unref(GocIntArray * array)171 goc_int_array_unref (GocIntArray *array)
172 {
173 	array->refs--;
174 	if (array->refs == 0) {
175 		g_free (array->vals);
176 		array->vals = NULL;
177 		g_free (array);
178 	}
179 }
180 
181 GType
goc_int_array_get_type(void)182 goc_int_array_get_type (void)
183 {
184     static GType type_int_array = 0;
185 
186     if (!type_int_array)
187 	type_int_array = g_boxed_type_register_static
188 	    ("GocIntArray",
189 	     (GBoxedCopyFunc) goc_int_array_ref,
190 	     (GBoxedFreeFunc) goc_int_array_unref);
191 
192     return type_int_array;
193 }
194