Fenix @develop
 
Loading...
Searching...
No Matches
fenix_exception.hpp
1/*
2//@HEADER
3// ************************************************************************
4//
5//
6// _|_|_|_| _|_|_|_| _| _| _|_|_| _| _|
7// _| _| _|_| _| _| _| _|
8// _|_|_| _|_|_| _| _| _| _| _|
9// _| _| _| _|_| _| _| _|
10// _| _|_|_|_| _| _| _|_|_| _| _|
11//
12//
13//
14//
15// Copyright (C) 2016 Rutgers University and Sandia Corporation
16//
17// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
18// the U.S. Government retains certain rights in this software.
19//
20// Redistribution and use in source and binary forms, with or without
21// modification, are permitted provided that the following conditions are
22// met:
23//
24// 1. Redistributions of source code must retain the above copyright
25// notice, this list of conditions and the following disclaimer.
26//
27// 2. Redistributions in binary form must reproduce the above copyright
28// notice, this list of conditions and the following disclaimer in the
29// documentation and/or other materials provided with the distribution.
30//
31// 3. Neither the name of the Corporation nor the names of the
32// contributors may be used to endorse or promote products derived from
33// this software without specific prior written permission.
34//
35// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
36// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
39// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
41// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
42// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
43// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
44// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46//
47// Author Marc Gamell, Eric Valenzuela, Keita Teranishi, Manish Parashar,
48// Rob Van der Wijngaart, Michael Heroux, and Matthew Whitlock
49//
50// Questions? Contact Keita Teranishi (knteran@sandia.gov) and
51// Marc Gamell (mgamell@cac.rutgers.edu)
52//
53// ************************************************************************
54//@HEADER
55*/
56
57#ifndef FENIX_EXCEPTION_HPP
58#define FENIX_EXCEPTION_HPP
59
60#include <mpi.h>
61#include <exception>
62#include <source_location>
63#include <string_view>
64#include <string>
65
66namespace fenix {
67
68struct CommException : public std::exception {
69 CommException(MPI_Comm comm, int fenix_error, int mpi_error)
70 : repaired_comm(comm), fenix_err(fenix_error), mpi_err(mpi_error) {};
71
72 MPI_Comm repaired_comm;
73 const int fenix_err;
74 const int mpi_err;
75};
76
77struct RuntimeException : public std::exception {
78 using Location = std::source_location;
79
80 RuntimeException(Location l = Location::current())
81 : RuntimeException(FENIX_ERROR_NOCATEGORY, "FENIX_ERROR_NOCATEGORY", l) {}
82
83 RuntimeException(const char* e_str, Location l = Location::current())
84 : RuntimeException(FENIX_ERROR_NOCATEGORY, e_str, l) {};
85
86 // Helper for preprocessor macro
88 const char* e_str, const char*, Location l = Location::current()
89 ) : RuntimeException(FENIX_ERROR_NOCATEGORY, e_str, l) {};
90
91 RuntimeException(int e, const char* e_str, Location l = Location::current())
92 : error(e), error_string(e_str), location(l) {};
93
94 // file:line function error_string
95 std::string to_string(int depth = 0) const noexcept {
96 std::string_view f = location.file_name();
97 std::string l = std::to_string(location.line());
98 std::string_view fn = location.function_name();
99
100 std::string ret;
101 ret.reserve(
102 3 + depth * 2 + f.size() + l.size() + fn.size() + error_string.size()
103 );
104 ret.assign(" ", depth * 2);
105 ret += f; ret += ":";
106 ret += l; ret += " ";
107 ret += fn; ret += " ";
108 ret += error_string;
109 return ret;
110 }
111
112 const char* what() const noexcept override {
113 m_string = to_string();
114 return m_string.c_str();
115 }
116
117 const int error;
118 const std::string_view error_string;
119 const std::source_location location;
120
121 private:
122 mutable std::string m_string;
123};
124
125} // namespace fenix
126
127// err can be a FENIX_ERROR_* value, or any string
128#define FENIX_THROW(err) throw fenix::RuntimeException(err, #err)
129#define FENIX_THROW_FROM(err, loc) throw fenix::RuntimeException(err, #err, loc)
130
131#endif
Definition fenix_exception.hpp:68
Definition fenix_exception.hpp:77