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 |
| 59 | |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 60 | //:: if msg.data_members: |
| 61 | // package private constructor - used by readers, builders, and factory |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 62 | ${impl_class}(${ |
| 63 | ", ".join("%s %s" %(prop.java_type.public_type, prop.name) for prop in msg.data_members) }) { |
| 64 | //:: for prop in msg.data_members: |
| 65 | this.${prop.name} = ${prop.name}; |
| 66 | //:: #endfor |
| 67 | } |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 68 | //:: else: |
| 69 | final static ${impl_class} INSTANCE = new ${impl_class}(); |
| 70 | // private empty constructor - use shared instance! |
| 71 | private ${impl_class}() { |
| 72 | } |
| 73 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 74 | |
| 75 | // Accessors for OF message fields |
Yotam Harchol | f25e814 | 2013-09-09 14:30:13 -0700 | [diff] [blame] | 76 | //:: 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] | 77 | |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 78 | //:: if os.path.exists("%s/custom/%s.java" % (template_dir, msg.name)): |
| 79 | //:: include("custom/%s.java" % msg.name, msg=msg) |
| 80 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 81 | |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 82 | //:: if msg.data_members: |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 83 | public ${msg.interface.name}.Builder createBuilder() { |
| 84 | return new BuilderWithParent(this); |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 87 | static class BuilderWithParent implements ${msg.interface.name}.Builder { |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 88 | final ${impl_class} parentMessage; |
| 89 | |
| 90 | // OF message fields |
| 91 | //:: for prop in msg.data_members: |
| 92 | private boolean ${prop.name}Set; |
| 93 | private ${prop.java_type.public_type} ${prop.name}; |
| 94 | //:: #endfor |
| 95 | |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 96 | BuilderWithParent(${impl_class} parentMessage) { |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 97 | this.parentMessage = parentMessage; |
| 98 | } |
| 99 | |
Yotam Harchol | f25e814 | 2013-09-09 14:30:13 -0700 | [diff] [blame] | 100 | //:: 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] | 101 | |
Andreas Wundsam | f89f782 | 2013-09-23 14:49:24 -0700 | [diff] [blame] | 102 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 103 | @Override |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 104 | public ${msg.interface.name} build() { |
Andreas Wundsam | f89f782 | 2013-09-23 14:49:24 -0700 | [diff] [blame] | 105 | //:: for prop in msg.data_members: |
| 106 | ${prop.java_type.public_type} ${prop.name} = this.${prop.name}Set ? this.${prop.name} : parentMessage.${prop.name}; |
| 107 | //:: if not prop.is_nullable and not prop.java_type.is_primitive: |
| 108 | if(${prop.name} == null) |
| 109 | throw new NullPointerException("Property ${prop.name} must not be null"); |
| 110 | //:: #endif |
| 111 | //:: #endfor |
| 112 | |
| 113 | // |
| 114 | //:: if os.path.exists("%s/custom/%s.Builder_normalize_stanza.java" % (template_dir, msg.name)): |
| 115 | //:: include("custom/%s.Builder_normalize_stanza.java" % msg.name, msg=msg, has_parent=False) |
| 116 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 117 | return new ${impl_class}( |
Andreas Wundsam | f89f782 | 2013-09-23 14:49:24 -0700 | [diff] [blame] | 118 | //:: for i, prop in enumerate(msg.data_members): |
| 119 | //:: comma = "," if i < len(msg.data_members)-1 else "" |
| 120 | ${prop.name}${comma} |
| 121 | //:: #endfor |
| 122 | ); |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 123 | } |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 124 | //:: 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] | 125 | //:: include("custom/%s.Builder.java" % msg.name, msg=msg, has_parent=True) |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 126 | //:: #endif |
| 127 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Andreas Wundsam | 5204de2 | 2013-07-30 11:34:45 -0700 | [diff] [blame] | 130 | static class Builder implements ${msg.interface.name}.Builder { |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 131 | // OF message fields |
| 132 | //:: for prop in msg.data_members: |
| 133 | private boolean ${prop.name}Set; |
| 134 | private ${prop.java_type.public_type} ${prop.name}; |
| 135 | //:: #endfor |
| 136 | |
Yotam Harchol | f25e814 | 2013-09-09 14:30:13 -0700 | [diff] [blame] | 137 | //:: 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] | 138 | // |
| 139 | @Override |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 140 | public ${msg.interface.name} build() { |
Andreas Wundsam | f89f782 | 2013-09-23 14:49:24 -0700 | [diff] [blame] | 141 | //:: for prop in msg.data_members: |
| 142 | //:: if prop.default_value: |
| 143 | ${prop.java_type.public_type} ${prop.name} = this.${prop.name}Set ? this.${prop.name} : ${prop.default_name}; |
| 144 | //:: else: |
| 145 | if(!this.${prop.name}Set) |
| 146 | throw new IllegalStateException("Property ${prop.name} doesn't have default value -- must be set"); |
| 147 | //:: #endif |
| 148 | //:: if not prop.is_nullable and not prop.java_type.is_primitive: |
| 149 | if(${prop.name} == null) |
| 150 | throw new NullPointerException("Property ${prop.name} must not be null"); |
| 151 | //:: #endif |
| 152 | //:: #endfor |
| 153 | |
| 154 | //:: if os.path.exists("%s/custom/%s.Builder_normalize_stanza.java" % (template_dir, msg.name)): |
| 155 | //:: include("custom/%s.Builder_normalize_stanza.java" % msg.name, msg=msg, has_parent=False) |
| 156 | //:: #endif |
| 157 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 158 | return new ${impl_class}( |
Andreas Wundsam | f89f782 | 2013-09-23 14:49:24 -0700 | [diff] [blame] | 159 | //:: for i, prop in enumerate(msg.data_members): |
| 160 | //:: comma = "," if i < len(msg.data_members)-1 else "" |
| 161 | ${prop.name}${comma} |
| 162 | //:: #endfor |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 163 | ); |
| 164 | } |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 165 | //:: 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] | 166 | //:: include("custom/%s.Builder.java" % msg.name, msg=msg, has_parent=False) |
Andreas Wundsam | bc679f7 | 2013-08-01 22:13:09 -0700 | [diff] [blame] | 167 | //:: #endif |
| 168 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 169 | } |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 170 | //:: else: |
| 171 | // no data members - do not support builder |
| 172 | public ${msg.interface.name}.Builder createBuilder() { |
| 173 | throw new UnsupportedOperationException("${impl_class} has no mutable properties -- builder unneeded"); |
| 174 | } |
| 175 | //:: #endif |
| 176 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 177 | |
| 178 | final static Reader READER = new Reader(); |
| 179 | static class Reader implements OFMessageReader<${msg.interface.name}> { |
| 180 | @Override |
| 181 | public ${msg.interface.name} readFrom(ChannelBuffer bb) throws OFParseError { |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 182 | //:: for prop in msg.members: |
| 183 | //:: 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] | 184 | int start = bb.readerIndex(); |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 185 | //:: break |
| 186 | //:: #endif |
| 187 | //:: #endfor |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 188 | //:: fields_with_length_member = {} |
| 189 | //:: for prop in msg.members: |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 190 | //:: if prop.is_virtual: |
| 191 | //:: continue |
| 192 | //:: elif prop.is_data: |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 193 | ${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] | 194 | length=fields_with_length_member[prop.c_name] if prop.c_name in fields_with_length_member else None)}; |
| 195 | //:: elif prop.is_pad: |
| 196 | // pad: ${prop.length} bytes |
| 197 | bb.skipBytes(${prop.length}); |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 198 | //:: elif prop.is_length_value: |
Andreas Wundsam | 83d877a | 2013-09-30 14:26:44 -0700 | [diff] [blame] | 199 | ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True)}; |
| 200 | //:: if prop.is_fixed_value: |
| 201 | if(${prop.name} != ${prop.value}) |
| 202 | throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name}); |
| 203 | //:: else: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 204 | if(${prop.name} < MINIMUM_LENGTH) |
| 205 | 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] | 206 | //:: #endif |
Yotam Harchol | 0178c20 | 2013-09-05 16:25:50 -0700 | [diff] [blame] | 207 | if(bb.readableBytes() + (bb.readerIndex() - start) < ${prop.name}) { |
| 208 | // Buffer does not have all data yet |
| 209 | bb.readerIndex(start); |
| 210 | return null; |
| 211 | } |
Andreas Wundsam | 83d877a | 2013-09-30 14:26:44 -0700 | [diff] [blame] | 212 | //:: elif prop.is_fixed_value: |
| 213 | // fixed value property ${prop.name} == ${prop.value} |
| 214 | ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)}; |
| 215 | if(${prop.name} != ${prop.priv_value}) |
| 216 | throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name}); |
| 217 | //:: elif prop.is_field_length_value: |
| 218 | //:: fields_with_length_member[prop.member.field_name] = prop.name |
| 219 | ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=True)}; |
| 220 | //:: else: |
| 221 | // fixme: todo ${prop.name} |
Yotam Harchol | 0178c20 | 2013-09-05 16:25:50 -0700 | [diff] [blame] | 222 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 223 | //:: #endfor |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 224 | //:: if msg.align: |
| 225 | // align message to ${msg.align} bytes |
| 226 | bb.skipBytes(((length + ${msg.align-1})/${msg.align} * ${msg.align} ) - length ); |
| 227 | //:: #endif |
| 228 | |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 229 | //:: if msg.data_members: |
Andreas Wundsam | f89f782 | 2013-09-23 14:49:24 -0700 | [diff] [blame] | 230 | //:: if os.path.exists("%s/custom/%s.Reader_normalize_stanza.java" % (template_dir, msg.name)): |
| 231 | //:: include("custom/%s.Reader_normalize_stanza.java" % msg.name, msg=msg, has_parent=False) |
| 232 | //:: #endif |
| 233 | return new ${impl_class}( |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 234 | ${",\n ".join( |
| 235 | [ prop.name for prop in msg.data_members])} |
| 236 | ); |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 237 | //:: else: |
| 238 | return INSTANCE; |
| 239 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
Yotam Harchol | 5c9d6f4 | 2013-08-01 11:09:20 -0700 | [diff] [blame] | 243 | public void writeTo(ChannelBuffer bb) { |
| 244 | WRITER.write(bb, this); |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | final static Writer WRITER = new Writer(); |
| 248 | static class Writer implements OFMessageWriter<${impl_class}> { |
| 249 | @Override |
Andreas Wundsam | a94273b | 2013-08-01 22:11:33 -0700 | [diff] [blame] | 250 | public void write(ChannelBuffer bb, ${impl_class} message) { |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 251 | //:: if not msg.is_fixed_length: |
Andreas Wundsam | 482f6d9 | 2013-07-24 16:10:21 -0700 | [diff] [blame] | 252 | int startIndex = bb.writerIndex(); |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 253 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 254 | //:: fields_with_length_member = {} |
| 255 | //:: for prop in msg.members: |
| 256 | //:: if prop.c_name in fields_with_length_member: |
| 257 | int ${prop.name}StartIndex = bb.writerIndex(); |
| 258 | //:: #endif |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 259 | //:: if prop.is_virtual: |
| 260 | //:: continue |
| 261 | //:: elif prop.is_data: |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 262 | ${prop.java_type.write_op(version, "message." + prop.name, pub_type=True)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 263 | //:: elif prop.is_pad: |
| 264 | // pad: ${prop.length} bytes |
| 265 | bb.writeZero(${prop.length}); |
| 266 | //:: elif prop.is_fixed_value: |
| 267 | // fixed value property ${prop.name} = ${prop.value} |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 268 | ${prop.java_type.write_op(version, prop.priv_value, pub_type=False)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 269 | //:: elif prop.is_length_value: |
| 270 | // ${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] | 271 | //:: if not msg.is_fixed_length: |
| 272 | int lengthIndex = bb.writerIndex(); |
| 273 | //:: #end |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 274 | ${prop.java_type.write_op(version, 0)}; |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 275 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 276 | //:: elif prop.is_field_length_value: |
| 277 | //:: fields_with_length_member[prop.member.field_name] = prop.name |
| 278 | // ${prop.name} is length indicator for ${prop.member.field_name}, will be |
| 279 | // udpated when ${prop.member.field_name} has been written |
| 280 | int ${prop.name}Index = bb.writerIndex(); |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 281 | ${prop.java_type.write_op(version, 0, pub_type=False)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 282 | //:: else: |
| 283 | // FIXME: todo write ${prop.name} |
| 284 | //:: #endif |
| 285 | //:: if prop.c_name in fields_with_length_member: |
| 286 | //:: length_member_name = fields_with_length_member[prop.c_name] |
| 287 | // update field length member ${length_member_name} |
| 288 | int ${prop.name}Length = bb.writerIndex() - ${prop.name}StartIndex; |
| 289 | bb.setShort(${length_member_name}Index, ${prop.name}Length); |
| 290 | //:: #endif |
| 291 | //:: #endfor |
| 292 | |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 293 | //:: if not msg.is_fixed_length: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 294 | // update length field |
| 295 | int length = bb.writerIndex() - startIndex; |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 296 | bb.setShort(lengthIndex, length); |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 297 | //:: if msg.align: |
| 298 | // align message to ${msg.align} bytes |
| 299 | bb.writeZero( ((length + ${msg.align-1})/${msg.align} * ${msg.align}) - length); |
| 300 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 301 | //:: #end |
| 302 | |
| 303 | } |
| 304 | } |
| 305 | |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 306 | @Override |
Andreas Wundsam | d7d5bb3 | 2013-07-30 12:26:31 -0700 | [diff] [blame] | 307 | public String toString() { |
| 308 | StringBuilder b = new StringBuilder("${msg.name}("); |
| 309 | //:: for i, prop in enumerate(msg.data_members): |
| 310 | //:: if i > 0: |
| 311 | b.append(", "); |
| 312 | //:: #endif |
| 313 | b.append("${prop.name}=").append(${ "Arrays.toString(%s)" % prop.name if prop.java_type.is_array else prop.name }); |
| 314 | //:: #endfor |
| 315 | b.append(")"); |
| 316 | return b.toString(); |
| 317 | } |
| 318 | |
| 319 | |
| 320 | @Override |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 321 | public boolean equals(Object obj) { |
| 322 | if (this == obj) |
| 323 | return true; |
| 324 | if (obj == null) |
| 325 | return false; |
| 326 | if (getClass() != obj.getClass()) |
| 327 | return false; |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 328 | //:: if len(msg.data_members) > 0: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 329 | ${msg.name} other = (${msg.name}) obj; |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 330 | //:: #endif |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 331 | |
| 332 | //:: for prop in msg.data_members: |
| 333 | //:: if prop.java_type.is_primitive: |
| 334 | if( ${prop.name} != other.${prop.name}) |
| 335 | return false; |
| 336 | //:: elif prop.java_type.is_array: |
| 337 | if (!Arrays.equals(${prop.name}, other.${prop.name})) |
| 338 | return false; |
Andreas Wundsam | d7d5bb3 | 2013-07-30 12:26:31 -0700 | [diff] [blame] | 339 | //:: else: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 340 | if (${prop.name} == null) { |
| 341 | if (other.${prop.name} != null) |
| 342 | return false; |
| 343 | } else if (!${prop.name}.equals(other.${prop.name})) |
| 344 | return false; |
| 345 | //:: #endif |
| 346 | //:: #endfor |
| 347 | return true; |
| 348 | } |
| 349 | |
| 350 | @Override |
| 351 | public int hashCode() { |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 352 | //:: if len(msg.data_members) > 0: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 353 | final int prime = 31; |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 354 | //:: #endif |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 355 | int result = 1; |
| 356 | |
| 357 | //:: for prop in msg.data_members: |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 358 | //:: if prop.java_type.pub_type == 'long': |
| 359 | result = prime * (int) (${prop.name} ^ (${prop.name} >>> 32)); |
| 360 | //:: elif prop.java_type.is_primitive: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 361 | result = prime * result + ${prop.name}; |
| 362 | //:: elif prop.java_type.is_array: |
| 363 | result = prime * result + Arrays.hashCode(${prop.name}); |
| 364 | //:: else: |
| 365 | result = prime * result + ((${prop.name} == null) ? 0 : ${prop.name}.hashCode()); |
| 366 | //:: #endif |
| 367 | //:: #endfor |
| 368 | return result; |
| 369 | } |
| 370 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 371 | |
| 372 | } |