1 #ifndef _OPTIONAL_H_ 2 #define _OPTIONAL_H_ 3 4 template<class T> 5 class Optional { 6 bool has; 7 T value; 8 9 public: Optional()10 Optional() : has(false), value() { 11 } 12 Optional(const T & val)13 Optional(const T& val) : has(true), value(val) { 14 } 15 16 const T& operator*() const { 17 return this->value; 18 } 19 T& operator*() { 20 return this->value; 21 } 22 T* operator->() { 23 return &this->value; 24 } 25 void operator=(const T& value) { 26 this->value = value; 27 this->has = true; 28 } 29 has_value()30 bool has_value() const { 31 return has; 32 } 33 reset()34 void reset() { 35 // Creating a new object may be problematic or expensive for some classes; 36 // however, for the types we use in later, this is OK. If Optional is used 37 // for more types in the future, we could switch to a different 38 // implementation of optional. 39 this->value = T(); 40 this->has= false; 41 } 42 }; 43 44 #endif // _OPTIONAL_H_ 45