summaryrefslogtreecommitdiff
path: root/js/src/Ice/EnumBase.js
blob: 320e882a4bf729f27e9c648feff4ec8a54b9926d (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// **********************************************************************
//
// Copyright (c) 2003-present 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.
//
// **********************************************************************

const Ice = require("../Ice/ModuleRegistry").Ice;
//
// Ice.EnumBase
//
class EnumBase
{
    constructor(name, value)
    {
        this._name = name;
        this._value = value;
    }

    equals(rhs)
    {
        if(this === rhs)
        {
            return true;
        }

        if(!(rhs instanceof Object.getPrototypeOf(this).constructor))
        {
            return false;
        }

        return this._value == rhs._value;
    }

    hashCode()
    {
        return this._value;
    }

    toString()
    {
        return this._name;
    }

    get name()
    {
        return this._name;
    }

    get value()
    {
        return this._value;
    }
}
Ice.EnumBase = EnumBase;

class EnumHelper
{
    constructor(enumType)
    {
        this._enumType = enumType;
    }

    write(os, v)
    {
        this._enumType._write(os, v);
    }

    writeOptional(os, tag, v)
    {
        this._enumType._writeOpt(os, tag, v);
    }

    read(is)
    {
        return this._enumType._read(is);
    }

    readOptional(is, tag)
    {
        return this._enumType._readOpt(is, tag);
    }
}

Ice.EnumHelper = EnumHelper;

const Slice = Ice.Slice;
Slice.defineEnum = function(enumerators)
{
    const type = class extends EnumBase
    {
    };

    const enums = [];
    let maxValue = 0;
    let firstEnum = null;

    for(const idx in enumerators)
    {
        const e = enumerators[idx][0];
        const value = enumerators[idx][1];
        const enumerator = new type(e, value);
        enums[value] = enumerator;
        if(!firstEnum)
        {
            firstEnum = enumerator;
        }
        Object.defineProperty(type, e, {
            enumerable: true,
            value: enumerator
        });
        if(value > maxValue)
        {
            maxValue = value;
        }
    }

    Object.defineProperty(type, "minWireSize", {get: () => 1});

    type._write = function(os, v)
    {
        if(v)
        {
            os.writeEnum(v);
        }
        else
        {
            os.writeEnum(firstEnum);
        }
    };

    type._read = function(is)
    {
        return is.readEnum(type);
    };

    type._writeOpt = function(os, tag, v)
    {
        if(v !== undefined)
        {
            if(os.writeOptional(tag, Ice.OptionalFormat.Size))
            {
                type._write(os, v);
            }
        }
    };

    type._readOpt = function(is, tag)
    {
        return is.readOptionalEnum(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;
};
module.exports.Ice = Ice;