]> git.sesse.net Git - casparcg/blob - common/except.h
[general] Changed permissions of files that should be marked as executable but wasn't.
[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 "os/stack_trace.h"
27 #include "log.h"
28
29 #include <exception>
30 #include <list>
31
32 #include <boost/exception/all.hpp>
33 #include <boost/exception/error_info.hpp>
34 #include <boost/throw_exception.hpp>
35 #include <boost/noncopyable.hpp>
36
37 namespace caspar {
38
39 typedef boost::error_info<struct tag_arg_name_info,             std::string>    arg_name_info_t;
40 typedef boost::error_info<struct tag_arg_value_info,    std::string>    arg_value_info_t;
41 typedef boost::error_info<struct tag_msg_info,                  std::string>    msg_info_t;
42 typedef boost::error_info<struct tag_call_stack_info,   std::string>    call_stack_info_t;
43 typedef boost::error_info<struct tag_error_info,                std::string>    error_info_t;
44 typedef boost::error_info<struct tag_source_info,               std::string>    source_info_t;
45 typedef boost::error_info<struct tag_file_name_info,    std::string>    file_name_info_t;
46 typedef boost::error_info<struct tag_context_info,              std::string>    context_info_t;
47
48 template<typename T>
49 inline arg_name_info_t          arg_name_info(const T& str)             {return arg_name_info_t(u8(str));}
50 template<typename T>
51 inline arg_value_info_t         arg_value_info(const T& str)    {return arg_value_info_t(u8(str));}
52 template<typename T>
53 inline msg_info_t                       msg_info(const T& str)                  {return msg_info_t(u8(str));}
54 template<typename T>
55 inline call_stack_info_t        call_stack_info(const T& str)   {return call_stack_info_t(u8(str));}
56 template<typename T>
57 inline error_info_t                     error_info(const T& str)                {return error_info_t(u8(str));}
58 template<typename T>
59 inline source_info_t            source_info(const T& str)               {return source_info_t(u8(str));}
60 template<typename T>
61 inline file_name_info_t         file_name_info(const T& str)    {return file_name_info_t(u8(str));}
62 template<typename T>
63 inline context_info_t           context_info(const T& str)              {return context_info_t(u8(str));}
64
65 typedef boost::error_info<struct tag_line_info, size_t>                                         line_info;
66 typedef boost::error_info<struct tag_nested_exception_, std::exception_ptr> nested_exception;
67
68 struct caspar_exception                 : virtual boost::exception, virtual std::exception
69 {
70         caspar_exception(){}
71         const char* what() const throw() override
72         {
73                 return boost::diagnostic_information_what(*this);
74         }
75 };
76
77 struct io_error                                 : virtual caspar_exception {};
78 struct directory_not_found              : virtual io_error {};
79 struct file_not_found                   : virtual io_error {};
80 struct file_read_error          : virtual io_error {};
81 struct file_write_error         : virtual io_error {};
82
83 struct invalid_argument                 : virtual caspar_exception {};
84 struct null_argument                    : virtual invalid_argument {};
85 struct out_of_range                             : virtual invalid_argument {};
86 struct programming_error                : virtual caspar_exception {};
87 struct bad_alloc                                : virtual caspar_exception {};
88
89 struct invalid_operation                : virtual caspar_exception {};
90 struct operation_failed                 : virtual caspar_exception {};
91 struct timed_out                                : virtual caspar_exception {};
92
93 struct not_implemented                  : virtual caspar_exception {};
94
95 struct user_error                               : virtual caspar_exception {};
96 struct expected_user_error              : virtual user_error {};
97 struct not_supported                    : virtual user_error {};
98
99 std::string get_context();
100
101 class scoped_context : boost::noncopyable
102 {
103 public:
104         scoped_context();
105         scoped_context(std::string msg);
106         template <typename Str>
107         scoped_context(Str msg)
108                 : scoped_context(u8(std::move(msg)))
109         {
110         }
111
112         ~scoped_context();
113         void replace_msg(std::string msg);
114         template <typename Str>
115         void replace_msg(std::string msg)
116         {
117                 replace_msg(u8(std::move(msg)));
118         }
119         void clear_msg();
120 private:
121         std::list<std::string>& for_thread_;
122         std::string*                    msg_;
123 };
124
125 #define _CASPAR_GENERATE_UNIQUE_IDENTIFIER_CAT(name, line) name##line
126 #define _CASPAR_GENERATE_UNIQUE_IDENTIFIER(name, line) _CASPAR_GENERATE_UNIQUE_IDENTIFIER_CAT(name, line)
127 #define CASPAR_SCOPED_CONTEXT_MSG(ctx_msg) ::caspar::scoped_context _CASPAR_GENERATE_UNIQUE_IDENTIFIER(SCOPED_CONTEXT, __LINE__)(u8(ctx_msg));
128
129 #define CASPAR_THROW_EXCEPTION(e) ::boost::exception_detail::throw_exception_((e << call_stack_info(caspar::get_call_stack()) << context_info(get_context())), BOOST_THROW_EXCEPTION_CURRENT_FUNCTION, ::caspar::log::remove_source_prefix(__FILE__), __LINE__)
130
131 std::string get_message_and_context(const caspar_exception& e);
132
133 }