summaryrefslogtreecommitdiff
path: root/java/common/Composite.tpl
blob: 4e1a54de8135c115be7baec4b399b59001905a7d (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package org.apache.qpidity.transport;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.qpidity.transport.codec.Decoder;
import org.apache.qpidity.transport.codec.Encodable;
import org.apache.qpidity.transport.codec.Encoder;

import org.apache.qpidity.transport.network.Frame;

${
from genutil import *

cls = klass(type)["@name"]

if type.name in ("control", "command"):
  base = "Method"
  size = 0
  pack = 2
  if type["segments"]:
    payload = "true"
  else:
    payload = "false"
  if type.name == "control" and cls == "connection":
    track = "Frame.L1"
  elif cls == "session" and type["@name"] in ("attach", "attached", "detach", "detached"):
    track = "Frame.L2"
  elif type.name == "command":
    track = "Frame.L4"
  else:
    track = "Frame.L3"
else:
  base = "Struct"
  size = type["@size"]
  pack = type["@pack"]
  payload = "false"
  track = "-1"

typecode = code(type)
}

public class $name extends $base {

    public static final int TYPE = $typecode;

    public final int getStructType() {
        return TYPE;
    }

    public final int getSizeWidth() {
        return $size;
    }

    public final int getPackWidth() {
        return $pack;
    }

    public final boolean hasPayload() {
        return $payload;
    }

    public final byte getEncodedTrack() {
        return $track;
    }

    private static final List<Field<?,?>> FIELDS = new ArrayList<Field<?,?>>();
    public List<Field<?,?>> getFields() { return FIELDS; }

${
fields = get_fields(type)
params = get_parameters(fields)
options = get_options(fields)

for f in fields:
  out("    private boolean has_$(f.name);\n")
  out("    private $(f.type) $(f.name);\n")
}

${
if fields:
  out("    public $name() {}\n")
}

    public $name($(", ".join(params))) {
${
for f in fields:
  if f.option: continue
  out("        $(f.set)($(f.name));\n")

for f in options:
  out("        boolean _$(f.name) = false;\n")

if options:
  out("""
        for (int i=0; i < _options.length; i++) {
            switch (_options[i]) {
""")

  for f in options:
    out("            case $(f.option): _$(f.name) = true; break;\n")

  out("""            case NO_OPTION: break;
            default: throw new IllegalArgumentException("invalid option: " + _options[i]);
            }
        }
""")

for f in options:
  out("        $(f.set)(_$(f.name));\n")
}
    }

    public <C> void dispatch(C context, MethodDelegate<C> delegate) {
        delegate.$(dromedary(name))(context, this);
    }

${
for f in fields:
  out("""
    public final boolean $(f.has)() {
        return has_$(f.name);
    }

    public final $name $(f.clear)() {
        this.has_$(f.name) = false;
        this.$(f.name) = $(f.default);
        this.dirty = true;
        return this;
    }

    public final $(f.type) $(f.get)() {
        return $(f.name);
    }

    public final $name $(f.set)($(f.type) value) {
        this.$(f.name) = value;
        this.has_$(f.name) = true;
        this.dirty = true;
        return this;
    }

    public final $name $(f.name)($(f.type) value) {
        this.$(f.name) = value;
        this.has_$(f.name) = true;
        this.dirty = true;
        return this;
    }

    static {
        FIELDS.add(new Field<$name,$(jref(jclass(f.type)))>($name.class, $(jref(jclass(f.type))).class, "$(f.name)", $(f.index)) {
            public boolean has(Object struct) {
                return check(struct).has_$(f.name);
            }
            public void has(Object struct, boolean value) {
                check(struct).has_$(f.name) = value;
            }
            public $(jref(f.type)) get(Object struct) {
                return check(struct).$(f.get)();
            }
            public void read(Decoder dec, Object struct) {
                check(struct).$(f.name) = $(f.read);
                check(struct).dirty = true;
            }
            public void write(Encoder enc, Object struct) {
               $(f.write);
            }
        });
    }
""")
}

    public void write(Encoder enc)
    {
${
flag_count = 8*int(pack)
reserved_count = flag_count - len(fields)

if pack > 0:
  for f in fields:
    if f.type == "boolean":
      out("        enc.writeBit(this.$(f.name));\n")
    else:
      out("        enc.writeBit(this.has_$(f.name));\n")
  for i in range(reserved_count):
    out("        enc.writeBit(false);\n")

for f in fields:
  if f.type == "boolean":
    continue
  if pack > 0:
    out("        if (this.has_$(f.name))\n    ")
  pre = ""
  post = ""
  if f.type_node.name == "struct":
    pre = "%s.TYPE, " % cname(f.type_node)
  elif f.type_node.name == "domain":
    post = ".getValue()"
  out("        enc.write$(f.coder)($(pre)this.$(f.name)$(post));\n")
}
    }

    public void read(Decoder dec)
    {
${
if pack > 0:
  for f in fields:
    if f.type == "boolean":
      out("        this.$(f.name) = dec.readBit();\n")
    else:
      out("        this.has_$(f.name) = dec.readBit();\n")
  for i in range(reserved_count):
    out("        dec.readBit();\n")

for f in fields:
  if f.type == "boolean":
    continue
  if pack > 0:
    out("        if (this.has_$(f.name))\n    ")
  pre = ""
  post = ""
  arg = ""
  if f.type_node.name == "struct":
    pre = "(%s)" % cname(f.type_node)
    arg = "%s.TYPE" % cname(f.type_node)
  elif f.type_node.name == "domain":
    pre = "%s.get(" % cname(f.type_node)
    post = ")"
  out("        this.$(f.name) = $(pre)dec.read$(f.coder)($(arg))$(post);\n")
}
    }

}