Removed double .load

Require `chandler` to be never null by installing `[]()noexcept{}` as the handler if given a null pointer.
This lets us remove the double test in `assertion`.
This commit is contained in:
Herb Sutter 2020-12-17 11:36:23 -08:00
parent b7548fd1cd
commit 0cbb9e221d

View File

@ -104,15 +104,16 @@ class contract_group {
public: public:
using handler = void (*)() noexcept; using handler = void (*)() noexcept;
contract_group(handler h) : chandler{h} { } contract_group(handler h) { set_handler(h); }
auto set_handler(handler h) -> handler { return chandler.exchange(h); } auto set_handler(handler h) -> handler { if (!h) h = []()noexcept{};
return chandler.exchange(h); }
auto get_handler() -> handler { return chandler; } auto get_handler() -> handler { return chandler; }
auto expects(bool b) { assertion(b); } auto expects(bool b) { assertion(b); }
auto ensures(bool b) { assertion(b); } auto ensures(bool b) { assertion(b); }
private: private:
auto assertion(bool b) { if (!b && chandler.load()) chandler.load()(); } auto assertion(bool b) { if (!b) chandler.load()(); }
std::atomic<handler> chandler; std::atomic<handler> chandler;
}; };