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

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 <QString>
0020 #include <qglobal.h>
0021 
0022 // ----------------------------------------------------------------------------
0023 // Project Includes
0024 
0025 #include "mymoneyunittestable.h"
0026 
0027 class QString;
0028 
0029 template <class Key, class Value> class QMap;
0030 
0031 /**
0032   * This class implements a container for key/value pairs. This is used
0033   * to store an arbitrary number of attributes with any of the engine
0034   * object. The container can also be used to have attributes that are
0035   * attached to this object only for a limited time (e.g. between
0036   * start of reconciliation end it's end).
0037   *
0038   * To give any class the ability to have a key/value pair container,
0039   * just derive the class from this one. See MyMoneyAccount as an example.
0040   */
0041 
0042 class MyMoneyKeyValueContainerPrivate;
0043 class KMM_MYMONEY_EXPORT MyMoneyKeyValueContainer
0044 {
0045     Q_DECLARE_PRIVATE(MyMoneyKeyValueContainer)
0046     KMM_MYMONEY_UNIT_TESTABLE
0047 
0048 protected:
0049     MyMoneyKeyValueContainerPrivate * d_ptr;
0050 
0051 public:
0052     MyMoneyKeyValueContainer();
0053 
0054     MyMoneyKeyValueContainer(const MyMoneyKeyValueContainer & other);
0055     MyMoneyKeyValueContainer(MyMoneyKeyValueContainer && other);
0056     MyMoneyKeyValueContainer & operator=(MyMoneyKeyValueContainer other);
0057     friend void swap(MyMoneyKeyValueContainer& first, MyMoneyKeyValueContainer& second);
0058     virtual ~MyMoneyKeyValueContainer();
0059 
0060     /**
0061      * This method can be used to retrieve the value for a specific @p key.
0062      * If the key is unknown in this container, an empty string will be returned.
0063      *
0064      * @param key const reference to QString with the key to search for
0065      * @param defaultValue const reference to the default value
0066      * @return value of this key. If the key does not exist,
0067      *         the @a defaultValue is returned.
0068      */
0069     QString value(const QString& key, const QString& defaultValue) const;
0070 
0071     /**
0072      * This method can be used to retrieve the value for a specific @p key.
0073      * If the key is unknown in this container, an empty string will be returned.
0074      *
0075      * @param key const reference to QString with the key to search for
0076      * @return value of this key. If the key does not exist,
0077      *         an empty string is returned.
0078      *
0079      * This is a convenience function for value(key, defaultValue)
0080      */
0081     QString value(const QString& key) const;
0082 
0083     /**
0084      * convenience method for type @c bool
0085      */
0086     bool value(const QString& key, bool defaultValue) const;
0087 
0088     /**
0089      * convenience method for type @c int
0090      */
0091     int value(const QString& key, int defaultValue) const;
0092 
0093     /**
0094      * This method is used to add a key/value pair to the container or
0095      * modify an existing pair.
0096      *
0097      * @param key const reference to QString with the key to search for
0098      * @param value const reference to QString with the value for this key
0099      * @param defaultValue when value has this defaultValue, the entry is removed
0100      */
0101     void setValue(const QString& key, const QString& value, const QString& defaultValue = QString());
0102 
0103     /**
0104      * Convenience method for setValue(const QString& key, const QString& value, const QString& defaultValue)
0105      */
0106     void setValue(const QString& key, int value, int defaultValue);
0107 
0108     /**
0109      * Convenience method for setValue(const QString& key, const QString& value, const QString& defaultValue)
0110      */
0111     void setValue(const QString& key, const char* value);
0112 
0113     /**
0114      * This method is used to add a key/value pair representing a boolean
0115      * value. If the @a value is equal to @a defaultValue the entry is
0116      * removed, otherwise it will be set to "yes" for @c true or "no"
0117      * for @c false.
0118      *
0119      * @param key const reference to QString with the key to search for
0120      * @param value the value for this key
0121      * @param defaultValue when value has this defaultValue, the entry is removed
0122      */
0123     void setValue(const QString& key, bool value, bool defaultValue);
0124 
0125     /**
0126       * This method is used to remove an existing key/value pair from the
0127       * container. If the key does not exist, the container is not changed.
0128       *
0129       * @param key const reference to QString with the key to remove
0130       */
0131     void deletePair(const QString& key);
0132 
0133     /**
0134       * This method clears all pairs currently in the container.
0135       */
0136     void clear();
0137 
0138     /**
0139       * This method is used to retrieve the whole set of key/value pairs
0140       * from the container. It is meant to be used for permanent storage
0141       * functionality.
0142       *
0143       * @return QMap<QString, QString> containing all key/value pairs of
0144       *         this container.
0145       */
0146     QMap<QString, QString> pairs() const;
0147 
0148     /**
0149       * This method is used to initially store a set of key/value pairs
0150       * in the container. It is meant to be used for loading functionality
0151       * from permanent storage.
0152       *
0153       * @param list const QMap<QString, QString> containing the set of
0154       *             key/value pairs to be loaded into the container.
0155       *
0156       * @note All existing key/value pairs in the container will be deleted.
0157       */
0158     void setPairs(const QMap<QString, QString>& list);
0159 
0160     /**
0161       * This operator tests for equality of two MyMoneyKeyValueContainer objects
0162       */
0163     bool operator == (const MyMoneyKeyValueContainer &) const;
0164 
0165     QString operator[](const QString& k) const;
0166 
0167     QString& operator[](const QString& k);
0168 };
0169 
0170 inline void swap(MyMoneyKeyValueContainer& first, MyMoneyKeyValueContainer& second) // krazy:exclude=inline
0171 {
0172     using std::swap;
0173     swap(first.d_ptr, second.d_ptr);
0174 }
0175 
0176 inline MyMoneyKeyValueContainer::MyMoneyKeyValueContainer(MyMoneyKeyValueContainer && other) : MyMoneyKeyValueContainer() // krazy:exclude=inline
0177 {
0178     swap(*this, other);
0179 }
0180 
0181 inline MyMoneyKeyValueContainer & MyMoneyKeyValueContainer::operator=(MyMoneyKeyValueContainer other) // krazy:exclude=inline
0182 {
0183     swap(*this, other);
0184     return *this;
0185 }
0186 
0187 #endif