1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module glib.Bytes;
26 
27 private import glib.ByteArray;
28 private import glib.ConstructionException;
29 private import glib.c.functions;
30 public  import glib.c.types;
31 private import gtkd.Loader;
32 
33 
34 /**
35  * A simple refcounted data type representing an immutable sequence of zero or
36  * more bytes from an unspecified origin.
37  * 
38  * The purpose of a #GBytes is to keep the memory region that it holds
39  * alive for as long as anyone holds a reference to the bytes.  When
40  * the last reference count is dropped, the memory is released. Multiple
41  * unrelated callers can use byte data in the #GBytes without coordinating
42  * their activities, resting assured that the byte data will not change or
43  * move while they hold a reference.
44  * 
45  * A #GBytes can come from many different origins that may have
46  * different procedures for freeing the memory region.  Examples are
47  * memory from g_malloc(), from memory slices, from a #GMappedFile or
48  * memory from other allocators.
49  * 
50  * #GBytes work well as keys in #GHashTable. Use g_bytes_equal() and
51  * g_bytes_hash() as parameters to g_hash_table_new() or g_hash_table_new_full().
52  * #GBytes can also be used as keys in a #GTree by passing the g_bytes_compare()
53  * function to g_tree_new().
54  * 
55  * The data pointed to by this bytes must not be modified. For a mutable
56  * array of bytes see #GByteArray. Use g_bytes_unref_to_array() to create a
57  * mutable array for a #GBytes sequence. To create an immutable #GBytes from
58  * a mutable #GByteArray, use the g_byte_array_free_to_bytes() function.
59  *
60  * Since: 2.32
61  */
62 public class Bytes
63 {
64 	/** the main Gtk struct */
65 	protected GBytes* gBytes;
66 	protected bool ownedRef;
67 
68 	/** Get the main Gtk struct */
69 	public GBytes* getBytesStruct(bool transferOwnership = false)
70 	{
71 		if (transferOwnership)
72 			ownedRef = false;
73 		return gBytes;
74 	}
75 
76 	/** the main Gtk struct as a void* */
77 	protected void* getStruct()
78 	{
79 		return cast(void*)gBytes;
80 	}
81 
82 	/**
83 	 * Sets our main struct and passes it to the parent class.
84 	 */
85 	public this (GBytes* gBytes, bool ownedRef = false)
86 	{
87 		this.gBytes = gBytes;
88 		this.ownedRef = ownedRef;
89 	}
90 
91 	~this ()
92 	{
93 		if ( Linker.isLoaded(LIBRARY_GLIB) && ownedRef )
94 			g_bytes_unref(gBytes);
95 	}
96 
97 
98 	/**
99 	 * Creates a new #GBytes from @data.
100 	 *
101 	 * @data is copied. If @size is 0, @data may be %NULL.
102 	 *
103 	 * Params:
104 	 *     data = the data to be used for the bytes
105 	 *
106 	 * Returns: a new #GBytes
107 	 *
108 	 * Since: 2.32
109 	 *
110 	 * Throws: ConstructionException GTK+ fails to create the object.
111 	 */
112 	public this(ubyte[] data)
113 	{
114 		auto __p = g_bytes_new(data.ptr, cast(size_t)data.length);
115 
116 		if(__p is null)
117 		{
118 			throw new ConstructionException("null returned by new");
119 		}
120 
121 		this(cast(GBytes*) __p);
122 	}
123 
124 	/**
125 	 * Creates a #GBytes from @data.
126 	 *
127 	 * When the last reference is dropped, @free_func will be called with the
128 	 * @user_data argument.
129 	 *
130 	 * @data must not be modified after this call is made until @free_func has
131 	 * been called to indicate that the bytes is no longer in use.
132 	 *
133 	 * @data may be %NULL if @size is 0.
134 	 *
135 	 * Params:
136 	 *     data = the data to be used for the bytes
137 	 *     freeFunc = the function to call to release the data
138 	 *     userData = data to pass to @free_func
139 	 *
140 	 * Returns: a new #GBytes
141 	 *
142 	 * Since: 2.32
143 	 *
144 	 * Throws: ConstructionException GTK+ fails to create the object.
145 	 */
146 	public this(ubyte[] data, GDestroyNotify freeFunc, void* userData)
147 	{
148 		auto __p = g_bytes_new_with_free_func(data.ptr, cast(size_t)data.length, freeFunc, userData);
149 
150 		if(__p is null)
151 		{
152 			throw new ConstructionException("null returned by new_with_free_func");
153 		}
154 
155 		this(cast(GBytes*) __p);
156 	}
157 
158 	/**
159 	 * Compares the two #GBytes values.
160 	 *
161 	 * This function can be used to sort GBytes instances in lexicographical order.
162 	 *
163 	 * If @bytes1 and @bytes2 have different length but the shorter one is a
164 	 * prefix of the longer one then the shorter one is considered to be less than
165 	 * the longer one. Otherwise the first byte where both differ is used for
166 	 * comparison. If @bytes1 has a smaller value at that position it is
167 	 * considered less, otherwise greater than @bytes2.
168 	 *
169 	 * Params:
170 	 *     bytes2 = a pointer to a #GBytes to compare with @bytes1
171 	 *
172 	 * Returns: a negative value if @bytes1 is less than @bytes2, a positive value
173 	 *     if @bytes1 is greater than @bytes2, and zero if @bytes1 is equal to
174 	 *     @bytes2
175 	 *
176 	 * Since: 2.32
177 	 */
178 	public int compare(Bytes bytes2)
179 	{
180 		return g_bytes_compare(gBytes, (bytes2 is null) ? null : bytes2.getBytesStruct());
181 	}
182 
183 	/**
184 	 * Compares the two #GBytes values being pointed to and returns
185 	 * %TRUE if they are equal.
186 	 *
187 	 * This function can be passed to g_hash_table_new() as the @key_equal_func
188 	 * parameter, when using non-%NULL #GBytes pointers as keys in a #GHashTable.
189 	 *
190 	 * Params:
191 	 *     bytes2 = a pointer to a #GBytes to compare with @bytes1
192 	 *
193 	 * Returns: %TRUE if the two keys match.
194 	 *
195 	 * Since: 2.32
196 	 */
197 	public bool equal(Bytes bytes2)
198 	{
199 		return g_bytes_equal(gBytes, (bytes2 is null) ? null : bytes2.getBytesStruct()) != 0;
200 	}
201 
202 	/**
203 	 * Get the byte data in the #GBytes. This data should not be modified.
204 	 *
205 	 * This function will always return the same pointer for a given #GBytes.
206 	 *
207 	 * %NULL may be returned if @size is 0. This is not guaranteed, as the #GBytes
208 	 * may represent an empty string with @data non-%NULL and @size as 0. %NULL will
209 	 * not be returned if @size is non-zero.
210 	 *
211 	 * Returns: a pointer to the byte data, or %NULL
212 	 *
213 	 * Since: 2.32
214 	 */
215 	public ubyte[] getData()
216 	{
217 		size_t size;
218 
219 		auto __p = g_bytes_get_data(gBytes, &size);
220 
221 		return cast(ubyte[])__p[0 .. size];
222 	}
223 
224 	/**
225 	 * Get the size of the byte data in the #GBytes.
226 	 *
227 	 * This function will always return the same value for a given #GBytes.
228 	 *
229 	 * Returns: the size
230 	 *
231 	 * Since: 2.32
232 	 */
233 	public size_t getSize()
234 	{
235 		return g_bytes_get_size(gBytes);
236 	}
237 
238 	/**
239 	 * Creates an integer hash code for the byte data in the #GBytes.
240 	 *
241 	 * This function can be passed to g_hash_table_new() as the @key_hash_func
242 	 * parameter, when using non-%NULL #GBytes pointers as keys in a #GHashTable.
243 	 *
244 	 * Returns: a hash value corresponding to the key.
245 	 *
246 	 * Since: 2.32
247 	 */
248 	public uint hash()
249 	{
250 		return g_bytes_hash(gBytes);
251 	}
252 
253 	/**
254 	 * Creates a #GBytes which is a subsection of another #GBytes. The @offset +
255 	 * @length may not be longer than the size of @bytes.
256 	 *
257 	 * A reference to @bytes will be held by the newly created #GBytes until
258 	 * the byte data is no longer needed.
259 	 *
260 	 * Since 2.56, if @offset is 0 and @length matches the size of @bytes, then
261 	 * @bytes will be returned with the reference count incremented by 1. If @bytes
262 	 * is a slice of another #GBytes, then the resulting #GBytes will reference
263 	 * the same #GBytes instead of @bytes. This allows consumers to simplify the
264 	 * usage of #GBytes when asynchronously writing to streams.
265 	 *
266 	 * Params:
267 	 *     offset = offset which subsection starts at
268 	 *     length = length of subsection
269 	 *
270 	 * Returns: a new #GBytes
271 	 *
272 	 * Since: 2.32
273 	 */
274 	public Bytes newFromBytes(size_t offset, size_t length)
275 	{
276 		auto __p = g_bytes_new_from_bytes(gBytes, offset, length);
277 
278 		if(__p is null)
279 		{
280 			return null;
281 		}
282 
283 		return new Bytes(cast(GBytes*) __p, true);
284 	}
285 
286 	alias doref = ref_;
287 	/**
288 	 * Increase the reference count on @bytes.
289 	 *
290 	 * Returns: the #GBytes
291 	 *
292 	 * Since: 2.32
293 	 */
294 	public Bytes ref_()
295 	{
296 		auto __p = g_bytes_ref(gBytes);
297 
298 		if(__p is null)
299 		{
300 			return null;
301 		}
302 
303 		return new Bytes(cast(GBytes*) __p, true);
304 	}
305 
306 	/**
307 	 * Releases a reference on @bytes.  This may result in the bytes being
308 	 * freed. If @bytes is %NULL, it will return immediately.
309 	 *
310 	 * Since: 2.32
311 	 */
312 	public void unref()
313 	{
314 		g_bytes_unref(gBytes);
315 	}
316 
317 	/**
318 	 * Unreferences the bytes, and returns a new mutable #GByteArray containing
319 	 * the same byte data.
320 	 *
321 	 * As an optimization, the byte data is transferred to the array without copying
322 	 * if this was the last reference to bytes and bytes was created with
323 	 * g_bytes_new(), g_bytes_new_take() or g_byte_array_free_to_bytes(). In all
324 	 * other cases the data is copied.
325 	 *
326 	 * Do not use it if @bytes contains more than %G_MAXUINT
327 	 * bytes. #GByteArray stores the length of its data in #guint, which
328 	 * may be shorter than #gsize, that @bytes is using.
329 	 *
330 	 * Returns: a new mutable #GByteArray containing the same byte data
331 	 *
332 	 * Since: 2.32
333 	 */
334 	public ByteArray unrefToArray()
335 	{
336 		auto __p = g_bytes_unref_to_array(gBytes);
337 
338 		if(__p is null)
339 		{
340 			return null;
341 		}
342 
343 		return new ByteArray(cast(GByteArray*) __p, true);
344 	}
345 
346 	/**
347 	 * Unreferences the bytes, and returns a pointer the same byte data
348 	 * contents.
349 	 *
350 	 * As an optimization, the byte data is returned without copying if this was
351 	 * the last reference to bytes and bytes was created with g_bytes_new(),
352 	 * g_bytes_new_take() or g_byte_array_free_to_bytes(). In all other cases the
353 	 * data is copied.
354 	 *
355 	 * Returns: a pointer to the same byte data, which should be
356 	 *     freed with g_free()
357 	 *
358 	 * Since: 2.32
359 	 */
360 	public ubyte[] unrefToData()
361 	{
362 		size_t size;
363 
364 		auto __p = g_bytes_unref_to_data(gBytes, &size);
365 
366 		return cast(ubyte[])__p[0 .. size];
367 	}
368 }