1 /*****************************************************************
3 | Platinum - AV Media Cache
5 | Copyright (c) 2004-2010, Plutinosoft, LLC.
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 ****************************************************************/
36 Simple Object Caching utility.
39 #ifndef _PLT_MEDIA_CACHE_H_
40 #define _PLT_MEDIA_CACHE_H_
42 /*----------------------------------------------------------------------
44 +---------------------------------------------------------------------*/
47 /*----------------------------------------------------------------------
49 +---------------------------------------------------------------------*/
51 The PLT_MediaCache template provides a way to hold references to object in
54 template <typename T
, typename U
>
58 typedef typename NPT_Map
<NPT_String
,T
>::Entry ElementEntry
;
59 typedef typename NPT_List
<ElementEntry
*>::Iterator ElementIterator
;
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
);
71 NPT_String
GenerateKey(const char* root
, const char* key
);
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
>
86 PLT_MediaCache
<T
,U
>::GenerateKey(const char* root
, const char* key
)
88 // TODO: There could be collision
89 NPT_String result
= root
;
95 /*----------------------------------------------------------------------
97 +---------------------------------------------------------------------*/
98 template <typename T
, typename U
>
101 PLT_MediaCache
<T
,U
>::Put(const char* root
,
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
));
119 /*----------------------------------------------------------------------
120 | PLT_MediaCache::Get
121 +---------------------------------------------------------------------*/
122 template <typename T
, typename U
>
125 PLT_MediaCache
<T
,U
>::Get(const char* root
,
130 NPT_AutoLock
lock(m_Mutex
);
132 NPT_String fullkey
= GenerateKey(root
, key
);
133 if (fullkey
.GetLength() == 0) return NPT_ERROR_INVALID_PARAMETERS
;
136 NPT_CHECK(m_Items
.Get(fullkey
, _value
));
140 m_Tags
.Get(fullkey
, _tag
);
141 if (_tag
) *tag
= *_tag
;
148 /*----------------------------------------------------------------------
149 | PLT_MediaCache::Clear
150 +---------------------------------------------------------------------*/
151 template <typename T
, typename U
>
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
;
165 if ((*entry
)->GetKey() == (fullkey
)) {
166 m_Items
.Erase(fullkey
);
167 m_Tags
.Erase(fullkey
);
172 return NPT_ERROR_NO_SUCH_ITEM
;
175 /*----------------------------------------------------------------------
176 | PLT_MediaCache::Clear
177 +---------------------------------------------------------------------*/
178 template <typename T
, typename U
>
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
;
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
);
203 #endif /* _PLT_MEDIA_CACHE_H_ */