aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/SafeScalar.h
blob: c5cb75717c7c0131814ee98ceb840ac83b48dd26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

// 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;
  }
  
  SafeScalar(T val) : val_(val), initialized_(true) {}
  SafeScalar& operator=(T val) {
    val_ = val;
    initialized_ = true;
  }
  
  operator T() const {
    VERIFY(initialized_ && "Uninitialized access.");
    return val_;
  }
 
 private:
  T val_;
  bool initialized_;
};