1 /*
2 * Copyright 2002-2011 Thomas Baumgart <tbaumgart@kde.org>
3 * Copyright 2017-2018 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef MYMONEYKEYVALUECONTAINER_H
20 #define MYMONEYKEYVALUECONTAINER_H
21
22 #include "kmm_mymoney_export.h"
23
24 /**
25 * @author Thomas Baumgart
26 */
27
28 // ----------------------------------------------------------------------------
29 // QT Includes
30
31 #include <qglobal.h>
32
33 // ----------------------------------------------------------------------------
34 // Project Includes
35
36 #include "mymoneyunittestable.h"
37
38 class QString;
39
40 template <class Key, class Value> class QMap;
41
42 /**
43 * This class implements a container for key/value pairs. This is used
44 * to store an arbitrary number of attributes with any of the engine
45 * object. The container can also be used to have attributes that are
46 * attached to this object only for a limited time (e.g. between
47 * start of reconciliation end it's end).
48 *
49 * To give any class the ability to have a key/value pair container,
50 * just derive the class from this one. See MyMoneyAccount as an example.
51 */
52
53 class MyMoneyKeyValueContainerPrivate;
54 class KMM_MYMONEY_EXPORT MyMoneyKeyValueContainer
55 {
56 Q_DECLARE_PRIVATE(MyMoneyKeyValueContainer)
57 KMM_MYMONEY_UNIT_TESTABLE
58
59 protected:
60 MyMoneyKeyValueContainerPrivate * d_ptr;
61
62 public:
63 MyMoneyKeyValueContainer();
64
65 MyMoneyKeyValueContainer(const MyMoneyKeyValueContainer & other);
66 MyMoneyKeyValueContainer(MyMoneyKeyValueContainer && other);
67 MyMoneyKeyValueContainer & operator=(MyMoneyKeyValueContainer other);
68 friend void swap(MyMoneyKeyValueContainer& first, MyMoneyKeyValueContainer& second);
69 virtual ~MyMoneyKeyValueContainer();
70
71 /**
72 * This method can be used to retrieve the value for a specific @p key.
73 * If the key is unknown in this container, an empty string will be returned.
74 *
75 * @param key const reference to QString with the key to search for
76 * @return reference to value of this key. If the key does not exist,
77 * an empty string is returned.
78 */
79 QString value(const QString& key) const;
80
81 /**
82 * This method is used to add a key/value pair to the container or
83 * modify an existing pair.
84 *
85 * @param key const reference to QString with the key to search for
86 * @param value const reference to QString with the value for this key
87 */
88 void setValue(const QString& key, const QString& value);
89
90 /**
91 * This method is used to remove an existing key/value pair from the
92 * container. If the key does not exist, the container is not changed.
93 *
94 * @param key const reference to QString with the key to remove
95 */
96 void deletePair(const QString& key);
97
98 /**
99 * This method clears all pairs currently in the container.
100 */
101 void clear();
102
103 /**
104 * This method is used to retrieve the whole set of key/value pairs
105 * from the container. It is meant to be used for permanent storage
106 * functionality.
107 *
108 * @return QMap<QString, QString> containing all key/value pairs of
109 * this container.
110 */
111 QMap<QString, QString> pairs() const;
112
113 /**
114 * This method is used to initially store a set of key/value pairs
115 * in the container. It is meant to be used for loading functionality
116 * from permanent storage.
117 *
118 * @param list const QMap<QString, QString> containing the set of
119 * key/value pairs to be loaded into the container.
120 *
121 * @note All existing key/value pairs in the container will be deleted.
122 */
123 void setPairs(const QMap<QString, QString>& list);
124
125 /**
126 * This operator tests for equality of two MyMoneyKeyValueContainer objects
127 */
128 bool operator == (const MyMoneyKeyValueContainer &) const;
129
130 QString operator[](const QString& k) const;
131
132 QString& operator[](const QString& k);
133 };
134
swap(MyMoneyKeyValueContainer & first,MyMoneyKeyValueContainer & second)135 inline void swap(MyMoneyKeyValueContainer& first, MyMoneyKeyValueContainer& second) // krazy:exclude=inline
136 {
137 using std::swap;
138 swap(first.d_ptr, second.d_ptr);
139 }
140
MyMoneyKeyValueContainer(MyMoneyKeyValueContainer && other)141 inline MyMoneyKeyValueContainer::MyMoneyKeyValueContainer(MyMoneyKeyValueContainer && other) : MyMoneyKeyValueContainer() // krazy:exclude=inline
142 {
143 swap(*this, other);
144 }
145
146 inline MyMoneyKeyValueContainer & MyMoneyKeyValueContainer::operator=(MyMoneyKeyValueContainer other) // krazy:exclude=inline
147 {
148 swap(*this, other);
149 return *this;
150 }
151
152 #endif
153