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

feat: support enums in D-Bus serialization and signatures #416

Merged
merged 4 commits into from
Mar 7, 2024

Conversation

hellow554
Copy link
Contributor

This PR adds the possiblity for C++20 compilers to use enums, enum classes and enum structs.

C++20 is required because of requires. I coulndn't find a way without it. If you're more successful than me, please open a PR or edit this PR, feel free :)

If anything bugs you, please comment

a test was not discovered, because the tests were run with c++17
I set the cxx_standard for the tests explicitly, so it will be detected
in the future
currently only useable if you have a c++20 compiler, but probably can be
fixed if needed
@hellow554
Copy link
Contributor Author

hellow554 commented Mar 4, 2024

This is some weird bug: actions/runner-images#9446, but nothing that strikes the user.

Here's a quick workaround: actions/runner-images#9446 (comment) this looks weird. Maybe better not use it 🙈

Copy link
Collaborator

@sangelovic sangelovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @hellow554, thank you for the PR, makes a lot of sense to also support enums in a generic way. I left a few comments...

include/sdbus-c++/Message.h Outdated Show resolved Hide resolved

template <typename Enum> requires std::is_enum_v<Enum>
Message& operator<<(const Enum &item) {
return operator<<(static_cast<std::underlying_type_t<Enum>>(item));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please follow the style and only declare the function here and define it below the class. Also, please follow the established style with the positioning of the opening curly brace. (ditto for operator>>)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, you can then move these functions out of C++20 #ifdef scope...

include/sdbus-c++/TypeTraits.h Outdated Show resolved Hide resolved
tests/CMakeLists.txt Outdated Show resolved Hide resolved
tests/unittests/TypeTraits_test.cpp Outdated Show resolved Hide resolved
tests/unittests/Message_test.cpp Outdated Show resolved Hide resolved
tests/unittests/Message_test.cpp Outdated Show resolved Hide resolved
tests/unittests/TypeTraits_test.cpp Outdated Show resolved Hide resolved
@sangelovic
Copy link
Collaborator

sangelovic commented Mar 7, 2024

Now I noticed in your description that it's OK for you if I edit your PR. I requested a number of changes... shall I do them in this PR?

@hellow554
Copy link
Contributor Author

Please go ahead and feel free to edit it.
I was not able to get it done with std::enable_if because of some partial specialisation stuff. Not sure anymore.

Do you want me to do things or should I justed accept your requests?

@sangelovic
Copy link
Collaborator

@hellow554 I'm doing the changes already... will have it in a few minutes.

@sangelovic sangelovic self-assigned this Mar 7, 2024
@sangelovic sangelovic changed the title Make use of Enum class|struct possible in sdbus signature feat: support enums in D-Bus serialization and signatures Mar 7, 2024
@sangelovic
Copy link
Collaborator

@hellow554 What do you think of my changes? I've implemented C++17 solution but also kept C++20 one of yours.

@sangelovic
Copy link
Collaborator

sangelovic commented Mar 7, 2024

It took me more than a few minutes as I expected at the beginning... I had an illogical compiler error with signature_of, which took me some time to figure out... I had a typo -- I used enable_if there instead of correct enable_if_t... Such an almost invisible difference :) So yes! C++20 with requires is simpler and saves us from such mistakes...

include/sdbus-c++/TypeTraits.h Show resolved Hide resolved
include/sdbus-c++/Message.h Outdated Show resolved Hide resolved
@@ -107,12 +107,10 @@ add_executable(sdbus-c++-unit-tests ${UNITTESTS_SRCS})
target_compile_definitions(sdbus-c++-unit-tests PRIVATE
LIBSYSTEMD_VERSION=${LIBSYSTEMD_VERSION}
SDBUS_HEADER=<${LIBSYSTEMD_IMPL}/sd-bus.h>)
target_compile_features(sdbus-c++-unit-tests PUBLIC cxx_std_20)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way you can never test any c++20 dependent code again.

What if we use try_compiles to check if c++20 is supported and if yes, we add a second target with just std=c++20 enabled? So we have 4 tests instead of 2? (or 3, if we leave out the intregation tests)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I thought again, and I'm fine with putting that line back. (I don't want to complicate things with try_compiles and additional targets.)

But, again, there is v2.0 about to be released which officially supports C++20. v2 will become the main line where the main focus will be onwards, v1 will be kept for bugfixing and maintenance. With that, we'll have C++20 library and tests covered with compilers that supports C++20.

Is it OK for you if we leave it out here and the things will work automatically with v2?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, I brought the cxx20 lines back, and I will leave them out in release/v2.0 when rebasing.

@hellow554
Copy link
Contributor Author

Looks really good to me. Thanks for the quick solution provided by you.
CI is failing with the exact same buggy toolchain error, so no worries there.

Thanks again!

@sangelovic
Copy link
Collaborator

I tried to fix the pipeline but there's more to it than I first anticipated. I don't have time for it, and anyways it's out of scope of this PR. So I reverted the cxx20 feature for the tests. I think it's fully OK that it's covered in the upcoming v2 (where all the pipeline adaptations for C++20 and that libstdc++ bug reported by clang have already been solved).

@sangelovic sangelovic merged commit 8a9cfc1 into Kistler-Group:master Mar 7, 2024
6 checks passed
@hellow554 hellow554 deleted the enum_class branch March 8, 2024 06:40
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

Successfully merging this pull request may close these issues.

2 participants