Advertisement
anechka_ne_plach

Untitled

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