// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. namespace WixToolset.Dtf.Compression.Cab { using System; using System.Collections.Generic; /// /// Generic class for managing allocations of integer handles /// for objects of a certain type. /// /// The type of objects the handles refer to. internal sealed class HandleManager where T : class { /// /// Auto-resizing list of objects for which handles have been allocated. /// Each handle is just an index into this list. When a handle is freed, /// the list item at that index is set to null. /// private List handles; /// /// Creates a new HandleManager instance. /// public HandleManager() { this.handles = new List(); } /// /// Gets the object of a handle, or null if the handle is invalid. /// /// The integer handle previously allocated /// for the desired object. /// The object for which the handle was allocated. public T this[int handle] { get { if (handle > 0 && handle <= this.handles.Count) { return this.handles[handle - 1]; } else { return null; } } } /// /// Allocates a new handle for an object. /// /// Object that the handle will refer to. /// New handle that can be later used to retrieve the object. public int AllocHandle(T obj) { this.handles.Add(obj); int handle = this.handles.Count; return handle; } /// /// Frees a handle that was previously allocated. Afterward the handle /// will be invalid and the object it referred to can no longer retrieved. /// /// Handle to be freed. public void FreeHandle(int handle) { if (handle > 0 && handle <= this.handles.Count) { this.handles[handle - 1] = null; } } } }