| 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: |
| 10 |
Optional() : has(false), value() { |
| 11 |
} |
| 12 |
|
| 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 |
|
| 30 |
bool has_value() const { |
| 31 |
return has; |
| 32 |
} |
| 33 |
|
| 34 |
void reset() { |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
this->value = T(); |
| 40 |
this->has= false; |
| 41 |
} |
| 42 |
}; |
| 43 |
|
| 44 |
#endif |