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 gobject.WeakRef;
26 
27 private import gobject.ObjectG;
28 private import gtkc.gobject;
29 public  import gtkc.gobjecttypes;
30 
31 
32 /**
33  * A structure containing a weak reference to a #GObject.  It can either
34  * be empty (i.e. point to %NULL), or point to an object for as long as
35  * at least one "strong" reference to that object exists. Before the
36  * object's #GObjectClass.dispose method is called, every #GWeakRef
37  * associated with becomes empty (i.e. points to %NULL).
38  * 
39  * Like #GValue, #GWeakRef can be statically allocated, stack- or
40  * heap-allocated, or embedded in larger structures.
41  * 
42  * Unlike g_object_weak_ref() and g_object_add_weak_pointer(), this weak
43  * reference is thread-safe: converting a weak pointer to a reference is
44  * atomic with respect to invalidation of weak pointers to destroyed
45  * objects.
46  * 
47  * If the object's #GObjectClass.dispose method results in additional
48  * references to the object being held, any #GWeakRefs taken
49  * before it was disposed will continue to point to %NULL.  If
50  * #GWeakRefs are taken after the object is disposed and
51  * re-referenced, they will continue to point to it until its refcount
52  * goes back to zero, at which point they too will be invalidated.
53  */
54 public class WeakRef
55 {
56 	/** the main Gtk struct */
57 	protected GWeakRef* gWeakRef;
58 
59 	/** Get the main Gtk struct */
60 	public GWeakRef* getWeakRefStruct()
61 	{
62 		return gWeakRef;
63 	}
64 
65 	/** the main Gtk struct as a void* */
66 	protected void* getStruct()
67 	{
68 		return cast(void*)gWeakRef;
69 	}
70 
71 	/**
72 	 * Sets our main struct and passes it to the parent class.
73 	 */
74 	public this (GWeakRef* gWeakRef)
75 	{
76 		this.gWeakRef = gWeakRef;
77 	}
78 
79 	/** */
80 	this(void* object)
81 	{
82 		g_weak_ref_init(gWeakRef, object);
83 	}
84 
85 	/**
86 	 */
87 
88 	/**
89 	 * Frees resources associated with a non-statically-allocated #GWeakRef.
90 	 * After this call, the #GWeakRef is left in an undefined state.
91 	 *
92 	 * You should only call this on a #GWeakRef that previously had
93 	 * g_weak_ref_init() called on it.
94 	 *
95 	 * Since: 2.32
96 	 */
97 	public void clear()
98 	{
99 		g_weak_ref_clear(gWeakRef);
100 	}
101 
102 	/**
103 	 * If @weak_ref is not empty, atomically acquire a strong
104 	 * reference to the object it points to, and return that reference.
105 	 *
106 	 * This function is needed because of the potential race between taking
107 	 * the pointer value and g_object_ref() on it, if the object was losing
108 	 * its last reference at the same time in a different thread.
109 	 *
110 	 * The caller should release the resulting reference in the usual way,
111 	 * by using g_object_unref().
112 	 *
113 	 * Return: the object pointed to
114 	 *     by @weak_ref, or %NULL if it was empty
115 	 *
116 	 * Since: 2.32
117 	 */
118 	public ObjectG get()
119 	{
120 		auto p = g_weak_ref_get(gWeakRef);
121 		
122 		if(p is null)
123 		{
124 			return null;
125 		}
126 		
127 		return ObjectG.getDObject!(ObjectG)(cast(GObject*) p, true);
128 	}
129 
130 	/**
131 	 * Initialise a non-statically-allocated #GWeakRef.
132 	 *
133 	 * This function also calls g_weak_ref_set() with @object on the
134 	 * freshly-initialised weak reference.
135 	 *
136 	 * This function should always be matched with a call to
137 	 * g_weak_ref_clear().  It is not necessary to use this function for a
138 	 * #GWeakRef in static storage because it will already be
139 	 * properly initialised.  Just use g_weak_ref_set() directly.
140 	 *
141 	 * Params:
142 	 *     object = a #GObject or %NULL
143 	 *
144 	 * Since: 2.32
145 	 */
146 	public void init(void* object)
147 	{
148 		g_weak_ref_init(gWeakRef, object);
149 	}
150 
151 	/**
152 	 * Change the object to which @weak_ref points, or set it to
153 	 * %NULL.
154 	 *
155 	 * You must own a strong reference on @object while calling this
156 	 * function.
157 	 *
158 	 * Params:
159 	 *     object = a #GObject or %NULL
160 	 *
161 	 * Since: 2.32
162 	 */
163 	public void set(void* object)
164 	{
165 		g_weak_ref_set(gWeakRef, object);
166 	}
167 }