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 | 66d67d8 | 2006-08-11 23:33:08 +0000 | [diff] [blame^] | 259 | CPP_SERVER_FUNCTION_DECLARATION = Template(""" void process_${function}(uint32_t seqid, """+CPP_TRANSPORTP+""" itrans, """+CPP_TRANSPORTP+""" otrans); |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 260 | """) |
| 261 | |
| 262 | CPP_SERVER_FUNCTION_DEFINITION = Template(""" |
Marc Slemko | 66d67d8 | 2006-08-11 23:33:08 +0000 | [diff] [blame^] | 263 | void ${service}ServerIf::process_${function}(uint32_t seqid, """+CPP_TRANSPORTP+""" itrans, """+CPP_TRANSPORTP+""" otrans) { |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 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 | |
Marc Slemko | 66d67d8 | 2006-08-11 23:33:08 +0000 | [diff] [blame^] | 271 | iprot->readMessageEnd(itrans); |
| 272 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 273 | ${returnValueDeclaration}; |
| 274 | |
| 275 | ${functionCall}; |
| 276 | |
| 277 | ${resultStructDeclaration}; |
| 278 | |
| 279 | ${returnToResult}; |
| 280 | |
Marc Slemko | 66d67d8 | 2006-08-11 23:33:08 +0000 | [diff] [blame^] | 281 | oprot->writeMessageBegin(otrans, """+CPP_PROTOCOL_REPLY+""", seqid); |
| 282 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 283 | ${resultStructWriter}; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 284 | |
Marc Slemko | 66d67d8 | 2006-08-11 23:33:08 +0000 | [diff] [blame^] | 285 | oprot->writeMessaeEnd(otrans); |
| 286 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 287 | otrans->flush(); |
| 288 | } |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 289 | """) |
| 290 | |
Marc Slemko | 66d67d8 | 2006-08-11 23:33:08 +0000 | [diff] [blame^] | 291 | CPP_SERVER_PROCESS_DEFINITION = Template(""" |
| 292 | bool ${service}ServerIf::process("""+CPP_TRANSPORTP+""" itrans, """+CPP_TRANSPORTP+""" otrans) { |
| 293 | |
| 294 | uint32_t xfer = 0; |
| 295 | |
| 296 | std::string name; |
| 297 | |
| 298 | """+CPP_MESSAGE_TYPE+""" messageType; |
| 299 | |
| 300 | uint32_t seqid; |
| 301 | |
| 302 | _iprot->readMessageBegin(_itrans, name, messageType, cseqid); |
| 303 | |
| 304 | if(messageType == """+CPP_PROTOCOL_CALL+""") { |
| 305 | ${callProcessSwitch} |
| 306 | } else { |
| 307 | throw """+CPP_EXCEPTION+"""(\"Unexpected message type\"); |
| 308 | } |
| 309 | } |
| 310 | """) |
| 311 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 312 | CPP_PROTOCOL_TSTOP = CPP_PROTOCOL_NS+"::T_STOP" |
| 313 | CPP_PROTOCOL_TTYPE = CPP_PROTOCOL_NS+"::TType" |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 314 | CPP_PROTOCOL_MESSAGE_TYPE = CPP_PROTOCOL_NS+"::TMessageType" |
| 315 | CPP_PROTOCOL_CALL = CPP_PROTOCOL_NS+"::T_CALL" |
| 316 | CPP_PROTOCOL_REPLY = CPP_PROTOCOL_NS+"::T_REPLY" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 317 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 318 | CPP_TTYPE_MAP = { |
| 319 | STOP_TYPE : CPP_PROTOCOL_NS+"::T_STOP", |
| 320 | VOID_TYPE : CPP_PROTOCOL_NS+"::T_VOID", |
| 321 | BOOL_TYPE : CPP_PROTOCOL_NS+"::T_BOOL", |
| 322 | UTF7_TYPE : CPP_PROTOCOL_NS+"::T_UTF7", |
| 323 | UTF7_TYPE : CPP_PROTOCOL_NS+"::T_UTF7", |
| 324 | UTF8_TYPE : CPP_PROTOCOL_NS+"::T_UTF8", |
| 325 | UTF16_TYPE : CPP_PROTOCOL_NS+"::T_UTF16", |
| 326 | U08_TYPE : CPP_PROTOCOL_NS+"::T_U08", |
| 327 | I08_TYPE : CPP_PROTOCOL_NS+"::T_I08", |
| 328 | I16_TYPE : CPP_PROTOCOL_NS+"::T_I16", |
| 329 | I32_TYPE : CPP_PROTOCOL_NS+"::T_I32", |
| 330 | I64_TYPE : CPP_PROTOCOL_NS+"::T_I64", |
| 331 | U08_TYPE : CPP_PROTOCOL_NS+"::T_U08", |
| 332 | U16_TYPE : CPP_PROTOCOL_NS+"::T_U16", |
| 333 | U32_TYPE : CPP_PROTOCOL_NS+"::T_U32", |
| 334 | U64_TYPE : CPP_PROTOCOL_NS+"::T_U64", |
| 335 | FLOAT_TYPE : CPP_PROTOCOL_NS+"::T_FLOAT", |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 336 | StructType : CPP_PROTOCOL_NS+"::T_STRUCT", |
| 337 | ListType : CPP_PROTOCOL_NS+"::T_LIST", |
| 338 | MapType : CPP_PROTOCOL_NS+"::T_MAP", |
| 339 | SetType : CPP_PROTOCOL_NS+"::T_SET" |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | def toWireType(ttype): |
| 343 | |
| 344 | if isinstance(ttype, PrimitiveType): |
| 345 | return CPP_TTYPE_MAP[ttype] |
| 346 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 347 | elif isinstance(ttype, EnumType): |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 348 | return CPP_TTYPE_MAP[I32_TYPE] |
| 349 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 350 | elif isinstance(ttype, TypedefType): |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 351 | return toWireType(toCanonicalType(ttype)) |
| 352 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 353 | elif isinstance(ttype, StructType) or isinstance(ttype, CollectionType): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 354 | return CPP_TTYPE_MAP[type(ttype)] |
| 355 | |
| 356 | else: |
| 357 | raise Exception, "No wire type for thrift type: "+str(ttype) |
| 358 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 359 | CPP_SERVER_DECLARATION = Template(""" |
| 360 | class ${service}ServerIf : public ${service}If, public """+CPP_PROCESSOR+""" { |
| 361 | public: |
| 362 | ${service}ServerIf("""+CPP_PROTOCOLP+""" protocol): _iprot(protocol), _oprot(protocol) {} |
| 363 | ${service}ServerIf("""+CPP_PROTOCOLP+""" iprot, """+CPP_PROTOCOLP+""" oprot) : _iprot(iprot), _oprot(oprot) {} |
| 364 | virtual ~${service}ServerIf() {} |
| 365 | bool process("""+CPP_TRANSPORTP+""" _itrans,"""+CPP_TRANSPORTP+""" _otrans); |
| 366 | protected: |
| 367 | """+CPP_PROTOCOLP+""" _iprot; |
| 368 | """+CPP_PROTOCOLP+""" _oprot; |
| 369 | private: |
| 370 | ${functionDeclarations}}; |
| 371 | """) |
| 372 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 373 | def toServerDeclaration(service, debugp=None): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 374 | |
| 375 | functionDeclarations = string.join([CPP_SERVER_FUNCTION_DECLARATION.substitute(function=function.name) for function in service.functionList], "") |
| 376 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 377 | return CPP_SERVER_DECLARATION.substitute(service=service.name, functionDeclarations=functionDeclarations) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 378 | |
| 379 | CPP_CLIENT_FUNCTION_DECLARATION = Template(""" ${functionDeclaration}; |
| 380 | """) |
| 381 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 382 | |
| 383 | CPP_CLIENT_FUNCTION_DEFINITION = Template(""" |
| 384 | ${returnDeclaration} ${service}Client::${function}(${argsDeclaration}) { |
| 385 | |
| 386 | uint32_t xfer = 0; |
| 387 | """+CPP_PROTOCOL_MESSAGE_TYPE+""" messageType; |
| 388 | uint32_t cseqid = 0; |
| 389 | uint32_t rseqid = 0; |
| 390 | |
| 391 | _oprot->writeMessageBegin(_otrans, """+CPP_PROTOCOL_CALL+""", cseqid); |
| 392 | |
| 393 | ${argsStructDeclaration}; |
| 394 | |
| 395 | ${argsToStruct}; |
| 396 | |
| 397 | ${argsStructWriter}; |
| 398 | |
| 399 | _otrans->flush(); |
| 400 | |
| 401 | _iprot->readMessageBegin(_itrans, messageType, rseqid); |
| 402 | |
| 403 | if(messageType != """+CPP_PROTOCOL_REPLY+""" || |
| 404 | rseqid != cseqid) { |
| 405 | throw """+CPP_EXCEPTION+"""(\"unexpected message type or id\"); |
| 406 | } |
| 407 | |
| 408 | ${resultStructDeclaration}; |
| 409 | |
| 410 | ${resultStructReader}; |
| 411 | |
| 412 | _iprot->readMessageEnd(_itrans); |
| 413 | |
| 414 | if(__result.__isset.success) { |
| 415 | ${success}; |
| 416 | } else { |
| 417 | throw """+CPP_EXCEPTION+"""(\"${function} failed\"); |
| 418 | } |
| 419 | } |
| 420 | """) |
| 421 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 422 | CPP_CLIENT_DECLARATION = Template(""" |
| 423 | class ${service}Client : public ${service}If { |
| 424 | |
| 425 | public: |
| 426 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 427 | ${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] | 428 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 429 | ${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] | 430 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 431 | ${functionDeclarations} |
| 432 | private: |
| 433 | """+CPP_TRANSPORTP+""" _itrans; |
| 434 | """+CPP_TRANSPORTP+""" _otrans; |
| 435 | """+CPP_PROTOCOLP+""" _iprot; |
| 436 | """+CPP_PROTOCOLP+""" _oprot; |
| 437 | };""") |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 438 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 439 | def toServerFunctionDefinition(servicePrefix, function, debugp=None): |
| 440 | result = "" |
| 441 | |
| 442 | argsStructDeclaration = typeToCTypeDeclaration(function.argsStruct)+" __args" |
| 443 | |
| 444 | argsStructReader = toReaderCall("__args", function.argsStruct, "_iprot") |
| 445 | |
| 446 | resultStructDeclaration = typeToCTypeDeclaration(function.resultStruct)+" __result" |
| 447 | |
| 448 | resultStructWriter = toWriterCall("__result", function.resultStruct, "_oprot") |
| 449 | |
| 450 | if function.returnType() != VOID_TYPE: |
| 451 | returnValueDeclaration = typeToCTypeDeclaration(toCanonicalType(function.returnType()))+" __returnValue" |
| 452 | functionCall = "__returnValue = " |
| 453 | returnToResult = "__result.success = __returnValue" |
| 454 | else: |
| 455 | returnValueDeclaration = "" |
| 456 | functionCall = "" |
| 457 | returnToResult = "" |
| 458 | |
| 459 | functionCall+= function.name+"("+string.join(["__args."+arg.name for arg in function.args()], ", ")+")" |
| 460 | |
| 461 | result+= CPP_SERVER_FUNCTION_DEFINITION.substitute(service=servicePrefix, function=function.name, |
| 462 | argsStructDeclaration=argsStructDeclaration, |
| 463 | argsStructReader=argsStructReader, |
| 464 | functionCall=functionCall, |
| 465 | returnToResult=returnToResult, |
| 466 | resultStructDeclaration=resultStructDeclaration, |
| 467 | resultStructWriter=resultStructWriter, |
| 468 | returnValueDeclaration=returnValueDeclaration) |
| 469 | |
| 470 | |
| 471 | |
| 472 | return result |
| 473 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 474 | def toServerServiceDefinition(service, debugp=None): |
| 475 | |
| 476 | result = "" |
| 477 | |
| 478 | for function in service.functionList: |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 479 | |
| 480 | result+= toServerFunctionDefinition(service.name, function, debugp) |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 481 | |
Marc Slemko | 66d67d8 | 2006-08-11 23:33:08 +0000 | [diff] [blame^] | 482 | |
| 483 | callProcessSwitch = "if"+string.join(["(name.compare(\""+function.name+"\") == 0) {"+toServerProcessFunctionCall(function)+";}" for function in service.functionList], "\n else if") |
| 484 | |
| 485 | result+= CPP_SERVER_PROCESS_DEFINITION(service=service.name, callProcessSwitch=callProcessSwitch) |
| 486 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 487 | return result |
| 488 | |
| 489 | def toServerDefinition(program, debugp=None): |
| 490 | |
| 491 | return string.join([toServerServiceDefinition(service) for service in program.serviceMap.values()], "\n") |
| 492 | |
| 493 | def toClientDeclaration(service, debugp=None): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 494 | |
| 495 | functionDeclarations = string.join([CPP_CLIENT_FUNCTION_DECLARATION.substitute(functionDeclaration=typeToCTypeDeclaration(function)) for function in service.functionList], "") |
| 496 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 497 | return CPP_CLIENT_DECLARATION.substitute(service=service.name, functionDeclarations=functionDeclarations)+"\n" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 498 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 499 | def toClientFunctionDefinition(servicePrefix, function, debugp=None): |
| 500 | |
| 501 | returnDeclaration = typeToCTypeDeclaration(function.returnType()) |
| 502 | |
| 503 | argsDeclaration = string.join([typeToCTypeDeclaration(function.args()[ix].type)+" __arg"+str(ix) for ix in range(len(function.args()))], ", ") |
| 504 | |
| 505 | argsStructDeclaration = typeToCTypeDeclaration(function.argsStruct)+" __args" |
| 506 | |
| 507 | argsStructWriter = toWriterCall("__args", function.argsStruct, "_oprot", "_otrans") |
| 508 | |
| 509 | argsToStruct= string.join([" __args."+function.args()[ix].name+" = __arg"+str(ix) for ix in range(len(function.args()))], ";\n") |
| 510 | |
| 511 | resultStructDeclaration = typeToCTypeDeclaration(function.resultStruct)+" __result" |
| 512 | |
| 513 | resultStructReader = toReaderCall("__result", function.resultStruct, "_iprot", "_itrans") |
| 514 | |
| 515 | if(toCanonicalType(function.returnType()) != VOID_TYPE): |
| 516 | |
| 517 | success = "return __result.success;" |
| 518 | else: |
| 519 | success = "" |
| 520 | |
| 521 | return CPP_CLIENT_FUNCTION_DEFINITION.substitute(service=servicePrefix, |
| 522 | function=function.name, |
| 523 | returnDeclaration=returnDeclaration, |
| 524 | argsDeclaration=argsDeclaration, |
| 525 | argsStructDeclaration=argsStructDeclaration, |
| 526 | argsStructWriter=argsStructWriter, |
| 527 | argsToStruct=argsToStruct, |
| 528 | resultStructDeclaration=resultStructDeclaration, |
| 529 | resultStructReader=resultStructReader, |
| 530 | success=success) |
| 531 | |
| 532 | def toClientServiceDefinition(service, debugp=None): |
| 533 | |
| 534 | result = "" |
| 535 | |
| 536 | for function in service.functionList: |
| 537 | |
| 538 | result+= toClientFunctionDefinition(service.name, function) |
| 539 | |
| 540 | return result |
| 541 | |
| 542 | def toClientDefinition(program, debugp=None): |
| 543 | |
| 544 | return string.join([toClientServiceDefinition(service) for service in program.serviceMap.values()], "\n") |
| 545 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 546 | def toServiceDeclaration(service, debugp=None): |
| 547 | return toServiceInterfaceDeclaration(service, debugp) + toServerDeclaration(service, debugp) + toClientDeclaration(service, debugp) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 548 | |
| 549 | def toGenDir(filename, suffix="cpp-gen", debugp=None): |
| 550 | |
| 551 | result = os.path.join(os.path.split(filename)[0], suffix) |
| 552 | |
| 553 | if not os.path.exists(result): |
| 554 | os.mkdir(result) |
| 555 | |
| 556 | return result |
| 557 | |
| 558 | def toBasename(filename, debugp=None): |
| 559 | """ Take the filename minus the path and\".thrift\" extension if present """ |
| 560 | |
| 561 | basename = os.path.split(filename)[1] |
| 562 | |
| 563 | tokens = os.path.splitext(basename) |
| 564 | |
| 565 | if tokens[1].lower() == ".thrift": |
| 566 | basename = tokens[0] |
| 567 | |
| 568 | if debugp: |
| 569 | debugp("toBasename("+str(filename)+") => "+str(basename)) |
| 570 | |
| 571 | return basename |
| 572 | |
| 573 | def toDefinitionHeaderName(filename, genDir=None, debugp=None): |
| 574 | |
| 575 | if not genDir: |
| 576 | genDir = toGenDir(filename) |
| 577 | |
| 578 | basename = toBasename(filename) |
| 579 | |
| 580 | result = os.path.join(genDir, basename+"_types.h") |
| 581 | |
| 582 | if debugp: |
| 583 | debugp("toDefinitionHeaderName("+str(filename)+", "+str(genDir)+") => "+str(basename)) |
| 584 | |
| 585 | return result |
| 586 | |
| 587 | def writeDefinitionHeader(program, filename, genDir=None, debugp=None): |
| 588 | |
| 589 | definitionHeader = toDefinitionHeaderName(filename, genDir) |
| 590 | |
| 591 | if debugp: |
| 592 | debugp("definitionHeader: "+str(definitionHeader)) |
| 593 | |
| 594 | cfile = CFile(definitionHeader, "w") |
| 595 | |
| 596 | basename = toBasename(filename) |
| 597 | |
| 598 | cfile.writeln(CPP_TYPES_HEADER.substitute(source=basename, date=time.ctime())) |
| 599 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 600 | cfile.write(toDefinitions(program.definitions)) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 601 | |
| 602 | cfile.writeln(CPP_TYPES_FOOTER.substitute(source=basename)) |
| 603 | |
| 604 | cfile.close() |
| 605 | |
| 606 | def toServicesHeaderName(filename, genDir=None, debugp=None): |
| 607 | |
| 608 | if not genDir: |
| 609 | genDir = toGenDir(filename) |
| 610 | |
| 611 | basename = toBasename(filename) |
| 612 | |
| 613 | result = os.path.join(genDir, basename+".h") |
| 614 | |
| 615 | if debugp: |
| 616 | debugp("toDefinitionHeaderName("+str(filename)+", "+str(genDir)+") => "+str(basename)) |
| 617 | |
| 618 | return result |
| 619 | |
| 620 | |
| 621 | def writeServicesHeader(program, filename, genDir=None, debugp=None): |
| 622 | |
| 623 | servicesHeader = toServicesHeaderName(filename, genDir) |
| 624 | |
| 625 | if debugp: |
| 626 | debugp("servicesHeader: "+str(servicesHeader)) |
| 627 | |
| 628 | cfile = CFile(servicesHeader, "w") |
| 629 | |
| 630 | basename = toBasename(filename) |
| 631 | |
| 632 | cfile.writeln(CPP_SERVICES_HEADER.substitute(source=basename, date=time.ctime())) |
| 633 | |
| 634 | services = [] |
| 635 | |
| 636 | # Build orderered list of service definitions by scanning definitions list for services |
| 637 | |
| 638 | for definition in program.definitions: |
| 639 | if isinstance(definition, Service) and definition.name in program.serviceMap: |
| 640 | services.append(definition) |
| 641 | |
| 642 | for service in services: |
| 643 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 644 | cfile.write(toServiceDeclaration(service)) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 645 | |
| 646 | cfile.writeln(CPP_SERVICES_FOOTER.substitute(source=basename)) |
| 647 | |
| 648 | cfile.close() |
| 649 | |
| 650 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 651 | CPP_STRUCT_READ = Template(""" |
| 652 | uint32_t read${name}Struct("""+CPP_PROTOCOLP+""" _iprot, """+CPP_TRANSPORTP+""" itrans, ${declaration}& value) { |
| 653 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 654 | std::string name; |
| 655 | uint32_t id; |
| 656 | uint32_t type; |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 657 | uint32_t xfer = 0; |
| 658 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 659 | while(true) { |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 660 | xfer+= _iprot->readFieldBegin(_itrans, name, type, id); |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 661 | if(type == """+CPP_PROTOCOL_TSTOP+""") { |
| 662 | break; |
| 663 | } |
| 664 | switch(id) { |
| 665 | ${readFieldListSwitch} |
| 666 | } |
| 667 | } |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 668 | |
| 669 | xfer+= _iprot->readStructEnd(_itrans); |
| 670 | |
| 671 | return xfer; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 672 | } |
| 673 | """) |
| 674 | |
| 675 | CPP_PRIMITIVE_TYPE_IO_METHOD_SUFFIX_MAP = { |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 676 | "void" :"Void", |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 677 | "bool" : "Bool", |
| 678 | "string": "String", |
| 679 | "utf7": "String", |
| 680 | "utf8": "String", |
| 681 | "utf16": "String", |
| 682 | "i08": "Byte", |
| 683 | "i16": "I16", |
| 684 | "i32": "I32", |
| 685 | "i64": "I64", |
| 686 | "u08": "Byte", |
| 687 | "u16": "U16", |
| 688 | "u32": "U32", |
| 689 | "u64": "U64", |
| 690 | "float": "Double" |
| 691 | } |
| 692 | |
| 693 | CPP_COLLECTION_TYPE_IO_METHOD_SUFFIX_MAP = { |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 694 | MapType : "map", |
| 695 | ListType : "list", |
| 696 | SetType : "set" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | def typeToIOMethodSuffix(ttype): |
| 700 | |
| 701 | if isinstance(ttype, PrimitiveType): |
| 702 | return CPP_PRIMITIVE_TYPE_IO_METHOD_SUFFIX_MAP[ttype.name] |
| 703 | |
| 704 | elif isinstance(ttype, CollectionType): |
| 705 | |
| 706 | result = CPP_COLLECTION_TYPE_IO_METHOD_SUFFIX_MAP[type(ttype)]+"_" |
| 707 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 708 | if isinstance(ttype, MapType): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 709 | result+= "k_"+typeToIOMethodSuffix(ttype.keyType)+"_" |
| 710 | |
| 711 | result += "v_"+typeToIOMethodSuffix(ttype.valueType) |
| 712 | |
| 713 | return result |
| 714 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 715 | elif isinstance(ttype, StructType): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 716 | return "struct_"+ttype.name |
| 717 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 718 | elif isinstance(ttype, TypedefType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 719 | return ttype.name |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 720 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 721 | elif isinstance(ttype, EnumType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 722 | return ttype.name |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 723 | |
| 724 | else: |
| 725 | raise Exception, "Unknown type "+str(ttype) |
| 726 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 727 | def toReaderCall(value, ttype, reader="iprot", transport="itrans"): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 728 | |
| 729 | suffix = typeToIOMethodSuffix(ttype) |
| 730 | |
| 731 | if isinstance(ttype, PrimitiveType): |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 732 | if ttype != VOID_TYPE: |
| 733 | return "xfer += "+reader+"->read"+suffix+"("+transport+", "+value+")" |
| 734 | else: |
| 735 | return "" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 736 | |
| 737 | elif isinstance(ttype, CollectionType): |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 738 | return "xfer+= read_"+suffix+"("+reader+", "+transport+", "+value+")" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 739 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 740 | elif isinstance(ttype, StructType): |
| 741 | return "xfer+= read_"+suffix+"("+reader+", "+transport+", "+value+")" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 742 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 743 | elif isinstance(ttype, TypedefType): |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 744 | return toReaderCall("reinterpret_cast<"+typeToCTypeDeclaration(ttype.definitionType)+"&>("+value+")", ttype.definitionType, reader) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 745 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 746 | elif isinstance(ttype, EnumType): |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 747 | return toReaderCall("reinterpret_cast<"+typeToCTypeDeclaration(I32_TYPE)+"&>("+value+")", I32_TYPE, reader) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 748 | |
| 749 | else: |
| 750 | raise Exception, "Unknown type "+str(ttype) |
| 751 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 752 | def toWriterCall(value, ttype, writer="oprot", transport="otrans"): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 753 | |
| 754 | suffix = typeToIOMethodSuffix(ttype) |
| 755 | |
| 756 | if isinstance(ttype, PrimitiveType): |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 757 | if ttype != VOID_TYPE: |
| 758 | return "xfer+= "+writer+"->write"+suffix+"("+transport+", "+value+")" |
| 759 | else: |
| 760 | return "" |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 761 | |
| 762 | elif isinstance(ttype, CollectionType): |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 763 | return "xfer+= write_"+suffix+"("+writer+", "+transport+", "+value+")" |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 764 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 765 | elif isinstance(ttype, StructType): |
| 766 | return "xfer+= write_"+suffix+"("+writer+", "+transport+", "+value+")" |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 767 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 768 | elif isinstance(ttype, TypedefType): |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 769 | return toWriterCall("reinterpret_cast<const "+typeToCTypeDeclaration(ttype.definitionType)+"&>("+value+")", ttype.definitionType, writer) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 770 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 771 | elif isinstance(ttype, EnumType): |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 772 | return toWriterCall("reinterpret_cast<const "+typeToCTypeDeclaration(I32_TYPE)+"&>("+value+")", I32_TYPE, writer) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 773 | |
| 774 | else: |
| 775 | raise Exception, "Unknown type "+str(ttype) |
| 776 | |
| 777 | CPP_READ_MAP_DEFINITION = Template(""" |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 778 | uint32_t read_${suffix}("""+CPP_PROTOCOLP+""" iprot, """+CPP_TRANSPORTP+""" itrans, ${declaration}& value) { |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 779 | |
| 780 | uint32_t count; |
| 781 | ${keyType} key; |
| 782 | ${valueType} elem; |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 783 | uint32_t xfer = 0; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 784 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 785 | xfer += iprot->readU32(itrans, count); |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 786 | |
| 787 | for(int ix = 0; ix < count; ix++) { |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 788 | ${keyReaderCall}; |
| 789 | ${valueReaderCall}; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 790 | value.insert(std::make_pair(key, elem)); |
| 791 | } |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 792 | |
| 793 | return xfer; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 794 | } |
| 795 | """) |
| 796 | |
| 797 | CPP_WRITE_MAP_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 | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 799 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 800 | uint32_t xfer = 0; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +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 | b2039e7 | 2006-08-09 01:00:17 +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 | ${keyWriterCall}; |
| 806 | ${valueWriterCall}; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 807 | } |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 808 | return xfer; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 809 | } |
| 810 | """) |
| 811 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 812 | CPP_READ_LIST_DEFINITION = Template(""" |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 813 | uint32_t read_${suffix}("""+CPP_PROTOCOLP+""" iprot, """+CPP_TRANSPORTP+""" itrans, ${declaration}& value) { |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 814 | |
| 815 | uint32_t count; |
| 816 | ${valueType} elem; |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 817 | uint32_t xfer = 0; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 818 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 819 | xfer+= iprot->readU32(itrans, count); |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 820 | |
| 821 | for(int ix = 0; ix < count; ix++) { |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 822 | ${valueReaderCall}; |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 823 | value.${insert}(elem); |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 824 | } |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 825 | return xfer; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 826 | } |
| 827 | """) |
| 828 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 829 | CPP_WRITE_LIST_DEFINITION = Template(""" |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 830 | 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] | 831 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 832 | uint32_t xfer = 0; |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 833 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 834 | xfer+= oprot->writeU32(otrans, value.size()); |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 835 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 836 | for(${declaration}::const_iterator ix = value.begin(); ix != value.end(); ++ix) { |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 837 | ${valueWriterCall}; |
| 838 | } |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 839 | return xfer; |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 840 | } |
| 841 | """) |
| 842 | |
| 843 | def toCollectionReaderDefinition(ttype): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 844 | |
| 845 | suffix = typeToIOMethodSuffix(ttype) |
| 846 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 847 | if isinstance(ttype, MapType): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 848 | keyReaderCall = toReaderCall("key", ttype.keyType) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 849 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 850 | valueReaderCall= toReaderCall("elem", ttype.valueType) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 851 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 852 | if isinstance(ttype, MapType): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 853 | return CPP_READ_MAP_DEFINITION.substitute(suffix=suffix, declaration=typeToCTypeDeclaration(ttype), |
| 854 | keyType=typeToCTypeDeclaration(ttype.keyType), |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 855 | keyReaderCall=keyReaderCall, |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 856 | valueType=typeToCTypeDeclaration(ttype.valueType), |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 857 | valueReaderCall=valueReaderCall) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 858 | |
| 859 | else: |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 860 | if isinstance(ttype, ListType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 861 | insert="push_back" |
| 862 | else: |
| 863 | insert="insert" |
| 864 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 865 | return CPP_READ_LIST_DEFINITION.substitute(suffix=suffix, declaration=typeToCTypeDeclaration(ttype), |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 866 | valueReaderCall=valueReaderCall, |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 867 | valueType=typeToCTypeDeclaration(ttype.valueType), |
| 868 | insert=insert) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 869 | |
| 870 | |
| 871 | def toCollectionWriterDefinition(ttype): |
| 872 | |
| 873 | suffix = typeToIOMethodSuffix(ttype) |
| 874 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 875 | if isinstance(ttype, MapType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 876 | keyWriterCall = toWriterCall("ix->first", ttype.keyType) |
| 877 | valueWriterCall = toWriterCall("ix->second", ttype.valueType) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 878 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 879 | else: |
| 880 | valueWriterCall= toWriterCall("*ix", ttype.valueType) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 881 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 882 | if isinstance(ttype, MapType): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 883 | return CPP_WRITE_MAP_DEFINITION.substitute(suffix=suffix, declaration=typeToCTypeDeclaration(ttype), |
| 884 | keyType=typeToCTypeDeclaration(ttype.keyType), |
| 885 | keyWriterCall=keyWriterCall, |
| 886 | valueType=typeToCTypeDeclaration(ttype.valueType), |
| 887 | valueWriterCall=valueWriterCall) |
| 888 | |
| 889 | else: |
| 890 | return CPP_WRITE_LIST_DEFINITION.substitute(suffix=suffix, declaration=typeToCTypeDeclaration(ttype), |
| 891 | valueWriterCall=valueWriterCall, |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 892 | valueType=typeToCTypeDeclaration(ttype.valueType)) |
| 893 | |
| 894 | |
| 895 | CPP_READ_STRUCT_DEFINITION = Template(""" |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 896 | uint32_t read_${suffix}("""+CPP_PROTOCOLP+""" iprot, """+CPP_TRANSPORTP+""" itrans, ${declaration}& value) { |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 897 | |
| 898 | std::string name; |
| 899 | """+CPP_PROTOCOL_TTYPE+""" type; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 900 | int16_t id; |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 901 | uint32_t xfer = 0; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 902 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 903 | xfer+= iprot->readStructBegin(itrans, name); |
| 904 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 905 | while(true) { |
| 906 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 907 | xfer+= iprot->readFieldBegin(itrans, name, type, id); |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 908 | |
| 909 | if(type == """+CPP_PROTOCOL_TSTOP+""") {break;} |
| 910 | |
| 911 | switch(id) { |
| 912 | ${fieldSwitch} |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 913 | default: xfer += iprot->skip(itrans, type); break; |
| 914 | } |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 915 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 916 | xfer+= iprot->readFieldEnd(itrans); |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 917 | } |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 918 | |
| 919 | xfer+= iprot->readStructEnd(itrans); |
| 920 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 921 | return xfer; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 922 | } |
| 923 | """) |
| 924 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 925 | CPP_WRITE_FIELD_DEFINITION = Template(""" |
| 926 | oprot->writeFieldBegin(otrans, \"${name}\", ${type}, ${id}); |
| 927 | ${fieldWriterCall}; |
| 928 | oprot->writeFieldEnd(otrans); |
| 929 | """) |
| 930 | |
| 931 | CPP_WRITE_STRUCT_DEFINITION = Template(""" |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 932 | 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] | 933 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 934 | uint32_t xfer = 0; |
| 935 | |
| 936 | xfer+= oprot->writeStructBegin(otrans, \"${name}\"); |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 937 | ${fieldWriterCalls} |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 938 | xfer+= oprot->writeFieldStop(otrans); |
| 939 | xfer += oprot->writeStructEnd(otrans); |
| 940 | return xfer; |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 941 | } |
| 942 | """) |
| 943 | |
| 944 | def toStructReaderDefinition(ttype): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 945 | |
| 946 | suffix = typeToIOMethodSuffix(ttype) |
| 947 | |
| 948 | # Sort field list in order of increasing ids |
| 949 | |
| 950 | fieldList = [] |
| 951 | fieldList+= ttype.fieldList |
| 952 | |
| 953 | fieldList.sort(lambda a,b: a.id - b.id) |
| 954 | |
| 955 | fieldSwitch="" |
| 956 | |
| 957 | for field in fieldList: |
| 958 | fieldSwitch+= " case "+str(field.id)+": " |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 959 | 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] | 960 | |
| 961 | 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] | 962 | |
| 963 | def toStructWriterDefinition(ttype): |
| 964 | |
| 965 | suffix = typeToIOMethodSuffix(ttype) |
| 966 | |
| 967 | writeCalls = "" |
| 968 | |
| 969 | for field in ttype.fieldList: |
| 970 | |
| 971 | writeCalls+= CPP_WRITE_FIELD_DEFINITION.substitute(name=field.name, type=toWireType(field.type), id=field.id, |
| 972 | fieldWriterCall=toWriterCall("value."+field.name, field.type)) |
| 973 | |
| 974 | 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] | 975 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 976 | def toReaderDefinition(ttype): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 977 | if isinstance(ttype, CollectionType): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 978 | return toCollectionReaderDefinition(ttype) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 979 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 980 | elif isinstance(ttype, StructType): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 981 | return toStructReaderDefinition(ttype) |
| 982 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 983 | elif isinstance(ttype, TypedefType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 984 | return "" |
| 985 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 986 | elif isinstance(ttype, EnumType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 987 | return "" |
| 988 | |
| 989 | else: |
| 990 | raise Exception, "Unsupported type: "+str(ttype) |
| 991 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 992 | def toWriterDefinition(ttype): |
| 993 | if isinstance(ttype, CollectionType): |
| 994 | return toCollectionWriterDefinition(ttype) |
| 995 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 996 | elif isinstance(ttype, StructType): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 997 | return toStructWriterDefinition(ttype) |
| 998 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 999 | elif isinstance(ttype, TypedefType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 1000 | return "" |
| 1001 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1002 | elif isinstance(ttype, EnumType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 1003 | return "" |
| 1004 | |
| 1005 | else: |
| 1006 | raise Exception, "Unsupported type: "+str(ttype) |
| 1007 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1008 | def toOrderedIOList(ttype, result=None): |
| 1009 | if not result: |
| 1010 | result = [] |
| 1011 | |
| 1012 | if ttype in result: |
| 1013 | return result |
| 1014 | |
| 1015 | elif isinstance(ttype, PrimitiveType): |
| 1016 | return result |
| 1017 | |
| 1018 | elif isinstance(ttype, CollectionType): |
| 1019 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1020 | if isinstance(ttype, MapType): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1021 | result = toOrderedIOList(ttype.keyType, result) |
| 1022 | |
| 1023 | result = toOrderedIOList(ttype.valueType, result) |
| 1024 | |
| 1025 | result.append(ttype) |
| 1026 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1027 | elif isinstance(ttype, StructType): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1028 | for field in ttype.fieldList: |
| 1029 | result = toOrderedIOList(field.type, result) |
| 1030 | result.append(ttype) |
| 1031 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1032 | elif isinstance(ttype, TypedefType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 1033 | result.append(ttype) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1034 | return result |
| 1035 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1036 | elif isinstance(ttype, EnumType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 1037 | result.append(ttype) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1038 | return result |
| 1039 | |
| 1040 | elif isinstance(ttype, Program): |
| 1041 | |
| 1042 | for struct in ttype.structMap.values(): |
| 1043 | result = toOrderedIOList(struct, result) |
| 1044 | |
| 1045 | for service in ttype.serviceMap.values(): |
| 1046 | result = toOrderedIOList(service, result) |
| 1047 | |
| 1048 | elif isinstance(ttype, Service): |
| 1049 | for function in ttype.functionList: |
| 1050 | result = toOrderedIOList(function, result) |
| 1051 | |
| 1052 | elif isinstance(ttype, Function): |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1053 | result = toOrderedIOList(ttype.returnType(), result) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1054 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 1055 | # skip the args struct itself and just order the arguments themselves |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1056 | # 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] | 1057 | # inline those struct definitions with the implementation, not in the types header |
| 1058 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1059 | for field in ttype.args(): |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 1060 | result = toOrderedIOList(field.type, result) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1061 | |
| 1062 | else: |
| 1063 | raise Exception, "Unsupported thrift type: "+str(ttype) |
| 1064 | |
| 1065 | return result |
| 1066 | |
| 1067 | def toIOMethodImplementations(program): |
| 1068 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 1069 | # get ordered list of all types that need marshallers: |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1070 | |
| 1071 | iolist = toOrderedIOList(program) |
| 1072 | |
| 1073 | result = "" |
| 1074 | |
| 1075 | for ttype in iolist: |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1076 | result+= toReaderDefinition(ttype) |
| 1077 | result+= toWriterDefinition(ttype) |
| 1078 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1079 | # For all function argument lists, we need to create both struct definitions |
| 1080 | # and io methods. We keep the struct definitions local, since they aren't part of the service API |
| 1081 | # |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 1082 | # Note that we don't need to do a depth-first traverse of arg structs since they can only include fields |
| 1083 | # we've already seen |
| 1084 | |
| 1085 | for service in program.serviceMap.values(): |
| 1086 | for function in service.functionList: |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 1087 | result+= toStructDefinition(function.argsStruct) |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1088 | result+= toReaderDefinition(function.argsStruct) |
| 1089 | result+= toWriterDefinition(function.argsStruct) |
| 1090 | result+= toStructDefinition(function.resultStruct) |
| 1091 | result+= toReaderDefinition(function.resultStruct) |
| 1092 | result+= toWriterDefinition(function.resultStruct) |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 1093 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1094 | return result; |
| 1095 | |
| 1096 | def toImplementationSourceName(filename, genDir=None, debugp=None): |
| 1097 | |
| 1098 | if not genDir: |
| 1099 | genDir = toGenDir(filename) |
| 1100 | |
| 1101 | basename = toBasename(filename) |
| 1102 | |
| 1103 | result = os.path.join(genDir, basename+".cc") |
| 1104 | |
| 1105 | if debugp: |
| 1106 | debugp("toDefinitionHeaderName("+str(filename)+", "+str(genDir)+") => "+str(basename)) |
| 1107 | |
| 1108 | return result |
| 1109 | |
| 1110 | def writeImplementationSource(program, filename, genDir=None, debugp=None): |
| 1111 | |
| 1112 | implementationSource = toImplementationSourceName(filename, genDir) |
| 1113 | |
| 1114 | if debugp: |
| 1115 | debugp("implementationSource: "+str(implementationSource)) |
| 1116 | |
| 1117 | cfile = CFile(implementationSource, "w") |
| 1118 | |
| 1119 | basename = toBasename(filename) |
| 1120 | |
| 1121 | cfile.writeln(CPP_IMPL_HEADER.substitute(source=basename, date=time.ctime())) |
| 1122 | |
| 1123 | cfile.write(toIOMethodImplementations(program)) |
| 1124 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 1125 | cfile.write(toServerDefinition(program)) |
| 1126 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1127 | cfile.write(toClientDefinition(program)) |
| 1128 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1129 | cfile.writeln(CPP_IMPL_FOOTER.substitute(source=basename)) |
| 1130 | |
| 1131 | cfile.close() |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1132 | |
| 1133 | class CPPGenerator(Generator): |
| 1134 | |
| 1135 | def __call__(self, program, filename, genDir=None, debugp=None): |
| 1136 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1137 | writeDefinitionHeader(program, filename, genDir, debugp) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1138 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1139 | writeServicesHeader(program, filename, genDir, debugp) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1140 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1141 | writeImplementationSource(program, filename, genDir, debugp) |