~ubuntu-branches/ubuntu/quantal/gnash/quantal-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// 
//   Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

#ifndef GNASH_MOVIELIBRARY_H
#define GNASH_MOVIELIBRARY_H

#include "rc.h"
#include "movie_definition.h"

#include <map>
#include <algorithm>

namespace gnash {

/// Library of SWF movies indexed by URL strings
//
/// Elements are actually movie_definitions, the ones
/// associated with URLS. They may be BitmapMovieDefinitions or
/// SWFMovieDefinitions.
class MovieLibrary
{
public:

    struct LibraryItem
    {
        boost::intrusive_ptr<movie_definition> def;
        unsigned hitCount;
    };

    typedef std::map<std::string, LibraryItem> LibraryContainer;

    MovieLibrary()
        : 
        _limit(8) 
    {
        RcInitFile& rcfile = RcInitFile::getDefaultInstance();
	    setLimit(rcfile.getMovieLibraryLimit());
    }
  
    /// Sets the maximum number of items to hold in the library. When adding new
    /// items, the one with the least hit count is being removed in that case.
    /// Zero is a valid limit (disables library). 
    void setLimit(LibraryContainer::size_type limit)
    {
        _limit = limit;  
        limitSize(_limit);  
    }

    bool get(const std::string& key,
            boost::intrusive_ptr<movie_definition>* ret)
    {
        LibraryContainer::iterator it = _map.find(key);
        if (it == _map.end()) return false;
        
        *ret = it->second.def;
        it->second.hitCount++;
        return true;
    }

#ifdef GNASH_USE_GC
    /// Mark all library elements as reachable (for GC)
    void markReachableResources() const
    {
        for (LibraryContainer::const_iterator i=_map.begin(), e=_map.end();
                i!=e; ++i)
        {
            i->second.def->setReachable();
        }
    }
#endif

    void add(const std::string& key, movie_definition* mov)
    {

        if (!_limit) return;

        if (_limit) limitSize(_limit - 1);

        LibraryItem temp;

        temp.def = mov;
        temp.hitCount = 0;

        _map[key] = temp;
    }
  

    void clear() { _map.clear(); }
  
private:

    static bool findWorstHitCount(const LibraryContainer::value_type& a,
                                const LibraryContainer::value_type& b)
    {
        return (a.second.hitCount < b.second.hitCount);
    }

    LibraryContainer _map;
    unsigned _limit;

    void limitSize(LibraryContainer::size_type max) {

        if (max < 1) {
            clear();
            return;
        }

        while (_map.size() > max) {
            _map.erase(std::min_element(_map.begin(), _map.end(),
                        &findWorstHitCount));
        }
    
    }
  
};

}
#endif 


// Local Variables:
// mode: C++
// c-basic-offset: 8 
// tab-width: 8
// indent-tabs-mode: t
// End: