Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef GNASH_MOVIELIBRARY_H
00020 #define GNASH_MOVIELIBRARY_H
00021
00022 #include "rc.h"
00023 #include "movie_definition.h"
00024
00025 #include <boost/intrusive_ptr.hpp>
00026 #include <string>
00027 #include <map>
00028 #include <algorithm>
00029 #include <boost/thread/thread.hpp>
00030
00031 namespace gnash {
00032
00034
00038 class MovieLibrary
00039 {
00040 public:
00041
00042 struct LibraryItem
00043 {
00044 boost::intrusive_ptr<movie_definition> def;
00045 unsigned hitCount;
00046 };
00047
00048 typedef std::map<std::string, LibraryItem> LibraryContainer;
00049
00050 MovieLibrary()
00051 :
00052 _limit(8)
00053 {
00054 RcInitFile& rcfile = RcInitFile::getDefaultInstance();
00055 setLimit(rcfile.getMovieLibraryLimit());
00056 }
00057
00061 void setLimit(LibraryContainer::size_type limit)
00062 {
00063 _limit = limit;
00064 limitSize(_limit);
00065 }
00066
00067 bool get(const std::string& key,
00068 boost::intrusive_ptr<movie_definition>* ret)
00069 {
00070 boost::mutex::scoped_lock lock(_mapMutex);
00071 LibraryContainer::iterator it = _map.find(key);
00072 if (it == _map.end()) return false;
00073
00074 *ret = it->second.def;
00075 it->second.hitCount++;
00076 return true;
00077 }
00078
00079 void add(const std::string& key, movie_definition* mov)
00080 {
00081
00082 if (!_limit) return;
00083
00084 if (_limit) limitSize(_limit - 1);
00085
00086 LibraryItem temp;
00087
00088 temp.def = mov;
00089 temp.hitCount = 0;
00090
00091 boost::mutex::scoped_lock lock(_mapMutex);
00092 _map[key] = temp;
00093 }
00094
00095
00096 void clear()
00097 {
00098 boost::mutex::scoped_lock lock(_mapMutex);
00099 _map.clear();
00100 }
00101
00102 private:
00103
00104 static bool findWorstHitCount(const LibraryContainer::value_type& a,
00105 const LibraryContainer::value_type& b)
00106 {
00107 return (a.second.hitCount < b.second.hitCount);
00108 }
00109
00110 LibraryContainer _map;
00111 unsigned _limit;
00112
00113 void limitSize(LibraryContainer::size_type max) {
00114
00115 if (max < 1) {
00116 clear();
00117 return;
00118 }
00119
00120 while (_map.size() > max) {
00121 boost::mutex::scoped_lock lock(_mapMutex);
00122 _map.erase(std::min_element(_map.begin(), _map.end(),
00123 &findWorstHitCount));
00124 }
00125
00126 }
00127
00128 mutable boost::mutex _mapMutex;
00129
00130 };
00131
00132 }
00133 #endif
00134
00135
00136
00137
00138
00139
00140
00141