Throw an exception rather than asserting when unable to handle a pixel format (#65).
[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 #include "compose.hpp"
35
36 /** @class StringError
37  *  @brief A parent class for exceptions using messages held in a std::string
38  */
39 class StringError : public std::exception
40 {
41 public:
42         /** @param w Error message */
43         StringError (std::string w) {
44                 _what = w;
45         }
46
47         virtual ~StringError () throw () {}
48
49         /** @return error message */
50         char const * what () const throw () {
51                 return _what.c_str ();
52         }
53
54 protected:
55         /** error message */
56         std::string _what;
57 };
58
59 /** @class DecodeError
60  *  @brief A low-level problem with the decoder (possibly due to the nature
61  *  of a source file).
62  */
63 class DecodeError : public StringError
64 {
65 public:
66         DecodeError (std::string s)
67                 : StringError (s)
68         {}
69 };
70
71 /** @class EncodeError
72  *  @brief A low-level problem with an encoder.
73  */
74 class EncodeError : public StringError
75 {
76 public:
77         EncodeError (std::string s)
78                 : StringError (s)
79         {}
80 };
81
82 /** @class FileError.
83  *  @brief Parent class for file-related errors.
84  */
85 class FileError : public StringError
86 {
87 public:
88         /** @param m Error message.
89          *  @param f Name of the file that this exception concerns.
90          */
91         FileError (std::string m, std::string f)
92                 : StringError (m)
93                 , _file (f)
94         {}
95
96         virtual ~FileError () throw () {}
97
98         /** @return name of the file that this exception concerns */
99         std::string file () const {
100                 return _file;
101         }
102
103 private:
104         /** name of the file that this exception concerns */
105         std::string _file;
106 };
107         
108
109 /** @class OpenFileError.
110  *  @brief Indicates that some error occurred when trying to open a file.
111  */
112 class OpenFileError : public FileError
113 {
114 public:
115         /** @param f File that we were trying to open */
116         OpenFileError (std::string f)
117                 : FileError ("could not open file " + f, f)
118         {}
119 };
120
121 /** @class CreateFileError.
122  *  @brief Indicates that some error occurred when trying to create a file.
123  */
124 class CreateFileError : public FileError
125 {
126 public:
127         /** @param f File that we were trying to create */
128         CreateFileError (std::string f)
129                 : FileError ("could not create file " + f, f)
130         {}
131 };
132
133
134 /** @class ReadFileError.
135  *  @brief Indicates that some error occurred when trying to read from a file
136  */
137 class ReadFileError : public FileError
138 {
139 public:
140         /** @param f File that we were trying to read from.
141          *  @param e errno value, or 0.
142          */
143         ReadFileError (std::string f, int e = 0)
144                 : FileError ("", f)
145         {
146                 _what = String::compose ("could not read from file %1 (%2)", f, strerror (e));
147         }
148 };
149
150 /** @class WriteFileError.
151  *  @brief Indicates that some error occurred when trying to write to a file
152  */
153 class WriteFileError : public FileError
154 {
155 public:
156         /** @param f File that we were trying to write to.
157          *  @param e errno value, or 0.
158          */
159         WriteFileError (std::string f, int e)
160                 : FileError ("", f)
161         {
162                 _what = String::compose ("could not write to file %1 (%2)", f, strerror (e));
163         }
164 };
165
166 /** @class SettingError.
167  *  @brief Indicates that something is wrong with a setting.
168  */
169 class SettingError : public StringError
170 {
171 public:
172         /** @param s Name of setting that was required.
173          *  @param m Message.
174          */
175         SettingError (std::string s, std::string m)
176                 : StringError (m)
177                 , _setting (s)
178         {}
179
180         virtual ~SettingError () throw () {}
181
182         /** @return name of setting in question */
183         std::string setting () const {
184                 return _setting;
185         }
186
187 private:
188         std::string _setting;
189 };
190
191 /** @class MissingSettingError.
192  *  @brief Indicates that a Film is missing a setting that is required for some operation.
193  */
194 class MissingSettingError : public SettingError
195 {
196 public:
197         /** @param s Name of setting that was required */
198         MissingSettingError (std::string s)
199                 : SettingError (s, "missing required setting " + s)
200         {}
201 };
202
203 /** @class BadSettingError
204  *  @brief Indicates that a setting is bad in some way.
205  */
206 class BadSettingError : public SettingError
207 {
208 public:
209         /** @param s Name of setting that is bad */
210         BadSettingError (std::string s, std::string m)
211                 : SettingError (s, m)
212         {}
213 };
214
215 /** @class NetworkError.
216  *  @brief Indicates some problem with communication on the network.
217  */
218 class NetworkError : public StringError
219 {
220 public:
221         NetworkError (std::string s)
222                 : StringError (s)
223         {}
224 };
225
226 class PixelFormatError : public StringError
227 {
228 public:
229         PixelFormatError (std::string o, AVPixelFormat f)
230                 : StringError (String::compose ("Cannot handle pixel format %1 during %2", f, o))
231         {}
232 };
233
234 class ExceptionStore
235 {
236 public:
237         bool thrown () const {
238                 boost::mutex::scoped_lock lm (_mutex);
239                 return _exception;
240         }
241         
242         void rethrow () {
243                 boost::mutex::scoped_lock lm (_mutex);
244                 boost::rethrow_exception (_exception);
245         }
246
247 protected:      
248         
249         void store_current () {
250                 boost::mutex::scoped_lock lm (_mutex);
251                 _exception = boost::current_exception ();
252         }
253
254 private:
255         boost::exception_ptr _exception;
256         mutable boost::mutex _mutex;
257 };
258
259         
260
261 #endif