xbmc
PltMediaCache.h
Go to the documentation of this file.
1 /*****************************************************************
2 |
3 | Platinum - AV Media Cache
4 |
5 | Copyright (c) 2004-2010, Plutinosoft, LLC.
6 | All rights reserved.
7 | http://www.plutinosoft.com
8 |
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.
13 |
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
21 |
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.
26 |
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
32 |
33 ****************************************************************/
34 
39 #ifndef _PLT_MEDIA_CACHE_H_
40 #define _PLT_MEDIA_CACHE_H_
41 
42 /*----------------------------------------------------------------------
43 | includes
44 +---------------------------------------------------------------------*/
45 #include "Neptune.h"
46 
47 /*----------------------------------------------------------------------
48 | PLT_MediaCache
49 +---------------------------------------------------------------------*/
54 template <typename T, typename U>
56 {
57 public:
58  typedef typename NPT_Map<NPT_String,T>::Entry ElementEntry;
60 
62  virtual ~PLT_MediaCache<T,U>() {}
63 
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);
68 
69 private:
70  // methods
71  NPT_String GenerateKey(const char* root, const char* key);
72 
73 private:
74  // members
75  NPT_Mutex m_Mutex;
76  NPT_Map<NPT_String, T> m_Items;
78 };
79 
80 /*----------------------------------------------------------------------
81 | PLT_MediaCache::GenerateKey
82 +---------------------------------------------------------------------*/
83 template <typename T, typename U>
84 inline
86 PLT_MediaCache<T,U>::GenerateKey(const char* root, const char* key)
87 {
88  // TODO: There could be collision
89  NPT_String result = root;
90  result += "/";
91  result += key;
92  return result;
93 }
94 
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)
105 {
106  NPT_AutoLock lock(m_Mutex);
107 
108  NPT_String fullkey = GenerateKey(root, key);
109  if (fullkey.GetLength() == 0) return NPT_ERROR_INVALID_PARAMETERS;
110 
111  m_Items.Erase(fullkey);
112  NPT_CHECK(m_Items.Put(fullkey, value));
113 
114  if (tag) NPT_CHECK(m_Tags.Put(fullkey, *tag));
115 
116  return NPT_SUCCESS;
117 }
118 
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 */)
129 {
130  NPT_AutoLock lock(m_Mutex);
131 
132  NPT_String fullkey = GenerateKey(root, key);
133  if (fullkey.GetLength() == 0) return NPT_ERROR_INVALID_PARAMETERS;
134 
135  T* _value = NULL;
136  NPT_CHECK(m_Items.Get(fullkey, _value));
137 
138  U* _tag;
139  if (tag) {
140  m_Tags.Get(fullkey, _tag);
141  if (_tag) *tag = *_tag;
142  }
143 
144  value = *_value;
145  return NPT_SUCCESS;
146 }
147 
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)
155 {
156  NPT_AutoLock lock(m_Mutex);
157 
158  NPT_String fullkey = GenerateKey(root, key);
159  if (fullkey.GetLength() == 0) return NPT_ERROR_INVALID_PARAMETERS;
160 
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;
169  }
170  }
171 
172  return NPT_ERROR_NO_SUCH_ITEM;
173 }
174 
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)
182 {
183  NPT_AutoLock lock(m_Mutex);
184 
185  if (!root || root[0]=='\0')
186  return m_Items.Clear();
187 
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);
197  }
198  }
199 
200  return NPT_SUCCESS;
201 }
202 
203 #endif /* _PLT_MEDIA_CACHE_H_ */
The PLT_MediaCache template provides a way to hold references to object in memory.
Definition: PltMediaCache.h:55
Definition: NptThreads.h:76
Definition: NptThreads.h:93
Definition: NptMap.h:47
#define U(j)
uj for division
Definition: bigint.c:73
Definition: NptList.h:63
Definition: NptStrings.h:57