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

How to override #1848

Open
Maxwell175 opened this issue May 18, 2024 · 7 comments
Open

How to override #1848

Maxwell175 opened this issue May 18, 2024 · 7 comments

Comments

@Maxwell175
Copy link

Maxwell175 commented May 18, 2024

Brief Description

In my situation I have a typedefed C enum that is intended to be passed as a parameter to a function. However, this function declaration merely identifies the parameter as a long so there is no way for CppSharp to figure out that the 2 are related. How can I override the parameter of this function to use the Enum type?

OS: Windows

Used headers
// attribute 
typedef enum
{
    SP_ATTR_TIMEOUT         = 1,
    SP_ATTR_LOG             = 2        
} SP_ATTR_ENUM;

SP_EXPORT int       SP_API SP_SetProperty   (SP_HANDLE hDiag, long lProperty, long lFlag, const void *lpValue);
@tritao
Copy link
Collaborator

tritao commented May 19, 2024

Need more info to be able to help you here.

@Maxwell175
Copy link
Author

Basically CppSharp generated a function with the following signature:

public static int SP_SetProperty(__IntPtr hDiag, int lProperty, int lFlag, __IntPtr lpValue)

I need to get it to change to:

public static int SP_SetProperty(__IntPtr hDiag, SP_ATTR_ENUM lProperty, int lFlag, __IntPtr lpValue)

What kind of approaches can I use for this?

@tritao
Copy link
Collaborator

tritao commented May 19, 2024

You can change the type of your parameter as a processing step, see for example the helper methods here: https://github.com/mono/CppSharp/blob/3f923b1/src/Generator/Library.cs#L474

You can do something like SetFunctionParameterUsage but instead use it to modify the type of the parameter to the one of the enum.

@Maxwell175
Copy link
Author

Maxwell175 commented May 19, 2024

So this is where I got stuck. I found the FindFunction function and used it to get the function, then grabbed the right parameter, but the problem is how to I set the type to an enum? Calling FindEnum returns an Enumeration object. I'm not sure how to properly put these together.

@tritao
Copy link
Collaborator

tritao commented May 19, 2024

So this is where I got stuck. I found the FindFunction function and used it to get the function, then grabbed the right parameter, but the problem is how to I set the type to an enum? Calling FindEnum returns an Enumeration object. I'm not sure how to properly put these together.

Think you need to do something like param.QualifiedType = new QualifiedType(TagType(enum)).

@Maxwell175
Copy link
Author

Got it. Here is the code I ended up with:

ctx.FindFunction("SP_SetProperty").First().Parameters[1].QualifiedType =
            new QualifiedType(new TagType(ctx.FindEnum("SP_ATTR_ENUM").First()));

Would be nice to have a helper function for this as well.

@tritao
Copy link
Collaborator

tritao commented May 19, 2024

Feel free to send a PR

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