Libthreadar  1.0.1
 All Classes Namespaces Files Functions Macros
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-2015 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
41 extern "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 
56 namespace libthreadar
57 {
58 
60 
80  {
81  public:
83 
85  exception_base(const std::string & x_msg) { msg_table.push_back(x_msg); };
86 
88  virtual ~exception_base() {};
89 
91  void push_message(const std::string & x_msg) { msg_table.push_back(x_msg); };
92 
94 
96  unsigned int size() const { return msg_table.size(); };
97 
99 
102  const std::string & operator [](unsigned int i) const { return msg_table[i]; };
103 
105 
108  std::string get_message(const std::string & sep) const;
109 
111 
114  virtual exception_base *clone() const = 0;
115 
116  protected:
118  void reset_first_message(const std::string & msg) { msg_table[0] = msg; };
119 
120  private:
121  std::vector<std::string> msg_table;
122  };
123 
125 
126  template<class T> exception_base *cloner(void * const ptr);
127 
129 
132  {
133  public:
134  exception_memory() : exception_base("lack of memory") {};
135 
136  protected:
137  virtual exception_base *clone() const { return cloner<exception_memory>((void *)this); };
138  };
139 
140  template<class T> exception_base *cloner(void * const ptr) { exception_base *ret = new (std::nothrow) T(*(reinterpret_cast<T const *>(ptr))); if(ret == NULL) throw exception_memory(); return ret; };
141 
142 
144 
145 #define THREADAR_BUG exception_bug(__FILE__, __LINE__)
146 
148 
151  {
152  public:
153  exception_bug(const std::string & file, int line) : exception_base("LIBTHREADAR BUG MET IN File " + file + " line " + std::to_string(line)) {};
154 
155  protected:
156  virtual exception_base *clone() const { return cloner<exception_bug>((void *)this); };
157  };
158 
159 
161 
164  {
165  public:
166  exception_thread(const std::string & x_msg) : exception_base(x_msg) {};
167 
168  protected:
169  virtual exception_base *clone() const { return cloner<exception_thread>((void *)this); };
170  };
171 
173 
176  {
177  public:
178  exception_system(const std::string & context, int error_code);
179 
180  protected:
181  virtual exception_base *clone() const { return cloner<exception_system>((void *)this); };
182  };
183 
185 
188  {
189  public:
190  exception_range(const std::string & msg): exception_base(msg) {};
191 
192  protected:
193  virtual exception_base *clone() const { return cloner<exception_range>((void *)this); };
194  };
195 
196 
198 
201  {
202  public:
203  exception_feature(const std::string & feature_name): exception_base(std::string("Unimplemented feature: ") + feature_name) {};
204 
205  protected:
206  virtual exception_base *clone() const { return cloner<exception_feature>((void *)this); };
207  };
208 
209 } // end of namespace
210 
211 #endif
exception_base * cloner(void *const ptr)
Template used by libthreadar to implement the clone() method for libthreadar exceptions.
Definition: exceptions.hpp:140
Exception used to report webdar internal bugs.
Definition: exceptions.hpp:150
unsigned int size() const
for site which need to display the information to the user
Definition: exceptions.hpp:96
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:156
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:169
Exception used to report error met when manipulating threads.
Definition: exceptions.hpp:163
Exception used to report an non-implemented feature.
Definition: exceptions.hpp:200
Exception used to report memory allocation failures.
Definition: exceptions.hpp:131
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:137
exception_base(const std::string &x_msg)
constructor
Definition: exceptions.hpp:85
virtual exception_base * clone() const =0
create a new object of the same type and value of the object which clone() method is invoked ...
Exception used to report operating system errors.
Definition: exceptions.hpp:175
const std::string & operator[](unsigned int i) const
for site which need to display the information to the user
Definition: exceptions.hpp:102
Pure virtual class parent of all webdar exceptions.
Definition: exceptions.hpp:79
virtual ~exception_base()
destructor
Definition: exceptions.hpp:88
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:91
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:206
std::string get_message(const std::string &sep) const
concatenated messages and use the given separator between messages
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:181
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:193
This is the only namespace used in libthreadar and all symbols provided by libthreadar are member of ...
Definition: barrier.hpp:46
void reset_first_message(const std::string &msg)
for libthreader internal use only
Definition: exceptions.hpp:118
Exception used to report out or range value or argument.
Definition: exceptions.hpp:187