1 /**
2 Holds an $(D GodotObject), but does not contribute to the reference count if the object is a reference.
3 
4 Copyright:
5 Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur.  
6 Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md)  
7 Copyright (c) 2017-2018 Godot-D contributors  
8 
9 License: $(LINK2 https://opensource.org/licenses/MIT, MIT License)
10 
11 
12 */
13 module godot.weakref;
14 import std.meta : AliasSeq, staticIndexOf;
15 import std.traits : Unqual;
16 import godot.d.meta;
17 import godot.core;
18 import godot.c;
19 import godot.d.bind;
20 import godot.d.reference;
21 import godot.object;
22 import godot.classdb;
23 import godot.reference;
24 /**
25 Holds an $(D GodotObject), but does not contribute to the reference count if the object is a reference.
26 
27 A weakref can hold a $(D Reference), without contributing to the reference counter. A weakref can be created from an $(D GodotObject) using $(D @GDScript.weakref). If this object is not a reference, weakref still works, however, it does not have any effect on the object. Weakrefs are useful in cases where multiple classes have variables that refer to each other. Without weakrefs, using these classes could lead to memory leaks, since both references keep each other from being released. Making part of the variables a weakref can prevent this cyclic dependency, and allows the references to be released.
28 */
29 @GodotBaseClass struct WeakRef
30 {
31 	enum string _GODOT_internal_name = "WeakRef";
32 public:
33 @nogc nothrow:
34 	union { godot_object _godot_object; Reference _GODOT_base; }
35 	alias _GODOT_base this;
36 	alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses);
37 	package(godot) __gshared bool _classBindingInitialized = false;
38 	package(godot) static struct _classBinding
39 	{
40 		__gshared:
41 		@GodotName("get_ref") GodotMethod!(Variant) getRef;
42 	}
43 	bool opEquals(in WeakRef other) const { return _godot_object.ptr is other._godot_object.ptr; }
44 	WeakRef opAssign(T : typeof(null))(T n) { _godot_object.ptr = null; }
45 	bool opEquals(typeof(null) n) const { return _godot_object.ptr is null; }
46 	mixin baseCasts;
47 	static WeakRef _new()
48 	{
49 		static godot_class_constructor constructor;
50 		if(constructor is null) constructor = _godot_api.godot_get_class_constructor("WeakRef");
51 		if(constructor is null) return typeof(this).init;
52 		return cast(WeakRef)(constructor());
53 	}
54 	@disable new(size_t s);
55 	/**
56 	Returns the $(D GodotObject) this weakref is referring to.
57 	*/
58 	Variant getRef() const
59 	{
60 		checkClassBinding!(typeof(this))();
61 		return ptrcall!(Variant)(_classBinding.getRef, _godot_object);
62 	}
63 }