1 /*
2  Copyright (C) 2010-2014 Kristian Duske
3 
4  This file is part of TrenchBroom.
5 
6  TrenchBroom 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  TrenchBroom 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 TrenchBroom. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef TrenchBroom_Exceptions_h
21 #define TrenchBroom_Exceptions_h
22 
23 #include <exception>
24 
25 #include "StringUtils.h"
26 
27 namespace TrenchBroom {
28 
29     class Exception : public std::exception {
30     protected:
31         String m_msg;
32     public:
Exception()33         Exception() throw() {}
throw()34         Exception(const String& str) throw() : m_msg(str) {}
~Exception()35         virtual ~Exception() throw() {}
36 
what()37         const char* what() const throw() {
38             return m_msg.c_str();
39         }
40     };
41 
42     template <class C>
43     class ExceptionStream : public Exception {
44     public:
ExceptionStream()45         ExceptionStream() throw() {}
throw()46         ExceptionStream(const String& str) throw() : Exception(str) {}
~ExceptionStream()47         virtual ~ExceptionStream() throw() {}
48 
49         template <typename T>
50         C& operator<< (T value) {
51             StringStream stream;
52             stream << m_msg << value;
53             m_msg = stream.str();
54             return static_cast<C&>(*this);
55         }
56     };
57 
58     class GeometryException : public ExceptionStream<GeometryException> {
59     public:
GeometryException()60         GeometryException() throw() {}
throw()61         GeometryException(const String& str) throw() : ExceptionStream(str) {}
throw()62         ~GeometryException() throw() {}
63     };
64 
65     class EntityAttributeException : public ExceptionStream<EntityAttributeException> {
66     public:
EntityAttributeException()67         EntityAttributeException() throw() {}
throw()68         EntityAttributeException(const String& str) throw() : ExceptionStream(str) {}
throw()69         ~EntityAttributeException() throw() {}
70     };
71 
72     class ParserException : public ExceptionStream<ParserException> {
73     public:
ParserException()74         ParserException() throw() {}
throw()75         ParserException(const String& str) throw() : ExceptionStream(str) {}
throw()76         ParserException(const size_t line, const size_t column, const String& str = "") throw() : ExceptionStream() {
77             *this << str << " (line " << line << ", column " << column << ")";
78         }
throw()79         ~ParserException() throw() {}
80     };
81 
82     class VboException : public ExceptionStream<VboException> {
83     public:
VboException()84         VboException() throw() {}
throw()85         VboException(const String& str) throw() : ExceptionStream(str) {}
throw()86         ~VboException() throw() {}
87     };
88 
89     class PathException : public ExceptionStream<PathException> {
90     public:
PathException()91         PathException() throw() {}
throw()92         PathException(const String& str) throw() : ExceptionStream(str) {}
throw()93         ~PathException() throw() {}
94     };
95 
96     class FileSystemException : public ExceptionStream<FileSystemException> {
97     public:
FileSystemException()98         FileSystemException() throw() {}
throw()99         FileSystemException(const String& str) throw() : ExceptionStream(str) {}
FileSystemException(const String & str,const PathException & e)100         FileSystemException(const String& str, const PathException& e) throw() : ExceptionStream(str + " (" + e.what() + ")") {}
throw()101         ~FileSystemException() throw() {}
102     };
103 
104     class FileNotFoundException : public ExceptionStream<FileNotFoundException> {
105     public:
FileNotFoundException()106         FileNotFoundException() throw() {}
throw()107         FileNotFoundException(const String& str) throw() : ExceptionStream(str) {}
FileNotFoundException(const String & str,const PathException & e)108         FileNotFoundException(const String& str, const PathException& e) throw() : ExceptionStream(str + " (" + e.what() + ")") {}
throw()109         ~FileNotFoundException() throw() {}
110     };
111 
112     class AssetException : public ExceptionStream<AssetException> {
113     public:
AssetException()114         AssetException() throw() {}
throw()115         AssetException(const String& str) throw() : ExceptionStream(str) {}
throw()116         ~AssetException() throw() {}
117     };
118 
119     class CommandProcessorException : public ExceptionStream<CommandProcessorException> {
120     public:
CommandProcessorException()121         CommandProcessorException() throw() {}
throw()122         CommandProcessorException(const String& str) throw() : ExceptionStream(str) {}
throw()123         ~CommandProcessorException() throw() {}
124     };
125 
126     class RenderException : public ExceptionStream<RenderException> {
127     public:
RenderException()128         RenderException() throw() {}
throw()129         RenderException(const String& str) throw() : ExceptionStream(str) {}
throw()130         ~RenderException() throw() {}
131     };
132 
133     class OctreeException : public ExceptionStream<OctreeException> {
134     public:
OctreeException()135         OctreeException() throw() {}
throw()136         OctreeException(const String& str) throw() : ExceptionStream(str) {}
throw()137         ~OctreeException() throw() {}
138     };
139 
140     class GameException : public ExceptionStream<GameException> {
141     public:
GameException()142         GameException() throw() {}
throw()143         GameException(const String& str) throw() : ExceptionStream(str) {}
throw()144         ~GameException() throw() {}
145     };
146 
147     class ResourceNotFoundException : public ExceptionStream<ResourceNotFoundException> {
148     public:
ResourceNotFoundException()149         ResourceNotFoundException() throw() {}
throw()150         ResourceNotFoundException(const String& str) throw() : ExceptionStream(str) {}
throw()151         ~ResourceNotFoundException() throw() {}
152     };
153 
154     class FileFormatException : public ExceptionStream<FileFormatException> {
155     public:
FileFormatException()156         FileFormatException() throw() {}
throw()157         FileFormatException(const String& str) throw() : ExceptionStream(str) {}
throw()158         ~FileFormatException() throw() {}
159     };
160 }
161 
162 #endif
163