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: Implementation of Safe Type Conversion Templates in C++ #1951

Closed
wants to merge 2 commits into from

Conversation

beats-dh
Copy link
Collaborator

@beats-dh beats-dh commented Dec 2, 2023

Objective

This Pull Request introduces a series of C++ templates designed to facilitate safe and efficient type conversions. The aim is to replace explicit static_cast calls in the existing codebase, enhancing both the safety and readability of the code.

Key Changes

  • Template get_default_value<T>(): Provides default values for type T, distinguishing between arithmetic and non-arithmetic types.
  • Template number_to_string<U>(value): Efficiently converts numerical values to strings using std::to_chars, applicable to both integral and floating-point types.
  • Template safe_convert<T, U>(value, callerName, defaultValue): Ensures safe type conversions, incorporating checks for overflow, underflow, precision loss, and non-finite floating-point values. Specialized for various type combinations, including conversions from enum to arithmetic types, and from string to numeric types.

Benefits

  • Enhanced Safety: Integrates thorough checks to mitigate common conversion issues such as overflow and loss of precision.
  • Increased Flexibility: Compatible with a broad array of data types and conversion scenarios.
  • Improved Code Readability and Maintenance: Streamlines the code by replacing numerous static_cast instances with more expressive and intentional function calls.

Usage Examples

Examples illustrating the practical application of each template are included in "src/utils/safe_convert.hpp", demonstrating their utility across diverse type conversion situations.

Notes

  • This PR refines the codebase without altering its fundamental logic, providing a more robust and legible method for type conversion.
  • A detailed review of the template implementations is advised to ensure comprehensive coverage of all project use cases.

@beats-dh beats-dh requested review from dudantas, luan, andersonfaaria and lgrossi and removed request for dudantas December 2, 2023 04:49
Copy link
Contributor

@luan luan left a comment

Choose a reason for hiding this comment

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

Looks good to me. I realize it's mostly a search and replace job so I only review the concept here.

Just gotta fix the builds.

@beats-dh beats-dh force-pushed the feat-conversion-explicit branch from 77a3bc8 to 7f67e03 Compare December 3, 2023 05:06
@beats-dh beats-dh marked this pull request as draft December 7, 2023 04:47
@beats-dh beats-dh force-pushed the feat-conversion-explicit branch from c16424d to 7bcae55 Compare January 3, 2024 02:54
@beats-dh beats-dh marked this pull request as ready for review January 3, 2024 02:55
@beats-dh beats-dh requested a review from luan January 3, 2024 02:55
Copy link

sonarqubecloud bot commented Jan 3, 2024

Quality Gate Failed Quality Gate failed

Failed conditions

C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarCloud

idea Catch issues before they fail your Quality Gate with our IDE extension SonarLint SonarLint

Copy link
Contributor

github-actions bot commented Feb 3, 2024

This PR is stale because it has been open 45 days with no activity.

@github-actions github-actions bot added the Stale No activity label Feb 3, 2024
@github-actions github-actions bot removed the Stale No activity label Feb 4, 2024
Copy link
Contributor

github-actions bot commented Mar 6, 2024

This PR is stale because it has been open 45 days with no activity.

@github-actions github-actions bot added the Stale No activity label Mar 6, 2024
@beats-dh beats-dh closed this Mar 31, 2024
@beats-dh beats-dh deleted the feat-conversion-explicit branch July 6, 2024 16:50
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.

3 participants