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