Libthreadar 1.6.0
exceptions.hpp
Go to the documentation of this file.
1/*********************************************************************/
2// libthreadar - is a library providing several C++ classes to work with threads
3// Copyright (C) 2014-2025 Denis Corbin
4//
5// This file is part of libthreadar
6//
7// libthreadar is free software: you can redistribute it and/or modify
8// it under the terms of the GNU Lesser 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.
11//
12// libhtreadar is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU Lesser General Public License for more details.
16//
17// You should have received a copy of the GNU Lesser General Public License
18// along with libthreadar. If not, see <http://www.gnu.org/licenses/>
19//
20//----
21// to contact the author: dar.linux@free.fr
22/*********************************************************************/
23
24#ifndef LIBTHREADAR_EXCEPTIONS_HPP
25#define LIBTHREADAR_EXCEPTIONS_HPP
26
38#include "config.h"
39
40 // C system headers
41extern "C"
42{
43#if HAVE_STRING_H
44#include <string.h>
45#endif
46}
47 // C++ standard headers
48#include <string>
49#include <vector>
50#include <new>
51#include <iostream>
52#include <sstream>
53
54 // libthreadar headers
55
56namespace libthreadar
57{
58
60
85
86
88 {
89 public:
91
93 exception_base(const std::string & x_msg) { msg_table.push_back(x_msg); };
94
96 exception_base(const exception_base & ref) = default;
97
99 exception_base(exception_base && ref) noexcept = default;
100
102 exception_base & operator = (const exception_base & ref) = default;
103
105 exception_base & operator = (exception_base && ref) noexcept = default;
106
108 virtual ~exception_base() = default;
109
111 void push_message(const std::string & x_msg) { msg_table.push_back(x_msg); };
112
114
116 unsigned int size() const { return msg_table.size(); };
117
119
122 const std::string & operator [](unsigned int i) const { return msg_table[i]; };
123
125
128 std::string get_message(const std::string & sep) const;
129
131
134 virtual exception_base *clone() const = 0;
135
136 protected:
138 void reset_first_message(const std::string & msg) { msg_table[0] = msg; };
139
140 private:
141 std::vector<std::string> msg_table;
142 };
143
145
146 template<class T> exception_base *cloner(void * const ptr);
147
149
152 {
153 public:
154 exception_memory() : exception_base("lack of memory") {};
155
156 protected:
157 virtual exception_base *clone() const { return cloner<exception_memory>((void *)this); };
158 };
159
160 template<class T> exception_base *cloner(void * const ptr) { exception_base *ret = new (std::nothrow) T(*(reinterpret_cast<T const *>(ptr))); if(ret == nullptr) throw exception_memory(); return ret; }
161
163
164#define THREADAR_BUG exception_bug(__FILE__, __LINE__)
165
167
170 {
171 public:
172 exception_bug(const std::string & file, int line) : exception_base("LIBTHREADAR BUG MET IN File " + file + " line " + std::to_string(line)) {};
173
174 protected:
175 virtual exception_base *clone() const { return cloner<exception_bug>((void *)this); };
176 };
177
178
180
183 {
184 public:
185 exception_thread(const std::string & x_msg) : exception_base(x_msg) {};
186
187 protected:
188 virtual exception_base *clone() const { return cloner<exception_thread>((void *)this); };
189 };
190
192
195 {
196 public:
197 exception_system(const std::string & context, int error_code);
198
199 protected:
200 virtual exception_base *clone() const { return cloner<exception_system>((void *)this); };
201 };
202
204
207 {
208 public:
209 exception_range(const std::string & msg): exception_base(msg) {};
210
211 protected:
212 virtual exception_base *clone() const { return cloner<exception_range>((void *)this); };
213 };
214
215
217
220 {
221 public:
222 exception_feature(const std::string & feature_name): exception_base(std::string("Unimplemented feature: ") + feature_name) {};
223
224 protected:
225 virtual exception_base *clone() const { return cloner<exception_feature>((void *)this); };
226 };
227
231
232} // end of namespace
233
234#endif
Pure virtual class parent of all libthreadar exceptions.
Definition: exceptions.hpp:88
virtual exception_base * clone() const =0
create a new object of the same type and value of the object which clone() method is invoked
void reset_first_message(const std::string &msg)
for libthreader internal use only
Definition: exceptions.hpp:138
exception_base(exception_base &&ref) noexcept=default
default move constructor is fine
std::string get_message(const std::string &sep) const
concatenated messages and use the given separator between messages
virtual ~exception_base()=default
destructor
unsigned int size() const
for site which need to display the information to the user
Definition: exceptions.hpp:116
exception_base(const exception_base &ref)=default
default copy constructor is fine
exception_base(const std::string &x_msg)
constructor
Definition: exceptions.hpp:93
const std::string & operator[](unsigned int i) const
for site which need to display the information to the user
Definition: exceptions.hpp:122
void push_message(const std::string &x_msg)
to be used in a catch clause to more detailed information of the problem cause, before rethrowing the...
Definition: exceptions.hpp:111
exception_base & operator=(const exception_base &ref)=default
default assignment operator is fine
Exception used to report webdar internal bugs.
Definition: exceptions.hpp:170
virtual exception_base * clone() const
create a new object of the same type and value of the object which clone() method is invoked
Definition: exceptions.hpp:175
Exception used to report an non-implemented feature.
Definition: exceptions.hpp:220
virtual exception_base * clone() const
create a new object of the same type and value of the object which clone() method is invoked
Definition: exceptions.hpp:225
Exception used to report memory allocation failures.
Definition: exceptions.hpp:152
virtual exception_base * clone() const
create a new object of the same type and value of the object which clone() method is invoked
Definition: exceptions.hpp:157
Exception used to report out or range value or argument.
Definition: exceptions.hpp:207
virtual exception_base * clone() const
create a new object of the same type and value of the object which clone() method is invoked
Definition: exceptions.hpp:212
Exception used to report operating system errors.
Definition: exceptions.hpp:195
virtual exception_base * clone() const
create a new object of the same type and value of the object which clone() method is invoked
Definition: exceptions.hpp:200
Exception used to report error met when manipulating threads.
Definition: exceptions.hpp:183
virtual exception_base * clone() const
create a new object of the same type and value of the object which clone() method is invoked
Definition: exceptions.hpp:188
This is the only namespace used in libthreadar and all symbols provided by libthreadar are member of ...
Definition: barrier.hpp:46
exception_base * cloner(void *const ptr)
Template used by libthreadar to implement the clone() method for libthreadar exceptions.
Definition: exceptions.hpp:160