9
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2004, 2006 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.resource.AbstractResourceManager;
|
|
14
|
|
15 import dwtx.jface.resource.ResourceManager;
|
|
16 import dwtx.jface.resource.DeviceResourceDescriptor;
|
|
17
|
|
18 import tango.util.collection.HashMap;
|
|
19 import tango.util.collection.model.Map;
|
|
20
|
|
21 import dwt.dwthelper.utils;
|
|
22
|
|
23 /**
|
|
24 * Abstract implementation of ResourceManager. Maintains reference counts for all previously
|
|
25 * allocated DWT resources. Delegates to the abstract method allocate(...) the first time a resource
|
|
26 * is referenced and delegates to the abstract method deallocate(...) the last time a reference is
|
|
27 * removed.
|
|
28 *
|
|
29 * @since 3.1
|
|
30 */
|
|
31 abstract class AbstractResourceManager : ResourceManager {
|
|
32
|
|
33 /**
|
|
34 * Map of ResourceDescriptor onto RefCount. (null when empty)
|
|
35 */
|
|
36 private HashMap!(DeviceResourceDescriptor,RefCount) map = null;
|
|
37
|
|
38 /**
|
|
39 * Holds a reference count for a previously-allocated resource
|
|
40 */
|
|
41 private static class RefCount {
|
|
42 Object resource;
|
|
43 int count = 1;
|
|
44
|
|
45 this(Object resource) {
|
|
46 this.resource = resource;
|
|
47 }
|
|
48 }
|
|
49
|
|
50 /**
|
|
51 * Called the first time a resource is requested. Should allocate and return a resource
|
|
52 * of the correct type.
|
|
53 *
|
|
54 * @since 3.1
|
|
55 *
|
|
56 * @param descriptor identifier for the resource to allocate
|
|
57 * @return the newly allocated resource
|
|
58 * @throws DeviceResourceException Thrown when allocation of an DWT device resource fails
|
|
59 */
|
|
60 protected abstract Object allocate(DeviceResourceDescriptor descriptor);
|
|
61
|
|
62 /**
|
|
63 * Called the last time a resource is dereferenced. Should release any resources reserved by
|
|
64 * allocate(...).
|
|
65 *
|
|
66 * @since 3.1
|
|
67 *
|
|
68 * @param resource resource being deallocated
|
|
69 * @param descriptor identifier for the resource
|
|
70 */
|
|
71 protected abstract void deallocate(Object resource, DeviceResourceDescriptor descriptor);
|
|
72
|
|
73 /* (non-Javadoc)
|
|
74 * @see ResourceManager#create(DeviceResourceDescriptor)
|
|
75 */
|
|
76 public final Object create(DeviceResourceDescriptor descriptor){
|
|
77
|
|
78 // Lazily allocate the map
|
|
79 if (map is null) {
|
|
80 map = new HashMap!(DeviceResourceDescriptor,RefCount);
|
|
81 }
|
|
82
|
|
83 // Get the current reference count
|
|
84 RefCount count = map.get(descriptor);
|
|
85 if (count !is null) {
|
|
86 // If this resource already exists, increment the reference count and return
|
|
87 // the existing resource.
|
|
88 count.count++;
|
|
89 return count.resource;
|
|
90 }
|
|
91
|
|
92 // Allocate and return a new resource (with ref count = 1)
|
|
93 Object resource = allocate(descriptor);
|
|
94
|
|
95 count = new RefCount(resource);
|
|
96 map.add(descriptor, count);
|
|
97
|
|
98 return resource;
|
|
99 }
|
|
100
|
|
101 /* (non-Javadoc)
|
|
102 * @see ResourceManager#destroy(DeviceResourceDescriptor)
|
|
103 */
|
|
104 public final void destroy(DeviceResourceDescriptor descriptor) {
|
|
105 // If the map is empty (null) then there are no resources to dispose
|
|
106 if (map is null) {
|
|
107 return;
|
|
108 }
|
|
109
|
|
110 // Find the existing resource
|
|
111 RefCount count = map.get(descriptor);
|
|
112 if (count !is null) {
|
|
113 // If the resource exists, decrement the reference count.
|
|
114 count.count--;
|
|
115 if (count.count is 0) {
|
|
116 // If this was the last reference, deallocate it.
|
|
117 deallocate(count.resource, descriptor);
|
|
118 map.removeKey(descriptor);
|
|
119 }
|
|
120 }
|
|
121
|
|
122 // Null out the map when empty to save a small amount of memory
|
|
123 if (map.drained()) {
|
|
124 map = null;
|
|
125 }
|
|
126 }
|
|
127
|
|
128 /**
|
|
129 * Deallocates any resources allocated by this registry that have not yet been
|
|
130 * deallocated.
|
|
131 *
|
|
132 * @since 3.1
|
|
133 */
|
|
134 public void dispose() {
|
|
135 super.dispose();
|
|
136
|
|
137 if (map is null) {
|
|
138 return;
|
|
139 }
|
|
140
|
|
141 foreach( key, val; map ){
|
|
142 deallocate(val.resource, key);
|
|
143 }
|
|
144
|
|
145 map = null;
|
|
146 }
|
|
147
|
|
148 /* (non-Javadoc)
|
|
149 * @see dwtx.jface.resource.ResourceManager#find(dwtx.jface.resource.DeviceResourceDescriptor)
|
|
150 */
|
|
151 public Object find(DeviceResourceDescriptor descriptor) {
|
|
152 if (map is null) {
|
|
153 return null;
|
|
154 }
|
|
155 RefCount refCount = cast(RefCount)map.get(descriptor);
|
|
156 if (refCount is null)
|
|
157 return null;
|
|
158 return refCount.resource;
|
|
159 }
|
|
160 }
|