amerkoleci/Vortice.Windows

Better code generation for ID2D1SvgElement class

sdcb opened this issue · 2 comments

sdcb commented

Suggested implementation:

public unsafe partial class ID2D1SvgElement : IEnumerable<ID2D1SvgElement>
{
    public IEnumerator<ID2D1SvgElement> GetEnumerator()
    {
        ID2D1SvgElement? child = GetFirstChild();
        while (child != null)
        {
            yield return child;
            child = child.GetNextChild(child);
        }
    }

    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

    public IEnumerable<ID2D1SvgElement> DescendantsAndSelf()
    {
        yield return this;
        foreach (ID2D1SvgElement child in this)
        {
            foreach (ID2D1SvgElement descendant in child.DescendantsAndSelf())
            {
                yield return descendant;
            }
        }
    }

    public IEnumerable<ID2D1SvgElement> Descendants()
    {
        foreach (ID2D1SvgElement child in this)
        {
            yield return child;
            foreach (ID2D1SvgElement grandchild in child.Descendants())
            {
                yield return grandchild;
            }
        }
    }

    public IEnumerable<string> AttributeNames
    {
        get
        {
            int count = GetSpecifiedAttributeCount();
            for (int i = 0; i < count; i++)
            {
                GetSpecifiedAttributeNameLength(i, out int nameLength, out _);

                IntPtr namePtr = Marshal.AllocHGlobal(nameLength);
                try
                {
                    bool success = GetSpecifiedAttributeName(i, namePtr, nameLength);
                    if (!success) continue;

                    string attributeName = Marshal.PtrToStringUni(namePtr, nameLength)!;
                    yield return attributeName;
                }
                finally
                {
                    Marshal.FreeHGlobal(namePtr);
                }
            }
        }
    }

    public string TagName {get;} // from GetTagName/GetTagNameLength
    public string TextValue {get;} // from GetTextValue/GetTextValueLength
}

I wants to do PR, however for reason unknown code doesn't compile in my local machine, please try help.

Some improvements landed here:

fc1540a