File indexing completed on 2024-05-19 05:07:15

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_SSTRING(exceptionMessage) MyMoneyException(exceptionMessage + std::string(", " __FILE__ ":" KMM_TOSTRING(__LINE__)))
0038 #define MYMONEYEXCEPTION_CSTRING(exceptionMessage) MyMoneyException(exceptionMessage " " __FILE__ ":" KMM_TOSTRING(__LINE__))
0039 
0040 // krazy:excludeall=dpointer
0041 // krazy:excludeall=inline
0042 
0043 #if defined(Q_OS_WIN)
0044 // Otherwise
0045 // non dll-interface class 'std::runtime_error' used as base for dll-interface class 'MyMoneyException'
0046 class MyMoneyException final : public std::runtime_error
0047 
0048 #else
0049 // Based on https://gcc.gnu.org/wiki/Visibility
0050 // custom exception classes should always be exported
0051 // Otherwise we get an error like that:
0052 // Expected exception of type MyMoneyException to be thrown but std::exception caught
0053 class KMM_MYMONEY_EXPORT MyMoneyException final : public std::runtime_error
0054 
0055 #endif
0056 {
0057 public:
0058     /**
0059       * The constructor to create a new MyMoneyException object.
0060       *
0061       * @param exceptionMessage reference to const char * containing the message
0062       *
0063       * An easier way to use this constructor is to use the macro
0064       * MYMONEYEXCEPTION(text) instead. It automatically assigns the file
0065       * and line parameter to the correct values.
0066       */
0067     explicit MyMoneyException(const char *exceptionMessage) : std::runtime_error(exceptionMessage) {}  // krazy:exclude=inline
0068     explicit MyMoneyException(const std::string& exceptionMessage)
0069         : std::runtime_error(exceptionMessage)
0070     {
0071     } // krazy:exclude=inline
0072 };
0073 
0074 #endif