-
Notifications
You must be signed in to change notification settings - Fork 155
Allow support for atomic variables; resolves #575 #577
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
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @liuzicheng1987, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces support for std::atomic variables within the reflection-for-serialization library. The changes are extensive, adding new headers and logic to recursively handle atomic types within structs, arrays, and smart pointers. The core idea is to treat atomic variables as their underlying types during serialization and deserialization. While the overall approach is sound, I've found several issues related to handling of const-volatile qualified types which could lead to bugs or compilation errors. I've also noticed some copy-paste errors in the new test files. My review comments provide specific suggestions to address these points.
| is_atomic<T>::set( | ||
| std::forward<typename is_atomic<Type>::RemoveAtomicT>(val[i]), | ||
| &((*_t)[i])); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an inconsistency in the use of T versus Type (which is std::remove_cvref_t<T>). The set function uses is_atomic<T>::set, but T can have cv-qualifiers, which would cause the base template of is_atomic to be instantiated instead of the correct specialization. This can lead to incorrect behavior or compilation errors. You should consistently use Type, which is the cv-ref removed version of T.
is_atomic<Type>::set(
std::forward<typename is_atomic<Type>::RemoveAtomicT>(val[i]),
&((*_t)[i]));| is_atomic<T>::set( | ||
| std::forward<typename is_atomic<T>::RemoveAtomicT>(val[i]), | ||
| &((*_t)[i])); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The set function for C-style arrays uses is_atomic<T> instead of is_atomic<Type> (where Type is std::remove_cvref_t<T>). This is incorrect if T has any cv-qualifiers, as it will instantiate the base template of is_atomic instead of a specific specialization. This applies to both the set call and the type for std::forward.
is_atomic<Type>::set(
std::forward<typename is_atomic<Type>::RemoveAtomicT>(val[i]),
&((*_t)[i]));
include/rfl/atomic/is_atomic.hpp
Outdated
| is_atomic<T>::set( | ||
| std::forward<typename is_atomic<T>::RemoveAtomicT>(*val), ptr.get()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The set function for std::shared_ptr uses is_atomic<T> instead of is_atomic<Type> (where Type is std::remove_cvref_t<T>). This is incorrect if T has any cv-qualifiers, as it will instantiate the base template of is_atomic instead of a specific specialization. This applies to both the set call and the type for std::forward.
is_atomic<Type>::set(
std::forward<typename is_atomic<Type>::RemoveAtomicT>(*val), ptr.get());
include/rfl/atomic/is_atomic.hpp
Outdated
| is_atomic<T>::set( | ||
| std::forward<typename is_atomic<T>::RemoveAtomicT>(*val), ptr.get()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The set function for std::unique_ptr uses is_atomic<T> instead of is_atomic<Type> (where Type is std::remove_cvref_t<T>). This is incorrect if T has any cv-qualifiers, as it will instantiate the base template of is_atomic instead of a specific specialization. This applies to both the set call and the type for std::forward.
is_atomic<Type>::set(
std::forward<typename is_atomic<Type>::RemoveAtomicT>(*val), ptr.get());| template <class... Fields> | ||
| struct is_atomic<NamedTuple<Fields...>> { | ||
| static constexpr bool value = | ||
| (is_atomic<typename Fields::Type>::value || ...); | ||
|
|
||
| using RemoveAtomicT = NamedTuple< | ||
| rfl::Field<Fields::name_, | ||
| typename is_atomic<typename Fields::Type>::RemoveAtomicT>...>; | ||
|
|
||
| static void set(RemoveAtomicT&& val, NamedTuple<Fields...>* _t) { | ||
| (is_atomic<typename Fields::Type>::set( | ||
| std::forward<typename is_atomic<typename Fields::Type>::RemoveAtomicT>( | ||
| val.template get<Fields::name_>()), | ||
| &(_t->template get<Fields::name_>())), | ||
| ...); | ||
| } | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the specialization for NamedTuple, typename Fields::Type is used directly. If a field's type has cv-qualifiers (e.g., const std::atomic<int>), is_atomic<...> will fall back to the base template, leading to incorrect behavior. You should use std::remove_cvref_t on the field's type before passing it to is_atomic to ensure the correct specialization is chosen.
template <class... Fields>
struct is_atomic<NamedTuple<Fields...>> {
template <class Field>
using CleanFieldType = std::remove_cvref_t<typename Field::Type>;
static constexpr bool value =
(is_atomic<CleanFieldType<Fields>>::value || ...);
using RemoveAtomicT = NamedTuple<
rfl::Field<Fields::name_,
typename is_atomic<CleanFieldType<Fields>>::RemoveAtomicT>...>;
static void set(RemoveAtomicT&& val, NamedTuple<Fields...>* _t) {
(is_atomic<CleanFieldType<Fields>>::set(
std::forward<typename is_atomic<
CleanFieldType<Fields>>::RemoveAtomicT>(
val.template get<Fields::name_>()),
&(_t->template get<Fields::name_>())),
...);
}
};
tests/json/test_atomic_box.cpp
Outdated
| namespace test_atomic_ref { | ||
|
|
||
| struct Stats { | ||
| std::atomic<std::uint64_t> bytes_downloaded; | ||
| std::atomic<bool> finished; | ||
| }; | ||
|
|
||
| TEST(json, test_atomic_ref) { | ||
| auto stats = rfl::make_ref<Stats>(123456789, true); | ||
|
|
||
| write_and_read(stats, R"({"bytes_downloaded":123456789,"finished":true})"); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There seems to be a copy-paste error in this test file. The filename is test_atomic_box.cpp, but the namespace is test_atomic_ref, the test is named test_atomic_ref, and it uses rfl::make_ref which is for rfl::Ref. This test should be for rfl::Box.
namespace test_atomic_box {
struct Stats {
std::atomic<std::uint64_t> bytes_downloaded;
std::atomic<bool> finished;
};
TEST(json, test_atomic_box) {
auto stats = rfl::make_box<Stats>(123456789, true);
write_and_read(stats, R"({"bytes_downloaded":123456789,"finished":true})");
}
tests/json/test_atomic_ref.cpp
Outdated
| namespace test_atomic_box { | ||
|
|
||
| struct Stats { | ||
| std::atomic<std::uint64_t> bytes_downloaded; | ||
| std::atomic<bool> finished; | ||
| }; | ||
|
|
||
| TEST(json, test_atomic_box) { | ||
| auto stats = rfl::make_box<Stats>(123456789, true); | ||
|
|
||
| write_and_read(stats, R"({"bytes_downloaded":123456789,"finished":true})"); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There seems to be a copy-paste error in this test file. The filename is test_atomic_ref.cpp, but the namespace is test_atomic_box, the test is named test_atomic_box, and it uses rfl::make_box which is for rfl::Box. This test should be for rfl::Ref.
namespace test_atomic_ref {
struct Stats {
std::atomic<std::uint64_t> bytes_downloaded;
std::atomic<bool> finished;
};
TEST(json, test_atomic_ref) {
auto stats = rfl::make_ref<Stats>(123456789, true);
write_and_read(stats, R"({"bytes_downloaded":123456789,"finished":true})");
}
No description provided.