Fix the build for older macOS.
[dcpomatic.git] / src / lib / exception_store.h
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #ifndef DCPOMATIC_EXCEPTION_STORE_H
22 #define DCPOMATIC_EXCEPTION_STORE_H
23
24 #include <boost/exception/all.hpp>
25 #include <boost/thread/mutex.hpp>
26
27 /** @class ExceptionStore
28  *  @brief A parent class for classes which have a need to catch and
29  *  re-throw exceptions.
30  *
31  *  This is intended for classes which run their own thread; they should do
32  *  something like
33  *
34  *  void my_thread ()
35  *  try {
36  *    // do things which might throw exceptions
37  *  } catch (...) {
38  *    store_current ();
39  *  }
40  *
41  *  and then in another thread call rethrow().  If any
42  *  exception was thrown by my_thread it will be stored by
43  *  store_current() and then rethrow() will re-throw it where
44  *  it can be handled.
45  */
46 class ExceptionStore
47 {
48 public:
49         void rethrow () {
50                 boost::mutex::scoped_lock lm (_exception_mutex);
51                 if (_exception) {
52                         boost::exception_ptr tmp = _exception;
53                         _exception = boost::exception_ptr ();
54                         boost::rethrow_exception (tmp);
55                 }
56         }
57
58 protected:
59
60         void store_current () {
61                 boost::mutex::scoped_lock lm (_exception_mutex);
62                 _exception = boost::current_exception ();
63         }
64
65 private:
66         boost::exception_ptr _exception;
67         mutable boost::mutex _exception_mutex;
68 };
69
70 #endif