123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- "use strict";
- module.exports = Enum;
-
-
- var ReflectionObject = require("./object");
- ((Enum.prototype = Object.create(ReflectionObject.prototype)).constructor = Enum).className = "Enum";
-
- var Namespace = require("./namespace"),
- util = require("./util");
-
-
- function Enum(name, values, options, comment, comments) {
- ReflectionObject.call(this, name, options);
-
- if (values && typeof values !== "object")
- throw TypeError("values must be an object");
-
-
-
- this.valuesById = {};
-
-
-
- this.values = Object.create(this.valuesById);
-
-
-
- this.comment = comment;
-
-
-
- this.comments = comments || {};
-
-
-
- this.reserved = undefined;
-
-
-
-
-
- if (values)
- for (var keys = Object.keys(values), i = 0; i < keys.length; ++i)
- if (typeof values[keys[i]] === "number")
- this.valuesById[ this.values[keys[i]] = values[keys[i]] ] = keys[i];
- }
-
-
-
-
- Enum.fromJSON = function fromJSON(name, json) {
- var enm = new Enum(name, json.values, json.options, json.comment, json.comments);
- enm.reserved = json.reserved;
- return enm;
- };
-
-
- Enum.prototype.toJSON = function toJSON(toJSONOptions) {
- var keepComments = toJSONOptions ? Boolean(toJSONOptions.keepComments) : false;
- return util.toObject([
- "options" , this.options,
- "values" , this.values,
- "reserved" , this.reserved && this.reserved.length ? this.reserved : undefined,
- "comment" , keepComments ? this.comment : undefined,
- "comments" , keepComments ? this.comments : undefined
- ]);
- };
-
-
- Enum.prototype.add = function add(name, id, comment) {
-
-
- if (!util.isString(name))
- throw TypeError("name must be a string");
-
- if (!util.isInteger(id))
- throw TypeError("id must be an integer");
-
- if (this.values[name] !== undefined)
- throw Error("duplicate name '" + name + "' in " + this);
-
- if (this.isReservedId(id))
- throw Error("id " + id + " is reserved in " + this);
-
- if (this.isReservedName(name))
- throw Error("name '" + name + "' is reserved in " + this);
-
- if (this.valuesById[id] !== undefined) {
- if (!(this.options && this.options.allow_alias))
- throw Error("duplicate id " + id + " in " + this);
- this.values[name] = id;
- } else
- this.valuesById[this.values[name] = id] = name;
-
- this.comments[name] = comment || null;
- return this;
- };
-
-
- Enum.prototype.remove = function remove(name) {
-
- if (!util.isString(name))
- throw TypeError("name must be a string");
-
- var val = this.values[name];
- if (val == null)
- throw Error("name '" + name + "' does not exist in " + this);
-
- delete this.valuesById[val];
- delete this.values[name];
- delete this.comments[name];
-
- return this;
- };
-
-
- Enum.prototype.isReservedId = function isReservedId(id) {
- return Namespace.isReservedId(this.reserved, id);
- };
-
-
- Enum.prototype.isReservedName = function isReservedName(name) {
- return Namespace.isReservedName(this.reserved, name);
- };
|