1 module hunt.imf.utils.room; 2 import hunt.imf.utils.element; 3 import std.traits; 4 import hunt.imf.io.context; 5 6 class Room(K = size_t , E = Element) 7 { 8 bool exists(K key) 9 { 10 synchronized(this) 11 { 12 auto e = key in _hash; 13 if(e is null) 14 return false; 15 else 16 return true; 17 } 18 } 19 20 bool add(K key , E entity) 21 { 22 synchronized(this) 23 { 24 if( key in _hash) 25 return false; 26 _hash[key] = entity; 27 return true; 28 } 29 } 30 31 void findEx(K key , void delegate(E e) dele) 32 { 33 synchronized(this) 34 { 35 auto e = key in _hash; 36 if( e is null) 37 dele(null); 38 else 39 dele(*e); 40 } 41 } 42 43 bool remove(K key) 44 { 45 synchronized(this) 46 { 47 return _hash.remove(key); 48 } 49 } 50 51 static if ( is(E == Element) || is(BaseClassesTuple!E[$-2] == Element)) 52 { 53 void broadCast(C )(C c , K[] excepts...) const 54 { 55 synchronized(this) 56 { 57 import std.algorithm.searching; 58 foreach(k , ref v ; _hash) 59 { 60 if(find!(" a == b")(excepts , k).length == 0) 61 v.context.sendMessage( c); 62 } 63 } 64 } 65 } 66 67 static if ( is(E == Element) || is(BaseClassesTuple!E[$-2] == Element)) 68 { 69 void broadCast(C , T)(C c , T t , K[] excepts...) 70 { 71 synchronized(this) 72 { 73 import std.algorithm.searching; 74 foreach(k , ref v ; _hash) 75 { 76 if(find!(" a == b")(excepts , k).length == 0) 77 v.context.sendMessage(c , t); 78 } 79 } 80 } 81 } 82 83 void traverse(void delegate( K , const E) dele) 84 { 85 synchronized(this) 86 { 87 foreach( k , v ; _hash) 88 { 89 dele(k , v); 90 } 91 } 92 } 93 94 size_t length() const 95 { 96 synchronized(this) 97 { 98 return _hash.length; 99 } 100 } 101 102 private: 103 E[K] _hash; 104 105 }