|
| 1 | +/* Copyright (c) 2019, 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 | +#ifndef NV_UTIL_NPP_EXCEPTIONS_H |
| 29 | +#define NV_UTIL_NPP_EXCEPTIONS_H |
| 30 | + |
| 31 | + |
| 32 | +#include <string> |
| 33 | +#include <sstream> |
| 34 | +#include <iostream> |
| 35 | + |
| 36 | +/// All npp related C++ classes are put into the npp namespace. |
| 37 | +namespace npp |
| 38 | +{ |
| 39 | + |
| 40 | + /// Exception base class. |
| 41 | + /// This exception base class will be used for everything C++ throught |
| 42 | + /// the NPP project. |
| 43 | + /// The exception contains a string message, as well as data fields for a string |
| 44 | + /// containing the name of the file as well as the line number where the exception was thrown. |
| 45 | + /// The easiest way of throwing exceptions and providing filename and line number is |
| 46 | + /// to use one of the ASSERT macros defined for that purpose. |
| 47 | + class Exception |
| 48 | + { |
| 49 | + public: |
| 50 | + /// Constructor. |
| 51 | + /// \param rMessage A message with information as to why the exception was thrown. |
| 52 | + /// \param rFileName The name of the file where the exception was thrown. |
| 53 | + /// \param nLineNumber Line number in the file where the exception was thrown. |
| 54 | + explicit |
| 55 | + Exception(const std::string &rMessage = "", const std::string &rFileName = "", unsigned int nLineNumber = 0) |
| 56 | + : sMessage_(rMessage), sFileName_(rFileName), nLineNumber_(nLineNumber) |
| 57 | + { }; |
| 58 | + |
| 59 | + Exception(const Exception &rException) |
| 60 | + : sMessage_(rException.sMessage_), sFileName_(rException.sFileName_), nLineNumber_(rException.nLineNumber_) |
| 61 | + { }; |
| 62 | + |
| 63 | + virtual |
| 64 | + ~Exception() |
| 65 | + { }; |
| 66 | + |
| 67 | + /// Get the exception's message. |
| 68 | + const |
| 69 | + std::string & |
| 70 | + message() |
| 71 | + const |
| 72 | + { |
| 73 | + return sMessage_; |
| 74 | + } |
| 75 | + |
| 76 | + /// Get the exception's file info. |
| 77 | + const |
| 78 | + std::string & |
| 79 | + fileName() |
| 80 | + const |
| 81 | + { |
| 82 | + return sFileName_; |
| 83 | + } |
| 84 | + |
| 85 | + /// Get the exceptions's line info. |
| 86 | + unsigned int |
| 87 | + lineNumber() |
| 88 | + const |
| 89 | + { |
| 90 | + return nLineNumber_; |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + /// Create a clone of this exception. |
| 95 | + /// This creates a new Exception object on the heap. It is |
| 96 | + /// the responsibility of the user of this function to free this memory |
| 97 | + /// (delete x). |
| 98 | + virtual |
| 99 | + Exception * |
| 100 | + clone() |
| 101 | + const |
| 102 | + { |
| 103 | + return new Exception(*this); |
| 104 | + } |
| 105 | + |
| 106 | + /// Create a single string with all the exceptions information. |
| 107 | + /// The virtual toString() method is used by the operator<<() |
| 108 | + /// so that all exceptions derived from this base-class can print |
| 109 | + /// their full information correctly even if a reference to their |
| 110 | + /// exact type is not had at the time of printing (i.e. the basic |
| 111 | + /// operator<<() is used). |
| 112 | + virtual |
| 113 | + std::string |
| 114 | + toString() |
| 115 | + const |
| 116 | + { |
| 117 | + std::ostringstream oOutputString; |
| 118 | + oOutputString << fileName() << ":" << lineNumber() << ": " << message(); |
| 119 | + return oOutputString.str(); |
| 120 | + } |
| 121 | + |
| 122 | + private: |
| 123 | + std::string sMessage_; ///< Message regarding the cause of the exception. |
| 124 | + std::string sFileName_; ///< Name of the file where the exception was thrown. |
| 125 | + unsigned int nLineNumber_; ///< Line number in the file where the exception was thrown |
| 126 | + }; |
| 127 | + |
| 128 | + /// Output stream inserter for Exception. |
| 129 | + /// \param rOutputStream The stream the exception information is written to. |
| 130 | + /// \param rException The exception that's being written. |
| 131 | + /// \return Reference to the output stream being used. |
| 132 | + std::ostream & |
| 133 | + operator << (std::ostream &rOutputStream, const Exception &rException) |
| 134 | + { |
| 135 | + rOutputStream << rException.toString(); |
| 136 | + return rOutputStream; |
| 137 | + } |
| 138 | + |
| 139 | + /// Basic assert macro. |
| 140 | + /// This macro should be used to enforce any kind of pre or post conditions. |
| 141 | + /// Unlike the C-runtime assert macro, this macro does not abort execution, but throws |
| 142 | + /// a C++ exception. The exception is automatically filled with information about the failing |
| 143 | + /// condition, the filename and line number where the exception was thrown. |
| 144 | + /// \note The macro is written in such a way that omitting a semicolon after its usage |
| 145 | + /// causes a compiler error. The correct way to invoke this macro is: |
| 146 | + /// NPP_ASSERT(n < MAX); |
| 147 | +#define NPP_ASSERT(C) do {if (!(C)) throw npp::Exception(#C " assertion faild!", __FILE__, __LINE__);} while(false) |
| 148 | + |
| 149 | + // ASSERT macro. |
| 150 | + // Same functionality as the basic assert macro with the added ability to pass |
| 151 | + // a message M. M should be a string literal. |
| 152 | + // Note: Never use code inside ASSERT() that causes a side-effect ASSERT macros may get compiled |
| 153 | + // out in release mode. |
| 154 | +#define NPP_ASSERT_MSG(C, M) do {if (!(C)) throw npp::Exception(#C " assertion faild! Message: " M, __FILE__, __LINE__);} while(false) |
| 155 | + |
| 156 | +#ifdef _DEBUG |
| 157 | + /// Basic debug assert macro. |
| 158 | + /// This macro is identical in every respect to NPP_ASSERT(C) but it does get compiled to a |
| 159 | + /// no-op in release builds. It is therefor of utmost importance to not put statements into |
| 160 | + /// this macro that cause side effects required for correct program execution. |
| 161 | +#define NPP_DEBUG_ASSERT(C) do {if (!(C)) throw npp::Exception(#C " debug assertion faild!", __FILE__, __LINE__);} while(false) |
| 162 | +#else |
| 163 | +#define NPP_DEBUG_ASSERT(C) |
| 164 | +#endif |
| 165 | + |
| 166 | + /// ASSERT for null-pointer test. |
| 167 | + /// It is safe to put code with side effects into this macro. Also: This macro never |
| 168 | + /// gets compiled to a no-op because resource allocation may fail based on external causes not under |
| 169 | + /// control of a software developer. |
| 170 | +#define NPP_ASSERT_NOT_NULL(P) do {if ((P) == 0) throw npp::Exception(#P " not null assertion faild!", __FILE__, __LINE__);} while(false) |
| 171 | + |
| 172 | + /// Macro for flagging methods as not implemented. |
| 173 | + /// The macro throws an exception with a message that an implementation was missing |
| 174 | +#define NPP_NOT_IMPLEMENTED() do {throw npp::Exception("Implementation missing!", __FILE__, __LINE__);} while(false) |
| 175 | + |
| 176 | + /// Macro for checking error return code of CUDA (runtime) calls. |
| 177 | + /// This macro never gets disabled. |
| 178 | +#define NPP_CHECK_CUDA(S) do {cudaError_t eCUDAResult; \ |
| 179 | + eCUDAResult = S; \ |
| 180 | + if (eCUDAResult != cudaSuccess) std::cout << "NPP_CHECK_CUDA - eCUDAResult = " << eCUDAResult << std::endl; \ |
| 181 | + NPP_ASSERT(eCUDAResult == cudaSuccess);} while (false) |
| 182 | + |
| 183 | + /// Macro for checking error return code for NPP calls. |
| 184 | +#define NPP_CHECK_NPP(S) do {NppStatus eStatusNPP; \ |
| 185 | + eStatusNPP = S; \ |
| 186 | + if (eStatusNPP != NPP_SUCCESS) std::cout << "NPP_CHECK_NPP - eStatusNPP = " << _cudaGetErrorEnum(eStatusNPP) << "("<< eStatusNPP << ")" << std::endl; \ |
| 187 | + NPP_ASSERT(eStatusNPP == NPP_SUCCESS);} while (false) |
| 188 | + |
| 189 | + /// Macro for checking error return codes from cuFFT calls. |
| 190 | +#define NPP_CHECK_CUFFT(S) do {cufftResult eCUFFTResult; \ |
| 191 | + eCUFFTResult = S; \ |
| 192 | + if (eCUFFTResult != NPP_SUCCESS) std::cout << "NPP_CHECK_CUFFT - eCUFFTResult = " << eCUFFTResult << std::endl; \ |
| 193 | + NPP_ASSERT(eCUFFTResult == CUFFT_SUCCESS);} while (false) |
| 194 | + |
| 195 | +} // npp namespace |
| 196 | + |
| 197 | +#endif // NV_UTIL_NPP_EXCEPTIONS_H |
0 commit comments