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