1 #ifndef _KVI_KVS_ARRAY_H_
2 #define _KVI_KVS_ARRAY_H_
3 //=============================================================================
4 //
5 //   File : KviKvsArray.h
6 //   Creation date : Tue 07 Oct 2003 01:07:31 by Szymon Stefanek
7 //
8 //   This file is part of the KVIrc IRC client distribution
9 //   Copyright (C) 2003-2010 Szymon Stefanek <pragma at kvirc dot net>
10 //
11 //   This program is FREE software. You can redistribute it and/or
12 //   modify it under the terms of the GNU General Public License
13 //   as published by the Free Software Foundation; either version 2
14 //   of the License, or (at your option) any later version.
15 //
16 //   This program is distributed in the HOPE that it will be USEFUL,
17 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
18 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 //   See the GNU General Public License for more details.
20 //
21 //   You should have received a copy of the GNU General Public License
22 //   along with this program. If not, write to the Free Software Foundation,
23 //   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 //
25 //=============================================================================
26 
27 /**
28 * \file KviKvsArray.h
29 * \author Szymon Stefanek
30 * \brief Handling of array data type in KVS
31 */
32 
33 #include "kvi_settings.h"
34 #include "KviKvsVariant.h"
35 #include "KviHeapObject.h"
36 
37 /**
38 * \class KviKvsArray
39 * \brief This class defines a new data type which contains array data
40 * \warning This class must not have virtual functions nor destructor. Otherwise it will happily
41 * crash on windows when it is allocated in modules and destroyed anywhere else around
42 */
43 class KVIRC_API KviKvsArray : public KviHeapObject
44 {
45 public:
46 	/**
47 	* \brief Constructs the array data
48 	* \return KviKvsArray
49 	*/
50 	KviKvsArray();
51 
52 	/**
53 	* \brief Constructs the array data
54 	*
55 	* This is a carbon copy
56 	* \param array The array to copy from
57 	* \return KviKvsArray
58 	*/
59 	KviKvsArray(const KviKvsArray & array);
60 
61 	/**
62 	* \brief Destroys the array data
63 	*/
64 	~KviKvsArray();
65 
66 protected:
67 	KviKvsVariant ** m_pData;
68 	kvs_uint_t m_uSize;
69 	kvs_uint_t m_uAllocSize;
70 
71 public:
72 	/**
73 	* \brief Unsets an element from the array
74 	* \param uIdx The index of the element to unset
75 	* \return void
76 	*/
77 	void unset(kvs_uint_t uIdx);
78 
79 	/**
80 	* \brief Sets an element into the array at the given index
81 	* \param uIdx The index of the element to set
82 	* \param pVal The value to set
83 	* \return void
84 	*/
85 	void set(kvs_uint_t uIdx, KviKvsVariant * pVal);
86 
87 	/**
88 	* Appends a variant to this array.
89 	*/
90 	void append(KviKvsVariant * pVal);
91 
92 	/**
93 	* \brief Returns the element at the given index
94 	* \param uIdx The index of the element to retrieve
95 	* \return KviKvsVariant *
96 	*/
at(kvs_uint_t uIdx)97 	KviKvsVariant * at(kvs_uint_t uIdx) const { return (uIdx < m_uSize) ? m_pData[uIdx] : 0; };
98 
99 	/**
100 	* \brief Returns the element at the given index
101 	*
102 	* If the element doesn't exists, it returns an empty element. If the index is out of
103 	* array bounds, it increases the array size, fillin the array in with zeros.
104 	* \param uIdx The index of the element to retrieve
105 	* \return KviKvsVariant *
106 	*/
107 	KviKvsVariant * getAt(kvs_uint_t uIdx);
108 
109 	/**
110 	* \brief Returns true if the array is empty
111 	* \return bool
112 	*/
isEmpty()113 	bool isEmpty() { return m_uSize == 0; };
114 
115 	/**
116 	* \brief Returns the size of the array
117 	* \return kvs_uint_t
118 	*/
size()119 	kvs_uint_t size() { return m_uSize; };
120 
121 	/**
122 	* \brief Appends data to the array converting it into a string
123 	* \param szBuffer The string to append
124 	* \return void
125 	*/
126 	void appendAsString(QString & szBuffer);
127 
128 	/**
129 	* \brief Serializes the array to a given buffer
130 	* \param szResult The buffer to store
131 	* \return void
132 	*/
133 	void serialize(QString & szResult);
134 
135 	/**
136 	* \brief Sorts the array
137 	* \return void
138 	*/
139 	void sort();
140 
141 	/**
142 	* \brief Sorts the array in reverse order
143 	* \return void
144 	*/
145 	void rsort();
146 
147 protected:
148 	/**
149 	* \brief Finds the new size of the array
150 	* \return void
151 	*/
152 	void findNewSize();
153 
154 private:
155 	/**
156 	* \brief Compares two elements of the array
157 	* \param pV1 The first element to compare
158 	* \param pV2 The second element to compare
159 	* \return int
160 	*/
161 	static int compare(const void * pV1, const void * pV2);
162 
163 	/**
164 	* \brief Compares two elements of the array in reverse order
165 	* \param pV1 The first element to compare
166 	* \param pV2 The second element to compare
167 	* \return int
168 	*/
169 	static int compareReverse(const void * pV1, const void * pV2);
170 };
171 
172 #endif // _KVI_KVS_ARRAY_H_
173