Advertisement
anechka_ne_plach

Untitled

Oct 15th, 2021
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.36 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.  
  58. private:
  59.     bool empty_ = true;
  60.     T value_;
  61.     std::exception_ptr eptr_;
  62. };
  63.  
  64. template <>
  65. class Try<void> : private Try<Slug> {
  66. public:
  67.     Try() {
  68.     }
  69.     template <typename Exv>
  70.     Try(const Exv& exv) : Try<Slug>(exv) {
  71.     }
  72.     void Throw() {
  73.         Try<Slug>::Throw();
  74.     }
  75.     bool IsFailed() {
  76.         return Try<Slug>::IsFailed();
  77.     }
  78. };
  79.  
  80. template <class Function, class... Args>
  81. auto TryRun(Function func, Args... args) {
  82.     using ReturnType = decltype(func(args...));
  83.     try {
  84.         func(args...);
  85.         if constexpr (std::is_same<ReturnType, void>::value) {
  86.             return Try<ReturnType>();
  87.         } else {
  88.             return Try<ReturnType>(func(args...));
  89.         }
  90.     } catch (std::exception& e) {
  91.         return Try<ReturnType>(std::current_exception());
  92.     } catch (const char* str) {
  93.         std::runtime_error e(str);
  94.         return Try<ReturnType>(e);
  95.     } catch (int i) {
  96.         std::runtime_error e(std::strerror(i));
  97.         return Try<ReturnType>(e);
  98.     } catch (...) {
  99.         std::runtime_error e("Unknown exception");
  100.         return Try<ReturnType>(e);
  101.     }
  102. }
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement