summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
59bdbbc)
Summary: I'm upgrading assertions to throws, since these are fatal in all circumstances. If something is explicitly `Optional`, it makes sense to fail loudly if it is misused in this manner.
Reviewed By: @yfeldblum
Differential Revision:
D2247612
* cout << *v << endl;
* }
*/
* cout << *v << endl;
* }
*/
#include <type_traits>
#include <utility>
#include <type_traits>
#include <utility>
# pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif // __GNUC__
# pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif // __GNUC__
+class OptionalEmptyException : public std::runtime_error {
+ public:
+ OptionalEmptyException()
+ : std::runtime_error("Empty Optional cannot be unwrapped") {}
+};
+
template<class Value>
class Optional {
public:
template<class Value>
class Optional {
public:
}
const Value& value() const {
}
const Value& value() const {
return value_;
}
Value& value() {
return value_;
}
Value& value() {
+ Value* get_pointer() { return hasValue_ ? &value_ : nullptr; }
+ const Value* get_pointer() const { return hasValue_ ? &value_ : nullptr; }
+
bool hasValue() const { return hasValue_; }
explicit operator bool() const {
bool hasValue() const { return hasValue_; }
explicit operator bool() const {
+ void require_value() const {
+ if (!hasValue_) {
+ throw OptionalEmptyException();
+ }
+ }
+
template<class... Args>
void construct(Args&&... args) {
const void* ptr = &value_;
template<class... Args>
void construct(Args&&... args) {
const void* ptr = &value_;
template<class T>
const T* get_pointer(const Optional<T>& opt) {
template<class T>
const T* get_pointer(const Optional<T>& opt) {
- return opt ? &opt.value() : nullptr;
+ return opt.get_pointer();
}
template<class T>
T* get_pointer(Optional<T>& opt) {
}
template<class T>
T* get_pointer(Optional<T>& opt) {
- return opt ? &opt.value() : nullptr;
+ return opt.get_pointer();
+TEST(Optional, Exceptions) {
+ Optional<int> empty;
+ EXPECT_THROW(empty.value(), OptionalEmptyException);
+}
+