amerkoleci / Vortice.Windows

.NET bindings for Direct3D12, Direct3D11, WIC, Direct2D1, XInput, XAudio, X3DAudio, DXC, Direct3D9 and DirectInput.
MIT License
1.01k stars 73 forks source link

Better code generation for ID2D1SvgElement class #389

Closed sdcb closed 4 months ago

sdcb commented 1 year ago

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.

amerkoleci commented 1 year ago

Some improvements landed here:

https://github.com/amerkoleci/Vortice.Windows/commit/fc1540a9d82cc60e49224225a43eaa4007ecd6b7

amerkoleci commented 4 months ago

Fixed by https://github.com/amerkoleci/Vortice.Windows/commit/47c698455bea71896f717bcb19e553d6a95518a7