Gnash  0.8.11dev
MovieLibrary.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
4 //
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.
9 //
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.
14 //
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
18 
19 #ifndef GNASH_MOVIELIBRARY_H
20 #define GNASH_MOVIELIBRARY_H
21 
22 #include "rc.h"
23 #include "movie_definition.h"
24 
25 #include <boost/intrusive_ptr.hpp>
26 #include <string>
27 #include <map>
28 #include <algorithm>
29 
30 namespace gnash {
31 
33 //
38 {
39 public:
40 
41  struct LibraryItem
42  {
43  boost::intrusive_ptr<movie_definition> def;
44  unsigned hitCount;
45  };
46 
47  typedef std::map<std::string, LibraryItem> LibraryContainer;
48 
50  :
51  _limit(8)
52  {
55  }
56 
60  void setLimit(LibraryContainer::size_type limit)
61  {
62  _limit = limit;
63  limitSize(_limit);
64  }
65 
66  bool get(const std::string& key,
67  boost::intrusive_ptr<movie_definition>* ret)
68  {
69  std::lock_guard<std::mutex> lock(_mapMutex);
70  LibraryContainer::iterator it = _map.find(key);
71  if (it == _map.end()) return false;
72 
73  *ret = it->second.def;
74  it->second.hitCount++;
75  return true;
76  }
77 
78  void add(const std::string& key, movie_definition* mov)
79  {
80 
81  if (!_limit) return;
82 
83  if (_limit) limitSize(_limit - 1);
84 
85  LibraryItem temp;
86 
87  temp.def = mov;
88  temp.hitCount = 0;
89 
90  std::lock_guard<std::mutex> lock(_mapMutex);
91  _map[key] = temp;
92  }
93 
94 
95  void clear()
96  {
97  std::lock_guard<std::mutex> lock(_mapMutex);
98  _map.clear();
99  }
100 
101 private:
102 
103  static bool findWorstHitCount(const LibraryContainer::value_type& a,
104  const LibraryContainer::value_type& b)
105  {
106  return (a.second.hitCount < b.second.hitCount);
107  }
108 
109  LibraryContainer _map;
110  unsigned _limit;
111 
112  void limitSize(LibraryContainer::size_type max) {
113 
114  if (max < 1) {
115  clear();
116  return;
117  }
118 
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));
123  }
124 
125  }
126 
127  mutable std::mutex _mapMutex;
128 
129 };
130 
131 }
132 #endif
133 
134 
135 // Local Variables:
136 // mode: C++
137 // c-basic-offset: 8
138 // tab-width: 8
139 // indent-tabs-mode: t
140 // End:
Definition: GnashKey.h:147
Client program&#39;s interface to the definition of a movie or sprite.
Definition: movie_definition.h:95
void setLimit(LibraryContainer::size_type limit)
Definition: MovieLibrary.h:60
Library of SWF movies indexed by URL strings.
Definition: MovieLibrary.h:37
unsigned hitCount
Definition: MovieLibrary.h:44
boost::intrusive_ptr< movie_definition > def
Definition: MovieLibrary.h:43
Definition: MovieLibrary.h:41
Anonymous namespace for callbacks, local functions, event handlers etc.
Definition: dbus_ext.cpp:40
static RcInitFile & getDefaultInstance()
Return the default instance of RC file.
Definition: rc.cpp:61
std::map< std::string, LibraryItem > LibraryContainer
Definition: MovieLibrary.h:47
Definition: GnashKey.h:148
Definition: rc.h:43
void clear()
Definition: MovieLibrary.h:95
MovieLibrary()
Definition: MovieLibrary.h:49
void add(const std::string &key, movie_definition *mov)
Definition: MovieLibrary.h:78
int getMovieLibraryLimit() const
Definition: rc.h:95