aboutsummaryrefslogtreecommitdiff
path: root/js/helper-classes/RDGE/src/core/script/objectManager.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/helper-classes/RDGE/src/core/script/objectManager.js')
-rw-r--r--js/helper-classes/RDGE/src/core/script/objectManager.js72
1 files changed, 72 insertions, 0 deletions
diff --git a/js/helper-classes/RDGE/src/core/script/objectManager.js b/js/helper-classes/RDGE/src/core/script/objectManager.js
new file mode 100644
index 00000000..0bb8b91b
--- /dev/null
+++ b/js/helper-classes/RDGE/src/core/script/objectManager.js
@@ -0,0 +1,72 @@
1/* <copyright>
2This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
4(c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved.
5</copyright> */
6
7/* generic handle-based object manager
8*/
9objectManager = function() {
10 this.guidCounter = 0;
11 this.objects = [];
12 this.numObjects = 0;
13 this.freelist = [];
14
15 this.reset = function() {
16 this.objects = [];
17 this.freelist = [];
18 this.guidCounter = 0;
19 }
20
21 // validHandle
22 this.validHandle = function(h) {
23 return this.handleToIndex(h) != -1;
24 }
25
26 // handleToIndex
27 this.handleToIndex = function(h) {
28 var index = ( h >> 16 ) & 0xFFFF;
29 if( this.objects[index] != null && h == this.objects[index].handle ) {
30 return index;
31 }
32 return -1;
33 }
34
35 // handleToObject
36 this.handleToObject = function(h) {
37 var index = this.handleToIndex( h );
38 if( index != -1 ) {
39 return this.objects[index];
40 }
41 return null;
42 }
43
44 // add object
45 this.addObject = function(ob) {
46 var index = this.objects.length;
47
48 if( this.freelist.length > 0 ) {
49 index = this.freelist.pop();
50 }
51 if( ++this.guidCounter >= 0xFFFF ) {
52 // wrap the counter, zero is reserved for invalid handles.
53 this.guidCounter = 1;
54 }
55 ob.handle = ( index << 16 | ++this.guidCounter );
56 this.objects[index] = ob;
57
58 return ob.handle;
59 }
60
61 // remove object
62 this.removeObject = function(h) {
63 var index = this.handleToIndex( h );
64 if( index != -1 ) {
65 if( this.objects[index].onremove != undefined ) {
66 this.objects[index].onremove();
67 }
68 this.objects[index] = null;
69 this.freelist.push(index);
70 }
71 }
72} \ No newline at end of file