1 module javascriptcore.WeakValue;
2 
3 private import glib.ConstructionException;
4 private import gobject.ObjectG;
5 private import gobject.Signals;
6 private import javascriptcore.Value;
7 private import javascriptcore.c.functions;
8 public  import javascriptcore.c.types;
9 private import std.algorithm;
10 
11 
12 /**
13  * JSCWeakValue represents a weak reference to a value in a #JSCContext. It can be used
14  * to keep a reference to a JavaScript value without protecting it from being garbage
15  * collected and without referencing the #JSCContext either.
16  */
17 public class WeakValue : ObjectG
18 {
19 	/** the main Gtk struct */
20 	protected JSCWeakValue* jSCWeakValue;
21 
22 	/** Get the main Gtk struct */
23 	public JSCWeakValue* getWeakValueStruct(bool transferOwnership = false)
24 	{
25 		if (transferOwnership)
26 			ownedRef = false;
27 		return jSCWeakValue;
28 	}
29 
30 	/** the main Gtk struct as a void* */
31 	protected override void* getStruct()
32 	{
33 		return cast(void*)jSCWeakValue;
34 	}
35 
36 	/**
37 	 * Sets our main struct and passes it to the parent class.
38 	 */
39 	public this (JSCWeakValue* jSCWeakValue, bool ownedRef = false)
40 	{
41 		this.jSCWeakValue = jSCWeakValue;
42 		super(cast(GObject*)jSCWeakValue, ownedRef);
43 	}
44 
45 
46 	/** */
47 	public static GType getType()
48 	{
49 		return jsc_weak_value_get_type();
50 	}
51 
52 	/**
53 	 * Create a new #JSCWeakValue for the JavaScript value referenced by @value.
54 	 *
55 	 * Params:
56 	 *     value = a #JSCValue
57 	 *
58 	 * Returns: a new #JSCWeakValue
59 	 *
60 	 * Throws: ConstructionException GTK+ fails to create the object.
61 	 */
62 	public this(Value value)
63 	{
64 		auto __p = jsc_weak_value_new((value is null) ? null : value.getValueStruct());
65 
66 		if(__p is null)
67 		{
68 			throw new ConstructionException("null returned by new");
69 		}
70 
71 		this(cast(JSCWeakValue*) __p, true);
72 	}
73 
74 	/**
75 	 * Get a #JSCValue referencing the JavaScript value of @weak_value.
76 	 *
77 	 * Returns: a new #JSCValue or %NULL if @weak_value was cleared.
78 	 */
79 	public Value getValue()
80 	{
81 		auto __p = jsc_weak_value_get_value(jSCWeakValue);
82 
83 		if(__p is null)
84 		{
85 			return null;
86 		}
87 
88 		return ObjectG.getDObject!(Value)(cast(JSCValue*) __p, true);
89 	}
90 
91 	/**
92 	 * This signal is emitted when the JavaScript value is destroyed.
93 	 */
94 	gulong addOnCleared(void delegate(WeakValue) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0)
95 	{
96 		return Signals.connect(this, "cleared", dlg, connectFlags ^ ConnectFlags.SWAPPED);
97 	}
98 }