|
| 1 | +/* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. |
| 2 | + * |
| 3 | + * Redistribution and use in source and binary forms, with or without |
| 4 | + * modification, are permitted provided that the following conditions |
| 5 | + * are met: |
| 6 | + * * Redistributions of source code must retain the above copyright |
| 7 | + * notice, this list of conditions and the following disclaimer. |
| 8 | + * * Redistributions in binary form must reproduce the above copyright |
| 9 | + * notice, this list of conditions and the following disclaimer in the |
| 10 | + * documentation and/or other materials provided with the distribution. |
| 11 | + * * Neither the name of NVIDIA CORPORATION nor the names of its |
| 12 | + * contributors may be used to endorse or promote products derived |
| 13 | + * from this software without specific prior written permission. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 16 | + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 19 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 | + * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + */ |
| 27 | + |
| 28 | +/* CUda UTility Library */ |
| 29 | +#ifndef COMMON_EXCEPTION_H_ |
| 30 | +#define COMMON_EXCEPTION_H_ |
| 31 | + |
| 32 | +// includes, system |
| 33 | +#include <stdlib.h> |
| 34 | +#include <exception> |
| 35 | +#include <iostream> |
| 36 | +#include <stdexcept> |
| 37 | +#include <string> |
| 38 | + |
| 39 | +//! Exception wrapper. |
| 40 | +//! @param Std_Exception Exception out of namespace std for easy typing. |
| 41 | +template <class Std_Exception> |
| 42 | +class Exception : public Std_Exception { |
| 43 | + public: |
| 44 | + //! @brief Static construction interface |
| 45 | + //! @return Alwayss throws ( Located_Exception<Exception>) |
| 46 | + //! @param file file in which the Exception occurs |
| 47 | + //! @param line line in which the Exception occurs |
| 48 | + //! @param detailed details on the code fragment causing the Exception |
| 49 | + static void throw_it(const char *file, const int line, |
| 50 | + const char *detailed = "-"); |
| 51 | + |
| 52 | + //! Static construction interface |
| 53 | + //! @return Alwayss throws ( Located_Exception<Exception>) |
| 54 | + //! @param file file in which the Exception occurs |
| 55 | + //! @param line line in which the Exception occurs |
| 56 | + //! @param detailed details on the code fragment causing the Exception |
| 57 | + static void throw_it(const char *file, const int line, |
| 58 | + const std::string &detailed); |
| 59 | + |
| 60 | + //! Destructor |
| 61 | + virtual ~Exception() throw(); |
| 62 | + |
| 63 | + private: |
| 64 | + //! Constructor, default (private) |
| 65 | + Exception(); |
| 66 | + |
| 67 | + //! Constructor, standard |
| 68 | + //! @param str string returned by what() |
| 69 | + explicit Exception(const std::string &str); |
| 70 | +}; |
| 71 | + |
| 72 | +//////////////////////////////////////////////////////////////////////////////// |
| 73 | +//! Exception handler function for arbitrary exceptions |
| 74 | +//! @param ex exception to handle |
| 75 | +//////////////////////////////////////////////////////////////////////////////// |
| 76 | +template <class Exception_Typ> |
| 77 | +inline void handleException(const Exception_Typ &ex) { |
| 78 | + std::cerr << ex.what() << std::endl; |
| 79 | + |
| 80 | + exit(EXIT_FAILURE); |
| 81 | +} |
| 82 | + |
| 83 | +//! Convenience macros |
| 84 | + |
| 85 | +//! Exception caused by dynamic program behavior, e.g. file does not exist |
| 86 | +#define RUNTIME_EXCEPTION(msg) \ |
| 87 | + Exception<std::runtime_error>::throw_it(__FILE__, __LINE__, msg) |
| 88 | + |
| 89 | +//! Logic exception in program, e.g. an assert failed |
| 90 | +#define LOGIC_EXCEPTION(msg) \ |
| 91 | + Exception<std::logic_error>::throw_it(__FILE__, __LINE__, msg) |
| 92 | + |
| 93 | +//! Out of range exception |
| 94 | +#define RANGE_EXCEPTION(msg) \ |
| 95 | + Exception<std::range_error>::throw_it(__FILE__, __LINE__, msg) |
| 96 | + |
| 97 | +//////////////////////////////////////////////////////////////////////////////// |
| 98 | +//! Implementation |
| 99 | + |
| 100 | +// includes, system |
| 101 | +#include <sstream> |
| 102 | + |
| 103 | +//////////////////////////////////////////////////////////////////////////////// |
| 104 | +//! Static construction interface. |
| 105 | +//! @param Exception causing code fragment (file and line) and detailed infos. |
| 106 | +//////////////////////////////////////////////////////////////////////////////// |
| 107 | +/*static*/ template <class Std_Exception> |
| 108 | +void Exception<Std_Exception>::throw_it(const char *file, const int line, |
| 109 | + const char *detailed) { |
| 110 | + std::stringstream s; |
| 111 | + |
| 112 | + // Quiet heavy-weight but exceptions are not for |
| 113 | + // performance / release versions |
| 114 | + s << "Exception in file '" << file << "' in line " << line << "\n" |
| 115 | + << "Detailed description: " << detailed << "\n"; |
| 116 | + |
| 117 | + throw Exception(s.str()); |
| 118 | +} |
| 119 | + |
| 120 | +//////////////////////////////////////////////////////////////////////////////// |
| 121 | +//! Static construction interface. |
| 122 | +//! @param Exception causing code fragment (file and line) and detailed infos. |
| 123 | +//////////////////////////////////////////////////////////////////////////////// |
| 124 | +/*static*/ template <class Std_Exception> |
| 125 | +void Exception<Std_Exception>::throw_it(const char *file, const int line, |
| 126 | + const std::string &msg) { |
| 127 | + throw_it(file, line, msg.c_str()); |
| 128 | +} |
| 129 | + |
| 130 | +//////////////////////////////////////////////////////////////////////////////// |
| 131 | +//! Constructor, default (private). |
| 132 | +//////////////////////////////////////////////////////////////////////////////// |
| 133 | +template <class Std_Exception> |
| 134 | +Exception<Std_Exception>::Exception() : Std_Exception("Unknown Exception.\n") {} |
| 135 | + |
| 136 | +//////////////////////////////////////////////////////////////////////////////// |
| 137 | +//! Constructor, standard (private). |
| 138 | +//! String returned by what(). |
| 139 | +//////////////////////////////////////////////////////////////////////////////// |
| 140 | +template <class Std_Exception> |
| 141 | +Exception<Std_Exception>::Exception(const std::string &s) : Std_Exception(s) {} |
| 142 | + |
| 143 | +//////////////////////////////////////////////////////////////////////////////// |
| 144 | +//! Destructor |
| 145 | +//////////////////////////////////////////////////////////////////////////////// |
| 146 | +template <class Std_Exception> |
| 147 | +Exception<Std_Exception>::~Exception() throw() {} |
| 148 | + |
| 149 | + // functions, exported |
| 150 | + |
| 151 | +#endif // COMMON_EXCEPTION_H_ |
0 commit comments