Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <exception>
  4. #include <stdexcept>
  5. #include <variant>
  6. #include <cstring>
  7.  
  8. template <class T>
  9. class Try {
  10. public:
  11.     Try() = default;
  12.  
  13.     explicit Try(const T& value) : value_(value) {
  14.         empty_ = false;
  15.     }
  16.  
  17.     template <typename Exception>
  18.     explicit Try(const Exception& exception) {
  19.         try {
  20.             throw exception;
  21.         } catch (const Exception& ex) {
  22.             eptr_ = std::current_exception();
  23.         }
  24.     }
  25.  
  26.     const T& Value() const {
  27.         if (eptr_) {
  28.             std::rethrow_exception(eptr_);
  29.         }
  30.         if (empty_) {
  31.             throw std::runtime_error{"Object is empty"};
  32.         }
  33.         return value_;
  34.     }
  35.  
  36.     void Throw() {
  37.         if (eptr_) {
  38.             std::rethrow_exception(eptr_);
  39.         }
  40.         throw std::runtime_error{"No exception"};
  41.     }
  42.  
  43.     bool IsFailed() const {
  44.         if (eptr_) {
  45.             return true;
  46.         }
  47.         return false;
  48.     }
  49.  
  50. private:
  51.     T value_;
  52.     std::exception_ptr eptr_;
  53.     bool empty_ = true;
  54. };
  55.  
  56. template <>
  57. class Try<void> {
  58. public:
  59.     Try() = default;
  60.  
  61.     template <typename Exception>
  62.     explicit Try(const Exception& exception) {
  63.         try {
  64.             throw exception;
  65.         } catch (const Exception& ex) {
  66.             eptr_ = std::current_exception();
  67.         }
  68.     }
  69.  
  70.     void Throw() {
  71.         if (eptr_) {
  72.             std::rethrow_exception(eptr_);
  73.         }
  74.         throw std::runtime_error{"No exception"};
  75.     }
  76.  
  77.     bool IsFailed() const {
  78.         if (eptr_) {
  79.             return true;
  80.         }
  81.         return false;
  82.     }
  83.  
  84. private:
  85.     std::exception_ptr eptr_;
  86. };
  87.  
  88. template <class Function, class... Args>
  89. auto TryRun(Function func, Args... args) {
  90.     using ReturnType = decltype(func(args...));
  91.     try {
  92.         if constexpr (std::is_void<ReturnType>::value) {
  93.             func(args...);
  94.             Try<ReturnType> nothing;
  95.             return nothing;
  96.  
  97.         } else {
  98.             Try<ReturnType> some_value(func(args...));
  99.             return some_value;
  100.         }
  101.     } catch (const std::exception& ex) {
  102.         Try<ReturnType> except(ex);
  103.         return except;
  104.     } catch (const char* ch) {
  105.         Try<ReturnType> except(std::runtime_error{ch});
  106.         return except;
  107.     } catch (int number) {
  108.         Try<ReturnType> except(std::strerror(number));
  109.         return except;
  110.     } catch (...) {
  111.         Try<ReturnType> except(std::runtime_error{"Unknown exception"});
  112.         return except;
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement