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

Use dynamic cast on reference when should not fail #176

Merged
merged 2 commits into from
Jul 12, 2024

Conversation

Lai-YT
Copy link
Collaborator

@Lai-YT Lai-YT commented Jul 10, 2024

There is a common pattern in our code where we dynamically cast a pointer and assert that it is not null. This pattern implies that the cast should not fail. Therefore, we can use the reference version of dynamic casting, which throws a std::bad_cast exception if the cast fails.
Additionally, we are using dynamic casting with unique pointers, which requires a manual get() to obtain the underlying pointer. Since get() should only be used when necessary, this change also eliminates its use.
Notice that static_cast has been replaced by dynamic_cast. This is because there is a concern that the static cast might not be valid, so we are still relying on the dynamic check.

Note

The remaining use cases are those where we use dynamic checks with an if to verify whether an object is of a certain type, which is expected to fail.

There is a common pattern in our code where we dynamically cast a
pointer and assert that it is not null. This pattern implies that the
cast should not fail. Therefore, we can use the reference version of
dynamic casting, which throws a `std::bad_cast` exception if the cast
fails.

Additionally, we are using dynamic casting with unique pointers, which
requires a manual `get()` to obtain the underlying pointer. Since
`get()` should only be used when necessary, this change also eliminates
its use.
@Lai-YT Lai-YT requested a review from leewei05 July 10, 2024 06:20
@Lai-YT Lai-YT self-assigned this Jul 10, 2024
@Lai-YT Lai-YT force-pushed the refactor-dynamic-cast-on-reference branch from 007ad82 to 5943f74 Compare July 10, 2024 06:21
Copy link
Contributor

@leewei05 leewei05 left a comment

Choose a reason for hiding this comment

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

🚀

@leewei05 leewei05 merged commit 77afe44 into fruits-lab:main Jul 12, 2024
8 checks passed
@Lai-YT Lai-YT deleted the refactor-dynamic-cast-on-reference branch July 12, 2024 05:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants