Advertisement
anechka_ne_plach

try hard

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