1 /** 2 Copyright: Copyright (c) 2014 Andrey Penechko. 3 License: a$(WEB boost.org/LICENSE_1_0.txt, Boost License 1.0). 4 Authors: Andrey Penechko. 5 */ 6 7 module anchovy.gui.databinding.list; 8 9 import anchovy.utils.signal : Signal; 10 11 abstract class List(ItemType) 12 { 13 alias ItemAddedSignal = Signal!(size_t, ItemType); 14 alias ItemRemovedSignal = Signal!(size_t, ItemType); 15 alias ItemChangedSignal = Signal!(size_t, ItemType); 16 alias ListChangedSignal = Signal!(); 17 18 ItemAddedSignal itemAddedSignal; 19 ItemRemovedSignal itemRemovedSignal; 20 ItemChangedSignal itemChangedSignal; 21 ListChangedSignal listChangedSignal; 22 23 24 ItemType opIndex(size_t index); 25 ItemType opIndexAssign(ItemType data, size_t index); 26 27 size_t length() @property; 28 29 size_t push(ItemType item); 30 ItemType remove(size_t index); 31 32 final ItemType pop() 33 { 34 return remove(length - 1); 35 } 36 } 37 38 import std.algorithm : remove; 39 40 class SimpleList(ItemType) : List!ItemType 41 { 42 protected: 43 ItemType[] _array; 44 45 public: 46 47 override ItemType opIndex(size_t index) 48 { 49 return _array[index]; 50 } 51 52 override ItemType opIndexAssign(ItemType data, size_t index) 53 { 54 _array[index] = data; 55 listChangedSignal.emit(); 56 itemChangedSignal.emit(index, _array[index]); 57 58 return _array[index]; 59 } 60 61 override size_t length() @property 62 { 63 return _array.length; 64 } 65 66 override size_t push(ItemType item) 67 { 68 _array ~= item; 69 70 listChangedSignal.emit(); 71 itemAddedSignal.emit(length - 1, _array[length-1]); 72 73 return length - 1; 74 } 75 76 override ItemType remove(size_t index) 77 { 78 ItemType item = _array[index]; 79 _array.remove(index); 80 81 listChangedSignal.emit(); 82 itemRemovedSignal.emit(index, item); 83 84 return item; 85 } 86 }