aboutsummaryrefslogtreecommitdiff
path: root/js/helper-classes/RDGE/src/core/script/objectManager.js
blob: 303a69db0ba01ada5551fb2809b49eb7cd4dc1e0 (plain)
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
/* <copyright>
This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/>
(c) Copyright 2011 Motorola Mobility, Inc.  All Rights Reserved.
</copyright> */

// RDGE namespaces
var RDGE = RDGE || {};

/* generic handle-based object manager */
RDGE.objectManager = function() {
	this.guidCounter = 0;
	this.objects = [];
	this.numObjects = 0;
	this.freelist = [];
	
	this.reset = function() {
		this.objects = [];
		this.freelist = [];
		this.guidCounter = 0;
	}
	
	// validHandle
	this.validHandle = function(h) {
		return this.handleToIndex(h) != -1;
	}
	
	// handleToIndex
	this.handleToIndex = function(h) {
		var index = ( h >> 16 ) & 0xFFFF;
		if( this.objects[index] != null && h == this.objects[index].handle ) {
			return index;
		}
		return -1;
	}

	// handleToObject	
	this.handleToObject = function(h) {
		var index = this.handleToIndex( h ); 
		if( index != -1 ) {
			return this.objects[index];
		}
		return null;
	}
	
	// add object
	this.addObject = function(ob) {
		var index = this.objects.length;

		if( this.freelist.length > 0 ) {
			index = this.freelist.pop(); 
		}
		if( ++this.guidCounter >= 0xFFFF ) {
			// wrap the counter, zero is reserved for invalid handles.
			this.guidCounter = 1;
		}
		ob.handle = ( index << 16 | ++this.guidCounter );				
		this.objects[index] = ob;
		
		return ob.handle;
	}
	
	// remove object
	this.removeObject = function(h) {	
		var index = this.handleToIndex( h );	
		if( index != -1 ) {
			if( this.objects[index].onremove != undefined ) {
				this.objects[index].onremove();
			}
			this.objects[index] = null;
			this.freelist.push(index);
		}
	}
}