listbinding.go 805 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package binding
  2. // DataList is the base interface for all bindable data lists.
  3. //
  4. // Since: 2.0
  5. type DataList interface {
  6. DataItem
  7. GetItem(index int) (DataItem, error)
  8. Length() int
  9. }
  10. type listBase struct {
  11. base
  12. items []DataItem
  13. }
  14. // GetItem returns the DataItem at the specified index.
  15. func (b *listBase) GetItem(i int) (DataItem, error) {
  16. b.lock.RLock()
  17. defer b.lock.RUnlock()
  18. if i < 0 || i >= len(b.items) {
  19. return nil, errOutOfBounds
  20. }
  21. return b.items[i], nil
  22. }
  23. // Length returns the number of items in this data list.
  24. func (b *listBase) Length() int {
  25. b.lock.RLock()
  26. defer b.lock.RUnlock()
  27. return len(b.items)
  28. }
  29. func (b *listBase) appendItem(i DataItem) {
  30. b.items = append(b.items, i)
  31. }
  32. func (b *listBase) deleteItem(i int) {
  33. b.items = append(b.items[:i], b.items[i+1:]...)
  34. }