From ef4d842d38016f84df03dc4ff819caa90e015072 Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Tue, 13 Jan 2015 18:41:06 +0300 Subject: [PATCH] refactoring --- src/dlangui/core/collections.d | 79 ++++++++++++++++++++++++++++++++++ src/dlangui/widgets/widget.d | 75 +------------------------------- 2 files changed, 80 insertions(+), 74 deletions(-) diff --git a/src/dlangui/core/collections.d b/src/dlangui/core/collections.d index 879c1f26..50c6d574 100644 --- a/src/dlangui/core/collections.d +++ b/src/dlangui/core/collections.d @@ -221,3 +221,82 @@ struct Collection(T, bool ownItems = false) { } } + +/** object list holder, owning its objects - on destroy of holder, all own objects will be destroyed */ +struct ObjectList(T) { + protected T[] _list; + protected int _count; + /** returns count of items */ + @property int count() const { return _count; } + /** get item by index */ + T get(int index) { + assert(index >= 0 && index < _count, "child index out of range"); + return _list[index]; + } + /// get item by index + T opIndex(int index) { + return get(index); + } + /** add item to list */ + T add(T item) { + if (_list.length <= _count) // resize + _list.length = _list.length < 4 ? 4 : _list.length * 2; + _list[_count++] = item; + return item; + } + /** add item to list */ + T insert(T item, int index = -1) { + if (index > _count || index < 0) + index = _count; + if (_list.length <= _count) // resize + _list.length = _list.length < 4 ? 4 : _list.length * 2; + for (int i = _count; i > index; i--) + _list[i] = _list[i - 1]; + _list[index] = item; + _count++; + return item; + } + /** find child index for item, return -1 if not found */ + int indexOf(T item) { + for (int i = 0; i < _count; i++) + if (_list[i] == item) + return i; + return -1; + } + /** find child index for item by id, return -1 if not found */ + static if (__traits(hasMember, T, "compareId")) { + int indexOf(string id) { + for (int i = 0; i < _count; i++) + if (_list[i].compareId(id)) + return i; + return -1; + } + } + /** remove item from list, return removed item */ + T remove(int index) { + assert(index >= 0 && index < _count, "child index out of range"); + T item = _list[index]; + for (int i = index; i < _count - 1; i++) + _list[i] = _list[i + 1]; + _count--; + return item; + } + /** Replace item with another value, destroy old value. */ + void replace(T item, int index) { + T old = _list[index]; + _list[index] = item; + destroy(old); + } + /** remove and destroy all items */ + void clear() { + for (int i = 0; i < _count; i++) { + destroy(_list[i]); + _list[i] = null; + } + _count = 0; + } + ~this() { + clear(); + } +} + diff --git a/src/dlangui/widgets/widget.d b/src/dlangui/widgets/widget.d index 71ed5051..fb05fc4e 100644 --- a/src/dlangui/widgets/widget.d +++ b/src/dlangui/widgets/widget.d @@ -38,6 +38,7 @@ module dlangui.widgets.widget; public import dlangui.core.types; public import dlangui.core.events; public import dlangui.core.i18n; +public import dlangui.core.collections; public import dlangui.widgets.styles; public import dlangui.graphics.drawbuf; @@ -1263,80 +1264,6 @@ class Widget { } -/** object list holder, owning its objects - on destroy of holder, all own objects will be destroyed */ -struct ObjectList(T) { - protected T[] _list; - protected int _count; - /** returns count of items */ - @property int count() const { return _count; } - /** get item by index */ - T get(int index) { - assert(index >= 0 && index < _count, "child index out of range"); - return _list[index]; - } - /** add item to list */ - T add(T item) { - if (_list.length <= _count) // resize - _list.length = _list.length < 4 ? 4 : _list.length * 2; - _list[_count++] = item; - return item; - } - /** add item to list */ - T insert(T item, int index = -1) { - if (index > _count || index < 0) - index = _count; - if (_list.length <= _count) // resize - _list.length = _list.length < 4 ? 4 : _list.length * 2; - for (int i = _count; i > index; i--) - _list[i] = _list[i - 1]; - _list[index] = item; - _count++; - return item; - } - /** find child index for item, return -1 if not found */ - int indexOf(T item) { - for (int i = 0; i < _count; i++) - if (_list[i] == item) - return i; - return -1; - } - /** find child index for item by id, return -1 if not found */ - static if (__traits(hasMember, T, "compareId")) { - int indexOf(string id) { - for (int i = 0; i < _count; i++) - if (_list[i].compareId(id)) - return i; - return -1; - } - } - /** remove item from list, return removed item */ - T remove(int index) { - assert(index >= 0 && index < _count, "child index out of range"); - T item = _list[index]; - for (int i = index; i < _count - 1; i++) - _list[i] = _list[i + 1]; - _count--; - return item; - } - /** Replace item with another value, destroy old value. */ - void replace(T item, int index) { - T old = _list[index]; - _list[index] = item; - destroy(old); - } - /** remove and destroy all items */ - void clear() { - for (int i = 0; i < _count; i++) { - destroy(_list[i]); - _list[i] = null; - } - _count = 0; - } - ~this() { - clear(); - } -} - /** Widget list holder. */ alias WidgetList = ObjectList!Widget;