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 #include <boost/thread/thread.hpp>
30 
31 namespace gnash {
32 
34 //
39 {
40 public:
41 
42  struct LibraryItem
43  {
44  boost::intrusive_ptr<movie_definition> def;
45  unsigned hitCount;
46  };
47 
48  typedef std::map<std::string, LibraryItem> LibraryContainer;
49 
51  :
52  _limit(8)
53  {
56  }
57 
61  void setLimit(LibraryContainer::size_type limit)
62  {
63  _limit = limit;
64  limitSize(_limit);
65  }
66 
67  bool get(const std::string& key,
68  boost::intrusive_ptr<movie_definition>* ret)
69  {
70  boost::mutex::scoped_lock lock(_mapMutex);
71  LibraryContainer::iterator it = _map.find(key);
72  if (it == _map.end()) return false;
73 
74  *ret = it->second.def;
75  it->second.hitCount++;
76  return true;
77  }
78 
79  void add(const std::string& key, movie_definition* mov)
80  {
81 
82  if (!_limit) return;
83 
84  if (_limit) limitSize(_limit - 1);
85 
86  LibraryItem temp;
87 
88  temp.def = mov;
89  temp.hitCount = 0;
90 
91  boost::mutex::scoped_lock lock(_mapMutex);
92  _map[key] = temp;
93  }
94 
95 
96  void clear()
97  {
98  boost::mutex::scoped_lock lock(_mapMutex);
99  _map.clear();
100  }
101 
102 private:
103 
104  static bool findWorstHitCount(const LibraryContainer::value_type& a,
105  const LibraryContainer::value_type& b)
106  {
107  return (a.second.hitCount < b.second.hitCount);
108  }
109 
110  LibraryContainer _map;
111  unsigned _limit;
112 
113  void limitSize(LibraryContainer::size_type max) {
114 
115  if (max < 1) {
116  clear();
117  return;
118  }
119 
120  while (_map.size() > max) {
121  boost::mutex::scoped_lock lock(_mapMutex);
122  _map.erase(std::min_element(_map.begin(), _map.end(),
123  &findWorstHitCount));
124  }
125 
126  }
127 
128  mutable boost::mutex _mapMutex;
129 
130 };
131 
132 }
133 #endif
134 
135 
136 // Local Variables:
137 // mode: C++
138 // c-basic-offset: 8
139 // tab-width: 8
140 // indent-tabs-mode: t
141 // End:
Definition: GnashKey.h:147
Client program's interface to the definition of a movie or sprite.
Definition: movie_definition.h:95
void setLimit(LibraryContainer::size_type limit)
Definition: MovieLibrary.h:61
Library of SWF movies indexed by URL strings.
Definition: MovieLibrary.h:38
unsigned hitCount
Definition: MovieLibrary.h:45
boost::intrusive_ptr< movie_definition > def
Definition: MovieLibrary.h:44
Definition: MovieLibrary.h:42
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:48
Definition: GnashKey.h:148
Definition: rc.h:43
int getMovieLibraryLimit() const
Definition: rc.h:95
void clear()
Definition: MovieLibrary.h:96
MovieLibrary()
Definition: MovieLibrary.h:50
void add(const std::string &key, movie_definition *mov)
Definition: MovieLibrary.h:79