File: System\Windows\Media\Generated\PointCollection.cs
Web Access
Project: src\src\Microsoft.DotNet.Wpf\src\PresentationCore\PresentationCore.csproj (PresentationCore)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
 
//
//
// This file was generated, please do not edit it directly.
//
// Please see MilCodeGen.html for more information.
//
 
using MS.Internal;
using MS.Internal.KnownBoxes;
using MS.Internal.Collections;
using MS.Internal.PresentationCore;
using MS.Utility;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime.InteropServices;
using System.ComponentModel.Design.Serialization;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Effects;
using System.Windows.Media.Media3D;
using System.Windows.Media.Animation;
using System.Windows.Media.Composition;
using System.Windows.Media.Imaging;
using System.Windows.Markup;
using System.Windows.Media.Converters;
using System.Security;
using SR=MS.Internal.PresentationCore.SR;
// These types are aliased to match the unamanaged names used in interop
using BOOL = System.UInt32;
using WORD = System.UInt16;
using Float = System.Single;
 
namespace System.Windows.Media
{
    /// <summary>
    /// A collection of Points.
    /// </summary>
 
    [TypeConverter(typeof(PointCollectionConverter))]
    [ValueSerializer(typeof(PointCollectionValueSerializer))] // Used by MarkupWriter
    public sealed partial class PointCollection : Freezable, IFormattable, IList, IList<Point>
    {
        //------------------------------------------------------
        //
        //  Public Methods
        //
        //------------------------------------------------------
 
        #region Public Methods
 
        /// <summary>
        ///     Shadows inherited Clone() with a strongly typed
        ///     version for convenience.
        /// </summary>
        public new PointCollection Clone()
        {
            return (PointCollection)base.Clone();
        }
 
        /// <summary>
        ///     Shadows inherited CloneCurrentValue() with a strongly typed
        ///     version for convenience.
        /// </summary>
        public new PointCollection CloneCurrentValue()
        {
            return (PointCollection)base.CloneCurrentValue();
        }
 
 
 
 
        #endregion Public Methods
 
        //------------------------------------------------------
        //
        //  Public Properties
        //
        //------------------------------------------------------
 
 
        #region IList<T>
 
        /// <summary>
        ///     Adds "value" to the list
        /// </summary>
        public void Add(Point value)
        {
            AddHelper(value);
        }
 
        /// <summary>
        ///     Removes all elements from the list
        /// </summary>
        public void Clear()
        {
            WritePreamble();
 
            _collection.Clear();
 
            ++_version;
            WritePostscript();
        }
 
        /// <summary>
        ///     Determines if the list contains "value"
        /// </summary>
        public bool Contains(Point value)
        {
            ReadPreamble();
 
            return _collection.Contains(value);
        }
 
        /// <summary>
        ///     Returns the index of "value" in the list
        /// </summary>
        public int IndexOf(Point value)
        {
            ReadPreamble();
 
            return _collection.IndexOf(value);
        }
 
        /// <summary>
        ///     Inserts "value" into the list at the specified position
        /// </summary>
        public void Insert(int index, Point value)
        {
            WritePreamble();
            _collection.Insert(index, value);
 
 
            ++_version;
            WritePostscript();
        }
 
        /// <summary>
        ///     Removes "value" from the list
        /// </summary>
        public bool Remove(Point value)
        {
            WritePreamble();
            int index = IndexOf(value);
            if (index >= 0)
            {
                // we already have index from IndexOf so instead of using Remove,
                // which will search the collection a second time, we'll use RemoveAt
                _collection.RemoveAt(index);
 
                ++_version;
                WritePostscript();
 
                return true;
            }
 
            // Collection_Remove returns true, calls WritePostscript,
            // increments version, and does UpdateResource if it succeeds
 
            return false;
        }
 
        /// <summary>
        ///     Removes the element at the specified index
        /// </summary>
        public void RemoveAt(int index)
        {
            RemoveAtWithoutFiringPublicEvents(index);
 
            // RemoveAtWithoutFiringPublicEvents incremented the version
 
            WritePostscript();
        }
 
 
        /// <summary>
        ///     Removes the element at the specified index without firing
        ///     the public Changed event.
        ///     The caller - typically a public method - is responsible for calling
        ///     WritePostscript if appropriate.
        /// </summary>
        internal void RemoveAtWithoutFiringPublicEvents(int index)
        {
            WritePreamble();
            _collection.RemoveAt(index);
 
 
            ++_version;
 
            // No WritePostScript to avoid firing the Changed event.
        }
 
 
        /// <summary>
        ///     Indexer for the collection
        /// </summary>
        public Point this[int index]
        {
            get
            {
                ReadPreamble();
 
                return _collection[index];
            }
            set
            {
                WritePreamble();
                _collection[ index ] = value;
 
 
                ++_version;
                WritePostscript();
            }
        }
 
        #endregion
 
        #region ICollection<T>
 
        /// <summary>
        ///     The number of elements contained in the collection.
        /// </summary>
        public int Count
        {
            get
            {
                ReadPreamble();
 
                return _collection.Count;
            }
        }
 
        /// <summary>
        ///     Copies the elements of the collection into "array" starting at "index"
        /// </summary>
        public void CopyTo(Point[] array, int index)
        {
            ReadPreamble();
 
            ArgumentNullException.ThrowIfNull(array);
 
            // This will not throw in the case that we are copying
            // from an empty collection.  This is consistent with the
            // BCL Collection implementations. (Windows 1587365)
            ArgumentOutOfRangeException.ThrowIfNegative(index);
            ArgumentOutOfRangeException.ThrowIfGreaterThan(index, array.Length - _collection.Count);
 
            _collection.CopyTo(array, index);
        }
 
        bool ICollection<Point>.IsReadOnly
        {
            get
            {
                ReadPreamble();
 
                return IsFrozen;
            }
        }
 
        #endregion
 
        #region IEnumerable<T>
 
        /// <summary>
        /// Returns an enumerator for the collection
        /// </summary>
        public Enumerator GetEnumerator()
        {
            ReadPreamble();
 
            return new Enumerator(this);
        }
 
        IEnumerator<Point> IEnumerable<Point>.GetEnumerator()
        {
            return this.GetEnumerator();
        }
 
        #endregion
 
        #region IList
 
        bool IList.IsReadOnly
        {
            get
            {
                return ((ICollection<Point>)this).IsReadOnly;
            }
        }
 
        bool IList.IsFixedSize
        {
            get
            {
                ReadPreamble();
 
                return IsFrozen;
            }
        }
 
        object IList.this[int index]
        {
            get
            {
                return this[index];
            }
            set
            {
                // Forwards to typed implementation
                this[index] = Cast(value);
            }
        }
 
        int IList.Add(object value)
        {
            // Forward to typed helper
            return AddHelper(Cast(value));
        }
 
        bool IList.Contains(object value)
        {
            if (value is Point)
            {
                return Contains((Point)value);
            }
 
            return false;
        }
 
        int IList.IndexOf(object value)
        {
            if (value is Point)
            {
                return IndexOf((Point)value);
            }
 
            return -1;
        }
 
        void IList.Insert(int index, object value)
        {
            // Forward to IList<T> Insert
            Insert(index, Cast(value));
        }
 
        void IList.Remove(object value)
        {
            if (value is Point)
            {
                Remove((Point)value);
            }
        }
 
        #endregion
 
        #region ICollection
 
        void ICollection.CopyTo(Array array, int index)
        {
            ReadPreamble();
 
            ArgumentNullException.ThrowIfNull(array);
 
            // This will not throw in the case that we are copying
            // from an empty collection.  This is consistent with the
            // BCL Collection implementations. (Windows 1587365)
            ArgumentOutOfRangeException.ThrowIfNegative(index);
            ArgumentOutOfRangeException.ThrowIfGreaterThan(index, array.Length - _collection.Count);
 
            if (array.Rank != 1)
            {
                throw new ArgumentException(SR.Collection_BadRank);
            }
 
            // Elsewhere in the collection we throw an AE when the type is
            // bad so we do it here as well to be consistent
            try
            {
                int count = _collection.Count;
                for (int i = 0; i < count; i++)
                {
                    array.SetValue(_collection[i], index + i);
                }
            }
            catch (InvalidCastException e)
            {
                throw new ArgumentException(SR.Format(SR.Collection_BadDestArray, this.GetType().Name), e);
            }
        }
 
        bool ICollection.IsSynchronized
        {
            get
            {
                ReadPreamble();
 
                return IsFrozen || Dispatcher != null;
            }
        }
 
        object ICollection.SyncRoot
        {
            get
            {
                ReadPreamble();
                return this;
            }
        }
        #endregion
 
        #region IEnumerable
 
        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
 
        #endregion
 
        #region Internal Helpers
 
        /// <summary>
        /// A frozen empty PointCollection.
        /// </summary>
        internal static PointCollection Empty
        {
            get
            {
                if (s_empty == null)
                {
                    PointCollection collection = new PointCollection();
                    collection.Freeze();
                    s_empty = collection;
                }
 
                return s_empty;
            }
        }
 
        /// <summary>
        /// Helper to return read only access.
        /// </summary>
        internal Point Internal_GetItem(int i)
        {
            return _collection[i];
        }
 
 
 
        #endregion
 
        #region Private Helpers
 
        private Point Cast(object value)
        {
            ArgumentNullException.ThrowIfNull(value);
 
            if (!(value is Point))
            {
                throw new System.ArgumentException(SR.Format(SR.Collection_BadType, this.GetType().Name, value.GetType().Name, "Point"));
            }
 
            return (Point) value;
        }
 
        // IList.Add returns int and IList<T>.Add does not. This
        // is called by both Adds and IList<T>'s just ignores the
        // integer
        private int AddHelper(Point value)
        {
            int index = AddWithoutFiringPublicEvents(value);
 
            // AddAtWithoutFiringPublicEvents incremented the version
 
            WritePostscript();
 
            return index;
        }
 
        internal int AddWithoutFiringPublicEvents(Point value)
        {
            int index = -1;
 
 
            WritePreamble();
            index = _collection.Add(value);
 
 
            ++_version;
 
            // No WritePostScript to avoid firing the Changed event.
 
            return index;
        }
 
 
 
        #endregion Private Helpers
 
        private static PointCollection s_empty;
 
 
        #region Public Properties
 
 
 
        #endregion Public Properties
 
        //------------------------------------------------------
        //
        //  Protected Methods
        //
        //------------------------------------------------------
 
        #region Protected Methods
 
        /// <summary>
        /// Implementation of <see cref="System.Windows.Freezable.CreateInstanceCore">Freezable.CreateInstanceCore</see>.
        /// </summary>
        /// <returns>The new Freezable.</returns>
        protected override Freezable CreateInstanceCore()
        {
            return new PointCollection();
        }
        /// <summary>
        /// Implementation of Freezable.CloneCore()
        /// </summary>
        protected override void CloneCore(Freezable source)
        {
            PointCollection sourcePointCollection = (PointCollection) source;
 
            base.CloneCore(source);
 
            int count = sourcePointCollection._collection.Count;
 
            _collection = new FrugalStructList<Point>(count);
 
            for (int i = 0; i < count; i++)
            {
                _collection.Add(sourcePointCollection._collection[i]);
            }
}
        /// <summary>
        /// Implementation of Freezable.CloneCurrentValueCore()
        /// </summary>
        protected override void CloneCurrentValueCore(Freezable source)
        {
            PointCollection sourcePointCollection = (PointCollection) source;
 
            base.CloneCurrentValueCore(source);
 
            int count = sourcePointCollection._collection.Count;
 
            _collection = new FrugalStructList<Point>(count);
 
            for (int i = 0; i < count; i++)
            {
                _collection.Add(sourcePointCollection._collection[i]);
            }
}
        /// <summary>
        /// Implementation of Freezable.GetAsFrozenCore()
        /// </summary>
        protected override void GetAsFrozenCore(Freezable source)
        {
            PointCollection sourcePointCollection = (PointCollection) source;
 
            base.GetAsFrozenCore(source);
 
            int count = sourcePointCollection._collection.Count;
 
            _collection = new FrugalStructList<Point>(count);
 
            for (int i = 0; i < count; i++)
            {
                _collection.Add(sourcePointCollection._collection[i]);
            }
}
        /// <summary>
        /// Implementation of Freezable.GetCurrentValueAsFrozenCore()
        /// </summary>
        protected override void GetCurrentValueAsFrozenCore(Freezable source)
        {
            PointCollection sourcePointCollection = (PointCollection) source;
 
            base.GetCurrentValueAsFrozenCore(source);
 
            int count = sourcePointCollection._collection.Count;
 
            _collection = new FrugalStructList<Point>(count);
 
            for (int i = 0; i < count; i++)
            {
                _collection.Add(sourcePointCollection._collection[i]);
            }
}
 
 
        #endregion ProtectedMethods
 
        //------------------------------------------------------
        //
        //  Internal Methods
        //
        //------------------------------------------------------
 
        #region Internal Methods
 
 
 
 
 
 
 
 
 
        #endregion Internal Methods
 
        //------------------------------------------------------
        //
        //  Internal Properties
        //
        //------------------------------------------------------
 
        #region Internal Properties
 
 
        /// <summary>
        /// Creates a string representation of this object based on the current culture.
        /// </summary>
        /// <returns>
        /// A string representation of this object.
        /// </returns>
        public override string ToString()
        {
            ReadPreamble();
            // Delegate to the internal method which implements all ToString calls.
            return ConvertToString(null /* format string */, null /* format provider */);
        }
 
        /// <summary>
        /// Creates a string representation of this object based on the IFormatProvider
        /// passed in.  If the provider is null, the CurrentCulture is used.
        /// </summary>
        /// <returns>
        /// A string representation of this object.
        /// </returns>
        public string ToString(IFormatProvider provider)
        {
            ReadPreamble();
            // Delegate to the internal method which implements all ToString calls.
            return ConvertToString(null /* format string */, provider);
        }
 
        /// <summary>
        /// Creates a string representation of this object based on the format string
        /// and IFormatProvider passed in.
        /// If the provider is null, the CurrentCulture is used.
        /// See the documentation for IFormattable for more information.
        /// </summary>
        /// <returns>
        /// A string representation of this object.
        /// </returns>
        string IFormattable.ToString(string format, IFormatProvider provider)
        {
            ReadPreamble();
            // Delegate to the internal method which implements all ToString calls.
            return ConvertToString(format, provider);
        }
 
        /// <summary>
        /// Creates a string representation of this object based on the format string
        /// and IFormatProvider passed in.
        /// If the provider is null, the CurrentCulture is used.
        /// See the documentation for IFormattable for more information.
        /// </summary>
        /// <returns>
        /// A string representation of this object.
        /// </returns>
        internal string ConvertToString(string format, IFormatProvider provider)
        {
            if (_collection.Count == 0)
            {
                return String.Empty;
            }
 
            StringBuilder str = new StringBuilder();
 
            // Consider using this separator
            // Helper to get the numeric list separator for a given culture.
            // char separator = MS.Internal.TokenizerHelper.GetNumericListSeparator(provider);
 
            for (int i=0; i<_collection.Count; i++)
            {
                str.AppendFormat(
                    provider,
                    "{0:" + format + "}",
                    _collection[i]);
 
                if (i != _collection.Count-1)
                {
                    str.Append(' ');
                }
            }
 
            return str.ToString();
        }
        /// <summary>
        /// Parse - returns an instance converted from the provided string
        /// using the current culture
        /// <param name="source"> string with PointCollection data </param>
        /// </summary>
        public static PointCollection Parse(string source)
        {
            IFormatProvider formatProvider = System.Windows.Markup.TypeConverterHelper.InvariantEnglishUS;
 
            TokenizerHelper th = new TokenizerHelper(source, formatProvider);
            PointCollection resource = new PointCollection();
 
            Point value;
 
            while (th.NextToken())
            {
                value = new Point(
                    Convert.ToDouble(th.GetCurrentToken(), formatProvider),
                    Convert.ToDouble(th.NextTokenRequired(), formatProvider));
 
                resource.Add(value);
            }
 
            return resource;
        }
 
        #endregion Internal Properties
 
        //------------------------------------------------------
        //
        //  Dependency Properties
        //
        //------------------------------------------------------
 
        #region Dependency Properties
 
 
 
        #endregion Dependency Properties
 
        //------------------------------------------------------
        //
        //  Internal Fields
        //
        //------------------------------------------------------
 
        #region Internal Fields
 
 
 
 
        internal FrugalStructList<Point> _collection;
        internal uint _version = 0;
 
 
        #endregion Internal Fields
 
        #region Enumerator
        /// <summary>
        /// Enumerates the items in a PointCollection
        /// </summary>
        public struct Enumerator : IEnumerator, IEnumerator<Point>
        {
            #region Constructor
 
            internal Enumerator(PointCollection list)
            {
                Debug.Assert(list != null, "list may not be null.");
 
                _list = list;
                _version = list._version;
                _index = -1;
                _current = default(Point);
            }
 
            #endregion
 
            #region Methods
 
            void IDisposable.Dispose()
            {
            }
 
            /// <summary>
            /// Advances the enumerator to the next element of the collection.
            /// </summary>
            /// <returns>
            /// true if the enumerator was successfully advanced to the next element,
            /// false if the enumerator has passed the end of the collection.
            /// </returns>
            public bool MoveNext()
            {
                _list.ReadPreamble();
 
                if (_version == _list._version)
                {
                    if (_index > -2 && _index < _list._collection.Count - 1)
                    {
                        _current = _list._collection[++_index];
                        return true;
                    }
                    else
                    {
                        _index = -2; // -2 indicates "past the end"
                        return false;
                    }
                }
                else
                {
                    throw new InvalidOperationException(SR.Enumerator_CollectionChanged);
                }
            }
 
            /// <summary>
            /// Sets the enumerator to its initial position, which is before the
            /// first element in the collection.
            /// </summary>
            public void Reset()
            {
                _list.ReadPreamble();
 
                if (_version == _list._version)
                {
                    _index = -1;
                }
                else
                {
                    throw new InvalidOperationException(SR.Enumerator_CollectionChanged);
                }
            }
 
            #endregion
 
            #region Properties
 
            object IEnumerator.Current
            {
                get
                {
                    return this.Current;
                }
            }
 
            /// <summary>
            /// Current element
            ///
            /// The behavior of IEnumerable&lt;T>.Current is undefined
            /// before the first MoveNext and after we have walked
            /// off the end of the list. However, the IEnumerable.Current
            /// contract requires that we throw exceptions
            /// </summary>
            public Point Current
            {
                get
                {
                    if (_index > -1)
                    {
                        return _current;
                    }
                    else if (_index == -1)
                    {
                        throw new InvalidOperationException(SR.Enumerator_NotStarted);
                    }
                    else
                    {
                        Debug.Assert(_index == -2, "expected -2, got " + _index + "\n");
                        throw new InvalidOperationException(SR.Enumerator_ReachedEnd);
                    }
                }
            }
 
            #endregion
 
            #region Data
            private Point _current;
            private PointCollection _list;
            private uint _version;
            private int _index;
            #endregion
        }
        #endregion
 
        #region Constructors
 
        //------------------------------------------------------
        //
        //  Constructors
        //
        //------------------------------------------------------
 
 
        /// <summary>
        /// Initializes a new instance that is empty.
        /// </summary>
        public PointCollection()
        {
            _collection = new FrugalStructList<Point>();
        }
 
        /// <summary>
        /// Initializes a new instance that is empty and has the specified initial capacity.
        /// </summary>
        /// <param name="capacity"> int - The number of elements that the new list is initially capable of storing. </param>
        public PointCollection(int capacity)
        {
            _collection = new FrugalStructList<Point>(capacity);
        }
 
        /// <summary>
        /// Creates a PointCollection with all of the same elements as collection
        /// </summary>
        public PointCollection(IEnumerable<Point> collection)
        {
            // The WritePreamble and WritePostscript aren't technically necessary
            // in the constructor as of 1/20/05 but they are put here in case
            // their behavior changes at a later date
 
            WritePreamble();
 
            ArgumentNullException.ThrowIfNull(collection);
 
            ICollection<Point> icollectionOfT = collection as ICollection<Point>;
 
            if (icollectionOfT != null)
            {
                _collection = new FrugalStructList<Point>(icollectionOfT);
            }
            else
            {
                ICollection icollection = collection as ICollection;
 
                if (icollection != null) // an IC but not and IC<T>
                {
                    _collection = new FrugalStructList<Point>(icollection);
                }
                else // not a IC or IC<T> so fall back to the slower Add
                {
                    _collection = new FrugalStructList<Point>();
 
                    foreach (Point item in collection)
                    {
                        _collection.Add(item);
                    }
                }
            }
 
 
 
 
 
 
 
            WritePostscript();
        }
 
        #endregion Constructors
    }
}