diff options
Diffstat (limited to 'js/src/Ice/EnumBase.js')
-rw-r--r-- | js/src/Ice/EnumBase.js | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/js/src/Ice/EnumBase.js b/js/src/Ice/EnumBase.js new file mode 100644 index 00000000000..a387219e81c --- /dev/null +++ b/js/src/Ice/EnumBase.js @@ -0,0 +1,163 @@ +// ********************************************************************** +// +// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved. +// +// This copy of Ice is licensed to you under the terms described in the +// ICE_LICENSE file included in this distribution. +// +// ********************************************************************** + +(function(global){ + require("Ice/Class"); + + var Slice = global.Slice || {}; + var Ice = global.Ice || {}; + // + // Ice.EnumBase + // + var EnumBase = Ice.Class({ + __init__: function(name, value) + { + this._name = name; + this._value = value; + }, + equals: function(rhs) + { + if(this === rhs) + { + return true; + } + + var proto = Object.getPrototypeOf(this); + if(!(rhs instanceof proto.constructor)) + { + return false; + } + + return this._value == rhs._value; + }, + hashCode: function() + { + return this._value; + }, + toString: function() + { + return this._name; + } + }); + + var prototype = EnumBase.prototype; + + Object.defineProperty(prototype, 'name', { + enumerable: true, + get: function() { return this._name; } + }); + + Object.defineProperty(prototype, 'value', { + enumerable: true, + get: function() { return this._value; } + }); + + var EnumHelper = Ice.Class({ + __init__: function(enumType) + { + this._enumType = enumType; + }, + write: function(os, v) + { + this._enumType.__write(os, v); + }, + writeOpt: function(os, tag, v) + { + this._enumType.__writeOpt(os, tag, v); + }, + read: function(is) + { + return this._enumType.__read(is); + }, + readOpt: function(is, tag) + { + return this._enumType.__readOpt(is, tag); + } + }); + + Ice.EnumHelper = EnumHelper; + + var write = function(os, v) + { + os.writeEnum(v); + }; + var writeOpt = function(os, tag, v) + { + os.writeOptValue(tag, Ice.OptionalFormat.Size, Ice.BasicStream.prototype.writeEnum, v); + }; + + Slice.defineEnum = function(enumerators) + { + var type = function(n, v) + { + EnumBase.call(this, n, v); + }; + + type.prototype = new EnumBase(); + type.prototype.constructor = type; + + var enums = []; + var maxValue = 0; + for(var e in enumerators) + { + var value = enumerators[e]; + var enumerator = new type(e, value); + enums[value] = enumerator; + Object.defineProperty(type, e, { + enumerable: true, + value: enumerator + }); + if(value > maxValue) + { + maxValue = value; + } + } + + Object.defineProperty(type, "minWireSize", { + get: function(){ return 1; } + }); + + type.__write = write; + type.__read = function(is) + { + return is.readEnum(type); + }; + type.__writeOpt = writeOpt; + type.__readOpt = function(is, tag) + { + return is.readOptEnum(tag, type); + }; + + type.__helper = new EnumHelper(type); + + Object.defineProperty(type, 'valueOf', { + value: function(v) { + if(v === undefined) + { + return type; + } + return enums[v]; } + }); + + Object.defineProperty(type, 'maxValue', { + value: maxValue + }); + + Object.defineProperty(type.prototype, 'maxValue', { + value: maxValue + }); + + return type; + }; + + Ice.EnumBase = EnumBase; + + global.Slice = Slice; + global.Ice = Ice; +}(typeof (global) === "undefined" ? window : global)); |