Parameterize Expects1 and Ensures` by contract group

Allows independently controlling handling of different categories of bugs, such as bounds checks vs. null checks.
User-extensible: Companies can instantiate their own `contract_group` objects for their own categories of contract checks, including for distinguishing contract "levels" like `Normal` vs. `Audit` by just creating those two groups that can then be controlled independently or in combination.
This commit is contained in:
Herb Sutter
2020-11-27 17:06:01 -08:00
parent c16e4ce59f
commit cbfd8cd734
13 changed files with 129 additions and 124 deletions
+4 -4
View File
@@ -17,7 +17,7 @@
#ifndef GSL_POINTERS_H
#define GSL_POINTERS_H
#include <gsl/assert> // for Ensures, Expects
#include <gsl/assert> // for contracts
#include <algorithm> // for forward
#include <cstddef> // for ptrdiff_t, nullptr_t, size_t
@@ -74,13 +74,13 @@ public:
template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
constexpr not_null(U&& u) : ptr_(std::forward<U>(u))
{
Expects(ptr_ != nullptr);
Expects(ptr_ != nullptr, Null);
}
template <typename = std::enable_if_t<!std::is_same<std::nullptr_t, T>::value>>
constexpr not_null(T u) : ptr_(std::move(u))
{
Expects(ptr_ != nullptr);
Expects(ptr_ != nullptr, Null);
}
template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
@@ -91,7 +91,7 @@ public:
not_null& operator=(const not_null& other) = default;
constexpr std::conditional_t<std::is_copy_constructible<T>::value, T, const T&> get() const
{
Ensures(ptr_ != nullptr);
Ensures(ptr_ != nullptr, Null);
return ptr_;
}