cross-language-cpp / djinni-generator

Command-line tool that generates gluecode from a djinni-IDL file
https://djinni.xlcpp.dev/djinni-generator/setup
Apache License 2.0
92 stars 19 forks source link

Make json serialisation methods static inline #136

Closed mutagene closed 1 year ago

mutagene commented 2 years ago

Previous switch from nlohmann-namespaced adl_serializer(...) method to putting the from_json/to_json in the same namespace as the json types can fall afoul of "Unused function from_json" warnings/errors when compiling with flags -Wunused-function or -Wall.

This change makes the definitions static inline to avoid such errors as it is quite common if e.g. logging to only ever use the to_json methods.

jothepro commented 2 years ago

I understand that the warnings by the compiler are not necessarily helpful in this case since the functions are generated and cannot be eliminated individually just because some of theme are not used.

But I have a few questions to help me better understand the provided solution:

Bear with me if my questions indicate that I may not have fully grasped the issue yet. The inline keyword is new to me and I had to do some research first, which lead me to the above questions.

a4z commented 2 years ago

This is a generated header, from some point of view like a system header. As such, it should not cause any warnings in user code.

This is usually archived by including such headers from a directory specified as -isystem. Then there will be no warnings. But the user needs to take care of that, specifying -isystem correctly.

From that point of view, we have nothing to do.

On the other hand, I think having an inline on functions that are implemented in a header that is not a template is correct. Otherwise, we could end up with multiple implementations of the same function if the header is included more than once. And therefore should be done. But I do not think we need the static keyword in this context.

On the other hand, for the template classes, we should not need the inline since templates should be inline per definition.

So yes, I think some things can be done with that code to make it look better, and this PR works in the right direction.

Can we give it a try, inline for free functions and no inline for templates? And if there are warnings, our tests should cover them ... in an optimal world. This needs of course not be done in the context of this PR, because I do not want to add a lot of work to @mutagene . So maybe a follow-up ticket could be nice ...

a4z commented 2 years ago

Hi @mutagene , I would like to get that change in But do you have the possibility to check if that would work, no static and just inline for the free functions and no inline for template functions?

Would be awesome if you could, since you also seem to have code that triggers problems, in case there are some.

mutagene commented 1 year ago

Sorry for the delay. I'm really grateful for your thoughtful feedback and I'm sorry I haven't had time to respond properly. I'll take a look soon.

a4z commented 1 year ago

Hi @mutagene , we recently added some fix to the generator. That speaks for a new release.

Do you think this PR has a chance to receive some updates the next time? Because if it does, we could well wait to have this improvement onboard.

mutagene commented 1 year ago

Thank you!

I added some suggestions since I think the static inline is redundant, and the static can be removed.

Klick through them if you want or leave it; we will take that in the future. It's more of a cosmetic fix.

Yes, I completely agree.

a4z commented 1 year ago

@jothepro , can you please glance over the PR and merge it if you think it's OK? (I would appreciate a second pair of 👀 since I added changes myself)

jothepro commented 1 year ago

I plan to do a full review tomorrow. 👍