Loading...
Searching...
No Matches
exceptions.hpp
Go to the documentation of this file.
/*********************************************************************/
// libthreadar - is a library providing several C++ classes to work with threads
// Copyright (C) 2014-2024 Denis Corbin
//
// This file is part of libthreadar
//
// libthreadar is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// libhtreadar is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with libthreadar. If not, see <http://www.gnu.org/licenses/>
//
//----
// to contact the author: dar.linux@free.fr
/*********************************************************************/
#ifndef LIBTHREADAR_EXCEPTIONS_HPP
#define LIBTHREADAR_EXCEPTIONS_HPP
#include "config.h"
// C system headers
extern "C"
{
#if HAVE_STRING_H
#include <string.h>
#endif
}
// C++ standard headers
#include <string>
#include <vector>
#include <new>
#include <iostream>
#include <sstream>
// libthreadar headers
namespace libthreadar
{
class exception_base
{
public:
exception_base(const std::string & x_msg) { msg_table.push_back(x_msg); };
exception_base(const exception_base & ref) = default;
exception_base(exception_base && ref) noexcept = default;
exception_base & operator = (const exception_base & ref) = default;
exception_base & operator = (exception_base && ref) noexcept = default;
virtual ~exception_base() = default;
void push_message(const std::string & x_msg) { msg_table.push_back(x_msg); };
unsigned int size() const { return msg_table.size(); };
const std::string & operator [](unsigned int i) const { return msg_table[i]; };
std::string get_message(const std::string & sep) const;
virtual exception_base *clone() const = 0;
protected:
void reset_first_message(const std::string & msg) { msg_table[0] = msg; };
private:
std::vector<std::string> msg_table;
};
template<class T> exception_base *cloner(void * const ptr);
class exception_memory : public exception_base
{
public:
exception_memory() : exception_base("lack of memory") {};
protected:
virtual exception_base *clone() const { return cloner<exception_memory>((void *)this); };
};
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; }
#define THREADAR_BUG exception_bug(__FILE__, __LINE__)
class exception_bug : public exception_base
{
public:
exception_bug(const std::string & file, int line) : exception_base("LIBTHREADAR BUG MET IN File " + file + " line " + std::to_string(line)) {};
protected:
virtual exception_base *clone() const { return cloner<exception_bug>((void *)this); };
};
class exception_thread : public exception_base
{
public:
exception_thread(const std::string & x_msg) : exception_base(x_msg) {};
protected:
virtual exception_base *clone() const { return cloner<exception_thread>((void *)this); };
};
class exception_system : public exception_base
{
public:
exception_system(const std::string & context, int error_code);
protected:
virtual exception_base *clone() const { return cloner<exception_system>((void *)this); };
};
class exception_range : public exception_base
{
public:
exception_range(const std::string & msg): exception_base(msg) {};
protected:
virtual exception_base *clone() const { return cloner<exception_range>((void *)this); };
};
class exception_feature : public exception_base
{
public:
exception_feature(const std::string & feature_name): exception_base(std::string("Unimplemented feature: ") + feature_name) {};
protected:
virtual exception_base *clone() const { return cloner<exception_feature>((void *)this); };
};
} // end of namespace
#endif
libthreadar::exception_base
Pure virtual class parent of all libthreadar exceptions.
Definition exceptions.hpp:88
libthreadar::exception_base::clone
virtual exception_base * clone() const =0
create a new object of the same type and value of the object which clone() method is invoked
libthreadar::exception_base::reset_first_message
void reset_first_message(const std::string &msg)
for libthreader internal use only
Definition exceptions.hpp:138
libthreadar::exception_base::exception_base
exception_base(exception_base &&ref) noexcept=default
default move constructor is fine
libthreadar::exception_base::get_message
std::string get_message(const std::string &sep) const
concatenated messages and use the given separator between messages
libthreadar::exception_base::~exception_base
virtual ~exception_base()=default
destructor
libthreadar::exception_base::size
unsigned int size() const
for site which need to display the information to the user
Definition exceptions.hpp:116
libthreadar::exception_base::exception_base
exception_base(const exception_base &ref)=default
default copy constructor is fine
libthreadar::exception_base::exception_base
exception_base(const std::string &x_msg)
constructor
Definition exceptions.hpp:93
libthreadar::exception_base::operator[]
const std::string & operator[](unsigned int i) const
for site which need to display the information to the user
Definition exceptions.hpp:122
libthreadar::exception_base::push_message
void push_message(const std::string &x_msg)
to be used in a catch clause to add context information before rethrowing the exception
Definition exceptions.hpp:111
libthreadar::exception_base::operator=
exception_base & operator=(const exception_base &ref)=default
default assignment operator is fine
libthreadar::exception_bug
Exception used to report webdar internal bugs.
Definition exceptions.hpp:170
libthreadar::exception_bug::clone
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
libthreadar::exception_feature
Exception used to report an non-implemented feature.
Definition exceptions.hpp:220
libthreadar::exception_feature::clone
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
libthreadar::exception_memory
Exception used to report memory allocation failures.
Definition exceptions.hpp:152
libthreadar::exception_memory::clone
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
libthreadar::exception_range
Exception used to report out or range value or argument.
Definition exceptions.hpp:207
libthreadar::exception_range::clone
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
libthreadar::exception_system
Exception used to report operating system errors.
Definition exceptions.hpp:195
libthreadar::exception_system::clone
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
libthreadar::exception_thread
Exception used to report error met when manipulating threads.
Definition exceptions.hpp:183
libthreadar::exception_thread::clone
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
libthreadar
This is the only namespace used in libthreadar and all symbols provided by libthreadar are member of ...
Definition barrier.hpp:46
libthreadar::cloner
exception_base * cloner(void *const ptr)
Template used by libthreadar to implement the clone() method for libthreadar exceptions.
Definition exceptions.hpp:160