Merge master.
[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         /* XXX: should be boost::filesystem::path */
116         OpenFileError (std::string f);
117 };
118
119 /** @class CreateFileError.
120  *  @brief Indicates that some error occurred when trying to create a file.
121  */
122 class CreateFileError : public FileError
123 {
124 public:
125         /** @param f File that we were trying to create */
126         CreateFileError (std::string f);
127 };
128
129
130 /** @class ReadFileError.
131  *  @brief Indicates that some error occurred when trying to read from a file
132  */
133 class ReadFileError : public FileError
134 {
135 public:
136         /** @param f File that we were trying to read from.
137          *  @param e errno value, or 0.
138          */
139         ReadFileError (std::string f, int e = 0);
140 };
141
142 /** @class WriteFileError.
143  *  @brief Indicates that some error occurred when trying to write to a file
144  */
145 class WriteFileError : public FileError
146 {
147 public:
148         /** @param f File that we were trying to write to.
149          *  @param e errno value, or 0.
150          */
151         WriteFileError (std::string f, int e);
152 };
153
154 /** @class SettingError.
155  *  @brief Indicates that something is wrong with a setting.
156  */
157 class SettingError : public StringError
158 {
159 public:
160         /** @param s Name of setting that was required.
161          *  @param m Message.
162          */
163         SettingError (std::string s, std::string m)
164                 : StringError (m)
165                 , _setting (s)
166         {}
167
168         virtual ~SettingError () throw () {}
169
170         /** @return name of setting in question */
171         std::string setting () const {
172                 return _setting;
173         }
174
175 private:
176         std::string _setting;
177 };
178
179 /** @class MissingSettingError.
180  *  @brief Indicates that a Film is missing a setting that is required for some operation.
181  */
182 class MissingSettingError : public SettingError
183 {
184 public:
185         /** @param s Name of setting that was required */
186         MissingSettingError (std::string s);
187 };
188
189 /** @class BadSettingError
190  *  @brief Indicates that a setting is bad in some way.
191  */
192 class BadSettingError : public SettingError
193 {
194 public:
195         /** @param s Name of setting that is bad */
196         BadSettingError (std::string s, std::string m)
197                 : SettingError (s, m)
198         {}
199 };
200
201 /** @class NetworkError.
202  *  @brief Indicates some problem with communication on the network.
203  */
204 class NetworkError : public StringError
205 {
206 public:
207         NetworkError (std::string s)
208                 : StringError (s)
209         {}
210 };
211
212 class PixelFormatError : public StringError
213 {
214 public:
215         PixelFormatError (std::string o, AVPixelFormat f);
216 };
217
218 class ExceptionStore
219 {
220 public:
221         bool thrown () const {
222                 boost::mutex::scoped_lock lm (_mutex);
223                 return _exception;
224         }
225         
226         void rethrow () {
227                 boost::mutex::scoped_lock lm (_mutex);
228                 boost::rethrow_exception (_exception);
229         }
230
231 protected:      
232         
233         void store_current () {
234                 boost::mutex::scoped_lock lm (_mutex);
235                 _exception = boost::current_exception ();
236         }
237
238 private:
239         boost::exception_ptr _exception;
240         mutable boost::mutex _mutex;
241 };
242
243         
244
245 #endif