Euphoria
result.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 #include <optional>
5 
6 
7 namespace eu
8 {
9  template <typename T, typename Error = std::string>
10  struct Result
11  {
13 
14  [[nodiscard]]
15  static Self create_value(const T& t)
16  {
17  return Self{t, std::nullopt};
18  }
19 
20  [[nodiscard]]
21  static Self create_error(const Error& error)
22  {
23  // argparse is assuming a empty string here...
24  // todo(Gustav): fix so we always set unknown error and let all argparse functions explcitly parse a empty string
25  // or just don't have a empty error message
26  return Self{std::nullopt, error};
27  }
28 
29  [[nodiscard]]
30  const T& get_value() const
31  {
32  return *value;
33  }
34 
35  [[nodiscard]]
36  const Error& get_error() const
37  {
38  return *error;
39  }
40 
41  operator bool() const
42  {
43  return value.has_value();
44  }
45 
46 
47  [[nodiscard]]
48  const T& operator *() const
49  {
50  return *value;
51  }
52  private:
53  std::optional<T> value;
54  std::optional<Error> error;
55 
56  Result
57  (
58  const std::optional<T>& v,
59  const std::optional<Error>& e
60  )
61  : value(v)
62  , error(e)
63  {
64  }
65  };
66 }
67 
Definition: assert.h:90
static Self create_error(const Error &error)
Definition: result.h:21
static Self create_value(const T &t)
Definition: result.h:15
const T & get_value() const
Definition: result.h:30
const Error & get_error() const
Definition: result.h:36
const T & operator*() const
Definition: result.h:48