~ubuntu-branches/ubuntu/saucy/gnash/saucy-proposed

« back to all changes in this revision

Viewing changes to libcore/MovieLibrary.h

  • Committer: Bazaar Package Importer
  • Author(s): Sindhudweep Narayan Sarkar
  • Date: 2009-10-07 00:06:10 UTC
  • mfrom: (1.1.12 upstream)
  • Revision ID: james.westby@ubuntu.com-20091007000610-mj9rwqe774gizn1j
Tags: 0.8.6-0ubuntu1
new upstream release 0.8.6 (LP: #435897)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
//   Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
 
3
// 
 
4
// This program is free software; you can redistribute it and/or modify
 
5
// it under the terms of the GNU General Public License as published by
 
6
// the Free Software Foundation; either version 3 of the License, or
 
7
// (at your option) any later version.
 
8
// 
 
9
// This program is distributed in the hope that it will be useful,
 
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
// GNU General Public License for more details.
 
13
// 
 
14
// You should have received a copy of the GNU General Public License
 
15
// along with this program; if not, write to the Free Software
 
16
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
17
 
 
18
#ifndef GNASH_MOVIELIBRARY_H
 
19
#define GNASH_MOVIELIBRARY_H
 
20
 
 
21
#include "rc.h"
 
22
#include "movie_definition.h"
 
23
 
 
24
#include <map>
 
25
#include <algorithm>
 
26
 
 
27
namespace gnash {
 
28
 
 
29
/// Library of SWF movies indexed by URL strings
 
30
//
 
31
/// Elements are actually movie_definitions, the ones
 
32
/// associated with URLS. They may be BitmapMovieDefinitions or
 
33
/// SWFMovieDefinitions.
 
34
class MovieLibrary
 
35
{
 
36
public:
 
37
 
 
38
    struct LibraryItem
 
39
    {
 
40
        boost::intrusive_ptr<movie_definition> def;
 
41
        unsigned hitCount;
 
42
    };
 
43
 
 
44
    typedef std::map<std::string, LibraryItem> LibraryContainer;
 
45
 
 
46
    MovieLibrary()
 
47
        : 
 
48
        _limit(8) 
 
49
    {
 
50
        RcInitFile& rcfile = RcInitFile::getDefaultInstance();
 
51
            setLimit(rcfile.getMovieLibraryLimit());
 
52
    }
 
53
  
 
54
    /// Sets the maximum number of items to hold in the library. When adding new
 
55
    /// items, the one with the least hit count is being removed in that case.
 
56
    /// Zero is a valid limit (disables library). 
 
57
    void setLimit(LibraryContainer::size_type limit)
 
58
    {
 
59
        _limit = limit;  
 
60
        limitSize(_limit);  
 
61
    }
 
62
 
 
63
    bool get(const std::string& key,
 
64
            boost::intrusive_ptr<movie_definition>* ret)
 
65
    {
 
66
        LibraryContainer::iterator it = _map.find(key);
 
67
        if (it == _map.end()) return false;
 
68
        
 
69
        *ret = it->second.def;
 
70
        it->second.hitCount++;
 
71
        return true;
 
72
    }
 
73
 
 
74
#ifdef GNASH_USE_GC
 
75
    /// Mark all library elements as reachable (for GC)
 
76
    void markReachableResources() const
 
77
    {
 
78
        for (LibraryContainer::const_iterator i=_map.begin(), e=_map.end();
 
79
                i!=e; ++i)
 
80
        {
 
81
            i->second.def->setReachable();
 
82
        }
 
83
    }
 
84
#endif
 
85
 
 
86
    void add(const std::string& key, movie_definition* mov)
 
87
    {
 
88
 
 
89
        if (!_limit) return;
 
90
 
 
91
        if (_limit) limitSize(_limit - 1);
 
92
 
 
93
        LibraryItem temp;
 
94
 
 
95
        temp.def = mov;
 
96
        temp.hitCount = 0;
 
97
 
 
98
        _map[key] = temp;
 
99
    }
 
100
  
 
101
 
 
102
    void clear() { _map.clear(); }
 
103
  
 
104
private:
 
105
 
 
106
    static bool findWorstHitCount(const LibraryContainer::value_type& a,
 
107
                                const LibraryContainer::value_type& b)
 
108
    {
 
109
        return (a.second.hitCount < b.second.hitCount);
 
110
    }
 
111
 
 
112
    LibraryContainer _map;
 
113
    unsigned _limit;
 
114
 
 
115
    void limitSize(LibraryContainer::size_type max) {
 
116
 
 
117
        if (max < 1) {
 
118
            clear();
 
119
            return;
 
120
        }
 
121
 
 
122
        while (_map.size() > max) {
 
123
            _map.erase(std::min_element(_map.begin(), _map.end(),
 
124
                        &findWorstHitCount));
 
125
        }
 
126
    
 
127
    }
 
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: