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}); |
| 198 | //:: elif prop.is_fixed_value: |
| 199 | // fixed value property ${prop.name} == ${prop.value} |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 200 | ${prop.java_type.priv_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)}; |
| 201 | if(${prop.name} != ${prop.priv_value}) |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 202 | throw new OFParseError("Wrong ${prop.name}: Expected=${prop.enum_value}(${prop.value}), got="+${prop.name}); |
| 203 | //:: elif prop.is_length_value: |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 204 | ${prop.java_type.public_type} ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 205 | if(${prop.name} < MINIMUM_LENGTH) |
| 206 | throw new OFParseError("Wrong ${prop.name}: Expected to be >= " + MINIMUM_LENGTH + ", was: " + ${prop.name}); |
| 207 | //:: elif prop.is_field_length_value: |
| 208 | //:: fields_with_length_member[prop.member.field_name] = prop.name |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 209 | int ${prop.name} = ${prop.java_type.read_op(version, pub_type=False)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 210 | //:: else: |
| 211 | // fixme: todo ${prop.name} |
| 212 | //:: #endif |
Yotam Harchol | 0178c20 | 2013-09-05 16:25:50 -0700 | [diff] [blame] | 213 | //:: if prop.is_length_value or prop.is_field_length_value: |
| 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 | } |
| 219 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 220 | //:: #endfor |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 221 | //:: if msg.align: |
| 222 | // align message to ${msg.align} bytes |
| 223 | bb.skipBytes(((length + ${msg.align-1})/${msg.align} * ${msg.align} ) - length ); |
| 224 | //:: #endif |
| 225 | |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 226 | //:: if msg.data_members: |
Andreas Wundsam | f89f782 | 2013-09-23 14:49:24 -0700 | [diff] [blame] | 227 | //:: if os.path.exists("%s/custom/%s.Reader_normalize_stanza.java" % (template_dir, msg.name)): |
| 228 | //:: include("custom/%s.Reader_normalize_stanza.java" % msg.name, msg=msg, has_parent=False) |
| 229 | //:: #endif |
| 230 | return new ${impl_class}( |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 231 | ${",\n ".join( |
| 232 | [ prop.name for prop in msg.data_members])} |
| 233 | ); |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 234 | //:: else: |
| 235 | return INSTANCE; |
| 236 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
Yotam Harchol | 5c9d6f4 | 2013-08-01 11:09:20 -0700 | [diff] [blame] | 240 | public void writeTo(ChannelBuffer bb) { |
| 241 | WRITER.write(bb, this); |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | final static Writer WRITER = new Writer(); |
| 245 | static class Writer implements OFMessageWriter<${impl_class}> { |
| 246 | @Override |
Andreas Wundsam | a94273b | 2013-08-01 22:11:33 -0700 | [diff] [blame] | 247 | public void write(ChannelBuffer bb, ${impl_class} message) { |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 248 | //:: if not msg.is_fixed_length: |
Andreas Wundsam | 482f6d9 | 2013-07-24 16:10:21 -0700 | [diff] [blame] | 249 | int startIndex = bb.writerIndex(); |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 250 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 251 | //:: fields_with_length_member = {} |
| 252 | //:: for prop in msg.members: |
| 253 | //:: if prop.c_name in fields_with_length_member: |
| 254 | int ${prop.name}StartIndex = bb.writerIndex(); |
| 255 | //:: #endif |
Andreas Wundsam | 99e931d | 2013-08-22 07:53:53 -0700 | [diff] [blame] | 256 | //:: if prop.is_virtual: |
| 257 | //:: continue |
| 258 | //:: elif prop.is_data: |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 259 | ${prop.java_type.write_op(version, "message." + prop.name, pub_type=True)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 260 | //:: elif prop.is_pad: |
| 261 | // pad: ${prop.length} bytes |
| 262 | bb.writeZero(${prop.length}); |
| 263 | //:: elif prop.is_fixed_value: |
| 264 | // fixed value property ${prop.name} = ${prop.value} |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 265 | ${prop.java_type.write_op(version, prop.priv_value, pub_type=False)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 266 | //:: elif prop.is_length_value: |
| 267 | // ${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] | 268 | //:: if not msg.is_fixed_length: |
| 269 | int lengthIndex = bb.writerIndex(); |
| 270 | //:: #end |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 271 | ${prop.java_type.write_op(version, 0)}; |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 272 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 273 | //:: elif prop.is_field_length_value: |
| 274 | //:: fields_with_length_member[prop.member.field_name] = prop.name |
| 275 | // ${prop.name} is length indicator for ${prop.member.field_name}, will be |
| 276 | // udpated when ${prop.member.field_name} has been written |
| 277 | int ${prop.name}Index = bb.writerIndex(); |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 278 | ${prop.java_type.write_op(version, 0, pub_type=False)}; |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 279 | //:: else: |
| 280 | // FIXME: todo write ${prop.name} |
| 281 | //:: #endif |
| 282 | //:: if prop.c_name in fields_with_length_member: |
| 283 | //:: length_member_name = fields_with_length_member[prop.c_name] |
| 284 | // update field length member ${length_member_name} |
| 285 | int ${prop.name}Length = bb.writerIndex() - ${prop.name}StartIndex; |
| 286 | bb.setShort(${length_member_name}Index, ${prop.name}Length); |
| 287 | //:: #endif |
| 288 | //:: #endfor |
| 289 | |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 290 | //:: if not msg.is_fixed_length: |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 291 | // update length field |
| 292 | int length = bb.writerIndex() - startIndex; |
Andreas Wundsam | 001b182 | 2013-08-02 22:25:55 -0700 | [diff] [blame] | 293 | bb.setShort(lengthIndex, length); |
Andreas Wundsam | 758c9cc | 2013-08-01 22:16:06 -0700 | [diff] [blame] | 294 | //:: if msg.align: |
| 295 | // align message to ${msg.align} bytes |
| 296 | bb.writeZero( ((length + ${msg.align-1})/${msg.align} * ${msg.align}) - length); |
| 297 | //:: #endif |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 298 | //:: #end |
| 299 | |
| 300 | } |
| 301 | } |
| 302 | |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 303 | @Override |
Andreas Wundsam | d7d5bb3 | 2013-07-30 12:26:31 -0700 | [diff] [blame] | 304 | public String toString() { |
| 305 | StringBuilder b = new StringBuilder("${msg.name}("); |
| 306 | //:: for i, prop in enumerate(msg.data_members): |
| 307 | //:: if i > 0: |
| 308 | b.append(", "); |
| 309 | //:: #endif |
| 310 | b.append("${prop.name}=").append(${ "Arrays.toString(%s)" % prop.name if prop.java_type.is_array else prop.name }); |
| 311 | //:: #endfor |
| 312 | b.append(")"); |
| 313 | return b.toString(); |
| 314 | } |
| 315 | |
| 316 | |
| 317 | @Override |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 318 | public boolean equals(Object obj) { |
| 319 | if (this == obj) |
| 320 | return true; |
| 321 | if (obj == null) |
| 322 | return false; |
| 323 | if (getClass() != obj.getClass()) |
| 324 | return false; |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 325 | //:: if len(msg.data_members) > 0: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 326 | ${msg.name} other = (${msg.name}) obj; |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 327 | //:: #endif |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 328 | |
| 329 | //:: for prop in msg.data_members: |
| 330 | //:: if prop.java_type.is_primitive: |
| 331 | if( ${prop.name} != other.${prop.name}) |
| 332 | return false; |
| 333 | //:: elif prop.java_type.is_array: |
| 334 | if (!Arrays.equals(${prop.name}, other.${prop.name})) |
| 335 | return false; |
Andreas Wundsam | d7d5bb3 | 2013-07-30 12:26:31 -0700 | [diff] [blame] | 336 | //:: else: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 337 | if (${prop.name} == null) { |
| 338 | if (other.${prop.name} != null) |
| 339 | return false; |
| 340 | } else if (!${prop.name}.equals(other.${prop.name})) |
| 341 | return false; |
| 342 | //:: #endif |
| 343 | //:: #endfor |
| 344 | return true; |
| 345 | } |
| 346 | |
| 347 | @Override |
| 348 | public int hashCode() { |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 349 | //:: if len(msg.data_members) > 0: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 350 | final int prime = 31; |
Yotam Harchol | a86e425 | 2013-09-06 15:36:28 -0700 | [diff] [blame] | 351 | //:: #endif |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 352 | int result = 1; |
| 353 | |
| 354 | //:: for prop in msg.data_members: |
Andreas Wundsam | 2bf357c | 2013-08-03 22:50:40 -0700 | [diff] [blame] | 355 | //:: if prop.java_type.pub_type == 'long': |
| 356 | result = prime * (int) (${prop.name} ^ (${prop.name} >>> 32)); |
| 357 | //:: elif prop.java_type.is_primitive: |
Andreas Wundsam | eeefb55 | 2013-07-30 11:04:35 -0700 | [diff] [blame] | 358 | result = prime * result + ${prop.name}; |
| 359 | //:: elif prop.java_type.is_array: |
| 360 | result = prime * result + Arrays.hashCode(${prop.name}); |
| 361 | //:: else: |
| 362 | result = prime * result + ((${prop.name} == null) ? 0 : ${prop.name}.hashCode()); |
| 363 | //:: #endif |
| 364 | //:: #endfor |
| 365 | return result; |
| 366 | } |
| 367 | |
Andreas Wundsam | 2730346 | 2013-07-16 12:52:35 -0700 | [diff] [blame] | 368 | |
| 369 | } |