Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 1 | //:: # Copyright 2013, Big Switch Networks, Inc. |
| 2 | //:: # |
| 3 | //:: # LoxiGen is licensed under the Eclipse Public License, version 1.0 (EPL), with |
| 4 | //:: # the following special exception: |
| 5 | //:: # |
| 6 | //:: # LOXI Exception |
| 7 | //:: # |
| 8 | //:: # As a special exception to the terms of the EPL, you may distribute libraries |
| 9 | //:: # generated by LoxiGen (LoxiGen Libraries) under the terms of your choice, provided |
| 10 | //:: # that copyright and licensing notices generated by LoxiGen are not altered or removed |
| 11 | //:: # from the LoxiGen Libraries and the notice provided below is (i) included in |
| 12 | //:: # the LoxiGen Libraries, if distributed in source code form and (ii) included in any |
| 13 | //:: # documentation for the LoxiGen Libraries, if distributed in binary form. |
| 14 | //:: # |
| 15 | //:: # Notice: "Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler." |
| 16 | //:: # |
| 17 | //:: # You may not use this file except in compliance with the EPL or LOXI Exception. You may obtain |
| 18 | //:: # a copy of the EPL at: |
| 19 | //:: # |
| 20 | //:: # http::: #www.eclipse.org/legal/epl-v10.html |
| 21 | //:: # |
| 22 | //:: # Unless required by applicable law or agreed to in writing, software |
| 23 | //:: # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 24 | //:: # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 25 | //:: # EPL for the specific language governing permissions and limitations |
| 26 | //:: # under the EPL. |
| 27 | //:: |
| 28 | //:: from loxi_ir import * |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 29 | //:: import os |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 30 | //:: import itertools |
| 31 | //:: import of_g |
| 32 | //:: include('_copyright.java') |
| 33 | |
| 34 | //:: include('_autogen.java') |
| 35 | |
| 36 | package ${msg.package}; |
| 37 | |
| 38 | //:: include("_imports.java", msg=msg) |
| 39 | |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 40 | class ${impl_class} implements ${msg.interface.inherited_declaration()} { |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 41 | // version: ${version} |
Yotam Harchol | 791e488 | 2013-09-05 16:32:56 -0700 | [diff] [blame] | 42 | final static byte WIRE_VERSION = ${version.int_version}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 43 | //:: if msg.is_fixed_length: |
Yotam Harchol | 791e488 | 2013-09-05 16:32:56 -0700 | [diff] [blame] | 44 | final static int LENGTH = ${msg.length}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 45 | //:: else: |
Yotam Harchol | 791e488 | 2013-09-05 16:32:56 -0700 | [diff] [blame] | 46 | final static int MINIMUM_LENGTH = ${msg.min_length}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 47 | //:: #endif |
| 48 | |
| 49 | //:: for prop in msg.data_members: |
Andreas Wundsam | f89f782 | 2013-09-23 14:49:24 -0700 | [diff] [blame] | 50 | //:: if prop.default_value: |
| 51 | private final static ${prop.java_type.public_type} ${prop.default_name} = ${prop.default_value}; |
| 52 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 53 | //:: #end |
| 54 | |
| 55 | // OF message fields |
| 56 | //:: for prop in msg.data_members: |
| 57 | private final ${prop.java_type.public_type} ${prop.name}; |
| 58 | //:: #endfor |
Andreas Wundsam | e962d37 | 2013-10-02 18:15:58 -0700 | [diff] [blame] | 59 | // |
| 60 | //:: if all(prop.default_value for prop in msg.data_members): |
| 61 | // Immutable default instance |
| 62 | final static ${impl_class} DEFAULT = new ${impl_class}( |
| 63 | ${", ".join(prop.default_name for prop in msg.data_members)} |
| 64 | ); |
| 65 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 66 | |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 67 | //:: if msg.data_members: |
| 68 | // package private constructor - used by readers, builders, and factory |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 69 | ${impl_class}(${ |
| 70 | ", ".join("%s %s" %(prop.java_type.public_type, prop.name) for prop in msg.data_members) }) { |
| 71 | //:: for prop in msg.data_members: |
| 72 | this.${prop.name} = ${prop.name}; |
| 73 | //:: #endfor |
| 74 | } |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 75 | //:: else: |
| 76 | final static ${impl_class} INSTANCE = new ${impl_class}(); |
| 77 | // private empty constructor - use shared instance! |
| 78 | private ${impl_class}() { |
| 79 | } |
| 80 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 81 | |
| 82 | // Accessors for OF message fields |
Yotam Harchol | f25e814 | 2013-09-09 14:30:13 -0700 | [diff] [blame] | 83 | //:: include("_field_accessors.java", msg=msg, generate_setters=False, builder=False, has_parent=False) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 84 | |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 85 | //:: if os.path.exists("%s/custom/%s.java" % (template_dir, msg.name)): |
| 86 | //:: include("custom/%s.java" % msg.name, msg=msg) |
| 87 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 88 | |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 89 | //:: if msg.data_members: |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 90 | public ${msg.interface.name}.Builder createBuilder() { |
| 91 | return new BuilderWithParent(this); |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 94 | static class BuilderWithParent implements ${msg.interface.name}.Builder { |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 95 | final ${impl_class} parentMessage; |
| 96 | |
| 97 | // OF message fields |
| 98 | //:: for prop in msg.data_members: |
| 99 | private boolean ${prop.name}Set; |
| 100 | private ${prop.java_type.public_type} ${prop.name}; |
| 101 | //:: #endfor |
| 102 | |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 103 | BuilderWithParent(${impl_class} parentMessage) { |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 104 | this.parentMessage = parentMessage; |
| 105 | } |
| 106 | |
Yotam Harchol | f25e814 | 2013-09-09 14:30:13 -0700 | [diff] [blame] | 107 | //:: include("_field_accessors.java", msg=msg, generate_setters=True, builder=True, has_parent=True) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 108 | |
Andreas Wundsam | f89f782 | 2013-09-23 14:49:24 -0700 | [diff] [blame] | 109 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 110 | @Override |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 111 | public ${msg.interface.name} build() { |
Andreas Wundsam | f89f782 | 2013-09-23 14:49:24 -0700 | [diff] [blame] | 112 | //:: for prop in msg.data_members: |
| 113 | ${prop.java_type.public_type} ${prop.name} = this.${prop.name}Set ? this.${prop.name} : parentMessage.${prop.name}; |
| 114 | //:: if not prop.is_nullable and not prop.java_type.is_primitive: |
| 115 | if(${prop.name} == null) |
| 116 | throw new NullPointerException("Property ${prop.name} must not be null"); |
| 117 | //:: #endif |
| 118 | //:: #endfor |
| 119 | |
| 120 | // |
| 121 | //:: if os.path.exists("%s/custom/%s.Builder_normalize_stanza.java" % (template_dir, msg.name)): |
| 122 | //:: include("custom/%s.Builder_normalize_stanza.java" % msg.name, msg=msg, has_parent=False) |
| 123 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 124 | return new ${impl_class}( |
Andreas Wundsam | f89f782 | 2013-09-23 14:49:24 -0700 | [diff] [blame] | 125 | //:: for i, prop in enumerate(msg.data_members): |
| 126 | //:: comma = "," if i < len(msg.data_members)-1 else "" |
| 127 | ${prop.name}${comma} |
| 128 | //:: #endfor |
| 129 | ); |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 130 | } |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 131 | //:: if os.path.exists("%s/custom/%s.Builder.java" % (template_dir, msg.name)): |
Yotam Harchol | 98af775 | 2013-08-22 14:59:38 -0700 | [diff] [blame] | 132 | //:: include("custom/%s.Builder.java" % msg.name, msg=msg, has_parent=True) |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 133 | //:: #endif |
| 134 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 137 | static class Builder implements ${msg.interface.name}.Builder { |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 138 | // OF message fields |
| 139 | //:: for prop in msg.data_members: |
| 140 | private boolean ${prop.name}Set; |
| 141 | private ${prop.java_type.public_type} ${prop.name}; |
| 142 | //:: #endfor |
| 143 | |
Yotam Harchol | f25e814 | 2013-09-09 14:30:13 -0700 | [diff] [blame] | 144 | //:: include("_field_accessors.java", msg=msg, generate_setters=True, builder=True, has_parent=False) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 145 | // |
| 146 | @Override |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 147 | public ${msg.interface.name} build() { |
Andreas Wundsam | f89f782 | 2013-09-23 14:49:24 -0700 | [diff] [blame] | 148 | //:: for prop in msg.data_members: |
| 149 | //:: if prop.default_value: |
| 150 | ${prop.java_type.public_type} ${prop.name} = this.${prop.name}Set ? this.${prop.name} : ${prop.default_name}; |
| 151 | //:: else: |
| 152 | if(!this.${prop.name}Set) |
| 153 | throw new IllegalStateException("Property ${prop.name} doesn't have default value -- must be set"); |
| 154 | //:: #endif |
| 155 | //:: if not prop.is_nullable and not prop.java_type.is_primitive: |
| 156 | if(${prop.name} == null) |
| 157 | throw new NullPointerException("Property ${prop.name} must not be null"); |
| 158 | //:: #endif |
| 159 | //:: #endfor |
| 160 | |
| 161 | //:: if os.path.exists("%s/custom/%s.Builder_normalize_stanza.java" % (template_dir, msg.name)): |
| 162 | //:: include("custom/%s.Builder_normalize_stanza.java" % msg.name, msg=msg, has_parent=False) |
| 163 | //:: #endif |
| 164 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 165 | return new ${impl_class}( |
Andreas Wundsam | f89f782 | 2013-09-23 14:49:24 -0700 | [diff] [blame] | 166 | //:: for i, prop in enumerate(msg.data_members): |
| 167 | //:: comma = "," if i < len(msg.data_members)-1 else "" |
| 168 | ${prop.name}${comma} |
| 169 | //:: #endfor |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 170 | ); |
| 171 | } |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 172 | //:: if os.path.exists("%s/custom/%s.Builder.java" % (template_dir, msg.name)): |
Yotam Harchol | 98af775 | 2013-08-22 14:59:38 -0700 | [diff] [blame] | 173 | //:: include("custom/%s.Builder.java" % msg.name, msg=msg, has_parent=False) |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 174 | //:: #endif |
| 175 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 176 | } |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 177 | //:: else: |
| 178 | // no data members - do not support builder |
| 179 | public ${msg.interface.name}.Builder createBuilder() { |
| 180 | throw new UnsupportedOperationException("${impl_class} has no mutable properties -- builder unneeded"); |
| 181 | } |
| 182 | //:: #endif |
| 183 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 184 | |
| 185 | final static Reader READER = new Reader(); |
| 186 | static class Reader implements OFMessageReader<${msg.interface.name}> { |
| 187 | @Override |
| 188 | public ${msg.interface.name} readFrom(ChannelBuffer bb) throws OFParseError { |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 189 | //:: for prop in msg.members: |
| 190 | //:: if not prop.is_virtual and (prop.is_length_value or prop.is_field_length_value): |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 191 | int start = bb.readerIndex(); |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 192 | //:: break |
| 193 | //:: #endif |
| 194 | //:: #endfor |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 195 | //:: fields_with_length_member = {} |
| 196 | //:: for prop in msg.members: |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 197 | //:: if prop.is_virtual: |
| 198 | //:: continue |
| 199 | //:: elif prop.is_data: |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 200 | ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True, |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 201 | length=fields_with_length_member[prop.c_name] if prop.c_name in fields_with_length_member else None)}; |
| 202 | //:: elif prop.is_pad: |
| 203 | // pad: ${prop.length} bytes |
| 204 | bb.skipBytes(${prop.length}); |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 205 | //:: elif prop.is_length_value: |
Andreas Wundsam | 83d877a | 2013-09-30 14:26:44 -0700 | [diff] [blame] | 206 | ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True)}; |
| 207 | //:: if prop.is_fixed_value: |
| 208 | if(${prop.name} != ${prop.value}) |
| 209 | throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name}); |
| 210 | //:: else: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 211 | if(${prop.name} < MINIMUM_LENGTH) |
| 212 | throw new OFParseError("Wrong ${prop.name}: Expected to be >= " + MINIMUM_LENGTH + ", was: " + ${prop.name}); |
Andreas Wundsam | 83d877a | 2013-09-30 14:26:44 -0700 | [diff] [blame] | 213 | //:: #endif |
Yotam Harchol | 0178c20 | 2013-09-05 16:25:50 -0700 | [diff] [blame] | 214 | if(bb.readableBytes() + (bb.readerIndex() - start) < ${prop.name}) { |
| 215 | // Buffer does not have all data yet |
| 216 | bb.readerIndex(start); |
| 217 | return null; |
| 218 | } |
Andreas Wundsam | 83d877a | 2013-09-30 14:26:44 -0700 | [diff] [blame] | 219 | //:: elif prop.is_fixed_value: |
| 220 | // fixed value property ${prop.name} == ${prop.value} |
| 221 | ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)}; |
| 222 | if(${prop.name} != ${prop.priv_value}) |
| 223 | throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name}); |
| 224 | //:: elif prop.is_field_length_value: |
| 225 | //:: fields_with_length_member[prop.member.field_name] = prop.name |
| 226 | ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True)}; |
| 227 | //:: else: |
| 228 | // fixme: todo ${prop.name} |
Yotam Harchol | 0178c20 | 2013-09-05 16:25:50 -0700 | [diff] [blame] | 229 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 230 | //:: #endfor |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 231 | //:: if msg.align: |
Andreas Wundsam | 87bdcf4 | 2013-10-23 15:19:01 -0700 | [diff] [blame] | 232 | //:: if msg.length_includes_align: |
| 233 | // align message to ${msg.align} bytes (length contains aligned value) |
| 234 | bb.skipBytes(length - (bb.readerIndex() - start)); |
| 235 | //:: else: |
| 236 | // align message to ${msg.align} bytes (length does not contain alignment) |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 237 | bb.skipBytes(((length + ${msg.align-1})/${msg.align} * ${msg.align} ) - length ); |
| 238 | //:: #endif |
Andreas Wundsam | 87bdcf4 | 2013-10-23 15:19:01 -0700 | [diff] [blame] | 239 | //:: #endif |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 240 | |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 241 | //:: if msg.data_members: |
Andreas Wundsam | f89f782 | 2013-09-23 14:49:24 -0700 | [diff] [blame] | 242 | //:: if os.path.exists("%s/custom/%s.Reader_normalize_stanza.java" % (template_dir, msg.name)): |
| 243 | //:: include("custom/%s.Reader_normalize_stanza.java" % msg.name, msg=msg, has_parent=False) |
| 244 | //:: #endif |
| 245 | return new ${impl_class}( |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 246 | ${",\n ".join( |
| 247 | [ prop.name for prop in msg.data_members])} |
| 248 | ); |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 249 | //:: else: |
| 250 | return INSTANCE; |
| 251 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Andreas Wundsam | 22ba3af | 2013-10-04 16:00:30 -0700 | [diff] [blame] | 255 | public void putTo(PrimitiveSink sink) { |
| 256 | FUNNEL.funnel(this, sink); |
| 257 | } |
| 258 | |
| 259 | final static ${impl_class}Funnel FUNNEL = new ${impl_class}Funnel(); |
| 260 | static class ${impl_class}Funnel implements Funnel<${impl_class}> { |
| 261 | private static final long serialVersionUID = 1L; |
| 262 | @Override |
| 263 | public void funnel(${impl_class} message, PrimitiveSink sink) { |
| 264 | //:: for prop in msg.members: |
| 265 | //:: if prop.is_virtual: |
| 266 | //:: continue |
| 267 | //:: elif prop.is_data: |
| 268 | ${prop.java_type.funnel_op(version, "message." + prop.name, pub_type=True)}; |
| 269 | //:: elif prop.is_pad: |
| 270 | // skip pad (${prop.length} bytes) |
| 271 | //:: elif prop.is_fixed_value: |
| 272 | // fixed value property ${prop.name} = ${prop.value} |
| 273 | ${prop.java_type.funnel_op(version, prop.priv_value, pub_type=False)}; |
| 274 | //:: else: |
| 275 | // FIXME: skip funnel of ${prop.name} |
| 276 | //:: #endif |
| 277 | //:: #endfor |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | |
Yotam Harchol | 5c9d6f4 | 2013-08-01 11:09:20 -0700 | [diff] [blame] | 282 | public void writeTo(ChannelBuffer bb) { |
| 283 | WRITER.write(bb, this); |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | final static Writer WRITER = new Writer(); |
| 287 | static class Writer implements OFMessageWriter<${impl_class}> { |
| 288 | @Override |
Andreas Wundsam | a94273b | 2013-08-01 22:11:33 -0700 | [diff] [blame] | 289 | public void write(ChannelBuffer bb, ${impl_class} message) { |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 290 | //:: if not msg.is_fixed_length: |
Andreas Wundsam | 482f6d9 | 2013-07-24 16:10:21 -0700 | [diff] [blame] | 291 | int startIndex = bb.writerIndex(); |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 292 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 293 | //:: fields_with_length_member = {} |
| 294 | //:: for prop in msg.members: |
| 295 | //:: if prop.c_name in fields_with_length_member: |
| 296 | int ${prop.name}StartIndex = bb.writerIndex(); |
| 297 | //:: #endif |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 298 | //:: if prop.is_virtual: |
| 299 | //:: continue |
| 300 | //:: elif prop.is_data: |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 301 | ${prop.java_type.write_op(version, "message." + prop.name, pub_type=True)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 302 | //:: elif prop.is_pad: |
| 303 | // pad: ${prop.length} bytes |
| 304 | bb.writeZero(${prop.length}); |
| 305 | //:: elif prop.is_fixed_value: |
| 306 | // fixed value property ${prop.name} = ${prop.value} |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 307 | ${prop.java_type.write_op(version, prop.priv_value, pub_type=False)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 308 | //:: elif prop.is_length_value: |
| 309 | // ${prop.name} is length of variable message, will be updated at the end |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 310 | //:: if not msg.is_fixed_length: |
| 311 | int lengthIndex = bb.writerIndex(); |
| 312 | //:: #end |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 313 | ${prop.java_type.write_op(version, 0)}; |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 314 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 315 | //:: elif prop.is_field_length_value: |
| 316 | //:: fields_with_length_member[prop.member.field_name] = prop.name |
| 317 | // ${prop.name} is length indicator for ${prop.member.field_name}, will be |
| 318 | // udpated when ${prop.member.field_name} has been written |
| 319 | int ${prop.name}Index = bb.writerIndex(); |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 320 | ${prop.java_type.write_op(version, 0, pub_type=False)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 321 | //:: else: |
| 322 | // FIXME: todo write ${prop.name} |
| 323 | //:: #endif |
| 324 | //:: if prop.c_name in fields_with_length_member: |
| 325 | //:: length_member_name = fields_with_length_member[prop.c_name] |
| 326 | // update field length member ${length_member_name} |
| 327 | int ${prop.name}Length = bb.writerIndex() - ${prop.name}StartIndex; |
| 328 | bb.setShort(${length_member_name}Index, ${prop.name}Length); |
| 329 | //:: #endif |
| 330 | //:: #endfor |
| 331 | |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 332 | //:: if not msg.is_fixed_length: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 333 | // update length field |
| 334 | int length = bb.writerIndex() - startIndex; |
Andreas Wundsam | 5da6851 | 2013-10-22 22:18:00 -0700 | [diff] [blame] | 335 | //:: if msg.align: |
| 336 | int alignedLength = ((length + ${msg.align-1})/${msg.align} * ${msg.align}); |
| 337 | //:: #endif |
| 338 | bb.setShort(lengthIndex, ${"alignedLength" if msg.length_includes_align else "length"}); |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 339 | //:: if msg.align: |
| 340 | // align message to ${msg.align} bytes |
Andreas Wundsam | 5da6851 | 2013-10-22 22:18:00 -0700 | [diff] [blame] | 341 | bb.writeZero(alignedLength - length); |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 342 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 343 | //:: #end |
| 344 | |
| 345 | } |
| 346 | } |
| 347 | |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 348 | @Override |
Andreas Wundsam | d7d5bb3 | 2013-07-30 12:26:31 -0700 | [diff] [blame] | 349 | public String toString() { |
| 350 | StringBuilder b = new StringBuilder("${msg.name}("); |
| 351 | //:: for i, prop in enumerate(msg.data_members): |
| 352 | //:: if i > 0: |
| 353 | b.append(", "); |
| 354 | //:: #endif |
| 355 | b.append("${prop.name}=").append(${ "Arrays.toString(%s)" % prop.name if prop.java_type.is_array else prop.name }); |
| 356 | //:: #endfor |
| 357 | b.append(")"); |
| 358 | return b.toString(); |
| 359 | } |
| 360 | |
| 361 | |
| 362 | @Override |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 363 | public boolean equals(Object obj) { |
| 364 | if (this == obj) |
| 365 | return true; |
| 366 | if (obj == null) |
| 367 | return false; |
| 368 | if (getClass() != obj.getClass()) |
| 369 | return false; |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 370 | //:: if len(msg.data_members) > 0: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 371 | ${msg.name} other = (${msg.name}) obj; |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 372 | //:: #endif |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 373 | |
| 374 | //:: for prop in msg.data_members: |
| 375 | //:: if prop.java_type.is_primitive: |
| 376 | if( ${prop.name} != other.${prop.name}) |
| 377 | return false; |
| 378 | //:: elif prop.java_type.is_array: |
| 379 | if (!Arrays.equals(${prop.name}, other.${prop.name})) |
| 380 | return false; |
Andreas Wundsam | d7d5bb3 | 2013-07-30 12:26:31 -0700 | [diff] [blame] | 381 | //:: else: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 382 | if (${prop.name} == null) { |
| 383 | if (other.${prop.name} != null) |
| 384 | return false; |
| 385 | } else if (!${prop.name}.equals(other.${prop.name})) |
| 386 | return false; |
| 387 | //:: #endif |
| 388 | //:: #endfor |
| 389 | return true; |
| 390 | } |
| 391 | |
| 392 | @Override |
| 393 | public int hashCode() { |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 394 | //:: if len(msg.data_members) > 0: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 395 | final int prime = 31; |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 396 | //:: #endif |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 397 | int result = 1; |
| 398 | |
| 399 | //:: for prop in msg.data_members: |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 400 | //:: if prop.java_type.pub_type == 'long': |
| 401 | result = prime * (int) (${prop.name} ^ (${prop.name} >>> 32)); |
Rob Vaterlaus | feee371 | 2013-09-30 11:24:19 -0700 | [diff] [blame] | 402 | //:: elif prop.java_type.pub_type == 'boolean': |
| 403 | result = prime * result + (${prop.name} ? 1231 : 1237); |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 404 | //:: elif prop.java_type.is_primitive: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 405 | result = prime * result + ${prop.name}; |
| 406 | //:: elif prop.java_type.is_array: |
| 407 | result = prime * result + Arrays.hashCode(${prop.name}); |
| 408 | //:: else: |
| 409 | result = prime * result + ((${prop.name} == null) ? 0 : ${prop.name}.hashCode()); |
| 410 | //:: #endif |
| 411 | //:: #endfor |
| 412 | return result; |
| 413 | } |
| 414 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 415 | |
| 416 | } |