Commit 53bcd888 authored by Hans Fugal's avatar Hans Fugal Committed by Dave Watson

Try Contains enum as an enum class

Summary: Somebody set us up the enum

Test Plan: Unit tests still build and pass. Will see if dependencies build and their unit tests pass. But I don't think anyone uses this outside of `Try` (they shouldn't anyway).

Reviewed By: hannesr@fb.com

FB internal diff: D1184839
parent 7bdae299
...@@ -24,9 +24,9 @@ namespace folly { namespace wangle { ...@@ -24,9 +24,9 @@ namespace folly { namespace wangle {
template <class T> template <class T>
Try<T>::Try(Try<T>&& t) : contains_(t.contains_) { Try<T>::Try(Try<T>&& t) : contains_(t.contains_) {
if (contains_ == VALUE) { if (contains_ == Contains::VALUE) {
new (&value_)T(std::move(t.value_)); new (&value_)T(std::move(t.value_));
} else if (contains_ == EXCEPTION) { } else if (contains_ == Contains::EXCEPTION) {
new (&e_)std::exception_ptr(t.e_); new (&e_)std::exception_ptr(t.e_);
} }
} }
...@@ -35,9 +35,9 @@ template <class T> ...@@ -35,9 +35,9 @@ template <class T>
Try<T>& Try<T>::operator=(Try<T>&& t) { Try<T>& Try<T>::operator=(Try<T>&& t) {
this->~Try(); this->~Try();
contains_ = t.contains_; contains_ = t.contains_;
if (contains_ == VALUE) { if (contains_ == Contains::VALUE) {
new (&value_)T(std::move(t.value_)); new (&value_)T(std::move(t.value_));
} else if (contains_ == EXCEPTION) { } else if (contains_ == Contains::EXCEPTION) {
new (&e_)std::exception_ptr(t.e_); new (&e_)std::exception_ptr(t.e_);
} }
return *this; return *this;
...@@ -45,9 +45,9 @@ Try<T>& Try<T>::operator=(Try<T>&& t) { ...@@ -45,9 +45,9 @@ Try<T>& Try<T>::operator=(Try<T>&& t) {
template <class T> template <class T>
Try<T>::~Try() { Try<T>::~Try() {
if (contains_ == VALUE) { if (contains_ == Contains::VALUE) {
value_.~T(); value_.~T();
} else if (contains_ == EXCEPTION) { } else if (contains_ == Contains::EXCEPTION) {
e_.~exception_ptr(); e_.~exception_ptr();
} }
} }
...@@ -66,8 +66,8 @@ const T& Try<T>::value() const { ...@@ -66,8 +66,8 @@ const T& Try<T>::value() const {
template <class T> template <class T>
void Try<T>::throwIfFailed() const { void Try<T>::throwIfFailed() const {
if (contains_ != VALUE) { if (contains_ != Contains::VALUE) {
if (contains_ == EXCEPTION) { if (contains_ == Contains::EXCEPTION) {
std::rethrow_exception(e_); std::rethrow_exception(e_);
} else { } else {
throw UsingUninitializedTry(); throw UsingUninitializedTry();
......
...@@ -23,7 +23,7 @@ class Try { ...@@ -23,7 +23,7 @@ class Try {
static_assert(!std::is_reference<T>::value, static_assert(!std::is_reference<T>::value,
"Try may not be used with reference types"); "Try may not be used with reference types");
enum Contains { enum class Contains {
VALUE, VALUE,
EXCEPTION, EXCEPTION,
NOTHING, NOTHING,
...@@ -32,10 +32,10 @@ class Try { ...@@ -32,10 +32,10 @@ class Try {
public: public:
typedef T element_type; typedef T element_type;
Try() : contains_(NOTHING) {} Try() : contains_(Contains::NOTHING) {}
explicit Try(const T& v) : contains_(VALUE), value_(v) {} explicit Try(const T& v) : contains_(Contains::VALUE), value_(v) {}
explicit Try(T&& v) : contains_(VALUE), value_(std::move(v)) {} explicit Try(T&& v) : contains_(Contains::VALUE), value_(std::move(v)) {}
explicit Try(std::exception_ptr e) : contains_(EXCEPTION), e_(e) {} explicit Try(std::exception_ptr e) : contains_(Contains::EXCEPTION), e_(e) {}
// move // move
Try(Try<T>&& t); Try(Try<T>&& t);
...@@ -58,8 +58,8 @@ class Try { ...@@ -58,8 +58,8 @@ class Try {
const T* operator->() const { return &value(); } const T* operator->() const { return &value(); }
T* operator->() { return &value(); } T* operator->() { return &value(); }
bool hasValue() const { return contains_ == VALUE; } bool hasValue() const { return contains_ == Contains::VALUE; }
bool hasException() const { return contains_ == EXCEPTION; } bool hasException() const { return contains_ == Contains::EXCEPTION; }
private: private:
Contains contains_; Contains contains_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment