Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1 | import time |
| 2 | import os |
| 3 | import os.path |
| 4 | from string import Template |
| 5 | from parser import * |
| 6 | from generator import * |
| 7 | |
| 8 | HEADER_COMMENT = """/** |
| 9 | * Autogenerated by Thrift |
| 10 | * ${date} |
| 11 | * |
| 12 | * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING |
| 13 | */ |
| 14 | """ |
| 15 | |
| 16 | CPP_TYPES_HEADER = Template(HEADER_COMMENT+""" |
| 17 | #if !defined(${source}_types_h_) |
| 18 | #define ${source}_types_h_ 1 |
| 19 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 20 | #include <Thrift.h> |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 21 | """) |
| 22 | |
| 23 | CPP_TYPES_FOOTER = Template(""" |
| 24 | #endif // !defined(${source}_types_h_) |
| 25 | """) |
| 26 | |
| 27 | CPP_SERVICES_HEADER = Template(HEADER_COMMENT+""" |
| 28 | #if !defined(${source}_h_) |
| 29 | #define ${source}_h_ 1 |
| 30 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 31 | #include <Thrift.h> |
| 32 | #include <TProcessor.h> |
| 33 | #include <protocol/TProtocol.h> |
| 34 | #include <transport/TTransport.h> |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 35 | #include \"${source}_types.h\" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 36 | """) |
| 37 | |
| 38 | CPP_SERVICES_FOOTER = Template(""" |
| 39 | #endif // !defined(${source}_h_)""") |
| 40 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 41 | CPP_IMPL_HEADER = Template(HEADER_COMMENT+""" |
| 42 | #include \"${source}.h\" |
| 43 | """) |
| 44 | |
| 45 | CPP_IMPL_FOOTER = Template("") |
| 46 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 47 | def cpp_debug(arg): |
| 48 | print(arg) |
| 49 | |
| 50 | class Indenter(object): |
| 51 | def __init__(self, level=0, step=4): |
| 52 | self.level = level |
| 53 | self.step = step |
| 54 | self.chunk = "" |
| 55 | for i in range(step): |
| 56 | self.chunk+= " " |
| 57 | self.prefix="" |
| 58 | |
| 59 | def inc(self): |
| 60 | self.level+= self.step |
| 61 | self.prefix += self.chunk |
| 62 | |
| 63 | def dec(self): |
| 64 | self.level-= self.step |
| 65 | if(self.level < 0): |
| 66 | raise Exception, "Illegal indent level" |
| 67 | self.prefix = self.prefix[:self.level] |
| 68 | |
| 69 | def __call__(self): |
| 70 | return self.prefix |
| 71 | |
| 72 | class CFile(file): |
| 73 | |
| 74 | def __init__(self, name, flags): |
| 75 | file.__init__(self, name, flags) |
| 76 | self.indent = Indenter() |
| 77 | self.newline = True |
| 78 | |
| 79 | def rwrite(self, value): |
| 80 | file.write(self, value) |
| 81 | |
| 82 | def write(self, value=""): |
| 83 | if self.newline: |
| 84 | self.rwrite(self.indent()) |
| 85 | self.newline = False |
| 86 | self.rwrite(value) |
| 87 | |
| 88 | def writeln(self, value=""): |
| 89 | self.write(value+"\n") |
| 90 | self.newline = True |
| 91 | |
| 92 | def beginBlock(self): |
| 93 | self.writeln("{") |
| 94 | self.indent.inc(); |
| 95 | |
| 96 | def endBlock(self, suffix=""): |
| 97 | self.indent.dec(); |
| 98 | self.writeln("}"+suffix) |
| 99 | |
| 100 | CPP_PRIMITIVE_MAP = { |
| 101 | "void" : "void", |
| 102 | "bool" : "bool", |
| 103 | "string": "std::string", |
| 104 | "utf7": "std::string", |
| 105 | "utf8": "std::wstring", |
| 106 | "utf16": "std::utf16", |
| 107 | "byte" : "uint8_t", |
| 108 | "i08": "int8_t", |
| 109 | "i16": "int16_t", |
| 110 | "i32": "int32_t", |
| 111 | "i64": "int64_t", |
| 112 | "u08": "uint8_t", |
| 113 | "u16": "uint16_t", |
| 114 | "u32": "uint32_t", |
| 115 | "u64": "uint64_t", |
| 116 | "float": "double" |
| 117 | } |
| 118 | |
| 119 | CPP_CONTAINER_MAP = { |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 120 | MapType : "std::map", |
| 121 | ListType: "std::list", |
| 122 | SetType : "std::set", |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | def typeToCTypeDeclaration(ttype): |
| 126 | |
| 127 | if isinstance(ttype, PrimitiveType): |
| 128 | return CPP_PRIMITIVE_MAP[ttype.name] |
| 129 | |
| 130 | elif isinstance(ttype, CollectionType): |
| 131 | |
| 132 | result = CPP_CONTAINER_MAP[type(ttype)]+"<" |
| 133 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 134 | if isinstance(ttype, MapType): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 135 | result+= typeToCTypeDeclaration(ttype.keyType)+", "+ typeToCTypeDeclaration(ttype.valueType) |
| 136 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 137 | elif isinstance(ttype, SetType) or isinstance(ttype, ListType): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 138 | result+= typeToCTypeDeclaration(ttype.valueType) |
| 139 | |
| 140 | else: |
| 141 | raise Exception, "Unknown Collection Type "+str(ttype) |
| 142 | |
| 143 | result+= "> " |
| 144 | |
| 145 | return result |
| 146 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 147 | elif isinstance(ttype, StructType): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 148 | return "struct "+ttype.name |
| 149 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 150 | elif isinstance(ttype, TypedefType): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 151 | return ttype.name; |
| 152 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 153 | elif isinstance(ttype, EnumType): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 154 | return ttype.name; |
| 155 | |
| 156 | elif isinstance(ttype, Function): |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 157 | return typeToCTypeDeclaration(ttype.returnType())+ " "+ttype.name+"("+string.join([typeToCTypeDeclaration(arg) for arg in ttype.args()], ", ")+")" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 158 | |
| 159 | elif isinstance(ttype, Field): |
| 160 | return typeToCTypeDeclaration(ttype.type)+ " "+ttype.name |
| 161 | |
| 162 | else: |
| 163 | raise Exception, "Unknown type "+str(ttype) |
| 164 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 165 | def toTypeDefDefinition(typedef): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 166 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 167 | return "typedef "+typeToCTypeDeclaration(typedef.definitionType)+" "+typedef.name+";" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 168 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 169 | def toEnumDefinition(enum): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 170 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 171 | result = "enum "+enum.name+" {\n" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 172 | |
| 173 | first = True |
| 174 | |
| 175 | for ed in enum.enumDefs: |
| 176 | if first: |
| 177 | first = False |
| 178 | else: |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 179 | result+= ",\n" |
| 180 | result+= " "+ed.name+" = "+str(ed.id) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 181 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 182 | result+= "\n};\n" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 183 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 184 | return result |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 185 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 186 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 187 | def toStructDefinition(struct): |
| 188 | |
| 189 | result = "struct "+struct.name+" {\n" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 190 | |
| 191 | for field in struct.fieldList: |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 192 | if toCanonicalType(field.type) != VOID_TYPE: |
| 193 | result += " "+typeToCTypeDeclaration(field)+";\n" |
| 194 | |
| 195 | result+= " struct {\n" |
| 196 | |
| 197 | for field in struct.fieldList: |
| 198 | result+= " bool "+field.name+";\n" |
| 199 | result+= " } __isset;\n" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 200 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 201 | result+= "};\n" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 202 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 203 | return result |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 204 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 205 | CPP_DEFINITION_MAP = { |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 206 | TypedefType : toTypeDefDefinition, |
| 207 | EnumType : toEnumDefinition, |
| 208 | StructType : toStructDefinition, |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 209 | Service : None |
| 210 | } |
| 211 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 212 | def toDefinitions(definitions): |
| 213 | |
| 214 | result = "" |
| 215 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 216 | for definition in definitions: |
| 217 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 218 | writer = CPP_DEFINITION_MAP[type(definition)] |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 219 | |
| 220 | if writer: |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 221 | result+= writer(definition)+"\n" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 222 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 223 | return result |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 224 | |
| 225 | CPP_THRIFT_NS = "facebook::thrift" |
| 226 | |
| 227 | CPP_INTERFACE_FUNCTION_DECLARATION = Template(""" virtual ${functionDeclaration} = 0; |
| 228 | """) |
| 229 | |
| 230 | CPP_INTERFACE_DECLARATION = Template(""" |
| 231 | class ${service}If { |
| 232 | public: |
| 233 | ~${service}If() {} |
| 234 | ${functionDeclarations}}; |
| 235 | """) |
| 236 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 237 | def toServiceInterfaceDeclaration(service, debugp=None): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 238 | |
| 239 | functionDeclarations = string.join([CPP_INTERFACE_FUNCTION_DECLARATION.substitute(service=service.name, functionDeclaration=typeToCTypeDeclaration(function)) for function in service.functionList], "") |
| 240 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 241 | return CPP_INTERFACE_DECLARATION.substitute(service=service.name, functionDeclarations=functionDeclarations) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 242 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 243 | CPP_EXCEPTION = CPP_THRIFT_NS+"::Exception" |
| 244 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 245 | CPP_SP = Template("boost::shared_ptr<${klass}> ") |
| 246 | |
| 247 | CPP_PROCESSOR = CPP_THRIFT_NS+"::TProcessor" |
| 248 | CPP_PROCESSORP = CPP_SP.substitute(klass=CPP_PROCESSOR) |
| 249 | |
| 250 | CPP_PROTOCOL_NS = CPP_THRIFT_NS+"::protocol" |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 251 | CPP_PROTOCOL = CPP_PROTOCOL_NS+"::TProtocol" |
| 252 | CPP_PROTOCOLP = CPP_SP.substitute(klass="const "+CPP_PROTOCOL) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 253 | |
| 254 | |
| 255 | CPP_TRANSPORT_NS = CPP_THRIFT_NS+"::transport" |
| 256 | CPP_TRANSPORT = CPP_TRANSPORT_NS+"::TTransport" |
| 257 | CPP_TRANSPORTP = CPP_SP.substitute(klass=CPP_TRANSPORT) |
| 258 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 259 | CPP_SERVER_FUNCTION_DECLARATION = Template(""" void process_${function}("""+CPP_TRANSPORTP+""" itrans, """+CPP_TRANSPORTP+""" otrans); |
| 260 | """) |
| 261 | |
| 262 | CPP_SERVER_FUNCTION_DEFINITION = Template(""" |
| 263 | void ${service}ServerIf::process_${function}("""+CPP_TRANSPORTP+""" itrans, """+CPP_TRANSPORTP+""" otrans) { |
| 264 | |
| 265 | uint32_t xfer = 0; |
| 266 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 267 | ${argsStructDeclaration}; |
| 268 | |
| 269 | ${argsStructReader}; |
| 270 | |
| 271 | ${returnValueDeclaration}; |
| 272 | |
| 273 | ${functionCall}; |
| 274 | |
| 275 | ${resultStructDeclaration}; |
| 276 | |
| 277 | ${returnToResult}; |
| 278 | |
| 279 | ${resultStructWriter}; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 280 | |
| 281 | otrans->flush(); |
| 282 | } |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 283 | """) |
| 284 | |
| 285 | CPP_PROTOCOL_TSTOP = CPP_PROTOCOL_NS+"::T_STOP" |
| 286 | CPP_PROTOCOL_TTYPE = CPP_PROTOCOL_NS+"::TType" |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 287 | CPP_PROTOCOL_MESSAGE_TYPE = CPP_PROTOCOL_NS+"::TMessageType" |
| 288 | CPP_PROTOCOL_CALL = CPP_PROTOCOL_NS+"::T_CALL" |
| 289 | CPP_PROTOCOL_REPLY = CPP_PROTOCOL_NS+"::T_REPLY" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 290 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 291 | CPP_TTYPE_MAP = { |
| 292 | STOP_TYPE : CPP_PROTOCOL_NS+"::T_STOP", |
| 293 | VOID_TYPE : CPP_PROTOCOL_NS+"::T_VOID", |
| 294 | BOOL_TYPE : CPP_PROTOCOL_NS+"::T_BOOL", |
| 295 | UTF7_TYPE : CPP_PROTOCOL_NS+"::T_UTF7", |
| 296 | UTF7_TYPE : CPP_PROTOCOL_NS+"::T_UTF7", |
| 297 | UTF8_TYPE : CPP_PROTOCOL_NS+"::T_UTF8", |
| 298 | UTF16_TYPE : CPP_PROTOCOL_NS+"::T_UTF16", |
| 299 | U08_TYPE : CPP_PROTOCOL_NS+"::T_U08", |
| 300 | I08_TYPE : CPP_PROTOCOL_NS+"::T_I08", |
| 301 | I16_TYPE : CPP_PROTOCOL_NS+"::T_I16", |
| 302 | I32_TYPE : CPP_PROTOCOL_NS+"::T_I32", |
| 303 | I64_TYPE : CPP_PROTOCOL_NS+"::T_I64", |
| 304 | U08_TYPE : CPP_PROTOCOL_NS+"::T_U08", |
| 305 | U16_TYPE : CPP_PROTOCOL_NS+"::T_U16", |
| 306 | U32_TYPE : CPP_PROTOCOL_NS+"::T_U32", |
| 307 | U64_TYPE : CPP_PROTOCOL_NS+"::T_U64", |
| 308 | FLOAT_TYPE : CPP_PROTOCOL_NS+"::T_FLOAT", |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 309 | StructType : CPP_PROTOCOL_NS+"::T_STRUCT", |
| 310 | ListType : CPP_PROTOCOL_NS+"::T_LIST", |
| 311 | MapType : CPP_PROTOCOL_NS+"::T_MAP", |
| 312 | SetType : CPP_PROTOCOL_NS+"::T_SET" |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | def toWireType(ttype): |
| 316 | |
| 317 | if isinstance(ttype, PrimitiveType): |
| 318 | return CPP_TTYPE_MAP[ttype] |
| 319 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 320 | elif isinstance(ttype, EnumType): |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 321 | return CPP_TTYPE_MAP[I32_TYPE] |
| 322 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 323 | elif isinstance(ttype, TypedefType): |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 324 | return toWireType(toCanonicalType(ttype)) |
| 325 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 326 | elif isinstance(ttype, StructType) or isinstance(ttype, CollectionType): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 327 | return CPP_TTYPE_MAP[type(ttype)] |
| 328 | |
| 329 | else: |
| 330 | raise Exception, "No wire type for thrift type: "+str(ttype) |
| 331 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 332 | CPP_SERVER_DECLARATION = Template(""" |
| 333 | class ${service}ServerIf : public ${service}If, public """+CPP_PROCESSOR+""" { |
| 334 | public: |
| 335 | ${service}ServerIf("""+CPP_PROTOCOLP+""" protocol): _iprot(protocol), _oprot(protocol) {} |
| 336 | ${service}ServerIf("""+CPP_PROTOCOLP+""" iprot, """+CPP_PROTOCOLP+""" oprot) : _iprot(iprot), _oprot(oprot) {} |
| 337 | virtual ~${service}ServerIf() {} |
| 338 | bool process("""+CPP_TRANSPORTP+""" _itrans,"""+CPP_TRANSPORTP+""" _otrans); |
| 339 | protected: |
| 340 | """+CPP_PROTOCOLP+""" _iprot; |
| 341 | """+CPP_PROTOCOLP+""" _oprot; |
| 342 | private: |
| 343 | ${functionDeclarations}}; |
| 344 | """) |
| 345 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 346 | def toServerDeclaration(service, debugp=None): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 347 | |
| 348 | functionDeclarations = string.join([CPP_SERVER_FUNCTION_DECLARATION.substitute(function=function.name) for function in service.functionList], "") |
| 349 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 350 | return CPP_SERVER_DECLARATION.substitute(service=service.name, functionDeclarations=functionDeclarations) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 351 | |
| 352 | CPP_CLIENT_FUNCTION_DECLARATION = Template(""" ${functionDeclaration}; |
| 353 | """) |
| 354 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 355 | |
| 356 | CPP_CLIENT_FUNCTION_DEFINITION = Template(""" |
| 357 | ${returnDeclaration} ${service}Client::${function}(${argsDeclaration}) { |
| 358 | |
| 359 | uint32_t xfer = 0; |
| 360 | """+CPP_PROTOCOL_MESSAGE_TYPE+""" messageType; |
| 361 | uint32_t cseqid = 0; |
| 362 | uint32_t rseqid = 0; |
| 363 | |
| 364 | _oprot->writeMessageBegin(_otrans, """+CPP_PROTOCOL_CALL+""", cseqid); |
| 365 | |
| 366 | ${argsStructDeclaration}; |
| 367 | |
| 368 | ${argsToStruct}; |
| 369 | |
| 370 | ${argsStructWriter}; |
| 371 | |
| 372 | _otrans->flush(); |
| 373 | |
| 374 | _iprot->readMessageBegin(_itrans, messageType, rseqid); |
| 375 | |
| 376 | if(messageType != """+CPP_PROTOCOL_REPLY+""" || |
| 377 | rseqid != cseqid) { |
| 378 | throw """+CPP_EXCEPTION+"""(\"unexpected message type or id\"); |
| 379 | } |
| 380 | |
| 381 | ${resultStructDeclaration}; |
| 382 | |
| 383 | ${resultStructReader}; |
| 384 | |
| 385 | _iprot->readMessageEnd(_itrans); |
| 386 | |
| 387 | if(__result.__isset.success) { |
| 388 | ${success}; |
| 389 | } else { |
| 390 | throw """+CPP_EXCEPTION+"""(\"${function} failed\"); |
| 391 | } |
| 392 | } |
| 393 | """) |
| 394 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 395 | CPP_CLIENT_DECLARATION = Template(""" |
| 396 | class ${service}Client : public ${service}If { |
| 397 | |
| 398 | public: |
| 399 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 400 | ${service}Client("""+CPP_TRANSPORTP+""" transport, """+CPP_PROTOCOLP+""" protocol): _itrans(transport), _otrans(transport), _iprot(protocol), _oprot(protocol) {} |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 401 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 402 | ${service}Client("""+CPP_TRANSPORTP+""" itrans, """+CPP_TRANSPORTP+""" otrans, """+CPP_PROTOCOLP+""" iprot, """+CPP_PROTOCOLP+""" oprot) : _itrans(itrans), _otrans(otrans), _iprot(iprot), _oprot(oprot) {} |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 403 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 404 | ${functionDeclarations} |
| 405 | private: |
| 406 | """+CPP_TRANSPORTP+""" _itrans; |
| 407 | """+CPP_TRANSPORTP+""" _otrans; |
| 408 | """+CPP_PROTOCOLP+""" _iprot; |
| 409 | """+CPP_PROTOCOLP+""" _oprot; |
| 410 | };""") |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 411 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 412 | def toServerFunctionDefinition(servicePrefix, function, debugp=None): |
| 413 | result = "" |
| 414 | |
| 415 | argsStructDeclaration = typeToCTypeDeclaration(function.argsStruct)+" __args" |
| 416 | |
| 417 | argsStructReader = toReaderCall("__args", function.argsStruct, "_iprot") |
| 418 | |
| 419 | resultStructDeclaration = typeToCTypeDeclaration(function.resultStruct)+" __result" |
| 420 | |
| 421 | resultStructWriter = toWriterCall("__result", function.resultStruct, "_oprot") |
| 422 | |
| 423 | if function.returnType() != VOID_TYPE: |
| 424 | returnValueDeclaration = typeToCTypeDeclaration(toCanonicalType(function.returnType()))+" __returnValue" |
| 425 | functionCall = "__returnValue = " |
| 426 | returnToResult = "__result.success = __returnValue" |
| 427 | else: |
| 428 | returnValueDeclaration = "" |
| 429 | functionCall = "" |
| 430 | returnToResult = "" |
| 431 | |
| 432 | functionCall+= function.name+"("+string.join(["__args."+arg.name for arg in function.args()], ", ")+")" |
| 433 | |
| 434 | result+= CPP_SERVER_FUNCTION_DEFINITION.substitute(service=servicePrefix, function=function.name, |
| 435 | argsStructDeclaration=argsStructDeclaration, |
| 436 | argsStructReader=argsStructReader, |
| 437 | functionCall=functionCall, |
| 438 | returnToResult=returnToResult, |
| 439 | resultStructDeclaration=resultStructDeclaration, |
| 440 | resultStructWriter=resultStructWriter, |
| 441 | returnValueDeclaration=returnValueDeclaration) |
| 442 | |
| 443 | |
| 444 | |
| 445 | return result |
| 446 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 447 | def toServerServiceDefinition(service, debugp=None): |
| 448 | |
| 449 | result = "" |
| 450 | |
| 451 | for function in service.functionList: |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 452 | |
| 453 | result+= toServerFunctionDefinition(service.name, function, debugp) |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 454 | |
| 455 | return result |
| 456 | |
| 457 | def toServerDefinition(program, debugp=None): |
| 458 | |
| 459 | return string.join([toServerServiceDefinition(service) for service in program.serviceMap.values()], "\n") |
| 460 | |
| 461 | def toClientDeclaration(service, debugp=None): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 462 | |
| 463 | functionDeclarations = string.join([CPP_CLIENT_FUNCTION_DECLARATION.substitute(functionDeclaration=typeToCTypeDeclaration(function)) for function in service.functionList], "") |
| 464 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 465 | return CPP_CLIENT_DECLARATION.substitute(service=service.name, functionDeclarations=functionDeclarations)+"\n" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 466 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 467 | def toClientFunctionDefinition(servicePrefix, function, debugp=None): |
| 468 | |
| 469 | returnDeclaration = typeToCTypeDeclaration(function.returnType()) |
| 470 | |
| 471 | argsDeclaration = string.join([typeToCTypeDeclaration(function.args()[ix].type)+" __arg"+str(ix) for ix in range(len(function.args()))], ", ") |
| 472 | |
| 473 | argsStructDeclaration = typeToCTypeDeclaration(function.argsStruct)+" __args" |
| 474 | |
| 475 | argsStructWriter = toWriterCall("__args", function.argsStruct, "_oprot", "_otrans") |
| 476 | |
| 477 | argsToStruct= string.join([" __args."+function.args()[ix].name+" = __arg"+str(ix) for ix in range(len(function.args()))], ";\n") |
| 478 | |
| 479 | resultStructDeclaration = typeToCTypeDeclaration(function.resultStruct)+" __result" |
| 480 | |
| 481 | resultStructReader = toReaderCall("__result", function.resultStruct, "_iprot", "_itrans") |
| 482 | |
| 483 | if(toCanonicalType(function.returnType()) != VOID_TYPE): |
| 484 | |
| 485 | success = "return __result.success;" |
| 486 | else: |
| 487 | success = "" |
| 488 | |
| 489 | return CPP_CLIENT_FUNCTION_DEFINITION.substitute(service=servicePrefix, |
| 490 | function=function.name, |
| 491 | returnDeclaration=returnDeclaration, |
| 492 | argsDeclaration=argsDeclaration, |
| 493 | argsStructDeclaration=argsStructDeclaration, |
| 494 | argsStructWriter=argsStructWriter, |
| 495 | argsToStruct=argsToStruct, |
| 496 | resultStructDeclaration=resultStructDeclaration, |
| 497 | resultStructReader=resultStructReader, |
| 498 | success=success) |
| 499 | |
| 500 | def toClientServiceDefinition(service, debugp=None): |
| 501 | |
| 502 | result = "" |
| 503 | |
| 504 | for function in service.functionList: |
| 505 | |
| 506 | result+= toClientFunctionDefinition(service.name, function) |
| 507 | |
| 508 | return result |
| 509 | |
| 510 | def toClientDefinition(program, debugp=None): |
| 511 | |
| 512 | return string.join([toClientServiceDefinition(service) for service in program.serviceMap.values()], "\n") |
| 513 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 514 | def toServiceDeclaration(service, debugp=None): |
| 515 | return toServiceInterfaceDeclaration(service, debugp) + toServerDeclaration(service, debugp) + toClientDeclaration(service, debugp) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 516 | |
| 517 | def toGenDir(filename, suffix="cpp-gen", debugp=None): |
| 518 | |
| 519 | result = os.path.join(os.path.split(filename)[0], suffix) |
| 520 | |
| 521 | if not os.path.exists(result): |
| 522 | os.mkdir(result) |
| 523 | |
| 524 | return result |
| 525 | |
| 526 | def toBasename(filename, debugp=None): |
| 527 | """ Take the filename minus the path and\".thrift\" extension if present """ |
| 528 | |
| 529 | basename = os.path.split(filename)[1] |
| 530 | |
| 531 | tokens = os.path.splitext(basename) |
| 532 | |
| 533 | if tokens[1].lower() == ".thrift": |
| 534 | basename = tokens[0] |
| 535 | |
| 536 | if debugp: |
| 537 | debugp("toBasename("+str(filename)+") => "+str(basename)) |
| 538 | |
| 539 | return basename |
| 540 | |
| 541 | def toDefinitionHeaderName(filename, genDir=None, debugp=None): |
| 542 | |
| 543 | if not genDir: |
| 544 | genDir = toGenDir(filename) |
| 545 | |
| 546 | basename = toBasename(filename) |
| 547 | |
| 548 | result = os.path.join(genDir, basename+"_types.h") |
| 549 | |
| 550 | if debugp: |
| 551 | debugp("toDefinitionHeaderName("+str(filename)+", "+str(genDir)+") => "+str(basename)) |
| 552 | |
| 553 | return result |
| 554 | |
| 555 | def writeDefinitionHeader(program, filename, genDir=None, debugp=None): |
| 556 | |
| 557 | definitionHeader = toDefinitionHeaderName(filename, genDir) |
| 558 | |
| 559 | if debugp: |
| 560 | debugp("definitionHeader: "+str(definitionHeader)) |
| 561 | |
| 562 | cfile = CFile(definitionHeader, "w") |
| 563 | |
| 564 | basename = toBasename(filename) |
| 565 | |
| 566 | cfile.writeln(CPP_TYPES_HEADER.substitute(source=basename, date=time.ctime())) |
| 567 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 568 | cfile.write(toDefinitions(program.definitions)) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 569 | |
| 570 | cfile.writeln(CPP_TYPES_FOOTER.substitute(source=basename)) |
| 571 | |
| 572 | cfile.close() |
| 573 | |
| 574 | def toServicesHeaderName(filename, genDir=None, debugp=None): |
| 575 | |
| 576 | if not genDir: |
| 577 | genDir = toGenDir(filename) |
| 578 | |
| 579 | basename = toBasename(filename) |
| 580 | |
| 581 | result = os.path.join(genDir, basename+".h") |
| 582 | |
| 583 | if debugp: |
| 584 | debugp("toDefinitionHeaderName("+str(filename)+", "+str(genDir)+") => "+str(basename)) |
| 585 | |
| 586 | return result |
| 587 | |
| 588 | |
| 589 | def writeServicesHeader(program, filename, genDir=None, debugp=None): |
| 590 | |
| 591 | servicesHeader = toServicesHeaderName(filename, genDir) |
| 592 | |
| 593 | if debugp: |
| 594 | debugp("servicesHeader: "+str(servicesHeader)) |
| 595 | |
| 596 | cfile = CFile(servicesHeader, "w") |
| 597 | |
| 598 | basename = toBasename(filename) |
| 599 | |
| 600 | cfile.writeln(CPP_SERVICES_HEADER.substitute(source=basename, date=time.ctime())) |
| 601 | |
| 602 | services = [] |
| 603 | |
| 604 | # Build orderered list of service definitions by scanning definitions list for services |
| 605 | |
| 606 | for definition in program.definitions: |
| 607 | if isinstance(definition, Service) and definition.name in program.serviceMap: |
| 608 | services.append(definition) |
| 609 | |
| 610 | for service in services: |
| 611 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 612 | cfile.write(toServiceDeclaration(service)) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 613 | |
| 614 | cfile.writeln(CPP_SERVICES_FOOTER.substitute(source=basename)) |
| 615 | |
| 616 | cfile.close() |
| 617 | |
| 618 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 619 | CPP_STRUCT_READ = Template(""" |
| 620 | uint32_t read${name}Struct("""+CPP_PROTOCOLP+""" _iprot, """+CPP_TRANSPORTP+""" itrans, ${declaration}& value) { |
| 621 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 622 | std::string name; |
| 623 | uint32_t id; |
| 624 | uint32_t type; |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 625 | uint32_t xfer = 0; |
| 626 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 627 | while(true) { |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 628 | xfer+= _iprot->readFieldBegin(_itrans, name, type, id); |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 629 | if(type == """+CPP_PROTOCOL_TSTOP+""") { |
| 630 | break; |
| 631 | } |
| 632 | switch(id) { |
| 633 | ${readFieldListSwitch} |
| 634 | } |
| 635 | } |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 636 | |
| 637 | xfer+= _iprot->readStructEnd(_itrans); |
| 638 | |
| 639 | return xfer; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 640 | } |
| 641 | """) |
| 642 | |
| 643 | CPP_PRIMITIVE_TYPE_IO_METHOD_SUFFIX_MAP = { |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 644 | "void" :"Void", |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 645 | "bool" : "Bool", |
| 646 | "string": "String", |
| 647 | "utf7": "String", |
| 648 | "utf8": "String", |
| 649 | "utf16": "String", |
| 650 | "i08": "Byte", |
| 651 | "i16": "I16", |
| 652 | "i32": "I32", |
| 653 | "i64": "I64", |
| 654 | "u08": "Byte", |
| 655 | "u16": "U16", |
| 656 | "u32": "U32", |
| 657 | "u64": "U64", |
| 658 | "float": "Double" |
| 659 | } |
| 660 | |
| 661 | CPP_COLLECTION_TYPE_IO_METHOD_SUFFIX_MAP = { |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 662 | MapType : "map", |
| 663 | ListType : "list", |
| 664 | SetType : "set" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | def typeToIOMethodSuffix(ttype): |
| 668 | |
| 669 | if isinstance(ttype, PrimitiveType): |
| 670 | return CPP_PRIMITIVE_TYPE_IO_METHOD_SUFFIX_MAP[ttype.name] |
| 671 | |
| 672 | elif isinstance(ttype, CollectionType): |
| 673 | |
| 674 | result = CPP_COLLECTION_TYPE_IO_METHOD_SUFFIX_MAP[type(ttype)]+"_" |
| 675 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 676 | if isinstance(ttype, MapType): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 677 | result+= "k_"+typeToIOMethodSuffix(ttype.keyType)+"_" |
| 678 | |
| 679 | result += "v_"+typeToIOMethodSuffix(ttype.valueType) |
| 680 | |
| 681 | return result |
| 682 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 683 | elif isinstance(ttype, StructType): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 684 | return "struct_"+ttype.name |
| 685 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 686 | elif isinstance(ttype, TypedefType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 687 | return ttype.name |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 688 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 689 | elif isinstance(ttype, EnumType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 690 | return ttype.name |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 691 | |
| 692 | else: |
| 693 | raise Exception, "Unknown type "+str(ttype) |
| 694 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 695 | def toReaderCall(value, ttype, reader="iprot", transport="itrans"): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 696 | |
| 697 | suffix = typeToIOMethodSuffix(ttype) |
| 698 | |
| 699 | if isinstance(ttype, PrimitiveType): |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 700 | if ttype != VOID_TYPE: |
| 701 | return "xfer += "+reader+"->read"+suffix+"("+transport+", "+value+")" |
| 702 | else: |
| 703 | return "" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 704 | |
| 705 | elif isinstance(ttype, CollectionType): |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 706 | return "xfer+= read_"+suffix+"("+reader+", "+transport+", "+value+")" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 707 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 708 | elif isinstance(ttype, StructType): |
| 709 | return "xfer+= read_"+suffix+"("+reader+", "+transport+", "+value+")" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 710 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 711 | elif isinstance(ttype, TypedefType): |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 712 | return toReaderCall("reinterpret_cast<"+typeToCTypeDeclaration(ttype.definitionType)+"&>("+value+")", ttype.definitionType, reader) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 713 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 714 | elif isinstance(ttype, EnumType): |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 715 | return toReaderCall("reinterpret_cast<"+typeToCTypeDeclaration(I32_TYPE)+"&>("+value+")", I32_TYPE, reader) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 716 | |
| 717 | else: |
| 718 | raise Exception, "Unknown type "+str(ttype) |
| 719 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 720 | def toWriterCall(value, ttype, writer="oprot", transport="otrans"): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 721 | |
| 722 | suffix = typeToIOMethodSuffix(ttype) |
| 723 | |
| 724 | if isinstance(ttype, PrimitiveType): |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 725 | if ttype != VOID_TYPE: |
| 726 | return "xfer+= "+writer+"->write"+suffix+"("+transport+", "+value+")" |
| 727 | else: |
| 728 | return "" |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 729 | |
| 730 | elif isinstance(ttype, CollectionType): |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 731 | return "xfer+= write_"+suffix+"("+writer+", "+transport+", "+value+")" |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 732 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 733 | elif isinstance(ttype, StructType): |
| 734 | return "xfer+= write_"+suffix+"("+writer+", "+transport+", "+value+")" |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 735 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 736 | elif isinstance(ttype, TypedefType): |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 737 | return toWriterCall("reinterpret_cast<const "+typeToCTypeDeclaration(ttype.definitionType)+"&>("+value+")", ttype.definitionType, writer) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 738 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 739 | elif isinstance(ttype, EnumType): |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 740 | return toWriterCall("reinterpret_cast<const "+typeToCTypeDeclaration(I32_TYPE)+"&>("+value+")", I32_TYPE, writer) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 741 | |
| 742 | else: |
| 743 | raise Exception, "Unknown type "+str(ttype) |
| 744 | |
| 745 | CPP_READ_MAP_DEFINITION = Template(""" |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 746 | uint32_t read_${suffix}("""+CPP_PROTOCOLP+""" iprot, """+CPP_TRANSPORTP+""" itrans, ${declaration}& value) { |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 747 | |
| 748 | uint32_t count; |
| 749 | ${keyType} key; |
| 750 | ${valueType} elem; |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 751 | uint32_t xfer = 0; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 752 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 753 | xfer += iprot->readU32(itrans, count); |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 754 | |
| 755 | for(int ix = 0; ix < count; ix++) { |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 756 | ${keyReaderCall}; |
| 757 | ${valueReaderCall}; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 758 | value.insert(std::make_pair(key, elem)); |
| 759 | } |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 760 | |
| 761 | return xfer; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 762 | } |
| 763 | """) |
| 764 | |
| 765 | CPP_WRITE_MAP_DEFINITION = Template(""" |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 766 | uint32_t write_${suffix}("""+CPP_PROTOCOLP+""" oprot, """+CPP_TRANSPORTP+""" otrans, const ${declaration}& value) { |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 767 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 768 | uint32_t xfer = 0; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 769 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 770 | xfer += oprot->writeU32(otrans, value.size()); |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 771 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 772 | for(${declaration}::const_iterator ix = value.begin(); ix != value.end(); ++ix) { |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 773 | ${keyWriterCall}; |
| 774 | ${valueWriterCall}; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 775 | } |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 776 | return xfer; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 777 | } |
| 778 | """) |
| 779 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 780 | CPP_READ_LIST_DEFINITION = Template(""" |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 781 | uint32_t read_${suffix}("""+CPP_PROTOCOLP+""" iprot, """+CPP_TRANSPORTP+""" itrans, ${declaration}& value) { |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 782 | |
| 783 | uint32_t count; |
| 784 | ${valueType} elem; |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 785 | uint32_t xfer = 0; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 786 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 787 | xfer+= iprot->readU32(itrans, count); |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 788 | |
| 789 | for(int ix = 0; ix < count; ix++) { |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 790 | ${valueReaderCall}; |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 791 | value.${insert}(elem); |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 792 | } |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 793 | return xfer; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 794 | } |
| 795 | """) |
| 796 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 797 | CPP_WRITE_LIST_DEFINITION = Template(""" |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 798 | uint32_t write_${suffix}("""+CPP_PROTOCOLP+""" oprot, """+CPP_TRANSPORTP+""" otrans, const ${declaration}& value) { |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 799 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 800 | uint32_t xfer = 0; |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 801 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 802 | xfer+= oprot->writeU32(otrans, value.size()); |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 803 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 804 | for(${declaration}::const_iterator ix = value.begin(); ix != value.end(); ++ix) { |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 805 | ${valueWriterCall}; |
| 806 | } |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 807 | return xfer; |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 808 | } |
| 809 | """) |
| 810 | |
| 811 | def toCollectionReaderDefinition(ttype): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 812 | |
| 813 | suffix = typeToIOMethodSuffix(ttype) |
| 814 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 815 | if isinstance(ttype, MapType): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 816 | keyReaderCall = toReaderCall("key", ttype.keyType) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 817 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 818 | valueReaderCall= toReaderCall("elem", ttype.valueType) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 819 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 820 | if isinstance(ttype, MapType): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 821 | return CPP_READ_MAP_DEFINITION.substitute(suffix=suffix, declaration=typeToCTypeDeclaration(ttype), |
| 822 | keyType=typeToCTypeDeclaration(ttype.keyType), |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 823 | keyReaderCall=keyReaderCall, |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 824 | valueType=typeToCTypeDeclaration(ttype.valueType), |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 825 | valueReaderCall=valueReaderCall) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 826 | |
| 827 | else: |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 828 | if isinstance(ttype, ListType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 829 | insert="push_back" |
| 830 | else: |
| 831 | insert="insert" |
| 832 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 833 | return CPP_READ_LIST_DEFINITION.substitute(suffix=suffix, declaration=typeToCTypeDeclaration(ttype), |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 834 | valueReaderCall=valueReaderCall, |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 835 | valueType=typeToCTypeDeclaration(ttype.valueType), |
| 836 | insert=insert) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 837 | |
| 838 | |
| 839 | def toCollectionWriterDefinition(ttype): |
| 840 | |
| 841 | suffix = typeToIOMethodSuffix(ttype) |
| 842 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 843 | if isinstance(ttype, MapType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 844 | keyWriterCall = toWriterCall("ix->first", ttype.keyType) |
| 845 | valueWriterCall = toWriterCall("ix->second", ttype.valueType) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 846 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 847 | else: |
| 848 | valueWriterCall= toWriterCall("*ix", ttype.valueType) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 849 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 850 | if isinstance(ttype, MapType): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 851 | return CPP_WRITE_MAP_DEFINITION.substitute(suffix=suffix, declaration=typeToCTypeDeclaration(ttype), |
| 852 | keyType=typeToCTypeDeclaration(ttype.keyType), |
| 853 | keyWriterCall=keyWriterCall, |
| 854 | valueType=typeToCTypeDeclaration(ttype.valueType), |
| 855 | valueWriterCall=valueWriterCall) |
| 856 | |
| 857 | else: |
| 858 | return CPP_WRITE_LIST_DEFINITION.substitute(suffix=suffix, declaration=typeToCTypeDeclaration(ttype), |
| 859 | valueWriterCall=valueWriterCall, |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 860 | valueType=typeToCTypeDeclaration(ttype.valueType)) |
| 861 | |
| 862 | |
| 863 | CPP_READ_STRUCT_DEFINITION = Template(""" |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 864 | uint32_t read_${suffix}("""+CPP_PROTOCOLP+""" iprot, """+CPP_TRANSPORTP+""" itrans, ${declaration}& value) { |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 865 | |
| 866 | std::string name; |
| 867 | """+CPP_PROTOCOL_TTYPE+""" type; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 868 | int16_t id; |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 869 | uint32_t xfer = 0; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 870 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 871 | xfer+= iprot->readStructBegin(itrans, name); |
| 872 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 873 | while(true) { |
| 874 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 875 | xfer+= iprot->readFieldBegin(itrans, name, type, id); |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 876 | |
| 877 | if(type == """+CPP_PROTOCOL_TSTOP+""") {break;} |
| 878 | |
| 879 | switch(id) { |
| 880 | ${fieldSwitch} |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 881 | default: xfer += iprot->skip(itrans, type); break; |
| 882 | } |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 883 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 884 | xfer+= iprot->readFieldEnd(itrans); |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 885 | } |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 886 | |
| 887 | xfer+= iprot->readStructEnd(itrans); |
| 888 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 889 | return xfer; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 890 | } |
| 891 | """) |
| 892 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 893 | CPP_WRITE_FIELD_DEFINITION = Template(""" |
| 894 | oprot->writeFieldBegin(otrans, \"${name}\", ${type}, ${id}); |
| 895 | ${fieldWriterCall}; |
| 896 | oprot->writeFieldEnd(otrans); |
| 897 | """) |
| 898 | |
| 899 | CPP_WRITE_STRUCT_DEFINITION = Template(""" |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 900 | uint32_t write_${suffix}("""+CPP_PROTOCOLP+""" oprot, """+CPP_TRANSPORTP+""" otrans, const ${declaration}& value) { |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 901 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 902 | uint32_t xfer = 0; |
| 903 | |
| 904 | xfer+= oprot->writeStructBegin(otrans, \"${name}\"); |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 905 | ${fieldWriterCalls} |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 906 | xfer+= oprot->writeFieldStop(otrans); |
| 907 | xfer += oprot->writeStructEnd(otrans); |
| 908 | return xfer; |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 909 | } |
| 910 | """) |
| 911 | |
| 912 | def toStructReaderDefinition(ttype): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 913 | |
| 914 | suffix = typeToIOMethodSuffix(ttype) |
| 915 | |
| 916 | # Sort field list in order of increasing ids |
| 917 | |
| 918 | fieldList = [] |
| 919 | fieldList+= ttype.fieldList |
| 920 | |
| 921 | fieldList.sort(lambda a,b: a.id - b.id) |
| 922 | |
| 923 | fieldSwitch="" |
| 924 | |
| 925 | for field in fieldList: |
| 926 | fieldSwitch+= " case "+str(field.id)+": " |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 927 | fieldSwitch+= toReaderCall("value."+field.name, field.type)+"; value.__isset."+field.name+" = true; break;\n" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 928 | |
| 929 | return CPP_READ_STRUCT_DEFINITION.substitute(suffix=suffix, declaration=typeToCTypeDeclaration(ttype), fieldSwitch=fieldSwitch) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 930 | |
| 931 | def toStructWriterDefinition(ttype): |
| 932 | |
| 933 | suffix = typeToIOMethodSuffix(ttype) |
| 934 | |
| 935 | writeCalls = "" |
| 936 | |
| 937 | for field in ttype.fieldList: |
| 938 | |
| 939 | writeCalls+= CPP_WRITE_FIELD_DEFINITION.substitute(name=field.name, type=toWireType(field.type), id=field.id, |
| 940 | fieldWriterCall=toWriterCall("value."+field.name, field.type)) |
| 941 | |
| 942 | return CPP_WRITE_STRUCT_DEFINITION.substitute(name=ttype.name, suffix=suffix, declaration=typeToCTypeDeclaration(ttype), fieldWriterCalls=writeCalls) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 943 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 944 | def toReaderDefinition(ttype): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 945 | if isinstance(ttype, CollectionType): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 946 | return toCollectionReaderDefinition(ttype) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 947 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 948 | elif isinstance(ttype, StructType): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 949 | return toStructReaderDefinition(ttype) |
| 950 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 951 | elif isinstance(ttype, TypedefType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 952 | return "" |
| 953 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 954 | elif isinstance(ttype, EnumType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 955 | return "" |
| 956 | |
| 957 | else: |
| 958 | raise Exception, "Unsupported type: "+str(ttype) |
| 959 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 960 | def toWriterDefinition(ttype): |
| 961 | if isinstance(ttype, CollectionType): |
| 962 | return toCollectionWriterDefinition(ttype) |
| 963 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 964 | elif isinstance(ttype, StructType): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 965 | return toStructWriterDefinition(ttype) |
| 966 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 967 | elif isinstance(ttype, TypedefType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 968 | return "" |
| 969 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 970 | elif isinstance(ttype, EnumType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 971 | return "" |
| 972 | |
| 973 | else: |
| 974 | raise Exception, "Unsupported type: "+str(ttype) |
| 975 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 976 | def toOrderedIOList(ttype, result=None): |
| 977 | if not result: |
| 978 | result = [] |
| 979 | |
| 980 | if ttype in result: |
| 981 | return result |
| 982 | |
| 983 | elif isinstance(ttype, PrimitiveType): |
| 984 | return result |
| 985 | |
| 986 | elif isinstance(ttype, CollectionType): |
| 987 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 988 | if isinstance(ttype, MapType): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 989 | result = toOrderedIOList(ttype.keyType, result) |
| 990 | |
| 991 | result = toOrderedIOList(ttype.valueType, result) |
| 992 | |
| 993 | result.append(ttype) |
| 994 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 995 | elif isinstance(ttype, StructType): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 996 | for field in ttype.fieldList: |
| 997 | result = toOrderedIOList(field.type, result) |
| 998 | result.append(ttype) |
| 999 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 1000 | elif isinstance(ttype, TypedefType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 1001 | result.append(ttype) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1002 | return result |
| 1003 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 1004 | elif isinstance(ttype, EnumType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 1005 | result.append(ttype) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1006 | return result |
| 1007 | |
| 1008 | elif isinstance(ttype, Program): |
| 1009 | |
| 1010 | for struct in ttype.structMap.values(): |
| 1011 | result = toOrderedIOList(struct, result) |
| 1012 | |
| 1013 | for service in ttype.serviceMap.values(): |
| 1014 | result = toOrderedIOList(service, result) |
| 1015 | |
| 1016 | elif isinstance(ttype, Service): |
| 1017 | for function in ttype.functionList: |
| 1018 | result = toOrderedIOList(function, result) |
| 1019 | |
| 1020 | elif isinstance(ttype, Function): |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 1021 | result = toOrderedIOList(ttype.returnType(), result) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1022 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 1023 | # skip the args struct itself and just order the arguments themselves |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 1024 | # we don't want the arg struct to be referred to until later, since we need to |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 1025 | # inline those struct definitions with the implementation, not in the types header |
| 1026 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 1027 | for field in ttype.args(): |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 1028 | result = toOrderedIOList(field.type, result) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1029 | |
| 1030 | else: |
| 1031 | raise Exception, "Unsupported thrift type: "+str(ttype) |
| 1032 | |
| 1033 | return result |
| 1034 | |
| 1035 | def toIOMethodImplementations(program): |
| 1036 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 1037 | # get ordered list of all types that need marshallers: |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1038 | |
| 1039 | iolist = toOrderedIOList(program) |
| 1040 | |
| 1041 | result = "" |
| 1042 | |
| 1043 | for ttype in iolist: |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1044 | result+= toReaderDefinition(ttype) |
| 1045 | result+= toWriterDefinition(ttype) |
| 1046 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 1047 | # For all function argument lists, we need to create both struct definitions |
| 1048 | # and io methods. We keep the struct definitions local, since they aren't part of the service API |
| 1049 | # |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 1050 | # Note that we don't need to do a depth-first traverse of arg structs since they can only include fields |
| 1051 | # we've already seen |
| 1052 | |
| 1053 | for service in program.serviceMap.values(): |
| 1054 | for function in service.functionList: |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 1055 | result+= toStructDefinition(function.argsStruct) |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 1056 | result+= toReaderDefinition(function.argsStruct) |
| 1057 | result+= toWriterDefinition(function.argsStruct) |
| 1058 | result+= toStructDefinition(function.resultStruct) |
| 1059 | result+= toReaderDefinition(function.resultStruct) |
| 1060 | result+= toWriterDefinition(function.resultStruct) |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 1061 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1062 | return result; |
| 1063 | |
| 1064 | def toImplementationSourceName(filename, genDir=None, debugp=None): |
| 1065 | |
| 1066 | if not genDir: |
| 1067 | genDir = toGenDir(filename) |
| 1068 | |
| 1069 | basename = toBasename(filename) |
| 1070 | |
| 1071 | result = os.path.join(genDir, basename+".cc") |
| 1072 | |
| 1073 | if debugp: |
| 1074 | debugp("toDefinitionHeaderName("+str(filename)+", "+str(genDir)+") => "+str(basename)) |
| 1075 | |
| 1076 | return result |
| 1077 | |
| 1078 | def writeImplementationSource(program, filename, genDir=None, debugp=None): |
| 1079 | |
| 1080 | implementationSource = toImplementationSourceName(filename, genDir) |
| 1081 | |
| 1082 | if debugp: |
| 1083 | debugp("implementationSource: "+str(implementationSource)) |
| 1084 | |
| 1085 | cfile = CFile(implementationSource, "w") |
| 1086 | |
| 1087 | basename = toBasename(filename) |
| 1088 | |
| 1089 | cfile.writeln(CPP_IMPL_HEADER.substitute(source=basename, date=time.ctime())) |
| 1090 | |
| 1091 | cfile.write(toIOMethodImplementations(program)) |
| 1092 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 1093 | cfile.write(toServerDefinition(program)) |
| 1094 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame^] | 1095 | cfile.write(toClientDefinition(program)) |
| 1096 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1097 | cfile.writeln(CPP_IMPL_FOOTER.substitute(source=basename)) |
| 1098 | |
| 1099 | cfile.close() |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1100 | |
| 1101 | class CPPGenerator(Generator): |
| 1102 | |
| 1103 | def __call__(self, program, filename, genDir=None, debugp=None): |
| 1104 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1105 | writeDefinitionHeader(program, filename, genDir, debugp) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1106 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1107 | writeServicesHeader(program, filename, genDir, debugp) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1108 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1109 | writeImplementationSource(program, filename, genDir, debugp) |