[build] Bumped version to 0.5.3
[adg.git] / adg / adg-segment.c
blob212ed070bdcd71621aeafac82c99850477536874
1 /* ADG - Automatic Drawing Generation
2 * Copyright (C) 2007,2008,2009 Nicola Fontana <ntd at entidi.it>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 /**
22 * SECTION:adg-segment
23 * @Section_Id:AdgSegment
24 * @title: AdgSegment
25 * @short_description: A wrapper for #CpmlSegment
27 * AdgSegment is a wrapper in #GType syntax of the #CpmlSegment struct.
28 * Furthermore, some dynamic memory functions are provided, such as
29 * shallow and deep duplication functions.
30 **/
32 /**
33 * AdgSegment:
35 * Another name for #CpmlSegment: check its documentation for the
36 * fields description and visibility details.
37 **/
40 #include "adg-segment.h"
42 #include <string.h>
45 GType
46 adg_segment_get_type(void)
48 static int segment_type = 0;
50 if (G_UNLIKELY(segment_type == 0))
51 segment_type = g_boxed_type_register_static("AdgSegment",
52 (GBoxedCopyFunc) adg_segment_dup,
53 g_free);
55 return segment_type;
58 /**
59 * adg_segment_dup:
60 * @segment: an #AdgSegment structure
62 * Duplicates @segment. This function makes a shallow duplication,
63 * that is the internal pointers of the resulting segment struct
64 * refer to the same memory as the original @segment. Check out
65 * adg_segment_deep_dup() if it is required also the content
66 * duplication.
68 * Returns: a shallow duplicate of @segment: must be freed
69 * with g_free() when no longer needed.
70 **/
71 AdgSegment *
72 adg_segment_dup(const AdgSegment *segment)
74 return g_memdup(segment, sizeof(AdgSegment));
77 /**
78 * adg_segment_deep_dup:
79 * @segment: an #AdgSegment structure
81 * Duplicates @segment. This function makes a deep duplication,
82 * that is it duplicates also the underlying data that defines
83 * the segment. The <structfield>path</structfield> field
84 * is set to %NULL as <structfield>data</structfield> is no
85 * more referring to the original cairo path.
87 * All the data is allocated in the same chunk of memory so freeing
88 * the returned pointer releases all the occupied memory.
90 * Returns: a deep duplicate of @segment: must be freed
91 * with g_free() when no longer needed.
92 **/
93 AdgSegment *
94 adg_segment_deep_dup(const AdgSegment *segment)
96 AdgSegment *dest;
97 gsize segment_size, data_size;
98 cairo_path_data_t *p_data;
100 segment_size = sizeof(AdgSegment);
101 data_size = segment->data == NULL ?
102 0 : sizeof(cairo_path_data_t) * segment->num_data;
103 dest = (AdgSegment *) g_malloc(segment_size + data_size);
104 p_data = (cairo_path_data_t *) ((gchar *) dest + segment_size);
106 dest->path = NULL;
108 if (data_size > 0) {
109 dest->data = memcpy(p_data, segment->data, data_size);
110 dest->num_data = segment->num_data;
111 } else {
112 dest->data = NULL;
113 dest->num_data = 0;
116 return dest;
120 * adg_segment_deep_copy:
121 * @segment: an #AdgSegment structure
122 * @src: the source segment to copy
124 * Makes a deep copy of @src to @segment. For a shallow copy, check out
125 * the cpml_segment_copy() API provided by the CPML library.
127 * This could seem a somewhat unusual operation because @segment should
128 * be "compatible" with @src: it is expected that they have the same
129 * <structfield>num_data</structfield> value. Anyway, it is convenient
130 * in some situation, such as when restoring the original data from a
131 * deep duplicated source:
133 * |[
134 * AdgSegment *backup;
136 * backup = adg_segment_deep_dup(&segment);
137 * // Now &segment can be modified
138 * ...
139 * adg_segment_deep_copy(&segment, backup);
140 * g_free(backup);
141 * ]|
143 * The struct fields of @segment are left untouched and used only to
144 * check if it is compatible with @src.
146 * Returns: @segment
148 AdgSegment *
149 adg_segment_deep_copy(AdgSegment *segment, const AdgSegment *src)
151 size_t n;
153 g_return_val_if_fail(segment != NULL, segment);
154 g_return_val_if_fail(src != NULL, segment);
155 g_return_val_if_fail(segment->num_data == src->num_data, segment);
157 if (src->num_data <= 0)
158 return segment;
160 n = sizeof(cairo_path_data_t) * segment->num_data;
162 memcpy(segment->data, src->data, n);
164 return segment;