File indexing completed on 2024-05-12 16:42:34

0001 /*
0002     SPDX-FileCopyrightText: 2002-2008 Thomas Baumgart <tbaumgart@kde.org>
0003     SPDX-FileCopyrightText: 2017-2018 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef MYMONEYEXCEPTION_H
0008 #define MYMONEYEXCEPTION_H
0009 
0010 #define KMM_STRINGIFY(x) #x
0011 #define KMM_TOSTRING(x) KMM_STRINGIFY(x)
0012 
0013 #include <stdexcept>
0014 #include "kmm_mymoney_export.h"
0015 
0016 /**
0017   * @file
0018   * @author Thomas Baumgart
0019   * @author Łukasz Wojniłowicz
0020   */
0021 
0022 /**
0023  * @def The MYMONEYEXCEPTION(exceptionMessage) define
0024  * This is the preferred constructor to create a new exception
0025  * object. It automatically inserts the filename and the source
0026  * code line into the object upon creation.
0027  */
0028 
0029 #define MYMONEYEXCEPTION(exceptionMessage) MyMoneyException(qPrintable(QString::fromLatin1("%1 %2:%3").arg(exceptionMessage, QString::fromLatin1(__FILE__), QString::number(__LINE__))))
0030 
0031 /**
0032  * @def The MYMONEYEXCEPTION(what) define
0033  * This is alternative constructor to create a new exception
0034  * object. It avoids needless string conversion if the input string is const char*
0035  */
0036 
0037 #define MYMONEYEXCEPTION_CSTRING(exceptionMessage) MyMoneyException(exceptionMessage " " __FILE__ ":" KMM_TOSTRING(__LINE__))
0038 
0039 // krazy:excludeall=dpointer
0040 // krazy:excludeall=inline
0041 
0042 #if defined(Q_OS_WIN)
0043 // Otherwise
0044 // non dll-interface class 'std::runtime_error' used as base for dll-interface class 'MyMoneyException'
0045 class MyMoneyException final : public std::runtime_error
0046 
0047 #else
0048 // Based on https://gcc.gnu.org/wiki/Visibility
0049 // custom exception classes should always be exported
0050 // Otherwise we get an error like that:
0051 // Expected exception of type MyMoneyException to be thrown but std::exception caught
0052 class KMM_MYMONEY_EXPORT MyMoneyException final : public std::runtime_error
0053 
0054 #endif
0055 {
0056 public:
0057     /**
0058       * The constructor to create a new MyMoneyException object.
0059       *
0060       * @param exceptionMessage reference to const char * containing the message
0061       *
0062       * An easier way to use this constructor is to use the macro
0063       * MYMONEYEXCEPTION(text) instead. It automatically assigns the file
0064       * and line parameter to the correct values.
0065       */
0066     explicit MyMoneyException(const char *exceptionMessage) : std::runtime_error(exceptionMessage) {}  // krazy:exclude=inline
0067 };
0068 
0069 #endif