~vanvugt/ubuntu/oneiric/mediatomb/fix-770964-784431

« back to all changes in this revision

Viewing changes to src/dictionary.h

  • Committer: Bazaar Package Importer
  • Author(s): Andres Mejia
  • Date: 2008-02-02 01:42:48 UTC
  • Revision ID: james.westby@ubuntu.com-20080202014248-cjouolddb8gi2zkz
Tags: upstream-0.10.0.dfsg1
ImportĀ upstreamĀ versionĀ 0.10.0.dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*MT*
 
2
    
 
3
    MediaTomb - http://www.mediatomb.cc/
 
4
    
 
5
    dictionary.h - this file is part of MediaTomb.
 
6
    
 
7
    Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>,
 
8
                       Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>
 
9
    
 
10
    Copyright (C) 2006-2007 Gena Batyan <bgeradz@mediatomb.cc>,
 
11
                            Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
 
12
                            Leonhard Wimmer <leo@mediatomb.cc>
 
13
    
 
14
    MediaTomb is free software; you can redistribute it and/or modify
 
15
    it under the terms of the GNU General Public License version 2
 
16
    as published by the Free Software Foundation.
 
17
    
 
18
    MediaTomb is distributed in the hope that it will be useful,
 
19
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
20
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
21
    GNU General Public License for more details.
 
22
    
 
23
    You should have received a copy of the GNU General Public License
 
24
    version 2 along with MediaTomb; if not, write to the Free Software
 
25
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 
26
    
 
27
    $Id: dictionary.h 1294 2007-05-13 16:28:24Z lww $
 
28
*/
 
29
 
 
30
/// \file dictionary.h
 
31
/// \brief Definiton of the DictionaryElement and Dictionary classes.
 
32
#ifndef __DICTIONARY_H__
 
33
#define __DICTIONARY_H__
 
34
 
 
35
#include "zmmf/zmmf.h"
 
36
#include "sync.h"
 
37
 
 
38
/// \brief This class should never be used directly, it is being used by the Dictionary class.
 
39
class DictionaryElement : public zmm::Object
 
40
{
 
41
public:
 
42
    /// \brief Constructor, stores the key and the value.
 
43
    DictionaryElement(zmm::String key, zmm::String value);
 
44
 
 
45
    /// \brief Changes the name of the key.
 
46
    /// \param key new key name.
 
47
    void setKey(zmm::String key);
 
48
 
 
49
    /// \brief Changes the key value.
 
50
    /// \param value new value.
 
51
    void setValue(zmm::String value);
 
52
 
 
53
    /// \brief Returns the key for this DictionaryElement.
 
54
    zmm::String getKey();
 
55
 
 
56
    /// \brief Returns the value for this DictionaryElement.
 
57
    zmm::String getValue();
 
58
 
 
59
protected:
 
60
    zmm::String key;
 
61
    zmm::String value;
 
62
};
 
63
 
 
64
/// \brief This class stores key:value pairs of String data and provides functions to access them.
 
65
class Dictionary : public zmm::Object
 
66
{
 
67
protected:
 
68
    /// \brief Array of DictionaryElements, representing our Dictionary.
 
69
    zmm::Ref<zmm::Array<DictionaryElement> > elements;
 
70
public:
 
71
 
 
72
    /// \brief Constructor, initializes the dictionary.
 
73
    Dictionary();
 
74
 
 
75
    /// \brief Adds a new key:value pair to the dictionary.
 
76
    void put(zmm::String key, zmm::String value);
 
77
 
 
78
    /// \brief Returns the value for a given key.
 
79
    zmm::String get(zmm::String key);
 
80
 
 
81
    /// \brief Returns the number of elements in the dictinary.
 
82
    int size();
 
83
    
 
84
    /// \brief Deletes a key value pair
 
85
    void remove(zmm::String key);
 
86
    
 
87
    /// \brief Returns an url encoded version of the whole dictionary.
 
88
    zmm::String encode();
 
89
 
 
90
    /// \brief Removes all elements from the dictionary.
 
91
    void clear();
 
92
    
 
93
    /// \brief Makes a dictionary out of url encoded data.
 
94
    void decode(zmm::String url);
 
95
 
 
96
    /// \brief Makes a shallow copy of the dictionary
 
97
    zmm::Ref<Dictionary> clone();
 
98
 
 
99
    /// \brief returns true if the dictionary is a subset of another dictionary.#
 
100
    bool isSubsetOf(zmm::Ref<Dictionary> other);
 
101
 
 
102
    /// \brief checks two dictionaries for equality
 
103
    bool equals(zmm::Ref<Dictionary> other);
 
104
 
 
105
    zmm::Ref<zmm::Array<DictionaryElement> > getElements();
 
106
 
 
107
    /// \brief Frees unnecessary memory
 
108
    inline void optimize() { elements->optimize(); }
 
109
};
 
110
 
 
111
 
 
112
/// \brief Reentrant version of the dictionary
 
113
class Dictionary_r : public Dictionary
 
114
{
 
115
public:
 
116
    Dictionary_r() : Dictionary()
 
117
    {
 
118
        mutex = zmm::Ref<Mutex>(new Mutex(true));
 
119
    }
 
120
    
 
121
    inline void put(zmm::String key, zmm::String value)
 
122
    {
 
123
        mutex->lock();
 
124
        Dictionary::put(key, value);
 
125
        mutex->unlock();
 
126
    }
 
127
    
 
128
    inline zmm::String get(zmm::String key)
 
129
    {
 
130
        mutex->lock();
 
131
        zmm::String ret = Dictionary::get(key);
 
132
        mutex->unlock();
 
133
        return ret;
 
134
    }
 
135
    
 
136
    inline void remove(zmm::String key)
 
137
    {
 
138
        mutex->lock();
 
139
        Dictionary::remove(key);
 
140
        mutex->unlock();
 
141
    }
 
142
    
 
143
    inline zmm::String encode()
 
144
    {
 
145
        mutex->lock();
 
146
        zmm::String ret = Dictionary::encode();
 
147
        mutex->unlock();
 
148
        return ret;
 
149
    }
 
150
    
 
151
    inline void clear()
 
152
    {
 
153
        mutex->lock();
 
154
        Dictionary::clear();
 
155
        mutex->unlock();
 
156
    }
 
157
    
 
158
    inline void decode(zmm::String url)
 
159
    {
 
160
        mutex->lock();
 
161
        Dictionary::decode(url);
 
162
        mutex->unlock();
 
163
    }
 
164
    
 
165
    inline zmm::Ref<Dictionary_r> clone()
 
166
    {
 
167
        mutex->lock();
 
168
        zmm::Ref<Dictionary_r> ret = RefCast(Dictionary::clone(), Dictionary_r);
 
169
        mutex->unlock();
 
170
        return ret;
 
171
    }
 
172
    
 
173
    inline bool isSubsetOf(zmm::Ref<Dictionary> other)
 
174
    {
 
175
        mutex->lock();
 
176
        bool ret = Dictionary::isSubsetOf(other);
 
177
        mutex->unlock();
 
178
        return ret;
 
179
    }
 
180
    
 
181
    inline bool equals(zmm::Ref<Dictionary> other)
 
182
    {
 
183
        mutex->lock();
 
184
        bool ret = Dictionary::equals(other);
 
185
        mutex->unlock();
 
186
        return ret;
 
187
    }                                                                         
 
188
    
 
189
    inline zmm::Ref<zmm::Array<DictionaryElement> > getElements()
 
190
    {
 
191
        mutex->lock();
 
192
        zmm::Ref<zmm::Array<DictionaryElement> > ret = Dictionary::getElements();
 
193
        mutex->unlock();
 
194
        return ret;
 
195
    }
 
196
    
 
197
    inline void optimize()
 
198
    {
 
199
        mutex->lock();
 
200
        Dictionary::optimize();
 
201
        mutex->unlock();
 
202
    }
 
203
    
 
204
protected:
 
205
    zmm::Ref<Mutex> mutex;
 
206
};
 
207
 
 
208
#endif // __DICTIONARY_H__