Advertisement
miguelmartins1987

C++ minimal 'expected' implementation

May 20th, 2022
1,086
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. enum class file_open_error
  6. {
  7.     file_not_found,
  8.     other
  9. };
  10.  
  11. namespace whatever
  12. {
  13.     template <class E>
  14.     class unexpected
  15.     {
  16.     public:
  17.         constexpr unexpected(const E& value)
  18.         {
  19.             val = value;
  20.         }
  21.         constexpr const E& value() const&
  22.         {
  23.             return val;
  24.         }
  25.     private:
  26.         E val;
  27.     };
  28.  
  29.     template <class T, class E>
  30.     class expected
  31.     {
  32.     public:
  33.         typedef T value_type;
  34.         typedef E error_type;
  35.         typedef unexpected<E> unexpected_type;
  36.  
  37.         constexpr expected(const value_type& value)
  38.         {
  39.             this->val = value;
  40.             this->has_val = true;
  41.         }
  42.         constexpr expected(const unexpected_type& value)
  43.         {
  44.             this->unexpect = value;
  45.             this->has_val = false;
  46.         }
  47.  
  48.         constexpr operator bool() const noexcept
  49.         {
  50.             return has_val;
  51.         }
  52.  
  53.         constexpr const T& operator *() const&
  54.         {
  55.             return val;
  56.         }
  57.  
  58.         constexpr const E& error() const&
  59.         {
  60.             return unexpect.value();
  61.         }
  62.     private:
  63.         bool has_val;
  64.         union
  65.         {
  66.             value_type val;
  67.             unexpected_type unexpect;
  68.         };
  69.     };
  70. }
  71.  
  72. whatever::expected<FILE*, file_open_error> open_file(std::string pathToFile)
  73. {
  74. #pragma warning(suppress : 4996)
  75.     FILE* file = fopen(pathToFile.c_str(), "r");
  76.     if (file)
  77.     {
  78.         return file;
  79.     }
  80.     else
  81.     {
  82.         switch (errno)
  83.         {
  84.         case ENOENT:
  85.             return whatever::unexpected<file_open_error>(file_open_error::file_not_found);
  86.         default:
  87.             return whatever::unexpected<file_open_error>(file_open_error::other);
  88.         }
  89.     }
  90. }
  91.  
  92. int main()
  93. {
  94.     whatever::expected<FILE*, file_open_error> file = open_file("C:/test.dat");
  95.     if (file)
  96.     {
  97.         std::cout << "File opened successfully!\n";
  98.         fclose(*file);
  99.     }
  100.     else
  101.     {
  102.         switch (file.error())
  103.         {
  104.         case file_open_error::file_not_found:
  105.             std::cout << "File not found!\n";
  106.             break;
  107.         default:
  108.             std::cout << "Other error!\n";
  109.             break;
  110.         }
  111.     }
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement