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

0001 /*
0002     SPDX-FileCopyrightText: 2002-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 MYMONEYKEYVALUECONTAINER_H
0008 #define MYMONEYKEYVALUECONTAINER_H
0009 
0010 #include "kmm_mymoney_export.h"
0011 
0012 /**
0013   * @author Thomas Baumgart
0014   */
0015 
0016 // ----------------------------------------------------------------------------
0017 // QT Includes
0018 
0019 #include <qglobal.h>
0020 
0021 // ----------------------------------------------------------------------------
0022 // Project Includes
0023 
0024 #include "mymoneyunittestable.h"
0025 
0026 class QString;
0027 
0028 template <class Key, class Value> class QMap;
0029 
0030 /**
0031   * This class implements a container for key/value pairs. This is used
0032   * to store an arbitrary number of attributes with any of the engine
0033   * object. The container can also be used to have attributes that are
0034   * attached to this object only for a limited time (e.g. between
0035   * start of reconciliation end it's end).
0036   *
0037   * To give any class the ability to have a key/value pair container,
0038   * just derive the class from this one. See MyMoneyAccount as an example.
0039   */
0040 
0041 class MyMoneyKeyValueContainerPrivate;
0042 class KMM_MYMONEY_EXPORT MyMoneyKeyValueContainer
0043 {
0044     Q_DECLARE_PRIVATE(MyMoneyKeyValueContainer)
0045     KMM_MYMONEY_UNIT_TESTABLE
0046 
0047 protected:
0048     MyMoneyKeyValueContainerPrivate * d_ptr;
0049 
0050 public:
0051     MyMoneyKeyValueContainer();
0052 
0053     MyMoneyKeyValueContainer(const MyMoneyKeyValueContainer & other);
0054     MyMoneyKeyValueContainer(MyMoneyKeyValueContainer && other);
0055     MyMoneyKeyValueContainer & operator=(MyMoneyKeyValueContainer other);
0056     friend void swap(MyMoneyKeyValueContainer& first, MyMoneyKeyValueContainer& second);
0057     virtual ~MyMoneyKeyValueContainer();
0058 
0059     /**
0060       * This method can be used to retrieve the value for a specific @p key.
0061       * If the key is unknown in this container, an empty string will be returned.
0062       *
0063       * @param key const reference to QString with the key to search for
0064       * @return reference to value of this key. If the key does not exist,
0065       *         an empty string is returned.
0066       */
0067     QString value(const QString& key) const;
0068 
0069     /**
0070       * This method is used to add a key/value pair to the container or
0071       * modify an existing pair.
0072       *
0073       * @param key const reference to QString with the key to search for
0074       * @param value const reference to QString with the value for this key
0075       */
0076     void setValue(const QString& key, const QString& value);
0077 
0078     /**
0079       * This method is used to remove an existing key/value pair from the
0080       * container. If the key does not exist, the container is not changed.
0081       *
0082       * @param key const reference to QString with the key to remove
0083       */
0084     void deletePair(const QString& key);
0085 
0086     /**
0087       * This method clears all pairs currently in the container.
0088       */
0089     void clear();
0090 
0091     /**
0092       * This method is used to retrieve the whole set of key/value pairs
0093       * from the container. It is meant to be used for permanent storage
0094       * functionality.
0095       *
0096       * @return QMap<QString, QString> containing all key/value pairs of
0097       *         this container.
0098       */
0099     QMap<QString, QString> pairs() const;
0100 
0101     /**
0102       * This method is used to initially store a set of key/value pairs
0103       * in the container. It is meant to be used for loading functionality
0104       * from permanent storage.
0105       *
0106       * @param list const QMap<QString, QString> containing the set of
0107       *             key/value pairs to be loaded into the container.
0108       *
0109       * @note All existing key/value pairs in the container will be deleted.
0110       */
0111     void setPairs(const QMap<QString, QString>& list);
0112 
0113     /**
0114       * This operator tests for equality of two MyMoneyKeyValueContainer objects
0115       */
0116     bool operator == (const MyMoneyKeyValueContainer &) const;
0117 
0118     QString operator[](const QString& k) const;
0119 
0120     QString& operator[](const QString& k);
0121 };
0122 
0123 inline void swap(MyMoneyKeyValueContainer& first, MyMoneyKeyValueContainer& second) // krazy:exclude=inline
0124 {
0125     using std::swap;
0126     swap(first.d_ptr, second.d_ptr);
0127 }
0128 
0129 inline MyMoneyKeyValueContainer::MyMoneyKeyValueContainer(MyMoneyKeyValueContainer && other) : MyMoneyKeyValueContainer() // krazy:exclude=inline
0130 {
0131     swap(*this, other);
0132 }
0133 
0134 inline MyMoneyKeyValueContainer & MyMoneyKeyValueContainer::operator=(MyMoneyKeyValueContainer other) // krazy:exclude=inline
0135 {
0136     swap(*this, other);
0137     return *this;
0138 }
0139 
0140 #endif