2021-04-21 15:45:31 -07:00
|
|
|
|
|
|
|
// A Scalar that asserts for uninitialized access.
|
|
|
|
template <typename T>
|
|
|
|
class SafeScalar {
|
|
|
|
public:
|
|
|
|
SafeScalar() : initialized_(false) {}
|
|
|
|
SafeScalar(const SafeScalar& other) { *this = other; }
|
|
|
|
SafeScalar& operator=(const SafeScalar& other) {
|
|
|
|
val_ = T(other);
|
|
|
|
initialized_ = true;
|
|
|
|
return *this;
|
|
|
|
}
|
2023-12-05 21:22:55 +00:00
|
|
|
|
2021-04-21 15:45:31 -07:00
|
|
|
SafeScalar(T val) : val_(val), initialized_(true) {}
|
|
|
|
SafeScalar& operator=(T val) {
|
|
|
|
val_ = val;
|
|
|
|
initialized_ = true;
|
|
|
|
}
|
2023-12-05 21:22:55 +00:00
|
|
|
|
2021-04-21 15:45:31 -07:00
|
|
|
operator T() const {
|
|
|
|
VERIFY(initialized_ && "Uninitialized access.");
|
|
|
|
return val_;
|
|
|
|
}
|
2023-12-05 21:22:55 +00:00
|
|
|
|
2021-04-21 15:45:31 -07:00
|
|
|
private:
|
|
|
|
T val_;
|
|
|
|
bool initialized_;
|
|
|
|
};
|