-
Notifications
You must be signed in to change notification settings - Fork 43
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
Alternative C API #189
base: main
Are you sure you want to change the base?
Alternative C API #189
Conversation
Silly me, I forgot to change the tests too (done in 7b35c05). I should have made 2 different PRs (one for passing structures by address, the other for the alternative API) sorry for that. |
Thank you @emmt ! int prima_minimize(const prima_algorithm_t algorithm, const prima_problem_t *problem, const prima_options_t *options, prima_result_t *const result); Why do we use pointers here for |
Because passing them by value is not necessary and is a waste of stack space. It is possible but rather unusual in C to pass structures by value (except maybe very small ones). The pointers of these are |
Then may I suggest int prima_minimize(const prima_algorithm_t algorithm, const prima_problem_t *const problem, const prima_options_t *const options, prima_result_t *const result); as neither the pointer itself nor its content is supposed to be changed in the function body? Of course, any change to the pointer itself will be lost since itself is passed by value, but this will effectively prevent the developers (ourselves, future ourselves, and others) from making mistakes when composing or modifying the implementation of this function. I had a discussion with @amontoison about it and this was the consensus. Thank you. |
Honestly, the |
Thanks for the explanation with stack space! For the For the new C API, it looks good to me. 👍 |
Then a user like me would need to check a documentation to make sure what the const really means, because, as you said, there are two different cases. If we use both, the user knows automatically that everything is supposed to be constant. |
@zaikunzhang Except that when there is a single |
This PR implements the alternative C API discussed here and here. The changes come in two commits:
the first commit (0aa2069) is a simple modification of the existing C API so that all structures are passed by address (not by value);
the second commit (d90d0a3) implements a draft of the proposed alternative C API with:
prima_context_t
storing all the parameters,prima_context_create
andprima_context_destroy
to create this structure and, when no longer needed, destroy it as well as related resources),prima_context_set_...
and accessorsprima_context_get_...
) to set and retrieve all parameters.All C examples compile and run fine with the first commit. Example are needed for the 2nd commit but it is mainly there for being discussed.
The two king of API can coexist. In fact the latter one is implemented on top of the former one and is intended to facilitate the writing of bindings in other languages without requiring to directly access C structures in these languages.