libpnicore
exceptions.hpp
1 //
2 // Declaration exception classes and related macros
3 //
4 // (c) Copyright 2011 DESY, Eugen Wintersberger <eugen.wintersberger@desy.de>
5 //
6 // This file is part of libpnicore.
7 //
8 // libpnicore is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation, either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // libpnicore 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 libpnicore. If not, see <http://www.gnu.org/licenses/>.
20 //
21 // ===========================================================================
22 //
23 // Created on: Apr 19, 2011
24 // Author: Eugen Wintersberger
25 //
26 //
27 
28 #pragma once
29 
30 #include<iostream>
31 #include<string>
32 #include<exception>
33 #include<typeinfo>
34 #include<list>
35 #include<boost/current_function.hpp>
36 
37 #include "../types/types.hpp"
38 
39 namespace pni{
40 namespace core{
41 
42 //================Macros related to exceptions==================================
43 
48 #define EXCEPTION_RECORD\
49  exception_record(__FILE__,__LINE__,BOOST_CURRENT_FUNCTION)
50 
59 
65 #define EXCEPTION_FORWARD(ETYPE)\
68  catch(ETYPE &error)\
69  {\
70  error.append(EXCEPTION_RECORD);\
71  throw error;\
72  }
73 
74  //========================Exception classes==================================
84 
89  class exception_record
91  {
92  private:
93  string _file;
94  size_t _line;
95  string _function;
96  public:
97  //===================constructors and destructor===================
99  exception_record() = delete;
100 
102  exception_record(const string &file,size_t line,const string &func):
103  _file(file),
104  _line(line),
105  _function(func)
106  {}
107 
108  //==================public member functions========================
110  const string &file() const { return _file; }
112  size_t line() const { return _line; }
114  const string &function() const { return _function; }
115  };
116 
117  //------------------------------------------------------------------------
122  std::ostream &operator<<(std::ostream &o,const exception_record &rec);
123 
124  //-------------------------------------------------------------------------
131  class exception:public std::exception
132  {
133  private:
135  string _name;
137  string _description;
139  std::list<exception_record> _records;
140  protected:
149  std::ostream &print(std::ostream &o) const;
150  public:
151  //===================public types==================================
153  typedef std::list<exception_record>::const_iterator const_iterator;
154  //===================constructors and destructor===================
156  exception();
157 
158  //-----------------------------------------------------------------
164  exception(const string &n);
165 
166  //-----------------------------------------------------------------
174  exception(const string &n, const exception_record &rec);
175 
176  //-----------------------------------------------------------------
185  exception(const string &n, const exception_record &rec,
186  const string &d);
187 
188  //-----------------------------------------------------------------
190  virtual ~exception() throw() { }
191 
192  //===================public member functions=======================
197  void name(const std::string &name) { _name = name; }
198 
199  //-----------------------------------------------------------------
205  const string &name() const { return _name; }
206 
207  //-----------------------------------------------------------------
213 
223  void append(const exception_record &n) { _records.push_back(n); }
229 
230  //-----------------------------------------------------------------
236  void description(const std::string &desc) { _description = desc; }
237 
238  //-----------------------------------------------------------------
245  const string &description() const { return _description; }
246 
247  //-----------------------------------------------------------------
256  const_iterator begin() const { return _records.begin(); }
257 
258  //-----------------------------------------------------------------
267  const_iterator end() const { return _records.end(); }
268 
269  //-----------------------------------------------------------------
271  friend std::ostream &operator<<(std::ostream &, const exception &);
272 
273  };
274 
275 
276  //--------------------------------------------------------------------------
285  {
286  public:
288  memory_allocation_error() : exception("memory_allocation_error")
289  { }
290 
291  //-----------------------------------------------------------------
299  const string &d):
300  exception("memory_allocation_eror", i, d)
301  { }
302 
303  //-----------------------------------------------------------------
306 
307  //-----------------------------------------------------------------
309  friend std::ostream &
310  operator<<(std::ostream &o,const memory_allocation_error &e);
311  };
312 
313  //-------------------------------------------------------------------------
322  {
323  public:
326  exception("memory_not_allocated_error")
327  {}
328 
329  //-----------------------------------------------------------------
338  const string &d):
339  exception("memory_not_allocated_error",i,d)
340  {}
341 
342  //----------------------------------------------------------------
345 
346  //----------------------------------------------------------------
348  friend std::ostream &operator<<(std::ostream &o,const
350  };
351 
352 
353  //-------------------------------------------------------------------------
361  {
362  public:
365  exception("shape_mismatch_error")
366  { }
367 
368  //----------------------------------------------------------------
377  const string &d) :
378  exception("shape_mismatch_error",i,d)
379  { }
380 
381  //----------------------------------------------------------------
383  ~shape_mismatch_error() throw() { }
384 
385  //----------------------------------------------------------------
387  friend std::ostream &
388  operator<<(std::ostream &o,const shape_mismatch_error &e);
389  };
390 
391  //--------------------------------------------------------------------------
400  {
401  public:
403  size_mismatch_error() : exception("size_mismatch_error")
404  { }
405 
406  //-----------------------------------------------------------------
415  const string &d) :
416  exception("size_mismatch_error", i,d)
417  { }
418 
419  //-----------------------------------------------------------------
421  ~size_mismatch_error() throw() { }
422 
423  //----------------------------------------------------------------
425  friend std::ostream &
426  operator<<(std::ostream &o,const size_mismatch_error &e);
427  };
428 
429  //--------------------------------------------------------------------------
437  class index_error: public exception
438  {
439  public:
441  index_error() : exception("index_error")
442  { }
443 
444  //-----------------------------------------------------------------
452  explicit index_error(const exception_record &i, const string &d) :
453  exception("index_error", i, d)
454  { }
455 
456  //-----------------------------------------------------------------
458  ~index_error() throw() { }
459 
460  //----------------------------------------------------------------
462  friend std::ostream &
463  operator<<(std::ostream &o,const index_error &e);
464  };
465 
466  //--------------------------------------------------------------------------
473  class key_error: public exception
474  {
475  public:
477  key_error():exception("key_error"){}
478 
479  //-----------------------------------------------------------------
487  explicit key_error(const exception_record &i,const string &d):
488  exception("key_error",i,d)
489  {}
490 
491  //-----------------------------------------------------------------
493  ~key_error() throw() {}
494 
495  //----------------------------------------------------------------
497  friend std::ostream &
498  operator<<(std::ostream &o,const key_error &e);
499  };
500 
501  //--------------------------------------------------------------------------
508  class file_error: public exception
509  {
510  public:
512  file_error() : exception("file_error") { }
513 
514  //-----------------------------------------------------------------
522  explicit file_error(const exception_record &i, const string &d) :
523  exception("file_error", i, d)
524  { }
525 
526  //-----------------------------------------------------------------
528  ~file_error() throw() { }
529 
530  //----------------------------------------------------------------
532  friend std::ostream &
533  operator<<(std::ostream &o,const file_error &e);
534 
535  };
536 
537  //--------------------------------------------------------------------------
544  class type_error: public exception
545  {
546  public:
548  type_error() : exception("type_error") { }
549 
550  //-----------------------------------------------------------------
558  explicit type_error(const exception_record &i, const string &d):
559  exception("type_error", i, d)
560  { }
561 
562  //-----------------------------------------------------------------
564  ~type_error() throw() { }
565 
566  //----------------------------------------------------------------
568  friend std::ostream &
569  operator<<(std::ostream &o,const type_error &e);
570  };
571 
572  //--------------------------------------------------------------------------
580  class value_error: public exception
581  {
582  public:
584  value_error() : exception("value_error") { }
585 
586  //-----------------------------------------------------------------
594  explicit value_error(const exception_record &i, const string &d):
595  exception("value_error", i, d)
596  { }
597 
598  //-----------------------------------------------------------------
600  ~value_error() throw() { }
601 
602  //----------------------------------------------------------------
604  friend std::ostream &
605  operator<<(std::ostream &o,const value_error &e);
606  };
607 
608  //--------------------------------------------------------------------------
616  class range_error: public exception
617  {
618  public:
620  range_error(): exception("range_error"){ }
621 
622  //-----------------------------------------------------------------
630  explicit range_error(const exception_record &i,const string &d):
631  exception("range_error",i,d)
632  { }
633 
634  //-----------------------------------------------------------------
636  ~range_error() throw() { }
637 
638  //----------------------------------------------------------------
640  friend std::ostream &
641  operator<<(std::ostream &o,const range_error &e);
642  };
643 
644  //--------------------------------------------------------------------------
654  {
655  public:
658  exception("not_implemented_error")
659  { }
660 
661  //-----------------------------------------------------------------
670  const string &d):
671  exception("not_implemented_error",i,d)
672  { }
673 
674  //-----------------------------------------------------------------
677 
678  //----------------------------------------------------------------
680  friend std::ostream &
681  operator<<(std::ostream &o,const not_implemented_error &e);
682  };
683 
684 
685  //--------------------------------------------------------------------------
693  {
694  public:
696  iterator_error():exception("iterator_error"){ }
697 
698  //-----------------------------------------------------------------
707  explicit iterator_error(const exception_record &i,const string &d):
708  exception("iterator_error",i,d)
709  { }
710 
711  //-----------------------------------------------------------------
713  ~iterator_error() throw() { }
714 
715  //-----------------------------------------------------------------
717  friend std::ostream &
718  operator<<(std::ostream &o,const iterator_error &e);
719  };
720 
721 
722  //-------------------------------------------------------------------------
731  {
732  public:
733  //------------------------------------------------------------------
735  cli_argument_error():exception("cli_argument_error"){}
736 
737  //------------------------------------------------------------------
744  cli_argument_error(const exception_record &r,const string &d):
745  exception("cli_argument_error",r,d)
746  {}
747 
748  //------------------------------------------------------------------
750  ~cli_argument_error() throw() {}
751 
752  //------------------------------------------------------------------
754  friend std::ostream &
755  operator<<(std::ostream &o,const cli_argument_error &e);
756  };
757 
758  //--------------------------------------------------------------------------
767  {
768  public:
769  //------------------------------------------------------------------
771  cli_option_error():exception("cli_option_error"){}
772 
773  //------------------------------------------------------------------
780  cli_option_error(const exception_record &r,const string &d):
781  exception("cli_option_error",r,d)
782  {}
783 
784  //------------------------------------------------------------------
785  ~cli_option_error() throw() {}
786 
787  //------------------------------------------------------------------
789  friend std::ostream &
790  operator<<(std::ostream &o,const cli_option_error &e);
791  };
792 
793  //--------------------------------------------------------------------------
801  class cli_error:public exception
802  {
803  public:
804  //------------------------------------------------------------------
806  cli_error():exception("cli_error"){}
807 
808  //------------------------------------------------------------------
815  cli_error(const exception_record &r,const string &d):
816  exception("cli_eror",r,d)
817  {}
818 
819  //------------------------------------------------------------------
821  ~cli_error() throw() {}
822 
823  //------------------------------------------------------------------
825  friend std::ostream &operator<<(std::ostream &o,const cli_error &e);
826  };
827 
828  //--------------------------------------------------------------------------
837  {
838  public:
839  //------------------------------------------------------------------
841  cli_help_request():exception("cli_help_request"){}
842 
843  //------------------------------------------------------------------
850  cli_help_request(const exception_record &r,const string &d):
851  exception("cli_help_request",r,d)
852  {}
853 
854  //------------------------------------------------------------------
856  ~cli_help_request() throw() {}
857 
858  //----------------------------------------------------------------
860  friend std::ostream &
861  operator<<(std::ostream &o,const cli_help_request &e);
862  };
863 }
864 }
~type_error()
destructor
Definition: exceptions.hpp:564
data range error
Definition: exceptions.hpp:616
iterator_error(const exception_record &i, const string &d)
constructor
Definition: exceptions.hpp:707
friend std::ostream & operator<<(std::ostream &o, const shape_mismatch_error &e)
output operator
value_error(const exception_record &i, const string &d)
constructor
Definition: exceptions.hpp:594
Size mismatch error.
Definition: exceptions.hpp:399
~memory_allocation_error()
destructor
Definition: exceptions.hpp:305
~iterator_error()
destructor
Definition: exceptions.hpp:713
exception record
Definition: exceptions.hpp:90
std::ostream & print(std::ostream &o) const
ouptut method
void append(const exception_record &n)
add a new issuer
Definition: exceptions.hpp:228
file_error()
default constructor
Definition: exceptions.hpp:512
help request
Definition: exceptions.hpp:836
range_error(const exception_record &i, const string &d)
constructor
Definition: exceptions.hpp:630
~index_error()
destructor
Definition: exceptions.hpp:458
data value error
Definition: exceptions.hpp:580
friend std::ostream & operator<<(std::ostream &o, const size_mismatch_error &e)
output operator
size_mismatch_error(const exception_record &i, const string &d)
constructor
Definition: exceptions.hpp:414
friend std::ostream & operator<<(std::ostream &o, const cli_help_request &e)
output operator
data type error
Definition: exceptions.hpp:544
friend std::ostream & operator<<(std::ostream &o, const file_error &e)
output operator
friend std::ostream & operator<<(std::ostream &o, const memory_not_allocated_error &e)
output operator
Shape mismatch error.
Definition: exceptions.hpp:360
friend std::ostream & operator<<(std::ostream &o, const iterator_error &e)
output operator
cli_option_error(const exception_record &r, const string &d)
constructor
Definition: exceptions.hpp:780
type_error(const exception_record &i, const string &d)
constructor
Definition: exceptions.hpp:558
std::list< exception_record > _records
exception records
Definition: exceptions.hpp:139
memory allocation error
Definition: exceptions.hpp:284
~file_error()
destructor
Definition: exceptions.hpp:528
size_t line() const
get file line number
Definition: exceptions.hpp:112
~value_error()
destructor
Definition: exceptions.hpp:600
~not_implemented_error()
destructor
Definition: exceptions.hpp:676
friend std::ostream & operator<<(std::ostream &o, const value_error &e)
output operator
const_iterator end() const
get iterator to the last error record
Definition: exceptions.hpp:267
general CLI error
Definition: exceptions.hpp:801
exception_record(const string &file, size_t line, const string &func)
constructor
Definition: exceptions.hpp:102
cli_error(const exception_record &r, const string &d)
constructor
Definition: exceptions.hpp:815
exception_record()=delete
no default constructor
const string & description() const
get the exceptions description
Definition: exceptions.hpp:245
string _file
source file where the exception occured
Definition: exceptions.hpp:93
index_error()
default constructor
Definition: exceptions.hpp:441
friend std::ostream & operator<<(std::ostream &o, const key_error &e)
output operator
index error
Definition: exceptions.hpp:437
not_implemented_error()
default construtor
Definition: exceptions.hpp:657
memory_allocation_error()
default constructor
Definition: exceptions.hpp:288
const string & file() const
get file name
Definition: exceptions.hpp:110
Definition: add_op.hpp:29
size_t _line
line number in the source file
Definition: exceptions.hpp:94
memory_not_allocated_error()
default constructor
Definition: exceptions.hpp:325
friend std::ostream & operator<<(std::ostream &o, const type_error &e)
output operator
const string & name() const
get the exceptions name
Definition: exceptions.hpp:205
not implemented exception
Definition: exceptions.hpp:653
shape_mismatch_error(const exception_record &i, const string &d)
constructor
Definition: exceptions.hpp:376
friend std::ostream & operator<<(std::ostream &o, const index_error &e)
output operator
exception()
default constructor
index_error(const exception_record &i, const string &d)
constructor
Definition: exceptions.hpp:452
~cli_argument_error()
destructor
Definition: exceptions.hpp:750
key error
Definition: exceptions.hpp:473
string _name
name of the exception
Definition: exceptions.hpp:135
shape_mismatch_error()
default constructor
Definition: exceptions.hpp:364
memory_not_allocated_error(const exception_record &i, const string &d)
constructor
Definition: exceptions.hpp:337
file_error(const exception_record &i, const string &d)
constructor
Definition: exceptions.hpp:522
Exceptions base class.
Definition: exceptions.hpp:131
void description(const std::string &desc)
set exception desccription
Definition: exceptions.hpp:236
~shape_mismatch_error()
destructor
Definition: exceptions.hpp:383
cli_argument_error()
default constructor
Definition: exceptions.hpp:735
size_mismatch_error()
default constructor
Definition: exceptions.hpp:403
string _description
description of the error occured
Definition: exceptions.hpp:137
iterator error
Definition: exceptions.hpp:692
key_error()
default constructor
Definition: exceptions.hpp:477
const_iterator begin() const
get iterator to first error record
Definition: exceptions.hpp:256
iterator_error()
default constructor
Definition: exceptions.hpp:696
~memory_not_allocated_error()
destructor
Definition: exceptions.hpp:344
type_error()
default constructor
Definition: exceptions.hpp:548
friend std::ostream & operator<<(std::ostream &o, const not_implemented_error &e)
output operator
cli_argument_error(const exception_record &r, const string &d)
constructor
Definition: exceptions.hpp:744
friend std::ostream & operator<<(std::ostream &, const exception &)
output operator for exceptions
string _function
function in which the error occured
Definition: exceptions.hpp:95
command line option error
Definition: exceptions.hpp:766
memory not allocated error
Definition: exceptions.hpp:321
void name(const std::string &name)
set the exception name
Definition: exceptions.hpp:197
cli_help_request(const exception_record &r, const string &d)
constructor
Definition: exceptions.hpp:850
~cli_error()
destructor
Definition: exceptions.hpp:821
~cli_help_request()
destructor
Definition: exceptions.hpp:856
file IO fails
Definition: exceptions.hpp:508
command line argument error
Definition: exceptions.hpp:730
value_error()
default constructor
Definition: exceptions.hpp:584
memory_allocation_error(const exception_record &i, const string &d)
constructor
Definition: exceptions.hpp:298
friend std::ostream & operator<<(std::ostream &o, const cli_argument_error &e)
output operator
std::ostream & operator<<(std::ostream &o, const exception_record &rec)
error record output operator
friend std::ostream & operator<<(std::ostream &o, const cli_error &e)
output operator
cli_help_request()
default constructor
Definition: exceptions.hpp:841
~size_mismatch_error()
destructor
Definition: exceptions.hpp:421
std::list< exception_record >::const_iterator const_iterator
const iterator over the error record
Definition: exceptions.hpp:153
~key_error()
destructor
Definition: exceptions.hpp:493
friend std::ostream & operator<<(std::ostream &o, const memory_allocation_error &e)
output operator
range_error()
default constructor
Definition: exceptions.hpp:620
friend std::ostream & operator<<(std::ostream &o, const range_error &e)
output operator
std::string string
String type.
Definition: types/types.hpp:69
cli_option_error()
default constructor
Definition: exceptions.hpp:771
key_error(const exception_record &i, const string &d)
constructor
Definition: exceptions.hpp:487
not_implemented_error(const exception_record &i, const string &d)
constructor
Definition: exceptions.hpp:669
cli_error()
default constructor
Definition: exceptions.hpp:806
~range_error()
destructor
Definition: exceptions.hpp:636
friend std::ostream & operator<<(std::ostream &o, const cli_option_error &e)
output operator
virtual ~exception()
virtual destructor
Definition: exceptions.hpp:190