aboutsummaryrefslogtreecommitdiff
path: root/node_modules/montage/core/bitfield.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/montage/core/bitfield.js')
-rwxr-xr-xnode_modules/montage/core/bitfield.js199
1 files changed, 199 insertions, 0 deletions
diff --git a/node_modules/montage/core/bitfield.js b/node_modules/montage/core/bitfield.js
new file mode 100755
index 00000000..2c274d3c
--- /dev/null
+++ b/node_modules/montage/core/bitfield.js
@@ -0,0 +1,199 @@
1/* <copyright>
2 This file contains proprietary software owned by Motorola Mobility, Inc.<br/>
3 No 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 Defines the BitField class, that compactly stores multiple values as a short series of bits.
8 @module montage/core/bitfield
9 @requires montage/core/core
10 */
11
12var Montage = require("montage").Montage;
13
14
15/**
16 The BitField object compactly stores multiple values as a short series of bits. This implementation is limited to 32 fields.
17 @class module:montage/core/bitfield.BitField
18 @classdesc Compactly stores multiple values as a short series of bits.
19 @extends module:montage/core/core.Montage
20 */
21var BitField = exports.BitField = Montage.create(Montage, /** @lends module:montage/core/bitfield.BitField# */ {
22
23 /**
24 Creates a new BitField object containing the fields provided in the propertyDescriptor parameter.
25 @function
26 @param {Object} propertyDescriptor An object containing one or more property name/value pairs. Each pair is added to the new BitField.
27 @returns {Object} A new BitField object that contains fields described by the property descriptor.
28 @example var bitField = BitField.create();
29 bitField = BitField.create().initWithDescriptor({
30 likes_golf: {
31 value: false
32 },
33 likes_basketball: {
34 value: true
35 },
36 likes_baseball: {
37 value: false
38 },
39 });
40 */
41 initWithDescriptor: {
42 enumerable: false,
43 value: function(propertyDescriptor) {
44 var fieldName;
45 this.reset();
46 for (fieldName in propertyDescriptor) {
47 this.addField(fieldName, propertyDescriptor[fieldName].value);
48 }
49 return this;
50 }
51 },
52
53 /**
54 Adds a new field to a BitField instance.
55 @function
56 @param {String} aFieldName The name of the field to add.
57 @param {Mixed} defaultValue The new field's default value.
58 */
59 addField: {
60 enumerable: false,
61 value: function(aFieldName, defaultValue) {
62 if (aFieldName in this) {
63 return;
64 }
65 if (this._fieldCount >= 32) {
66 throw "BitField 32 fields limit reached.";
67 }
68 //We try to recycle slots as limited to 32bits
69 this._trueValue += (this._fields[aFieldName] = this._constantsToReuse.length ? this._constantsToReuse.shift() : (1 << this._fieldCount));
70 Montage.defineProperty(this, aFieldName, {
71 enumerable: true,
72 get: function() {
73 return (this._value === this._trueValue);
74 },
75 set: function(value) {
76 value ? (this._value |= this._fields[aFieldName]) : (this._value &= ~ (this._fields[aFieldName]));
77 if (this.value) {
78 this.callDelegateMethod();
79 }
80 }
81 });
82 this._fieldCount++;
83 if (!! defaultValue) {
84 this[aFieldName] = true;
85 }
86 }
87 },
88
89 /**
90 @private
91 */
92 _constantsToReuse: {
93 enumerable: false,
94 value: []
95 },
96
97 /**
98 Removes a field from the bitfield.
99 @function
100 @param {String} aFieldName The name of the field to remove.
101 */
102 removeField: {
103 enumerable: false,
104 value: function(aFieldName) {
105 delete this[aFieldName];
106 this._constantsToReuse.push(this._fields[aFieldName]);
107 this._trueValue -= this._fields[aFieldName];
108 delete this._fields[aFieldName];
109 }
110 },
111
112 /**
113 The BitField object's delegate.
114 @type {Property}
115 @default null
116 */
117 delegate: {
118 enumerable: false,
119 value: null
120 },
121
122 /**
123 @function
124 @returns Nothing
125 */
126 callDelegateMethod: {
127 value: function() {
128 var delegateMethod;
129 if (this.delegate && typeof (delegateMethod = this.delegate.bitFieldDidBecomeTrue) === "function") {
130 delegateMethod.call(this.delegate, this);
131 }
132 },
133 enumerable: false
134 },
135/**
136 @type {Function}
137 @default {Number} 0
138 */
139 value: {
140 enumerable: false,
141 get: function() {
142 return (this._value === this._trueValue);
143 }
144 },
145
146 /**
147 @private
148 */
149 _fieldCount: {
150 enumerable: false,
151 value: 0
152 },
153/**
154 @private
155*/
156 _value: {
157 enumerable: false,
158 value: 0
159 },
160/**
161 @private
162*/
163 _trueValue: {
164 enumerable: false,
165 value: 0
166 },
167/**
168 @function
169 */
170 reset: {
171 enumerable: false,
172 value: function() {
173 this._value = 0x0;
174 }
175 },
176/**
177 @private
178*/
179 _fields: {
180 enumerable: false,
181 value: {}
182 },
183/**
184 @function
185 @returns result
186 */
187 toString: {
188 value: function() {
189 var fieldNames = this._fields,
190 i,
191 iField,
192 result = "";
193 for (i = 0; (iField = fieldNames[i]); i++) {
194 result += iField + "[" + (this._value & fieldNames[iField]) + "], ";
195 }
196 return result;
197 }
198 }
199});