Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better code generation for ID2D1SvgElement class #389

Open
sdcb opened this issue Mar 28, 2023 · 1 comment
Open

Better code generation for ID2D1SvgElement class #389

sdcb opened this issue Mar 28, 2023 · 1 comment

Comments

@sdcb
Copy link
Contributor

sdcb commented Mar 28, 2023

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
Copy link
Owner

Some improvements landed here:

fc1540a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants