ScummVM API documentation
error.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 //=============================================================================
23 //
24 // Universal error class, that may be used both as a return value or
25 // thrown as an exception.
26 //
27 //=============================================================================
28 
29 #ifndef AGS_SHARED_UTIL_ERROR_H
30 #define AGS_SHARED_UTIL_ERROR_H
31 
32 #include "common/std/memory.h"
33 #include "ags/shared/util/string.h"
34 
35 namespace AGS3 {
36 namespace AGS {
37 namespace Shared {
38 
39 class Error;
40 typedef std::shared_ptr<Error> PError;
41 
42 //
43 // A simple struct, that provides several fields to describe an error in the program.
44 // If wanted, may be reworked into subclass of std::exception.
45 //
46 class Error {
47 public:
48  Error(int code, String general, PError inner_error = PError()) : _code(code), _general(general), _innerError(inner_error) {
49  }
50  Error(int code, String general, String comment, PError inner_error = PError()) : _code(code), _general(general), _comment(comment), _innerError(inner_error) {
51  }
52  Error(String general, PError inner_error = PError()) : _code(0), _general(general), _innerError(inner_error) {
53  }
54  Error(String general, String comment, PError inner_error = PError()) : _code(0), _general(general), _comment(comment), _innerError(inner_error) {
55  }
56 
57 
58  // Error code is a number, defining error subtype. It is not much use to the end-user,
59  // but may be checked in the program to know more precise cause of the error.
60  int Code() const {
61  return _code;
62  }
63  // General description of this error type and subtype.
64  String General() const {
65  return _general;
66  }
67  // Any complementary information.
68  String Comment() const {
69  return _comment;
70  }
71  PError InnerError() const {
72  return _innerError;
73  }
74  // Full error message combines general description and comment.
75  // NOTE: if made a child of std::exception, FullMessage may be substituted
76  // or complemented with virtual const char* what().
77  String FullMessage() const {
78  String msg;
79  const Error *err = this;
80  do {
81  msg.Append(err->General());
82  if (!err->Comment().IsEmpty()) {
83  msg.AppendChar('\n');
84  msg.Append(err->Comment());
85  }
86  err = err->InnerError().get();
87  if (err)
88  msg.AppendChar('\n');
89  } while (err);
90  return msg;
91  }
92 
93 private:
94  int _code; // numeric code, for specific uses
95  String _general; // general description of this error class
96  String _comment; // additional information about particular case
97  PError _innerError; // previous error that caused this one
98 };
99 
100 
101 // ErrorHandle is a helper class that lets you have an Error object
102 // wrapped in a smart pointer. ErrorHandle's only data member is a
103 // shared_ptr, which means that it does not cause too much data copying
104 // when used as a function's return value.
105 // Note, that the reason to have distinct class instead of a shared_ptr's
106 // typedef is an inverted boolean comparison:
107 // shared_ptr converts to 'true' when it contains an object, but ErrorHandle
108 // returns 'true' when it *does NOT* contain an object, meaning there
109 // is no error.
110 template <class T> class ErrorHandle {
111 public:
112  static ErrorHandle<T> None() {
113  return ErrorHandle();
114  }
115 
116  ErrorHandle() {}
117  ErrorHandle(T *err) : _error(err) {
118  }
119  ErrorHandle(std::shared_ptr<T> err) : _error(err) {
120  }
121 
122  bool HasError() const {
123  return _error.get() != NULL;
124  }
125  explicit operator bool() const {
126  return _error.get() == nullptr;
127  }
128  operator PError() const {
129  return _error;
130  }
131  T *operator ->() const {
132  return _error.operator->();
133  }
134  T &operator *() const {
135  return _error.operator * ();
136  }
137 
138 private:
139  std::shared_ptr<T> _error;
140 };
141 
142 
143 // Basic error handle, containing Error object
144 typedef ErrorHandle<Error> HError;
145 
146 
147 // TypedCodeError is the Error's subclass, which only purpose is to override
148 // error code type in constructor and Code() getter, that may be useful if
149 // you'd like to restrict code values to particular enumerator.
150 // TODO: a type identifier as a part of template (string, or perhaps a int16
151 // to be placed at high-bytes in Code) to be able to distinguish error group.
152 template <typename CodeType, String(*GetErrorText)(CodeType)>
153 class TypedCodeError : public Error {
154 public:
155  TypedCodeError(CodeType code, PError inner_error = PError()) : Error(code, GetErrorText(code), inner_error) {
156  }
157  TypedCodeError(CodeType code, String comment, PError inner_error = PError()) :
158  Error(code, GetErrorText(code), comment, inner_error) {
159  }
160 
161  CodeType Code() const {
162  return (CodeType)Error::Code();
163  }
164 };
165 
166 } // namespace Shared
167 } // namespace AGS
168 } // namespace AGS3
169 
170 #endif
Definition: achievements_tables.h:27
PointerType get() const
Definition: ptr.h:229
Definition: error.h:153
Definition: string.h:62
Definition: error.h:46
Definition: ptr.h:159
Definition: error.h:110
Definition: ags.h:40