1 /** 2 Base class for reference-counted objects. 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.reference; 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 /** 25 Base class for reference-counted objects. 26 27 Base class for any object that keeps a reference count. $(D Resource) and many other helper objects inherit this class. 28 Unlike other $(D GodotObject) types, References keep an internal reference counter so that they are automatically released when no longer in use, and only then. References therefore do not need to be freed manually with $(D GodotObject.free). 29 In the vast majority of use cases, instantiating and using $(D Reference)-derived types is all you need to do. The methods provided in this class are only for advanced users, and can cause issues if misused. 30 $(B Note:) In C#, references will not be freed instantly after they are no longer in use. Instead, garbage collection will run periodically and will free references that are no longer in use. This means that unused references will linger on for a while before being removed. 31 */ 32 @GodotBaseClass struct Reference 33 { 34 package(godot) enum string _GODOT_internal_name = "Reference"; 35 public: 36 @nogc nothrow: 37 union { /** */ godot_object _godot_object; /** */ GodotObject _GODOT_base; } 38 alias _GODOT_base this; 39 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 40 package(godot) __gshared bool _classBindingInitialized = false; 41 package(godot) static struct GDNativeClassBinding 42 { 43 __gshared: 44 @GodotName("init_ref") GodotMethod!(bool) initRef; 45 @GodotName("reference") GodotMethod!(bool) reference; 46 @GodotName("unreference") GodotMethod!(bool) unreference; 47 } 48 /// 49 pragma(inline, true) bool opEquals(in Reference other) const 50 { return _godot_object.ptr is other._godot_object.ptr; } 51 /// 52 pragma(inline, true) typeof(null) opAssign(typeof(null) n) 53 { _godot_object.ptr = n; return null; } 54 /// 55 pragma(inline, true) bool opEquals(typeof(null) n) const 56 { return _godot_object.ptr is n; } 57 /// 58 size_t toHash() const @trusted { return cast(size_t)_godot_object.ptr; } 59 mixin baseCasts; 60 /// Construct a new instance of Reference. 61 /// Note: use `memnew!Reference` instead. 62 static Reference _new() 63 { 64 static godot_class_constructor constructor; 65 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("Reference"); 66 if(constructor is null) return typeof(this).init; 67 return cast(Reference)(constructor()); 68 } 69 @disable new(size_t s); 70 /** 71 Initializes the internal reference counter. Use this only if you really know what you are doing. 72 Returns whether the initialization was successful. 73 */ 74 bool initRef() 75 { 76 checkClassBinding!(typeof(this))(); 77 return ptrcall!(bool)(GDNativeClassBinding.initRef, _godot_object); 78 } 79 /** 80 Increments the internal reference counter. Use this only if you really know what you are doing. 81 Returns `true` if the increment was successful, `false` otherwise. 82 */ 83 bool reference() 84 { 85 checkClassBinding!(typeof(this))(); 86 return ptrcall!(bool)(GDNativeClassBinding.reference, _godot_object); 87 } 88 /** 89 Decrements the internal reference counter. Use this only if you really know what you are doing. 90 Returns `true` if the decrement was successful, `false` otherwise. 91 */ 92 bool unreference() 93 { 94 checkClassBinding!(typeof(this))(); 95 return ptrcall!(bool)(GDNativeClassBinding.unreference, _godot_object); 96 } 97 }