guard_exceptions.h
Go to the documentation of this file.
1 //
2 // Bareflank Hypervisor
3 //
4 // Copyright (C) 2015 Assured Information Security, Inc.
5 // Author: Rian Quinn <quinnr@ainfosec.com>
6 // Author: Brendan Kerrigan <kerriganb@ainfosec.com>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library 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 GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 
22 #ifndef GUARD_EXCEPTIONS_H
23 #define GUARD_EXCEPTIONS_H
24 
25 #include <iostream>
26 #include <error_codes.h>
27 
28 template<class T, class E> E
29 guard_exceptions(E error_code, T func)
30 {
31  try
32  {
33  func();
34 
35  return SUCCESS;
36  }
37  catch (std::bad_alloc &e)
38  {
39  return BF_BAD_ALLOC;
40  }
41  catch (std::exception &e)
42  {
43  std::cout << '\n';
44  std::cerr << "----------------------------------------" << '\n';
45  std::cerr << "- Standard Exception Caught -" << '\n';
46  std::cerr << "----------------------------------------" << '\n';
47  std::cerr << e.what() << '\n';
48  }
49  catch (...)
50  {
51  std::cout << '\n';
52  std::cerr << "----------------------------------------" << '\n';
53  std::cerr << "- Unknown Exception Caught -" << '\n';
54  std::cerr << "----------------------------------------" << '\n';
55  }
56 
57  return error_code;
58 }
59 
60 template<class T> void
62 {
63  guard_exceptions(0L, std::forward<T>(func));
64 }
65 
66 #endif
#define BF_BAD_ALLOC
Definition: error_codes.h:128
E guard_exceptions(E error_code, T func)
#define SUCCESS
Definition: error_codes.h:42