Skip to content

Commit

Permalink
Add more general TYPE_SAFE_ARITHMETIC_POLICY
Browse files Browse the repository at this point in the history
This replaces TYPE_SAFE_ARITHMETIC_UB.

Fixes #106, closes #107.
  • Loading branch information
foonathan committed Mar 26, 2020
1 parent c432d29 commit a0290b5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
21 changes: 16 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,22 @@ else()
set(_type_safe_enable_wrapper 0)
endif()

option(TYPE_SAFE_ARITHMETIC_UB "whether or not the undefined_behavior_arithmetic policy is used" ON)
if(${TYPE_SAFE_ARITHMETIC_UB})
set(_type_safe_arithmetic_ub 1)
set(TYPE_SAFE_ARITHMETIC_POLICY "ub" CACHE STRING "which policy (ub/checked/default) is going to be used")
option(TYPE_SAFE_ARITHMETIC_UB "deprecated" ON)

if(NOT ${TYPE_SAFE_ARITHMETIC_UB})
set(_type_safe_arithmetic_policy 0)
message(DEPRECATION "option TYPE_SAFE_ARITHMETIC_UB is deprecated, use TYPE_SAFE_ARITHMETIC_POLICY instead")
message(STATUS "arithmetic_policy_default is default_arithmetic")
elseif(${TYPE_SAFE_ARITHMETIC_POLICY} STREQUAL "ub")
set(_type_safe_arithmetic_policy 1)
message(STATUS "arithmetic_policy_default is undefined_behavior_arithmetic")
elseif(${TYPE_SAFE_ARITHMETIC_POLICY} STREQUAL "checked")
set(_type_safe_arithmetic_policy 2)
message(STATUS "arithmetic_policy_default is checked_arithmetic")
else()
set(_type_safe_arithmetic_ub 0)
set(_type_safe_arithmetic_policy 0)
message(STATUS "arithmetic_policy_default is default_arithmetic")
endif()

# interface target
Expand Down Expand Up @@ -88,7 +99,7 @@ target_compile_definitions(type_safe INTERFACE
TYPE_SAFE_ENABLE_ASSERTIONS=${_type_safe_enable_assertions}
TYPE_SAFE_ENABLE_PRECONDITION_CHECKS=${_type_safe_enable_precondition_checks}
TYPE_SAFE_ENABLE_WRAPPER=${_type_safe_enable_wrapper}
TYPE_SAFE_ARITHMETIC_UB=${_type_safe_arithmetic_ub})
TYPE_SAFE_ARITHMETIC_POLICY=${_type_safe_arithmetic_policy})
target_link_libraries(type_safe INTERFACE debug_assert)

if(MSVC)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Behavior can be customized with the following macros:

* `TYPE_SAFE_ENABLE_ASSERTIONS` (default is `1`): whether or not assertions are enabled in this library
* `TYPE_SAFE_ENABLE_WRAPPER` (default is `1`): whether or not the typedefs in `type_safe/types.hpp` use the wrapper classes
* `TYPE_SAFE_ARITHMETIC_UB` (default is `1`): whether under/overflow in the better integer types is UB.
* `TYPE_SAFE_ARITHMETIC_POLICY` (`ub`/`checked`/`default`, default is `ub`): whether under/overflow in the better integer types is UB, an exception, or the default behavior

If you're using CMake there is the target `type_safe` available after you've called `add_subdirectory(path/to/type_safe)`.
Simply link this target to your target and it will setup everything automagically.
Expand Down
11 changes: 6 additions & 5 deletions include/type_safe/arithmetic_policy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,15 @@ class checked_arithmetic
}
};

#if TYPE_SAFE_ARITHMETIC_UB
#if TYPE_SAFE_ARITHMETIC_POLICY == 1
/// The default `ArithmeticPolicy`.
///
/// It depends on the [TYPE_SAFE_ARITHMETIC_UB]() macro,
/// and is either [ts::undefined_behavior_arithmetic]() or [ts::default_arithmetic]().
/// \exclude target
/// \module types
/// It depends on the [TYPE_SAFE_ARITHMETIC_POLICY]() macro,
/// and is either [ts::undefined_behavior_arithmetic](), [ts::checked_arithmetic](), or
/// [ts::default_arithmetic](). \exclude target \module types
using arithmetic_policy_default = undefined_behavior_arithmetic;
#elif TYPE_SAFE_ARITHMETIC_POLICY == 2
using arithmetic_policy_default = checked_arithmetic;
#else
using arithmetic_policy_default = default_arithmetic;
#endif
Expand Down
8 changes: 4 additions & 4 deletions include/type_safe/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
# define TYPE_SAFE_ENABLE_WRAPPER 1
#endif

#ifndef TYPE_SAFE_ARITHMETIC_UB
/// Controls whether [ts::arithmetic_policy_default]() is [ts::undefined_behavior_arithmetic]() or
/// [ts::default_arithmetic]().
#ifndef TYPE_SAFE_ARITHMETIC_POLICY
/// Controls whether [ts::arithmetic_policy_default]() is [ts::undefined_behavior_arithmetic](),
/// [ts::checked_arithmetic](), or [ts::default_arithmetic]().
///
/// It is [ts::undefined_behavior_arithmetic]() by default.
# define TYPE_SAFE_ARITHMETIC_UB 1
# define TYPE_SAFE_ARITHMETIC_POLICY 1
#endif

#ifndef TYPE_SAFE_DELETE_FUNCTIONS
Expand Down

0 comments on commit a0290b5

Please sign in to comment.