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

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