Fix crash on analysing audio when we think the film is zero length.
[dcpomatic.git] / src / lib / exceptions.h
index b66d7baa7593bdab18b60335d7d4d7b54c7afbd3..277355117ee097f86dce6d1d2c084436e8def4db 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>
+extern "C" {
+#include <libavutil/pixfmt.h>
+}
+#include "compose.hpp"
 
 /** @class StringError
  *  @brief A parent class for exceptions using messages held in a std::string
@@ -77,6 +85,9 @@ public:
 class FileError : public StringError
 {
 public:
+       /** @param m Error message.
+        *  @param f Name of the file that this exception concerns.
+        */
        FileError (std::string m, std::string f)
                : StringError (m)
                , _file (f)
@@ -84,11 +95,13 @@ public:
 
        virtual ~FileError () throw () {}
 
+       /** @return name of the file that this exception concerns */
        std::string file () const {
                return _file;
        }
 
 private:
+       /** name of the file that this exception concerns */
        std::string _file;
 };
        
@@ -127,15 +140,10 @@ public:
        /** @param f File that we were trying to read from.
         *  @param e errno value, or 0.
         */
-       ReadFileError (std::string f, int e)
+       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));
        }
 };
 
@@ -151,12 +159,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));
        }
 };
 
@@ -220,18 +223,39 @@ public:
        {}
 };
 
-class PlayError : public StringError
+class PixelFormatError : public StringError
 {
 public:
-       PlayError (std::string s)
-               : StringError (s)
+       PixelFormatError (std::string o, AVPixelFormat f)
+               : StringError (String::compose ("Cannot handle pixel format %1 during %2", f, o))
        {}
 };
 
-class DVDError : public StringError
+class ExceptionStore
 {
 public:
-       DVDError (std::string s)
-               : StringError (s)
-       {}
+       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