2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #ifndef GNASH_MOVIELIBRARY_H
20 #define GNASH_MOVIELIBRARY_H
23 #include "movie_definition.h"
25 #include <boost/intrusive_ptr.hpp>
32 /// Library of SWF movies indexed by URL strings
34 /// Elements are actually movie_definitions, the ones
35 /// associated with URLS. They may be BitmapMovieDefinitions or
36 /// SWFMovieDefinitions.
43 boost::intrusive_ptr
<movie_definition
> def
;
47 typedef std::map
<std::string
, LibraryItem
> LibraryContainer
;
53 RcInitFile
& rcfile
= RcInitFile::getDefaultInstance();
54 setLimit(rcfile
.getMovieLibraryLimit());
57 /// Sets the maximum number of items to hold in the library. When adding new
58 /// items, the one with the least hit count is being removed in that case.
59 /// Zero is a valid limit (disables library).
60 void setLimit(LibraryContainer::size_type limit
)
66 bool get(const std::string
& key
,
67 boost::intrusive_ptr
<movie_definition
>* ret
)
69 std::lock_guard
<std::mutex
> lock(_mapMutex
);
70 LibraryContainer::iterator it
= _map
.find(key
);
71 if (it
== _map
.end()) return false;
73 *ret
= it
->second
.def
;
74 it
->second
.hitCount
++;
78 void add(const std::string
& key
, movie_definition
* mov
)
83 if (_limit
) limitSize(_limit
- 1);
90 std::lock_guard
<std::mutex
> lock(_mapMutex
);
97 std::lock_guard
<std::mutex
> lock(_mapMutex
);
103 static bool findWorstHitCount(const LibraryContainer::value_type
& a
,
104 const LibraryContainer::value_type
& b
)
106 return (a
.second
.hitCount
< b
.second
.hitCount
);
109 LibraryContainer _map
;
112 void limitSize(LibraryContainer::size_type max
) {
119 while (_map
.size() > max
) {
120 std::lock_guard
<std::mutex
> lock(_mapMutex
);
121 _map
.erase(std::min_element(_map
.begin(), _map
.end(),
122 &findWorstHitCount
));
127 mutable std::mutex _mapMutex
;
139 // indent-tabs-mode: t