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