1 module datadriven.storage; 2 3 import datadriven.api; 4 import cbor; 5 6 struct HashmapComponentStorage(ComponentType) 7 { 8 private ComponentType[EntityId] components; 9 10 void add(EntityId eid, ComponentType component) 11 { 12 //assert(eid !in components); 13 components[eid] = component; 14 } 15 16 void remove(EntityId eid) 17 { 18 components.remove(eid); 19 } 20 21 void removeAll() 22 { 23 components = null; 24 } 25 26 size_t length() @property 27 { 28 return components.length; 29 } 30 31 ComponentType* get(EntityId eid) 32 { 33 return eid in components; 34 } 35 36 auto byKeyValue() @property 37 { 38 return components.byKeyValue; 39 } 40 41 size_t serialize(ubyte[] sink) 42 { 43 size_t size = encodeCborMapHeader(sink[], components.length); 44 foreach(keyValue; components.byKeyValue) { 45 size += encodeCbor(sink[size..$], keyValue.key); 46 size += encodeCbor(sink[size..$], keyValue.value); 47 } 48 return size; 49 } 50 51 void deserialize(ubyte[] input) 52 { 53 components = null;//.clear(); //introduced in 2.071 54 decodeCbor(input, components); 55 } 56 } 57 58 static assert(isComponentStorage!(HashmapComponentStorage!int, int));