1 /** 2 Base class for anything that keeps a reference count. 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.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 /** 24 Base class for anything that keeps a reference count. 25 26 Resource and many other helper objects inherit this. References keep an internal reference counter so they are only released when no longer in use. 27 */ 28 @GodotBaseClass struct Reference 29 { 30 enum string _GODOT_internal_name = "Reference"; 31 public: 32 @nogc nothrow: 33 union { godot_object _godot_object; GodotObject _GODOT_base; } 34 alias _GODOT_base this; 35 alias BaseClasses = AliasSeq!(typeof(_GODOT_base), typeof(_GODOT_base).BaseClasses); 36 package(godot) __gshared bool _classBindingInitialized = false; 37 package(godot) static struct _classBinding 38 { 39 __gshared: 40 @GodotName("init_ref") GodotMethod!(bool) initRef; 41 @GodotName("reference") GodotMethod!(bool) reference; 42 @GodotName("unreference") GodotMethod!(bool) unreference; 43 } 44 bool opEquals(in Reference other) const { return _godot_object.ptr is other._godot_object.ptr; } 45 Reference opAssign(T : typeof(null))(T n) { _godot_object.ptr = null; } 46 bool opEquals(typeof(null) n) const { return _godot_object.ptr is null; } 47 mixin baseCasts; 48 static Reference _new() 49 { 50 static godot_class_constructor constructor; 51 if(constructor is null) constructor = _godot_api.godot_get_class_constructor("Reference"); 52 if(constructor is null) return typeof(this).init; 53 return cast(Reference)(constructor()); 54 } 55 @disable new(size_t s); 56 /** 57 58 */ 59 bool initRef() 60 { 61 checkClassBinding!(typeof(this))(); 62 return ptrcall!(bool)(_classBinding.initRef, _godot_object); 63 } 64 /** 65 Increase the internal reference counter. Use this only if you really know what you are doing. 66 */ 67 bool reference() 68 { 69 checkClassBinding!(typeof(this))(); 70 return ptrcall!(bool)(_classBinding.reference, _godot_object); 71 } 72 /** 73 Decrease the internal reference counter. Use this only if you really know what you are doing. 74 */ 75 bool unreference() 76 { 77 checkClassBinding!(typeof(this))(); 78 return ptrcall!(bool)(_classBinding.unreference, _godot_object); 79 } 80 }