File: Result.h

package info (click to toggle)
pymol 3.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 74,084 kB
  • sloc: cpp: 482,660; python: 89,328; ansic: 29,512; javascript: 6,792; sh: 84; makefile: 25
file content (233 lines) | stat: -rw-r--r-- 5,292 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#pragma once

#include "Util2.h"
#include <string>
#include <type_traits>

namespace pymol
{
struct Void {
};

/**
 * Expressive error handling alternative
 */
class Error
{
public:
  enum Code {
    DEFAULT,
    QUIET,
    MEMORY,
    INCENTIVE_ONLY,
  };

  Error() {}

  /**
   * Construct from string
   */
  explicit Error(std::string s)
      : m_errMsg(std::move(s))
  {
  }

  /**
   * Construct from error code.
   */
  explicit Error(Code code)
      : m_code(code)
  {
  }

  /**
   * Retrieves error message
   * @return error message
   */

  const std::string& what() const noexcept { return m_errMsg; }

  /**
   * Error code
   */
  Code code() const noexcept { return m_code; }

  /**
   * Make an error instance with error code.
   */
  template <Code C, typename... PrintableTs>
  static Error make(PrintableTs&&... ts)
  {
    auto error = Error(join_to_string(std::forward<PrintableTs>(ts)...));
    error.m_code = C;
    return error;
  }

private:
  std::string m_errMsg;
  Code m_code = DEFAULT;
};

/**
 * Creates Error message
 * @param ts collection of printable values to be joined
 * into an error message
 */
template<typename... PrintableTs>
Error make_error(PrintableTs&&... ts)
{
  return Error(join_to_string(std::forward<PrintableTs>(ts)...));
}

/**
 * Class that expresses the expected result of a function
 */

template <typename ResultT=Void> class Result
{

public:
  using type = ResultT;

  Result() = default;

  /**
   * Constructor alternative that allows for convertible types
   * param r result returned from function with a type convertible to ResultT
   */

  Result(type r) : m_result(std::move(r)) {}

  /**
   * Constructor alternative that takes in pymol::Error. Value of expected type
   * should not be taken at this point.
   * @param e error object to express why value should not be used
   */

  Result(Error e) : m_error{std::move(e)}, m_valid{false} {}

  /**
   * Construct from error code.
   */
  Result(Error::Code code)
      : m_error(code)
      , m_valid{false}
  {
  }

  /**
   * Determines whether the value of the expected type can be used.
   */

  explicit operator bool() const noexcept { return m_valid; }

  /**
   * Retrieves the underlying error object
   */

  const Error& error() const noexcept { return m_error; }

  /**
   * Rvalue reference to the underlying error object
   */
  Error&& error_move() noexcept
  {
    assert(!m_valid);
    return std::move(m_error);
  }

  /**
   * Retrieves the value of the expected object
   */

  ResultT& result() { return m_result; }

  /**
   * Retrieves the value of the expected object
   */

  const ResultT& result() const { return m_result; }

  /**
   * Pointer to the expected object. Never nullptr. Call is invalid if this
   * instance is in error state.
   */
  ResultT* operator->()
  {
    assert(m_valid);
    return &m_result;
  }

  /**
   * Reference to the expected object. Behavior is undefined if this
   * instance is in error state.
   */
  ResultT& operator*() &
  {
    assert(m_valid);
    return m_result;
  }

  ResultT&& operator*() &&
  {
    assert(m_valid);
    return std::move(m_result);
  }

  /**
   * Const reference to the expected object. Behavior is undefined if this
   * instance is in error state.
   */
  const ResultT& operator*() const &
  {
    assert(m_valid);
    return m_result;
  }

  const ResultT&& operator*() const &&
  {
    assert(m_valid);
    return std::move(m_result);
  }

private:
  ResultT m_result;
  Error m_error;
  bool m_valid{true};
};

} // namespace pymol

/**
 * If `res` is in error state, return from the calling scope with `res.error()`.
 * @param res Expression of type pymol::Result
 * @note Inspired by `g_return_val_if_fail` from glib, except that the check
 * will always be performed, there is nothing like `G_DISABLE_CHECKS`.
 */
#define p_return_if_error(res)                                                 \
  {                                                                            \
    auto&& _res_evaluated_ = res;                                              \
    if (!_res_evaluated_)                                                      \
      return _res_evaluated_.error_move();                                     \
  }

/**
 * Like p_return_if_error but add a prefix to the error message.
 */
#define p_return_if_error_prefixed(res, prefix)                                \
  {                                                                            \
    auto&& _res_evaluated_ = res;                                              \
    if (!_res_evaluated_)                                                      \
      return pymol::make_error(prefix, _res_evaluated_.error().what());        \
  }

/**
 * If `expr` evaluates to false, return from the calling scope with `val`.
 * @note Inspired by `g_return_val_if_fail` from glib, except that the check
 * will always be performed, there is nothing like `G_DISABLE_CHECKS`.
 */
#define p_return_val_if_fail(expr, val)                                        \
  {                                                                            \
    if (!(expr))                                                               \
      return val;                                                              \
  }