My Project
 All Classes Namespaces Files Functions Typedefs Enumerations Enumerator Properties Macros
Exception.h
1 /*
2  * Copyright (C) 2013 Canonical Ltd
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 3 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Michi Henning <michi.henning@canonical.com>
17  */
18 
19 #ifndef UNITY_EXCEPTION_H
20 #define UNITY_EXCEPTION_H
21 
22 #include <unity/SymbolExport.h>
23 #include <unity/util/NonCopyable.h>
24 
25 #include <exception>
26 #include <string>
27 #include <memory>
28 
29 namespace unity
30 {
31 
32 class ExceptionImplBase;
33 
104 class UNITY_API Exception : public std::exception, public std::nested_exception
105 {
106 public:
108  Exception(Exception const&);
109  Exception& operator=(Exception const&);
110  virtual ~Exception() noexcept;
112 
120  virtual char const* what() const noexcept = 0;
121 
128  virtual std::exception_ptr self() const = 0;
129 
130  std::string reason() const;
131 
132  std::string to_string(std::string const& indent = " ") const;
133  std::string to_string(int indent_level, std::string const& indent) const;
134 
135  std::exception_ptr remember(std::exception_ptr earlier_exception);
136  std::exception_ptr get_earlier() const noexcept;
137 
138 protected:
140  Exception(std::shared_ptr<ExceptionImplBase> const& derived);
142  ExceptionImplBase* pimpl() const noexcept;
143 
144 private:
145  std::shared_ptr<ExceptionImplBase> p_; // shared_ptr instead of unique_ptr because exceptions must be copyable
146 };
147 
148 } // namespace unity
149 
150 #endif