Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ExceptionHandler.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <exception>
9#include <ext/iostream>
10#include <functional>
11#include <optional>
12#include <vector>
13
14namespace alib {
15
17private:
18 struct NestedException {
19 std::string type;
20 std::optional < std::string > desc;
21
22 explicit NestedException ( std::string type_, std::string desc_ );
23 explicit NestedException ( std::string type_ );
24 };
25
26 friend ext::ostream & operator << ( ext::ostream & os, const NestedException & exception );
27
28public:
30 public:
31 template < typename ... Params >
32 void push_back ( Params && ... params ) {
33 exceptions.emplace_back ( std::forward < std::string > ( params ) ... );
34 }
35
36 const std::vector < NestedException > & getExceptions ( ) const { return exceptions; }
37
38 private:
39 std::vector < NestedException > exceptions;
40 };
41
42private:
43 static int handlerRec ( NestedExceptionContainer & output, const std::vector < std::function < int ( NestedExceptionContainer & ) > >::const_reverse_iterator & iter );
44
45 static std::vector < std::function < int ( NestedExceptionContainer & ) > > & handlers ( );
46
47 template < class ExceptionType >
48 static void addHandlerDeduce ( std::function < void ( NestedExceptionContainer &, const ExceptionType & ) > handler, int result ) {
49 handlers ( ).push_back ( [ = ] ( NestedExceptionContainer & output ) {
50 try {
51 throw;
52 } catch ( const ExceptionType & exception ) {
53 handler ( output, exception );
54 return ExceptionHandler::rethrow_if_nested ( output, exception, result );
55 }
56 } );
57 }
58
59 static int rethrow_if_nested ( NestedExceptionContainer &, const std::exception & e, int result );
60
61public:
62 static int handle ( NestedExceptionContainer & output );
63 static int handle ( ext::ostream & output );
64
65 template < int Result, class Callable >
66 static void addHandler ( Callable handler ) {
67 addHandlerDeduce ( std::function ( handler ), Result );
68 }
69};
70
71} /* namespace alib */
72
Definition: ExceptionHandler.hpp:29
void push_back(Params &&... params)
Definition: ExceptionHandler.hpp:32
const std::vector< NestedException > & getExceptions() const
Definition: ExceptionHandler.hpp:36
Definition: ExceptionHandler.hpp:16
static int handle(NestedExceptionContainer &output)
Definition: ExceptionHandler.cpp:64
static void addHandler(Callable handler)
Definition: ExceptionHandler.hpp:66
friend ext::ostream & operator<<(ext::ostream &os, const NestedException &exception)
Definition: ExceptionHandler.cpp:19
Definition: ostream.h:14
Definition: ExceptionHandler.cpp:13
for(const StateType &state :fsm.getStates()) renamingData.insert(std Rename::RenamedAutomaton< T > result(renamingData.at(fsm.getInitialState()))
Definition: Rename.h:253
Definition: CommonException.cpp:18