cart-elc

Source code for CART-ELC
git clone git://git.laack.co/cart-elc.git
Log | Files | Refs | README | LICENSE

SafeScalar.h (605B)


      1 
      2 // A Scalar that asserts for uninitialized access.
      3 template<typename T>
      4 class SafeScalar {
      5  public:
      6   SafeScalar() : initialized_(false) {}
      7   SafeScalar(const SafeScalar& other) {
      8     *this = other;
      9   }
     10   SafeScalar& operator=(const SafeScalar& other) {
     11     val_ = T(other);
     12     initialized_ = true;
     13     return *this;
     14   }
     15   
     16   SafeScalar(T val) : val_(val), initialized_(true) {}
     17   SafeScalar& operator=(T val) {
     18     val_ = val;
     19     initialized_ = true;
     20   }
     21   
     22   operator T() const {
     23     VERIFY(initialized_ && "Uninitialized access.");
     24     return val_;
     25   }
     26  
     27  private:
     28   T val_;
     29   bool initialized_;
     30 };