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.traits;
17 import godot.core;
18 import godot.c;
19 import godot.d.bind;
20 import godot.d.reference;
21 import godot.globalenums;
22 import godot.object;
23 import godot.classdb;
24 import godot.reference;
25 /**
26 Holds an $(D GodotObject), but does not contribute to the reference count if the object is a reference.
27 
28 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.
29 */
30 @GodotBaseClass struct WeakRef
31 {
32 	package(godot) enum string _GODOT_internal_name = "WeakRef";
33 public:
34 @nogc nothrow:
35 	union { /** */ godot_object _godot_object; /** */ Reference _GODOT_base; }
36 	alias _GODOT_base this;
37 	alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses);
38 	package(godot) __gshared bool _classBindingInitialized = false;
39 	package(godot) static struct GDNativeClassBinding
40 	{
41 		__gshared:
42 		@GodotName("get_ref") GodotMethod!(Variant) getRef;
43 	}
44 	/// 
45 	pragma(inline, true) bool opEquals(in WeakRef other) const
46 	{ return _godot_object.ptr is other._godot_object.ptr; }
47 	/// 
48 	pragma(inline, true) typeof(null) opAssign(typeof(null) n)
49 	{ _godot_object.ptr = n; return null; }
50 	/// 
51 	pragma(inline, true) bool opEquals(typeof(null) n) const
52 	{ return _godot_object.ptr is n; }
53 	/// 
54 	size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; }
55 	mixin baseCasts;
56 	/// Construct a new instance of WeakRef.
57 	/// Note: use `memnew!WeakRef` instead.
58 	static WeakRef _new()
59 	{
60 		static godot_class_constructor constructor;
61 		if(constructor is null) constructor = _godot_api.godot_get_class_constructor("WeakRef");
62 		if(constructor is null) return typeof(this).init;
63 		return cast(WeakRef)(constructor());
64 	}
65 	@disable new(size_t s);
66 	/**
67 	Returns the $(D GodotObject) this weakref is referring to.
68 	*/
69 	Variant getRef() const
70 	{
71 		checkClassBinding!(typeof(this))();
72 		return ptrcall!(Variant)(GDNativeClassBinding.getRef, _godot_object);
73 	}
74 }