ARB
ErrorOrType.h
Go to the documentation of this file.
1 // ========================================================= //
2 // //
3 // File : ErrorOrType.h //
4 // Purpose : provide "hybrid" result type //
5 // //
6 // Coded by Ralf Westram (coder@reallysoft.de) in Apr 19 //
7 // http://www.arb-home.de/ //
8 // //
9 // ========================================================= //
10 
11 #ifndef ERRORORTYPE_H
12 #define ERRORORTYPE_H
13 
14 #ifndef ARB_ERROR_H
15 #include "arb_error.h"
16 #endif
17 
18 template<typename TYPE>
19 class ErrorOr {
24  ARB_ERROR error;
25  TYPE value;
26 
27 #if defined(ASSERTION_USED)
28  mutable bool checked;
29 #endif
30 
31  void mark_checked() const {
32 #if defined(ASSERTION_USED)
33  checked = true;
34 #endif
35  }
36  void expect_checked() const { arb_assert(checked); }
37 
38 public:
39  ErrorOr(ARB_ERROR err, const TYPE& t) :
40  error(err),
41  value(t)
42 #if defined(ASSERTION_USED)
43  , checked(false)
44 #endif
45  {
46  }
47 
48  bool hasError() const { mark_checked(); return error; }
49  bool hasValue() const { return !hasError(); }
50 
51  ARB_ERROR getError() const {
52  expect_checked();
54  return error;
55  }
56  TYPE getValue() const {
57  expect_checked();
59  error.expect_no_error();
60  return value;
61  }
62 };
63 
64 #else
65 #error ErrorOrType.h included twice
66 #endif // ERRORORTYPE_H
#define arb_assert(cond)
Definition: arb_assert.h:245
ErrorOr(ARB_ERROR err, const TYPE &t)
Definition: ErrorOrType.h:39
bool hasError() const
Definition: ErrorOrType.h:48
TYPE getValue() const
Definition: ErrorOrType.h:56
#define false
Definition: ureadseq.h:13
void expect_no_error() const
Definition: arb_error.h:136
GB_write_int const char GB_write_autoconv_string WRITE_SKELETON(write_pointer, GBDATA *,"%p", GB_write_pointer) char *AW_awa if)(!gb_var) return strdup("")
Definition: AW_awar.cxx:166
bool hasValue() const
Definition: ErrorOrType.h:49
ARB_ERROR getError() const
Definition: ErrorOrType.h:51
PT1_TYPE
Definition: probe_tree.h:185