Brought documentation up to date.
[videolink.git] / xpcom_support.cpp
1 // Copyright 2005 Ben Hutchings <ben@decadent.org.uk>.
2 // See the file "COPYING" for licence details.
3
4 #include <cassert>
5 #include <memory>
6 #include <stdexcept>
7
8 #include "xpcom_support.hpp"
9
10 namespace xpcom_support
11 {
12     void throw_exception(nsresult error)
13     {
14         assert(NS_ERROR_GET_SEVERITY(error) == NS_ERROR_SEVERITY_ERROR);
15
16         // TODO: look up error message
17         char message[30];
18         std::sprintf(message, "XPCOM error %08x", error);
19
20         switch (error)
21         {
22         case NS_ERROR_OUT_OF_MEMORY:
23             throw std::bad_alloc();
24
25         case NS_ERROR_NOT_INITIALIZED:
26         case NS_ERROR_ALREADY_INITIALIZED:
27         case NS_ERROR_INVALID_POINTER:
28         case NS_ERROR_ILLEGAL_VALUE:
29         case NS_BASE_STREAM_CLOSED:
30         case NS_BASE_STREAM_ILLEGAL_ARGS:
31             assert(!"internal error detected by XPCOM function");
32             throw std::logic_error(message);
33
34         default:
35             throw std::runtime_error(message);
36         }
37     }
38 }