]> git.sesse.net Git - casparcg/blob - common/except.h
2e76fff94e95763f1e1da75159ace6d7f0c56cab
[casparcg] / common / except.h
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\r
5 *\r
6 * CasparCG is free software: you can redistribute it and/or modify\r
7 * it under the terms of the GNU General Public License as published by\r
8 * the Free Software Foundation, either version 3 of the License, or\r
9 * (at your option) any later version.\r
10 *\r
11 * CasparCG is distributed in the hope that it will be useful,\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 * GNU General Public License for more details.\r
15 *\r
16 * You should have received a copy of the GNU General Public License\r
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #pragma once\r
23 \r
24 #include "utf.h"\r
25 \r
26 #include "log.h"\r
27 \r
28 #include <exception>\r
29 #include <boost/exception/all.hpp>\r
30 #include <boost/exception/error_info.hpp>\r
31 #include <boost/throw_exception.hpp>\r
32 \r
33 struct _EXCEPTION_RECORD;\r
34 struct _EXCEPTION_POINTERS;\r
35 \r
36 typedef _EXCEPTION_RECORD EXCEPTION_RECORD;\r
37 typedef _EXCEPTION_POINTERS EXCEPTION_POINTERS;\r
38 \r
39 namespace caspar {\r
40 \r
41 typedef boost::error_info<struct tag_arg_name_info, std::string>        arg_name_info_t;\r
42 typedef boost::error_info<struct tag_arg_value_info, std::string>       arg_value_info_t;\r
43 typedef boost::error_info<struct tag_msg_info, std::string>                     msg_info_t;\r
44 typedef boost::error_info<struct tag_call_stack_info, std::string>      call_stack_info_t;\r
45 typedef boost::error_info<struct tag_msg_info, std::string>                     error_info_t;\r
46 typedef boost::error_info<struct tag_source_info, std::string>          source_info_t;\r
47 typedef boost::error_info<struct tag_file_name_info, std::string>       file_name_info_t;\r
48 \r
49 template<typename T>\r
50 inline arg_name_info_t          arg_name_info(const T& str)             {return arg_name_info_t(u8(str));}\r
51 template<typename T>\r
52 inline arg_value_info_t         arg_value_info(const T& str)    {return arg_value_info_t(u8(str));}\r
53 template<typename T>\r
54 inline msg_info_t                       msg_info(const T& str)                  {return msg_info_t(u8(str));}\r
55 template<typename T>\r
56 inline call_stack_info_t        call_stack_info(const T& str)   {return call_stack_info_t(u8(str));}\r
57 template<typename T>\r
58 inline error_info_t                     error_info(const T& str)                {return error_info_t(u8(str));}\r
59 template<typename T>\r
60 inline source_info_t            source_info(const T& str)               {return source_info_t(u8(str));}\r
61 template<typename T>\r
62 inline file_name_info_t file_name_info(const T& str)    {return file_name_info_t(u8(str));}\r
63 \r
64 typedef boost::error_info<struct tag_line_info, size_t>                                         line_info;\r
65 typedef boost::error_info<struct tag_nested_exception_, std::exception_ptr> nested_exception;\r
66 \r
67 struct caspar_exception                 : virtual boost::exception, virtual std::exception \r
68 {\r
69         caspar_exception(){}\r
70         explicit caspar_exception(const char* msg) : std::exception(msg) {}\r
71 };\r
72 \r
73 struct io_error                                 : virtual caspar_exception {};\r
74 struct directory_not_found              : virtual io_error {};\r
75 struct file_not_found                   : virtual io_error {};\r
76 struct file_read_error          : virtual io_error {};\r
77 struct file_write_error         : virtual io_error {};\r
78 \r
79 struct invalid_argument                 : virtual caspar_exception {};\r
80 struct null_argument                    : virtual invalid_argument {};\r
81 struct out_of_range                             : virtual invalid_argument {};\r
82 struct bad_alloc                                : virtual caspar_exception {};\r
83 \r
84 struct invalid_operation                : virtual caspar_exception {};\r
85 struct operation_failed                 : virtual caspar_exception {};\r
86 struct timed_out                                : virtual caspar_exception {};\r
87 \r
88 struct not_supported                    : virtual caspar_exception {};\r
89 struct not_implemented                  : virtual caspar_exception {};\r
90 \r
91 class win32_exception : public std::exception\r
92 {\r
93 public:\r
94         typedef const void* address;\r
95         static void install_handler();\r
96 \r
97         address location() const { return location_; }\r
98         unsigned int error_code() const { return errorCode_; }\r
99         virtual const char* what() const { return message_;     }\r
100 \r
101 protected:\r
102         win32_exception(const EXCEPTION_RECORD& info);\r
103         static void Handler(unsigned int errorCode, EXCEPTION_POINTERS* pInfo);\r
104 \r
105 private:\r
106         const char* message_;\r
107 \r
108         address location_;\r
109         unsigned int errorCode_;\r
110 };\r
111 \r
112 class win32_access_violation : public win32_exception\r
113 {\r
114         mutable char messageBuffer_[256];\r
115 \r
116 public:\r
117         bool is_write() const { return isWrite_; }\r
118         address bad_address() const { return badAddress_;}\r
119         virtual const char* what() const;\r
120 \r
121 protected:\r
122         win32_access_violation(const EXCEPTION_RECORD& info);\r
123         friend void win32_exception::Handler(unsigned int errorCode, EXCEPTION_POINTERS* pInfo);\r
124 \r
125 private:\r
126         bool isWrite_;\r
127         address badAddress_;\r
128 };\r
129 \r
130 #define CASPAR_THROW_EXCEPTION(e) BOOST_THROW_EXCEPTION(e << call_stack_info(caspar::log::internal::get_call_stack()))\r
131 \r
132 }\r
133 \r
134 namespace std\r
135 {\r
136 \r
137 inline bool operator!=(const std::exception_ptr& lhs, const std::exception_ptr& rhs)\r
138 {\r
139         return !(lhs == rhs);\r
140 }\r
141 \r
142 inline bool operator!=(const std::exception_ptr& lhs, std::nullptr_t)\r
143 {\r
144         return !(lhs == nullptr);\r
145 }\r
146 \r
147 inline bool operator!=(std::nullptr_t, const std::exception_ptr& rhs)\r
148 {\r
149         return !(nullptr == rhs);\r
150 }\r
151 \r
152 }