1 /*
2  * This file is part of d-dazzle.
3  *
4  * d-dazzle 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  * d-dazzle 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 d-dazzle; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 module dazzle.Ring;
20 
21 private import dazzle.c.functions;
22 public  import dazzle.c.types;
23 private import glib.ConstructionException;
24 private import glib.MemorySlice;
25 private import gobject.ObjectG;
26 private import gtkd.Loader;
27 
28 
29 /** */
30 public final class Ring
31 {
32 	/** the main Gtk struct */
33 	protected DzlRing* dzlRing;
34 	protected bool ownedRef;
35 
36 	/** Get the main Gtk struct */
37 	public DzlRing* getRingStruct(bool transferOwnership = false)
38 	{
39 		if (transferOwnership)
40 			ownedRef = false;
41 		return dzlRing;
42 	}
43 
44 	/** the main Gtk struct as a void* */
45 	protected void* getStruct()
46 	{
47 		return cast(void*)dzlRing;
48 	}
49 
50 	/**
51 	 * Sets our main struct and passes it to the parent class.
52 	 */
53 	public this (DzlRing* dzlRing, bool ownedRef = false)
54 	{
55 		this.dzlRing = dzlRing;
56 		this.ownedRef = ownedRef;
57 	}
58 
59 	~this ()
60 	{
61 		if ( Linker.isLoaded(LIBRARY_DAZZLE) && ownedRef )
62 			dzl_ring_unref(dzlRing);
63 	}
64 
65 
66 	/** */
67 	public @property ubyte* data()
68 	{
69 		return dzlRing.data;
70 	}
71 
72 	/** Ditto */
73 	public @property void data(ubyte* value)
74 	{
75 		dzlRing.data = value;
76 	}
77 
78 	/** */
79 	public @property uint len()
80 	{
81 		return dzlRing.len;
82 	}
83 
84 	/** Ditto */
85 	public @property void len(uint value)
86 	{
87 		dzlRing.len = value;
88 	}
89 
90 	/** */
91 	public @property uint pos()
92 	{
93 		return dzlRing.pos;
94 	}
95 
96 	/** Ditto */
97 	public @property void pos(uint value)
98 	{
99 		dzlRing.pos = value;
100 	}
101 
102 	/** */
103 	public static GType getType()
104 	{
105 		return dzl_ring_get_type();
106 	}
107 
108 	/**
109 	 * Creates a new instance of #DzlRing with the given number of elements.
110 	 *
111 	 * Params:
112 	 *     elementSize = The size per element.
113 	 *     reservedSize = The number of elements to allocate.
114 	 *     elementDestroy = Notification called when removing an element.
115 	 *
116 	 * Returns: A new #DzlRing.
117 	 *
118 	 * Throws: ConstructionException GTK+ fails to create the object.
119 	 */
120 	public this(uint elementSize, uint reservedSize, GDestroyNotify elementDestroy)
121 	{
122 		auto p = dzl_ring_sized_new(elementSize, reservedSize, elementDestroy);
123 
124 		if(p is null)
125 		{
126 			throw new ConstructionException("null returned by sized_new");
127 		}
128 
129 		this(cast(DzlRing*) p);
130 	}
131 
132 	/**
133 	 * Appends @len values located at @data.
134 	 *
135 	 * Params:
136 	 *     data = A pointer to the array of values.
137 	 *     len = The number of values.
138 	 *
139 	 * Returns: the index of the first item.
140 	 */
141 	public uint appendVals(void* data, uint len)
142 	{
143 		return dzl_ring_append_vals(dzlRing, data, len);
144 	}
145 
146 	alias foreac = foreach_;
147 	/**
148 	 * Calls @func for every item in the #DzlRing starting from the most recently
149 	 * inserted element to the least recently inserted.
150 	 *
151 	 * Params:
152 	 *     func = A #GFunc to call for each element.
153 	 *     userData = user data for @func.
154 	 */
155 	public void foreach_(GFunc func, void* userData)
156 	{
157 		dzl_ring_foreach(dzlRing, func, userData);
158 	}
159 
160 	alias doref = ref_;
161 	/**
162 	 * Atomically increments the reference count of @ring by one.
163 	 *
164 	 * Returns: The @ring pointer.
165 	 */
166 	public Ring ref_()
167 	{
168 		auto p = dzl_ring_ref(dzlRing);
169 
170 		if(p is null)
171 		{
172 			return null;
173 		}
174 
175 		return ObjectG.getDObject!(Ring)(cast(DzlRing*) p, true);
176 	}
177 
178 	/**
179 	 * Atomically decrements the reference count of @ring by one.  When the
180 	 * reference count reaches zero, the structure is freed.
181 	 */
182 	public void unref()
183 	{
184 		dzl_ring_unref(dzlRing);
185 	}
186 }