Make exception strings translatable (#81).
[dcpomatic.git] / src / lib / exceptions.h
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef DVDOMATIC_EXCEPTIONS_H
21 #define DVDOMATIC_EXCEPTIONS_H
22
23 /** @file  src/exceptions.h
24  *  @brief Our exceptions.
25  */
26
27 #include <stdexcept>
28 #include <cstring>
29 #include <boost/exception/all.hpp>
30 #include <boost/thread.hpp>
31 extern "C" {
32 #include <libavutil/pixfmt.h>
33 }
34
35 /** @class StringError
36  *  @brief A parent class for exceptions using messages held in a std::string
37  */
38 class StringError : public std::exception
39 {
40 public:
41         /** @param w Error message */
42         StringError (std::string w) {
43                 _what = w;
44         }
45
46         virtual ~StringError () throw () {}
47
48         /** @return error message */
49         char const * what () const throw () {
50                 return _what.c_str ();
51         }
52
53 protected:
54         /** error message */
55         std::string _what;
56 };
57
58 /** @class DecodeError
59  *  @brief A low-level problem with the decoder (possibly due to the nature
60  *  of a source file).
61  */
62 class DecodeError : public StringError
63 {
64 public:
65         DecodeError (std::string s)
66                 : StringError (s)
67         {}
68 };
69
70 /** @class EncodeError
71  *  @brief A low-level problem with an encoder.
72  */
73 class EncodeError : public StringError
74 {
75 public:
76         EncodeError (std::string s)
77                 : StringError (s)
78         {}
79 };
80
81 /** @class FileError.
82  *  @brief Parent class for file-related errors.
83  */
84 class FileError : public StringError
85 {
86 public:
87         /** @param m Error message.
88          *  @param f Name of the file that this exception concerns.
89          */
90         FileError (std::string m, std::string f)
91                 : StringError (m)
92                 , _file (f)
93         {}
94
95         virtual ~FileError () throw () {}
96
97         /** @return name of the file that this exception concerns */
98         std::string file () const {
99                 return _file;
100         }
101
102 private:
103         /** name of the file that this exception concerns */
104         std::string _file;
105 };
106         
107
108 /** @class OpenFileError.
109  *  @brief Indicates that some error occurred when trying to open a file.
110  */
111 class OpenFileError : public FileError
112 {
113 public:
114         /** @param f File that we were trying to open */
115         OpenFileError (std::string f);
116 };
117
118 /** @class CreateFileError.
119  *  @brief Indicates that some error occurred when trying to create a file.
120  */
121 class CreateFileError : public FileError
122 {
123 public:
124         /** @param f File that we were trying to create */
125         CreateFileError (std::string f);
126 };
127
128
129 /** @class ReadFileError.
130  *  @brief Indicates that some error occurred when trying to read from a file
131  */
132 class ReadFileError : public FileError
133 {
134 public:
135         /** @param f File that we were trying to read from.
136          *  @param e errno value, or 0.
137          */
138         ReadFileError (std::string f, int e = 0);
139 };
140
141 /** @class WriteFileError.
142  *  @brief Indicates that some error occurred when trying to write to a file
143  */
144 class WriteFileError : public FileError
145 {
146 public:
147         /** @param f File that we were trying to write to.
148          *  @param e errno value, or 0.
149          */
150         WriteFileError (std::string f, int e);
151 };
152
153 /** @class SettingError.
154  *  @brief Indicates that something is wrong with a setting.
155  */
156 class SettingError : public StringError
157 {
158 public:
159         /** @param s Name of setting that was required.
160          *  @param m Message.
161          */
162         SettingError (std::string s, std::string m)
163                 : StringError (m)
164                 , _setting (s)
165         {}
166
167         virtual ~SettingError () throw () {}
168
169         /** @return name of setting in question */
170         std::string setting () const {
171                 return _setting;
172         }
173
174 private:
175         std::string _setting;
176 };
177
178 /** @class MissingSettingError.
179  *  @brief Indicates that a Film is missing a setting that is required for some operation.
180  */
181 class MissingSettingError : public SettingError
182 {
183 public:
184         /** @param s Name of setting that was required */
185         MissingSettingError (std::string s);
186 };
187
188 /** @class BadSettingError
189  *  @brief Indicates that a setting is bad in some way.
190  */
191 class BadSettingError : public SettingError
192 {
193 public:
194         /** @param s Name of setting that is bad */
195         BadSettingError (std::string s, std::string m)
196                 : SettingError (s, m)
197         {}
198 };
199
200 /** @class NetworkError.
201  *  @brief Indicates some problem with communication on the network.
202  */
203 class NetworkError : public StringError
204 {
205 public:
206         NetworkError (std::string s)
207                 : StringError (s)
208         {}
209 };
210
211 class PixelFormatError : public StringError
212 {
213 public:
214         PixelFormatError (std::string o, AVPixelFormat f);
215 };
216
217 class ExceptionStore
218 {
219 public:
220         bool thrown () const {
221                 boost::mutex::scoped_lock lm (_mutex);
222                 return _exception;
223         }
224         
225         void rethrow () {
226                 boost::mutex::scoped_lock lm (_mutex);
227                 boost::rethrow_exception (_exception);
228         }
229
230 protected:      
231         
232         void store_current () {
233                 boost::mutex::scoped_lock lm (_mutex);
234                 _exception = boost::current_exception ();
235         }
236
237 private:
238         boost::exception_ptr _exception;
239         mutable boost::mutex _mutex;
240 };
241
242         
243
244 #endif