Remove minor code duplication

This commit is contained in:
Herb Sutter 2020-12-18 12:34:07 -08:00
parent 71fd73fa04
commit e02baff283

View File

@ -109,14 +109,11 @@ public:
using handler = void (*)(); using handler = void (*)();
#endif #endif
constexpr contract_group (handler h) : chandler(h ? h : []()noexcept{}) { } constexpr contract_group (handler h) : chandler(sanitize(h)) { }
#if __cplusplus >= 202002L #if __cplusplus >= 202002L
constexpr auto set_handler(handler h) -> handler { return std::exchange(chandler, h ? h : []()noexcept{}); } constexpr auto set_handler(handler h) -> handler { return std::exchange(chandler, sanitize(h)); }
#else // VESTIGIAL, remove when no longer needed for downlevel compilers #else // VESTIGIAL, remove when no longer needed for downlevel compilers
constexpr auto set_handler(handler h) -> handler { auto old = chandler; constexpr auto set_handler(handler h) -> handler { auto old = chandler; chandler = sanitize(h); return old; }
chandler = h ? h : []()noexcept{};
return old;
}
#endif #endif
constexpr auto get_handler() -> handler { return chandler; } constexpr auto get_handler() -> handler { return chandler; }
@ -124,6 +121,7 @@ public:
constexpr void ensures (bool b) { assertion(b); } constexpr void ensures (bool b) { assertion(b); }
private: private:
constexpr void assertion(bool b) { if (!b) chandler(); } constexpr void assertion(bool b) { if (!b) chandler(); }
constexpr auto sanitize(handler h) -> handler { return h ? h : []()noexcept{}; }
handler chandler; handler chandler;
}; };