-
Notifications
You must be signed in to change notification settings - Fork 1
/
htmlrc.h
175 lines (143 loc) · 5.24 KB
/
htmlrc.h
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* $Header: d:/cvsroot/tads/html/htmlrc.h,v 1.2 1999/05/17 02:52:22 MJRoberts Exp $ */
/*
* Copyright (c) 1997 by Michael J. Roberts. All Rights Reserved.
*
* Please see the accompanying license file, LICENSE.TXT, for information
* on using and copying this software.
*/
/*
Name
htmlrc.h - resource cache
Function
Implements a cache for resources, such as images and sounds.
Because these binary resources can be large objects, it's desirable
to re-use them whenever possible, rather than load a separate copy
of the resource for each use.
Resources are identified by URL's, and contain associated binary
data loaded from external files. The details of loading and using
the binary data associated with the resource are not part of the
cache's responsibilities; the cache merely provides the reference
tracking mechanism.
This cache:
- Allows a single resource to be referenced multiple times, and tracks
the references, so that the resource is deleted from memory when and
only when nothing is referencing it. (Of course, this requires
that the objects doing the referencing are cooperating by adding
and removing references when necessary, but apart from that,
client objects don't need to worry about the operations of the
cache.)
- Finds a resource that's already in memory, given an URL to the
resource.
Notes
Modified
10/25/97 MJRoberts - Creation
*/
#ifndef HTMLRC_H
#define HTMLRC_H
#ifndef HTMLURL_H
#include "htmlurl.h"
#endif
/* ------------------------------------------------------------------------ */
/*
* Resource cache object. The cache object implements referencing
* counting, and automatically deletes itself when its reference count
* drops to zero. The cache object owns the associated resource, and
* deletes the resource when the cache object is deleted.
*/
class CHtmlResCacheObject
{
public:
/* create a cache object */
CHtmlResCacheObject(const CHtmlUrl *url,
class CHtmlSysResource *resource,
class CHtmlResCache *cache)
{
/* remember the resource */
resource_ = resource;
/* remember the URL */
url_.set_url(url);
/* no references yet -- all references must be explicitly added */
refcnt_ = 0;
/* remember the cache */
cache_ = cache;
}
/* get the URL of the resource */
const textchar_t *get_url() const { return url_.get_url(); }
/* get the resource */
class CHtmlSysResource *get_resource() { return resource_; }
/*
* Get the resource as specific resource subtypes. These downcast
* routines use the typesafe downcast routines in the
* CHtmlSysResource interface, so these methods return null when
* called on inappropriate resource types.
*/
class CHtmlSysImage *get_image();
class CHtmlSysSound *get_sound();
/*
* Add/remove a reference. When the last reference is removed, the
* object is automatically deleted.
*/
void add_ref() { ++refcnt_; }
void remove_ref()
{
--refcnt_;
if (refcnt_ == 0)
delete this;
}
private:
/*
* the constructor is never called explicitly by a client; instead,
* the client simply removes its reference, and we'll automatically
* delete ourselves when our reference count drops to zero
*/
~CHtmlResCacheObject();
/* URL of the resource */
CHtmlUrl url_;
/* the resource object - contains the resource's binary data */
class CHtmlSysResource *resource_;
/* number of references */
long refcnt_;
/* cache containing the resource */
class CHtmlResCache *cache_;
};
/* ------------------------------------------------------------------------ */
/*
* Resource cache
*/
class CHtmlResCache
{
friend class CHtmlResCacheObject;
public:
CHtmlResCache();
~CHtmlResCache();
/*
* Add an object to the cache. Note that although the cache has a
* reference to the object, it is a "weak" reference, in that it
* doesn't add to the reference count for the object and the
* presence of this reference doesn't keep the object in memory.
* Instead, the cache object is responsible for notifying us if it's
* deleted, and we'll remove our reference to the object.
*/
void add(class CHtmlResCacheObject *obj);
/*
* Find an existing cache object matching an URL. Returns null if
* there is no entry in the cache matching the URL.
*/
class CHtmlResCacheObject *find(const CHtmlUrl *url);
/*
* Find an existing cache object matching an URL, or create a new
* one if one is not already in the cache.
*/
class CHtmlResCacheObject *find_or_create(class CHtmlSysWin *win,
class CHtmlResFinder *resfinder,
const CHtmlUrl *url);
private:
/*
* Remove an object from the cache. This should never be called
* except by the cache object itself as it's about to be deleted.
*/
void remove(class CHtmlResCacheObject *obj);
/* hash table containing the cache objects */
class CHtmlHashTable *hashtable_;
};
#endif /* HTMLRC_H */