1 /** 2 Variant hashmap/dictionary type. 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.core.dictionary; 14 15 import godot.c; 16 import godot.core; 17 18 /** 19 Associative container which contains values referenced by unique keys. Dictionaries are always passed by reference. 20 */ 21 struct Dictionary 22 { 23 int opApply(int delegate(const(Variant), ref Variant) dg) 24 { 25 const(godot_variant)* k = _godot_api.godot_dictionary_next(&_godot_dictionary, null); 26 while(k) 27 { 28 Variant* v = cast(Variant*)_godot_api.godot_dictionary_operator_index( 29 &_godot_dictionary, k); 30 int res = dg(*cast(const(Variant*))k, *v); 31 if(res) return res; 32 k = _godot_api.godot_dictionary_next(&_godot_dictionary, k); 33 } 34 return 0; 35 } 36 37 int opApply(int delegate(const(Variant), ref const(Variant)) dg) const 38 { 39 const(godot_variant)* k = _godot_api.godot_dictionary_next(&_godot_dictionary, null); 40 while(k) 41 { 42 Variant* v = cast(Variant*)_godot_api.godot_dictionary_operator_index( 43 cast(godot_dictionary*)&_godot_dictionary, k); 44 int res = dg(*cast(const(Variant*))k, *v); 45 if(res) return res; 46 k = _godot_api.godot_dictionary_next(&_godot_dictionary, k); 47 } 48 return 0; 49 } 50 51 52 @nogc nothrow: 53 54 package(godot) godot_dictionary _godot_dictionary; 55 56 @disable this(); 57 58 this(this) 59 { 60 const godot_dictionary tmp = _godot_dictionary; 61 _godot_api.godot_dictionary_new_copy(&_godot_dictionary, &tmp); 62 } 63 64 Dictionary opAssign(in Dictionary other) 65 { 66 _godot_api.godot_dictionary_destroy(&_godot_dictionary); 67 _godot_api.godot_dictionary_new_copy(&_godot_dictionary, &other._godot_dictionary); 68 return this; 69 } 70 71 /// FIXME: naming convention fail again 72 static Dictionary empty_dictionary() 73 { 74 Dictionary d = void; 75 _godot_api.godot_dictionary_new(&d._godot_dictionary); 76 return d; 77 } 78 79 void clear() 80 { 81 _godot_api.godot_dictionary_clear(&_godot_dictionary); 82 } 83 84 bool empty() const 85 { 86 return cast(bool)_godot_api.godot_dictionary_empty(&_godot_dictionary); 87 } 88 89 void erase(K)(in K key) if(is(K : Variant) || Variant.compatibleToGodot!K) 90 { 91 const Variant k = key; 92 _godot_api.godot_dictionary_erase(&_godot_dictionary, &k._godot_variant); 93 } 94 95 bool has(K)(in K key) const if(is(K : Variant) || Variant.compatibleToGodot!K) 96 { 97 const Variant k = key; 98 return cast(bool)_godot_api.godot_dictionary_has(&_godot_dictionary, &k._godot_variant); 99 } 100 101 bool hasAll(in Array keys) const 102 { 103 return cast(bool)_godot_api.godot_dictionary_has_all(&_godot_dictionary, &keys._godot_array); 104 } 105 106 uint hash() const 107 { 108 return _godot_api.godot_dictionary_hash(&_godot_dictionary); 109 } 110 111 Array keys() const 112 { 113 Array a = void; 114 a._godot_array = _godot_api.godot_dictionary_keys(&_godot_dictionary); 115 return a; 116 } 117 118 Variant* opIndex(K)(in K key) if(is(K : Variant) || Variant.compatibleToGodot!K) 119 { 120 const Variant k = key; 121 return cast(Variant*)_godot_api.godot_dictionary_operator_index(&_godot_dictionary, &k._godot_variant); 122 } 123 124 Variant opIndex(K)(in K key) const if(is(K : Variant) || Variant.compatibleToGodot!K) 125 { 126 const Variant k = key; 127 Variant ret = void; 128 ret._godot_variant = _godot_api.godot_dictionary_get(&_godot_dictionary, &k._godot_variant); 129 return ret; 130 } 131 132 void opIndexAssign(K, V)(in auto ref V value, in auto ref K key) if( 133 (is(K : Variant) || Variant.compatibleToGodot!K) && 134 (is(V : Variant) || Variant.compatibleToGodot!V) ) 135 { 136 const Variant k = key; 137 const Variant v = value; 138 _godot_api.godot_dictionary_set(&_godot_dictionary, &k._godot_variant, &v._godot_variant); 139 } 140 141 int size() const 142 { 143 return _godot_api.godot_dictionary_size(&_godot_dictionary); 144 } 145 146 String toJson() const 147 { 148 godot_string s = _godot_api.godot_dictionary_to_json(&_godot_dictionary); 149 return cast(String)s; 150 } 151 152 Array values() const 153 { 154 godot_array a = _godot_api.godot_dictionary_values(&_godot_dictionary); 155 return cast(Array)a; 156 } 157 158 ~this() 159 { 160 _godot_api.godot_dictionary_destroy(&_godot_dictionary); 161 } 162 } 163