crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
Exception.hpp
Go to the documentation of this file.
1 /*
2  *
3  * ---
4  *
5  * Copyright (C) 2020 Anselm Schmidt (ans[ät]ohai.su)
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version in addition to the terms of any
11  * licences already herein identified.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  *
21  * ---
22  *
23  * Exception.hpp
24  *
25  * Base class for custom exceptions.
26  *
27  * Created on: Feb 5, 2019
28  * Author: ans
29  */
30 
31 #ifndef MAIN_EXCEPTION_HPP_
32 #define MAIN_EXCEPTION_HPP_
33 
34 #include <stdexcept> // std::runtime_error
35 #include <string> // std::string
36 #include <string_view> // std::string_view
37 
38 /*
39  * MACROS FOR CLASS CREATION
40  */
41 
43 
49 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage)
50 #define MAIN_EXCEPTION_CLASS() class Exception : public Main::Exception { \
51  public: \
52  explicit Exception( \
53  const std::string& description \
54  ) : Main::Exception(description) {} \
55  }
56 
58 
64 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage, bugprone-macro-parentheses)
65 #define MAIN_EXCEPTION_SUBCLASS(NAME) class NAME : public Exception { \
66  public: \
67  explicit NAME( \
68  const std::string& description \
69  ) : Exception(description) {} \
70  }
71 
72 namespace crawlservpp::Main {
73 
74  /*
75  * DECLARATION
76  */
77 
79 
90  class Exception : public std::runtime_error {
91  public:
94 
95  explicit Exception(const std::string& description);
96 
98  ~Exception() override = default;
99 
103 
104  [[nodiscard]] std::string_view view() const noexcept;
105 
107 
110 
113  Exception(const Exception& other) = default;
114 
116  Exception& operator=(const Exception& other) = default;
117 
119  Exception(Exception&& other) = default;
120 
122  Exception& operator=(Exception&& other) = default;
123 
125 
126  private:
127  std::string_view stringView;
128  };
129 
130  /*
131  * IMPLEMENTATION
132  */
133 
135 
150  inline Exception::Exception(const std::string& description)
151  : std::runtime_error(description), stringView(std::runtime_error::what()) {}
152 
154 
158  inline std::string_view Exception::view() const noexcept {
159  return this->stringView;
160  }
161 
162 } /* namespace crawlservpp::Main */
163 
164 #endif /* MAIN_EXCEPTION_HPP_ */
Exception & operator=(const Exception &other)=default
Default copy assignment operator.
~Exception() override=default
Default destructor.
Exception(const std::string &description)
Constructor for creating a new exception.
Definition: Exception.hpp:150
std::string_view view() const noexcept
Gets the description of the exception as a view to the underlying string.
Definition: Exception.hpp:158
Base class for all exceptions thrown by the application.
Definition: Exception.hpp:90
Namespace for the main classes of the program.
Definition: App.cpp:34