Merge pull request #26220 from 78andyp/blurayfixes
[xbmc.git] / lib / libUPnP / Platinum / Source / Devices / MediaServer / PltMediaCache.h
blob352e4bec30ffd4e5fff0af1dcc96f5cb5a31f94c
1 /*****************************************************************
3 | Platinum - AV Media Cache
5 | Copyright (c) 2004-2010, Plutinosoft, LLC.
6 | All rights reserved.
7 | http://www.plutinosoft.com
9 | This program is free software; you can redistribute it and/or
10 | modify it under the terms of the GNU General Public License
11 | as published by the Free Software Foundation; either version 2
12 | of the License, or (at your option) any later version.
14 | OEMs, ISVs, VARs and other distributors that combine and
15 | distribute commercially licensed software with Platinum software
16 | and do not wish to distribute the source code for the commercially
17 | licensed software under version 2, or (at your option) any later
18 | version, of the GNU General Public License (the "GPL") must enter
19 | into a commercial license agreement with Plutinosoft, LLC.
20 | licensing@plutinosoft.com
22 | This program is distributed in the hope that it will be useful,
23 | but WITHOUT ANY WARRANTY; without even the implied warranty of
24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 | GNU General Public License for more details.
27 | You should have received a copy of the GNU General Public License
28 | along with this program; see the file LICENSE.txt. If not, write to
29 | the Free Software Foundation, Inc.,
30 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 | http://www.gnu.org/licenses/gpl-2.0.html
33 ****************************************************************/
35 /** @file
36 Simple Object Caching utility.
39 #ifndef _PLT_MEDIA_CACHE_H_
40 #define _PLT_MEDIA_CACHE_H_
42 /*----------------------------------------------------------------------
43 | includes
44 +---------------------------------------------------------------------*/
45 #include "Neptune.h"
47 /*----------------------------------------------------------------------
48 | PLT_MediaCache
49 +---------------------------------------------------------------------*/
50 /**
51 The PLT_MediaCache template provides a way to hold references to object in
52 memory.
53 */
54 template <typename T, typename U>
55 class PLT_MediaCache
57 public:
58 typedef typename NPT_Map<NPT_String,T>::Entry ElementEntry;
59 typedef typename NPT_List<ElementEntry*>::Iterator ElementIterator;
61 PLT_MediaCache() {}
62 virtual ~PLT_MediaCache() {}
64 NPT_Result Put(const char* root, const char* key, T& value, U* tag = NULL);
65 NPT_Result Get(const char* root, const char* key, T& value, U* tag = NULL);
66 NPT_Result Clear(const char* root, const char* key);
67 NPT_Result Clear(const char* root = NULL);
69 private:
70 // methods
71 NPT_String GenerateKey(const char* root, const char* key);
73 private:
74 // members
75 NPT_Mutex m_Mutex;
76 NPT_Map<NPT_String, T> m_Items;
77 NPT_Map<NPT_String, U> m_Tags;
80 /*----------------------------------------------------------------------
81 | PLT_MediaCache::GenerateKey
82 +---------------------------------------------------------------------*/
83 template <typename T, typename U>
84 inline
85 NPT_String
86 PLT_MediaCache<T,U>::GenerateKey(const char* root, const char* key)
88 // TODO: There could be collision
89 NPT_String result = root;
90 result += "/";
91 result += key;
92 return result;
95 /*----------------------------------------------------------------------
96 | PLT_MediaCache::Put
97 +---------------------------------------------------------------------*/
98 template <typename T, typename U>
99 inline
100 NPT_Result
101 PLT_MediaCache<T,U>::Put(const char* root,
102 const char* key,
103 T& value,
104 U* tag)
106 NPT_AutoLock lock(m_Mutex);
108 NPT_String fullkey = GenerateKey(root, key);
109 if (fullkey.GetLength() == 0) return NPT_ERROR_INVALID_PARAMETERS;
111 m_Items.Erase(fullkey);
112 NPT_CHECK(m_Items.Put(fullkey, value));
114 if (tag) NPT_CHECK(m_Tags.Put(fullkey, *tag));
116 return NPT_SUCCESS;
119 /*----------------------------------------------------------------------
120 | PLT_MediaCache::Get
121 +---------------------------------------------------------------------*/
122 template <typename T, typename U>
123 inline
124 NPT_Result
125 PLT_MediaCache<T,U>::Get(const char* root,
126 const char* key,
127 T& value,
128 U* tag /* = NULL */)
130 NPT_AutoLock lock(m_Mutex);
132 NPT_String fullkey = GenerateKey(root, key);
133 if (fullkey.GetLength() == 0) return NPT_ERROR_INVALID_PARAMETERS;
135 T* _value = NULL;
136 NPT_CHECK(m_Items.Get(fullkey, _value));
138 U* _tag;
139 if (tag) {
140 m_Tags.Get(fullkey, _tag);
141 if (_tag) *tag = *_tag;
144 value = *_value;
145 return NPT_SUCCESS;
148 /*----------------------------------------------------------------------
149 | PLT_MediaCache::Clear
150 +---------------------------------------------------------------------*/
151 template <typename T, typename U>
152 inline
153 NPT_Result
154 PLT_MediaCache<T,U>::Clear(const char* root, const char* key)
156 NPT_AutoLock lock(m_Mutex);
158 NPT_String fullkey = GenerateKey(root, key);
159 if (fullkey.GetLength() == 0) return NPT_ERROR_INVALID_PARAMETERS;
161 ElementIterator entries = m_Items.GetEntries().GetFirstItem();
162 ElementIterator entry;
163 while (entries) {
164 entry = entries++;
165 if ((*entry)->GetKey() == (fullkey)) {
166 m_Items.Erase(fullkey);
167 m_Tags.Erase(fullkey);
168 return NPT_SUCCESS;
172 return NPT_ERROR_NO_SUCH_ITEM;
175 /*----------------------------------------------------------------------
176 | PLT_MediaCache::Clear
177 +---------------------------------------------------------------------*/
178 template <typename T, typename U>
179 inline
180 NPT_Result
181 PLT_MediaCache<T,U>::Clear(const char* root)
183 NPT_AutoLock lock(m_Mutex);
185 if (!root || root[0]=='\0')
186 return m_Items.Clear();
188 NPT_String key = GenerateKey(root, "");
189 ElementIterator entries = m_Items.GetEntries().GetFirstItem();
190 ElementIterator entry;
191 while (entries) {
192 entry = entries++;
193 NPT_String entry_key = (*entry)->GetKey();
194 if (entry_key.StartsWith(key)) {
195 m_Items.Erase(entry_key);
196 m_Tags.Erase(entry_key);
200 return NPT_SUCCESS;
203 #endif /* _PLT_MEDIA_CACHE_H_ */