File indexing completed on 2024-05-12 05:06:41

0001 /*
0002     SPDX-FileCopyrightText: 2005-2019 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 MYMONEYPRICE_H
0008 #define MYMONEYPRICE_H
0009 
0010 // ----------------------------------------------------------------------------
0011 // QT Includes
0012 
0013 #include <QMetaType>
0014 
0015 // ----------------------------------------------------------------------------
0016 // KDE Includes
0017 
0018 // ----------------------------------------------------------------------------
0019 // Project Includes
0020 
0021 #include "kmm_mymoney_export.h"
0022 
0023 class QString;
0024 class QDate;
0025 class QDomElement;
0026 
0027 class MyMoneyMoney;
0028 
0029 #include "qcontainerfwd.h"
0030 
0031 /**
0032   * @author Thomas Baumgart
0033   */
0034 
0035 /**
0036   * This class represents an exchange rate of a security, currency or commodity
0037   * based on another security, currency or commodity for a specific date.
0038   * The term security is used in this class as a placeholder for all
0039   * those previously mentioned items.
0040   * In general, the other security is a currency.
0041   *
0042   * The securities and the rate form the following equation:
0043   *
0044   * @code
0045   *
0046   *   toSecurity = rate * fromSecurity
0047   *
0048   * @endcode
0049   *
0050   * Using the @p rate() member function, one can retrieve the conversion rate based
0051   * upon the @p toSecurity or the @p fromSecurity.
0052   */
0053 class MyMoneyPricePrivate;
0054 class KMM_MYMONEY_EXPORT MyMoneyPrice
0055 {
0056     Q_DECLARE_PRIVATE(MyMoneyPrice)
0057     MyMoneyPricePrivate * d_ptr;
0058 
0059 public:
0060     MyMoneyPrice();
0061     explicit MyMoneyPrice(const QString& from,
0062                           const QString& to,
0063                           const QDomElement& node);
0064     explicit MyMoneyPrice(const QString& from,
0065                           const QString& to,
0066                           const QDate& date,
0067                           const MyMoneyMoney& rate,
0068                           const QString& source);
0069     MyMoneyPrice(const MyMoneyPrice & other);
0070     MyMoneyPrice(MyMoneyPrice && other);
0071     MyMoneyPrice & operator=(MyMoneyPrice other);
0072     friend void swap(MyMoneyPrice& first, MyMoneyPrice& second);
0073     virtual ~MyMoneyPrice();
0074 
0075     /**
0076       * This method returns the price information based on the
0077       * security referenced by @p id. If @p id is empty (default), the
0078       * price is returned based on the toSecurity. If this price
0079       * object is invalid (see isValid()) MyMoneyMoney(1,1) is returned.
0080       *
0081       * @param id return price to be the factor to be used to convert a value into
0082       *           the corresponding value in security @p id.
0083       *
0084       * @return returns the exchange rate (price) as MyMoneyMoney object.
0085       *
0086       * If @p id is not empty and does not match either security ids of this price
0087       * an exception will be thrown.
0088       *
0089       * Example:
0090       * Assume the following code, where you have a price object and
0091       * and you wish to convert from an amount in GBP (@p valGBP) to ADF (@p valADF).
0092       * Then your code will look like this:
0093       *
0094       * @code
0095       *
0096       * MyMoneyPrice price("ADF", "GBP", QDate(2005,9,20), MyMoneyMoney(1,3), "User");
0097       * MyMoneyMoney valADF, valGBP(100,1);
0098       *
0099       * valADF = valGBP * price.rate("ADF");
0100       *
0101       * @endcode
0102       *
0103       * valADF will contain the value 300 after the assignment operation, because @p price.rate("ADF") returned
0104       * @p 3/1 even though the price information kept with the object was @p 1/3, but based on the other
0105       * conversion direction (from ADF to GBP).
0106       */
0107     MyMoneyMoney rate(const QString& id) const;
0108 
0109     QDate date() const;
0110     QString source() const;
0111     QString from() const;
0112     QString to() const;
0113 
0114     /**
0115       * Check whether the object is valid or not. A MyMoneyPrice object
0116       * is valid if the date is valid and both security ids are set. In case
0117       * of an invalid object, price() always returns 1.
0118       *
0119       * @retval true if price object is valid
0120       * @retval false if price object is not valid
0121       */
0122     bool isValid() const;
0123 
0124     // Equality operator
0125     bool operator == (const MyMoneyPrice &) const;
0126 
0127     // Inequality operator
0128     bool operator != (const MyMoneyPrice &right) const;
0129 
0130     /**
0131       * This method checks if a reference to the given object exists. It returns,
0132       * a @p true if the object is referencing the one requested by the
0133       * parameter @p id. If it does not, this method returns @p false.
0134       *
0135       * @param id id of the object to be checked for references
0136       * @retval true This object references object with id @p id.
0137       * @retval false This object does not reference the object with id @p id.
0138       */
0139     bool hasReferenceTo(const QString& id) const;
0140 
0141     /**
0142      * @copydoc MyMoneyObject::referencedObjects
0143      */
0144     QSet<QString> referencedObjects() const;
0145 };
0146 
0147 inline void swap(MyMoneyPrice& first, MyMoneyPrice& second) // krazy:exclude=inline
0148 {
0149     using std::swap;
0150     swap(first.d_ptr, second.d_ptr);
0151 }
0152 
0153 inline MyMoneyPrice::MyMoneyPrice(MyMoneyPrice && other) : MyMoneyPrice() // krazy:exclude=inline
0154 {
0155     swap(*this, other);
0156 }
0157 
0158 inline MyMoneyPrice & MyMoneyPrice::operator=(MyMoneyPrice other) // krazy:exclude=inline
0159 {
0160     swap(*this, other);
0161     return *this;
0162 }
0163 
0164 typedef QPair<QString, QString> MyMoneySecurityPair;
0165 typedef QMap<QDate, MyMoneyPrice> MyMoneyPriceEntries;
0166 typedef QMap<MyMoneySecurityPair, MyMoneyPriceEntries> MyMoneyPriceList;
0167 
0168 /**
0169   * Make it possible to hold @ref MyMoneyPrice objects inside @ref QVariant objects.
0170   */
0171 Q_DECLARE_METATYPE(MyMoneyPrice)
0172 
0173 #endif