Skip to content

Commit a0290b5

Browse files
committed
Add more general TYPE_SAFE_ARITHMETIC_POLICY
This replaces TYPE_SAFE_ARITHMETIC_UB. Fixes #106, closes #107.
1 parent c432d29 commit a0290b5

4 files changed

Lines changed: 27 additions & 15 deletions

File tree

CMakeLists.txt

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,22 @@ else()
3636
set(_type_safe_enable_wrapper 0)
3737
endif()
3838

39-
option(TYPE_SAFE_ARITHMETIC_UB "whether or not the undefined_behavior_arithmetic policy is used" ON)
40-
if(${TYPE_SAFE_ARITHMETIC_UB})
41-
set(_type_safe_arithmetic_ub 1)
39+
set(TYPE_SAFE_ARITHMETIC_POLICY "ub" CACHE STRING "which policy (ub/checked/default) is going to be used")
40+
option(TYPE_SAFE_ARITHMETIC_UB "deprecated" ON)
41+
42+
if(NOT ${TYPE_SAFE_ARITHMETIC_UB})
43+
set(_type_safe_arithmetic_policy 0)
44+
message(DEPRECATION "option TYPE_SAFE_ARITHMETIC_UB is deprecated, use TYPE_SAFE_ARITHMETIC_POLICY instead")
45+
message(STATUS "arithmetic_policy_default is default_arithmetic")
46+
elseif(${TYPE_SAFE_ARITHMETIC_POLICY} STREQUAL "ub")
47+
set(_type_safe_arithmetic_policy 1)
48+
message(STATUS "arithmetic_policy_default is undefined_behavior_arithmetic")
49+
elseif(${TYPE_SAFE_ARITHMETIC_POLICY} STREQUAL "checked")
50+
set(_type_safe_arithmetic_policy 2)
51+
message(STATUS "arithmetic_policy_default is checked_arithmetic")
4252
else()
43-
set(_type_safe_arithmetic_ub 0)
53+
set(_type_safe_arithmetic_policy 0)
54+
message(STATUS "arithmetic_policy_default is default_arithmetic")
4455
endif()
4556

4657
# interface target
@@ -88,7 +99,7 @@ target_compile_definitions(type_safe INTERFACE
8899
TYPE_SAFE_ENABLE_ASSERTIONS=${_type_safe_enable_assertions}
89100
TYPE_SAFE_ENABLE_PRECONDITION_CHECKS=${_type_safe_enable_precondition_checks}
90101
TYPE_SAFE_ENABLE_WRAPPER=${_type_safe_enable_wrapper}
91-
TYPE_SAFE_ARITHMETIC_UB=${_type_safe_arithmetic_ub})
102+
TYPE_SAFE_ARITHMETIC_POLICY=${_type_safe_arithmetic_policy})
92103
target_link_libraries(type_safe INTERFACE debug_assert)
93104

94105
if(MSVC)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Behavior can be customized with the following macros:
7474

7575
* `TYPE_SAFE_ENABLE_ASSERTIONS` (default is `1`): whether or not assertions are enabled in this library
7676
* `TYPE_SAFE_ENABLE_WRAPPER` (default is `1`): whether or not the typedefs in `type_safe/types.hpp` use the wrapper classes
77-
* `TYPE_SAFE_ARITHMETIC_UB` (default is `1`): whether under/overflow in the better integer types is UB.
77+
* `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
7878

7979
If you're using CMake there is the target `type_safe` available after you've called `add_subdirectory(path/to/type_safe)`.
8080
Simply link this target to your target and it will setup everything automagically.

include/type_safe/arithmetic_policy.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -237,14 +237,15 @@ class checked_arithmetic
237237
}
238238
};
239239

240-
#if TYPE_SAFE_ARITHMETIC_UB
240+
#if TYPE_SAFE_ARITHMETIC_POLICY == 1
241241
/// The default `ArithmeticPolicy`.
242242
///
243-
/// It depends on the [TYPE_SAFE_ARITHMETIC_UB]() macro,
244-
/// and is either [ts::undefined_behavior_arithmetic]() or [ts::default_arithmetic]().
245-
/// \exclude target
246-
/// \module types
243+
/// It depends on the [TYPE_SAFE_ARITHMETIC_POLICY]() macro,
244+
/// and is either [ts::undefined_behavior_arithmetic](), [ts::checked_arithmetic](), or
245+
/// [ts::default_arithmetic](). \exclude target \module types
247246
using arithmetic_policy_default = undefined_behavior_arithmetic;
247+
#elif TYPE_SAFE_ARITHMETIC_POLICY == 2
248+
using arithmetic_policy_default = checked_arithmetic;
248249
#else
249250
using arithmetic_policy_default = default_arithmetic;
250251
#endif

include/type_safe/config.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929
# define TYPE_SAFE_ENABLE_WRAPPER 1
3030
#endif
3131

32-
#ifndef TYPE_SAFE_ARITHMETIC_UB
33-
/// Controls whether [ts::arithmetic_policy_default]() is [ts::undefined_behavior_arithmetic]() or
34-
/// [ts::default_arithmetic]().
32+
#ifndef TYPE_SAFE_ARITHMETIC_POLICY
33+
/// Controls whether [ts::arithmetic_policy_default]() is [ts::undefined_behavior_arithmetic](),
34+
/// [ts::checked_arithmetic](), or [ts::default_arithmetic]().
3535
///
3636
/// It is [ts::undefined_behavior_arithmetic]() by default.
37-
# define TYPE_SAFE_ARITHMETIC_UB 1
37+
# define TYPE_SAFE_ARITHMETIC_POLICY 1
3838
#endif
3939

4040
#ifndef TYPE_SAFE_DELETE_FUNCTIONS

0 commit comments

Comments
 (0)