~ubuntu-branches/ubuntu/natty/znc/natty-backports

« back to all changes in this revision

Viewing changes to Utils.h

  • Committer: Bazaar Package Importer
  • Author(s): Patrick Matthäi
  • Date: 2009-05-23 16:17:47 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20090523161747-dtgbsub23o1sl7ss
Tags: 0.070-1
* New upstream release.
  - Add new pkgconfig files to znc-dev.
* Fix typo in get-orig-source target.
* Merge 0.058-2~bpo40+1 and 0.058-2~bpo40+2 changelog.
* Add recommends on the new source package znc-extra.
* Add my own copyright for the Debian packaging.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
#include "ZNCString.h"
13
13
#include <assert.h>
14
14
#include <cstdio>
 
15
#include <fcntl.h>
15
16
#include <map>
16
17
#include <sys/file.h>
17
18
#include <sys/time.h>
18
19
#include <unistd.h>
19
20
#include <vector>
 
21
#include <iostream>
20
22
 
21
23
using std::map;
22
24
using std::vector;
 
25
using std::pair;
 
26
using std::cout;
 
27
using std::endl;
23
28
 
24
29
#define DEBUG(f) do { \
25
30
        if (CUtils::Debug()) { \
73
78
                return iTime;
74
79
        }
75
80
#ifdef HAVE_LIBSSL
76
 
        static void GenerateCert(FILE *pOut, bool bEncPrivKey = false, const CString& sHost = "");
 
81
        static void GenerateCert(FILE *pOut, const CString& sHost = "");
77
82
#endif /* HAVE_LIBSSL */
78
83
 
79
84
private:
164
169
 * @author prozac <prozac@rottenboy.com>
165
170
 * @brief Insert an object with a time-to-live and check later if it still exists
166
171
 */
167
 
template<typename T>
 
172
template<typename K, typename V = bool>
168
173
class TCacheMap {
169
174
public:
170
175
        TCacheMap(unsigned int uTTL = 5000) {
177
182
         * @brief This function adds an item to the cache using the default time-to-live value
178
183
         * @param Item the item to add to the cache
179
184
         */
180
 
        void AddItem(const T& Item) {
 
185
        void AddItem(const K& Item) {
181
186
                AddItem(Item, m_uTTL);
182
187
        }
183
188
 
186
191
         * @param Item the item to add to the cache
187
192
         * @param uTTL the time-to-live for this specific item
188
193
         */
189
 
        void AddItem(const T& Item, unsigned int uTTL) {
190
 
                if (!uTTL) {                    // If time-to-live is zero we don't want to waste our time adding it
191
 
                        RemItem(Item);          // Remove the item incase it already exists
192
 
                        return;
193
 
                }
194
 
 
195
 
                m_mItems[Item] = CUtils::GetMillTime() + uTTL;
 
194
        void AddItem(const K& Item, unsigned int uTTL) {
 
195
                if (!uTTL) {                    // If time-to-live is zero we don't want to waste our time adding it
 
196
                        RemItem(Item);          // Remove the item incase it already exists
 
197
                        return;
 
198
                }
 
199
 
 
200
                m_mItems[Item] = value(CUtils::GetMillTime() + uTTL, V());
 
201
                Cleanup();
 
202
        }
 
203
 
 
204
        /**
 
205
         * @brief This function adds an item to the cache using the default time-to-live value
 
206
         * @param Item the item to add to the cache
 
207
         * @param Val The value associated with the key Item
 
208
         */
 
209
        void AddItem(const K& Item, const V& Val) {
 
210
                AddItem(Item, Val, m_uTTL);
 
211
        }
 
212
 
 
213
        /**
 
214
         * @brief This function adds an item to the cache using a custom time-to-live value
 
215
         * @param Item the item to add to the cache
 
216
         * @param Val The value associated with the key Item
 
217
         * @param uTTL the time-to-live for this specific item
 
218
         */
 
219
        void AddItem(const K& Item, const V& Val, unsigned int uTTL) {
 
220
                if (!uTTL) {                    // If time-to-live is zero we don't want to waste our time adding it
 
221
                        RemItem(Item);          // Remove the item incase it already exists
 
222
                        return;
 
223
                }
 
224
 
 
225
                m_mItems[Item] = value(CUtils::GetMillTime() + uTTL, Val);
196
226
                Cleanup();
197
227
        }
198
228
 
201
231
         * @param Item The item to check for
202
232
         * @return true if item exists
203
233
         */
204
 
        bool HasItem(const T& Item) {
 
234
        bool HasItem(const K& Item) {
205
235
                Cleanup();
206
236
                return (m_mItems.find(Item) != m_mItems.end());
207
237
        }
208
238
 
209
239
        /**
 
240
         * @brief Performs a Cleanup() and returns a pointer to the object, or NULL
 
241
         * @param Item The item to check for
 
242
         * @return Pointer to the item or NULL if there is no suitable one
 
243
         */
 
244
        V* GetItem(const K& Item) {
 
245
                Cleanup();
 
246
                iterator it = m_mItems.find(Item);
 
247
                if (it == m_mItems.end())
 
248
                        return NULL;
 
249
                return &it->second.second;
 
250
        }
 
251
 
 
252
        /**
210
253
         * @brief Removes a specific item from the cache
211
254
         * @param Item The item to be removed
212
255
         * @return true if item existed and was removed, false if it never existed
213
256
         */
214
 
        bool RemItem(const T& Item) {
 
257
        bool RemItem(const K& Item) {
215
258
                return m_mItems.erase(Item);
216
259
        }
217
260
 
219
262
         * @brief Cycles through the queue removing all of the stale entries
220
263
         */
221
264
        void Cleanup() {
222
 
                typename map<T, unsigned long long>::iterator it = m_mItems.begin();
 
265
                iterator it = m_mItems.begin();
223
266
 
224
267
                while (it != m_mItems.end()) {
225
 
                        if (CUtils::GetMillTime() > (it->second)) {
 
268
                        if (CUtils::GetMillTime() > (it->second.first)) {
226
269
                                m_mItems.erase(it++);
227
270
                        } else {
228
271
                                ++it;
233
276
        // Setters
234
277
        void SetTTL(unsigned int u) { m_uTTL = u; }
235
278
        // !Setters
 
279
        // Getters
 
280
        unsigned int GetTTL() { return m_uTTL; }
 
281
        // !Getters
236
282
private:
237
 
        map<T, unsigned long long>      m_mItems;       //!< Map of cached items.  The value portion of the map is for the expire time
 
283
        typedef pair<unsigned long long, V> value;
 
284
        typedef typename map<K, value>::iterator iterator;
 
285
        map<K, value>   m_mItems;       //!< Map of cached items.  The value portion of the map is for the expire time
238
286
        unsigned int    m_uTTL;                                 //!< Default time-to-live duration
239
287
};
240
288