Advertisement
anechka_ne_plach

Untitled

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