Gnash
0.8.10
|
00001 // 00002 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 00003 // Free Software Foundation, Inc 00004 // 00005 // This program is free software; you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation; either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // This program is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with this program; if not, write to the Free Software 00017 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 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 // Local Variables: 00137 // mode: C++ 00138 // c-basic-offset: 8 00139 // tab-width: 8 00140 // indent-tabs-mode: t 00141 // End: