Merge branch 'master' of ssh://carlh.dnsalias.org/home/carl/git/dvdomatic
[dcpomatic.git] / src / lib / exceptions.h
index bf8e85f0bdfb7d939f76b90ad41c4571361bd874..6de8806e4e8debc24eeef6100235a9a44498f96f 100644 (file)
 
 */
 
+#ifndef DVDOMATIC_EXCEPTIONS_H
+#define DVDOMATIC_EXCEPTIONS_H
+
 /** @file  src/exceptions.h
  *  @brief Our exceptions.
  */
 
 #include <stdexcept>
-#include <sstream>
 #include <cstring>
+#include <boost/exception/all.hpp>
+#include <boost/thread.hpp>
+#include "compose.hpp"
 
 /** @class StringError
  *  @brief A parent class for exceptions using messages held in a std::string
@@ -135,12 +140,7 @@ public:
        ReadFileError (std::string f, int e = 0)
                : FileError ("", f)
        {
-               std::stringstream s;
-               s << "could not read from file " << f;
-               if (e) {
-                       s << " (" << strerror (e) << ")";
-               }
-               _what = s.str ();
+               _what = String::compose ("could not read from file %1 (%2)", f, strerror (e));
        }
 };
 
@@ -156,12 +156,7 @@ public:
        WriteFileError (std::string f, int e)
                : FileError ("", f)
        {
-               std::stringstream s;
-               s << "could not write to file " << f;
-               if (e) {
-                       s << " (" << strerror (e) << ")";
-               }
-               _what = s.str ();
+               _what = String::compose ("could not write to file %1 (%2)", f, strerror (e));
        }
 };
 
@@ -224,3 +219,30 @@ public:
                : StringError (s)
        {}
 };
+
+class ExceptionStore
+{
+public:
+       bool thrown () const {
+               boost::mutex::scoped_lock lm (_mutex);
+               return _exception;
+       }
+       
+       void rethrow () {
+               boost::mutex::scoped_lock lm (_mutex);
+               boost::rethrow_exception (_exception);
+       }
+
+protected:     
+       
+       void store_current () {
+               boost::mutex::scoped_lock lm (_mutex);
+               _exception = boost::current_exception ();
+       }
+
+private:
+       boost::exception_ptr _exception;
+       mutable boost::mutex _mutex;
+};
+
+#endif