Beatmup
exception.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 
21 #include <exception>
22 #include <string>
23 #include <cstdio>
24 
25 
26 #if defined(_MSC_VER)
27 #define NOEXCEPT
28 #else
29 #define NOEXCEPT noexcept
30 #endif
31 
32 namespace Beatmup {
33 
34  /**
35  Base class for all exceptions
36  */
37  class Exception : public std::exception {
38  private:
39  std::string message;
40  protected:
41  inline Exception(const char* message) : message(message) {}
42 
43  template<typename ...Args>
44  inline Exception(const char* message, const Args&... args) {
45  static const int MAX_LENGTH = 4*1024;
46  char out[MAX_LENGTH];
47 #if _MSC_VER
48  sprintf_s(out, MAX_LENGTH, message, args...);
49 #else
50  snprintf(out, MAX_LENGTH, message, args...);
51 #endif
52  this->message.assign(out);
53  }
54 
55  public:
56  virtual inline const char* what() const NOEXCEPT override {
57  return message.c_str();
58  }
59  };
60 
61  class RuntimeError : public Exception {
62  public:
63  inline RuntimeError(const std::string& message): Exception(message.c_str()) {}
64  inline static void check(const bool condition, const std::string& message) {
65  if (!condition)
66  throw RuntimeError(message);
67  }
68  };
69 
70  class InvalidArgument : public Exception {
71  protected:
72  template<typename datatype> InvalidArgument(const char* message, const datatype value): Exception(message, value) {}
73  public:
74  inline InvalidArgument(const std::string& message): Exception(message.c_str()) {}
75  inline static void check(const bool condition, const std::string& message) {
76  if (!condition)
77  throw InvalidArgument(message);
78  }
79  };
80 
81  class OutOfRange : public InvalidArgument {
82  private:
83  template<typename datatype> OutOfRange(const char* message, const datatype value): InvalidArgument(message, value) {}
84  public:
85  template<typename datatype>
86  inline static void check(const datatype value, const datatype min, const datatype max, const char* message) {
87  if (value < min || max < value)
88  throw OutOfRange(message, value);
89  }
90 
91  template<typename datatype>
92  inline static void checkMin(const datatype value, const datatype min, const char* message) {
93  if (value < min)
94  throw OutOfRange(message, value);
95  }
96  };
97 
98  class IOError : public Exception {
99  private:
100  std::string filename;
101  public:
102  inline IOError(const std::string& filename, const char * message):
103  Exception("Cannot access %s:\n%s", filename.c_str(), message),
104  filename(filename) { }
105  const std::string& getFilename() const { return filename; }
106  };
107 
108  /**
109  %Exception thrown when a required input of an AbstractTask was not assigned prior to the task execution.
110  */
111  class NullTaskInput : public Exception {
112  public:
113  inline NullTaskInput(const char* which) : Exception("Task input is not set: %s", which) {}
114 
115  inline static void check(const void* pointer, const char* which) {
116  if (!pointer)
117  throw NullTaskInput(which);
118  }
119  };
120 
121  /**
122  %Exception thrown when an implementation restriction is encountered.
123  */
125  public:
126  inline ImplementationUnsupported(const char* description) : Exception(description) {}
127  };
128 
129  /**
130  %Exception thrown when something happens that should never do.
131  */
132  class Insanity : public Exception {
133  private:
134  inline Insanity(const char* message) : Exception(message) {}
135  public:
136  static inline void insanity(const char* message) {
137  throw Beatmup::Insanity(message);
138  }
139  };
140 
141 #ifdef BEATMUP_DEBUG
142  /**
143  Thrown when a debugging check does not pass
144  */
145  class DebugAssertion : public Exception {
146  private:
147  template<typename ...Args>
148  inline DebugAssertion(const std::string& message, const Args&... args) : Exception(message.c_str(), args...) {}
149  public:
150  template<typename ...Args>
151  static inline void check(const bool condition, const std::string& message, const Args&... args) {
152  if (!condition)
153  throw DebugAssertion(message, args...);
154  }
155  };
156 #endif
157 }
158 
159 
160 #ifdef BEATMUP_DEBUG
161 #define BEATMUP_ASSERT_DEBUG(C) Beatmup::DebugAssertion::check((C), "Debug assertion failed.\n" #C);
162 #else
163 #define BEATMUP_ASSERT_DEBUG(C)
164 #endif
Base class for all exceptions.
Definition: exception.h:37
Exception(const char *message)
Definition: exception.h:41
virtual const char * what() const NOEXCEPT override
Definition: exception.h:56
std::string message
Definition: exception.h:39
Exception(const char *message, const Args &... args)
Definition: exception.h:44
std::string filename
Definition: exception.h:100
IOError(const std::string &filename, const char *message)
Definition: exception.h:102
const std::string & getFilename() const
Definition: exception.h:105
Exception thrown when an implementation restriction is encountered.
Definition: exception.h:124
ImplementationUnsupported(const char *description)
Definition: exception.h:126
Exception thrown when something happens that should never do.
Definition: exception.h:132
static void insanity(const char *message)
Definition: exception.h:136
Insanity(const char *message)
Definition: exception.h:134
static void check(const bool condition, const std::string &message)
Definition: exception.h:75
InvalidArgument(const char *message, const datatype value)
Definition: exception.h:72
InvalidArgument(const std::string &message)
Definition: exception.h:74
Exception thrown when a required input of an AbstractTask was not assigned prior to the task executio...
Definition: exception.h:111
NullTaskInput(const char *which)
Definition: exception.h:113
static void check(const void *pointer, const char *which)
Definition: exception.h:115
static void checkMin(const datatype value, const datatype min, const char *message)
Definition: exception.h:92
OutOfRange(const char *message, const datatype value)
Definition: exception.h:83
static void check(const datatype value, const datatype min, const datatype max, const char *message)
Definition: exception.h:86
RuntimeError(const std::string &message)
Definition: exception.h:63
static void check(const bool condition, const std::string &message)
Definition: exception.h:64
#define NOEXCEPT
Definition: exception.h:29
CustomPoint< numeric > min(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:724
CustomPoint< numeric > max(const CustomPoint< numeric > &a, const CustomPoint< numeric > &b)
Definition: geometry.h:728
JNIEnv jlong jint out