if (contains_ == Contains::VALUE) {
new (&value_)T(std::move(t.value_));
} else if (contains_ == Contains::EXCEPTION) {
- new (&e_)std::unique_ptr<exception_wrapper>(std::move(t.e_));
+ new (&e_) exception_wrapper(std::move(t.e_));
}
}
new (&value_) T();
} else if (t.hasException()) {
contains_ = Contains::EXCEPTION;
- new (&e_) std::unique_ptr<exception_wrapper>(
- std::make_unique<exception_wrapper>(t.exception()));
+ new (&e_) exception_wrapper(t.exception());
}
}
if (contains_ == Contains::VALUE) {
new (&value_)T(std::move(t.value_));
} else if (contains_ == Contains::EXCEPTION) {
- new (&e_)std::unique_ptr<exception_wrapper>(std::move(t.e_));
+ new (&e_) exception_wrapper(std::move(t.e_));
}
return *this;
}
if (contains_ == Contains::VALUE) {
new (&value_)T(t.value_);
} else if (contains_ == Contains::EXCEPTION) {
- new (&e_)std::unique_ptr<exception_wrapper>();
- e_ = std::make_unique<exception_wrapper>(*(t.e_));
+ new (&e_) exception_wrapper(t.e_);
}
}
if (contains_ == Contains::VALUE) {
new (&value_)T(t.value_);
} else if (contains_ == Contains::EXCEPTION) {
- new (&e_)std::unique_ptr<exception_wrapper>();
- e_ = std::make_unique<exception_wrapper>(*(t.e_));
+ new (&e_) exception_wrapper(t.e_);
}
return *this;
}
if (LIKELY(contains_ == Contains::VALUE)) {
value_.~T();
} else if (UNLIKELY(contains_ == Contains::EXCEPTION)) {
- e_.~unique_ptr<exception_wrapper>();
+ e_.~exception_wrapper();
}
}
void Try<T>::throwIfFailed() const {
if (contains_ != Contains::VALUE) {
if (contains_ == Contains::EXCEPTION) {
- e_->throw_exception();
+ e_.throw_exception();
} else {
throw UsingUninitializedTry();
}
void Try<void>::throwIfFailed() const {
if (!hasValue_) {
- e_->throw_exception();
+ e_.throw_exception();
}
}
* @param e The exception_wrapper
*/
explicit Try(exception_wrapper e)
- : contains_(Contains::EXCEPTION),
- e_(std::make_unique<exception_wrapper>(std::move(e))) {}
+ : contains_(Contains::EXCEPTION), e_(std::move(e)) {}
/*
* DEPRECATED
: contains_(Contains::EXCEPTION) {
try {
std::rethrow_exception(ep);
- } catch (const std::exception& e) {
- e_ = std::make_unique<exception_wrapper>(std::current_exception(), e);
+ } catch (std::exception& e) {
+ e_ = exception_wrapper(std::current_exception(), e);
} catch (...) {
- e_ = std::make_unique<exception_wrapper>(std::current_exception());
+ e_ = exception_wrapper(std::current_exception());
}
}
*/
template <class Ex>
bool hasException() const {
- return hasException() && e_->is_compatible_with<Ex>();
+ return hasException() && e_.is_compatible_with<Ex>();
}
exception_wrapper& exception() {
if (UNLIKELY(!hasException())) {
throw TryException("exception(): Try does not contain an exception");
}
- return *e_;
+ return e_;
}
const exception_wrapper& exception() const {
if (UNLIKELY(!hasException())) {
throw TryException("exception(): Try does not contain an exception");
}
- return *e_;
+ return e_;
}
/*
* @returns True if the Try held an Ex and func was executed, false otherwise
*/
template <class Ex, class F>
+ bool withException(F func) {
+ if (!hasException()) {
+ return false;
+ }
+ return e_.with_exception(std::move(func));
+ }
+ template <class Ex, class F>
bool withException(F func) const {
if (!hasException()) {
return false;
}
- return e_->with_exception(std::move(func));
+ return e_.with_exception(std::move(func));
}
template <bool isTry, typename R>
Contains contains_;
union {
T value_;
- std::unique_ptr<exception_wrapper> e_;
+ exception_wrapper e_;
};
};
*
* @param e The exception_wrapper
*/
- explicit Try(exception_wrapper e)
- : hasValue_(false),
- e_(std::make_unique<exception_wrapper>(std::move(e))) {}
+ explicit Try(exception_wrapper e) : hasValue_(false), e_(std::move(e)) {}
/*
* DEPRECATED
try {
std::rethrow_exception(ep);
} catch (const std::exception& e) {
- e_ = std::make_unique<exception_wrapper>(std::current_exception(), e);
+ e_ = exception_wrapper(std::current_exception(), e);
} catch (...) {
- e_ = std::make_unique<exception_wrapper>(std::current_exception());
+ e_ = exception_wrapper(std::current_exception());
}
}
// Copy assigner
Try& operator=(const Try<void>& t) {
hasValue_ = t.hasValue_;
- if (t.e_) {
- e_ = std::make_unique<exception_wrapper>(*t.e_);
- }
+ e_ = t.e_;
return *this;
}
// Copy constructor
// @returns True if the Try contains an exception of type Ex, false otherwise
template <class Ex>
bool hasException() const {
- return hasException() && e_->is_compatible_with<Ex>();
+ return hasException() && e_.is_compatible_with<Ex>();
}
/*
if (UNLIKELY(!hasException())) {
throw TryException("exception(): Try does not contain an exception");
}
- return *e_;
+ return e_;
}
const exception_wrapper& exception() const {
if (UNLIKELY(!hasException())) {
throw TryException("exception(): Try does not contain an exception");
}
- return *e_;
+ return e_;
}
/*
* @returns True if the Try held an Ex and func was executed, false otherwise
*/
template <class Ex, class F>
+ bool withException(F func) {
+ if (!hasException()) {
+ return false;
+ }
+ return e_.with_exception(std::move(func));
+ }
+ template <class Ex, class F>
bool withException(F func) const {
if (!hasException()) {
return false;
}
- return e_->with_exception(std::move(func));
+ return e_.with_exception(std::move(func));
}
template <bool, typename R>
private:
bool hasValue_;
- std::unique_ptr<exception_wrapper> e_{nullptr};
+ exception_wrapper e_;
};
/*