// Copyright (C) Stichting Deltares 2017. All rights reserved. // // This file is part of the Dam Engine. // // The Dam Engine is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. namespace Deltares.DamEngine.Data.Standard { /// /// Delegate void SingleArgumentDelegate /// /// /// The argument. public delegate void SingleArgumentDelegate(T argument) where T : class; /// /// Extends the standard List<T> class with delegates which are called on adding or removing items /// /// The generic type to create the list for public class DelegatedList : VirtualList where T : class { private SingleArgumentDelegate addMethod; /// /// Initializes a new instance of the class. /// public DelegatedList() {} /// /// Initializes a new instance of the class. /// /// The add method. public DelegatedList(SingleArgumentDelegate addMethod) { AddMethod = addMethod; } /// /// Sets the add method. /// /// /// The add method. /// public SingleArgumentDelegate AddMethod { set { addMethod = value; } } private void InvokeAddMethod(T newItem) { if (addMethod != null) { addMethod(newItem); } } #region List Overrides /// /// Gets or sets the element at the specified index. /// /// The index. /// public override T this[int index] { get { return base[index]; } set { T newItem = value; base[index] = newItem; InvokeAddMethod(newItem); } } /// /// Adds the specified item. /// /// The item. public override void Add(T item) { base.Add(item); InvokeAddMethod(item); } /// /// Clears this instance. /// public override void Clear() { foreach (var itemToRemove in ToArray()) { Remove(itemToRemove); } } /// /// Removes the specified item. /// /// The item. /// public override bool Remove(T item) { bool removed = base.Remove(item); return removed; } /// /// Inserts the specified index. /// /// The index. /// The item. public override void Insert(int index, T item) { base.Insert(index, item); InvokeAddMethod(item); } #endregion } }