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 |
Marc Slemko | 53027fc | 2006-08-17 01:12:11 +0000 | [diff] [blame] | 5 | from thrift.parser import * |
| 6 | from thrift.generator import * |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 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 | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 21 | ${namespacePrefix} |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 22 | """) |
| 23 | |
| 24 | CPP_TYPES_FOOTER = Template(""" |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 25 | ${namespaceSuffix} |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 26 | #endif // !defined(${source}_types_h_) |
| 27 | """) |
| 28 | |
| 29 | CPP_SERVICES_HEADER = Template(HEADER_COMMENT+""" |
| 30 | #if !defined(${source}_h_) |
| 31 | #define ${source}_h_ 1 |
| 32 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 33 | #include <Thrift.h> |
| 34 | #include <TProcessor.h> |
| 35 | #include <protocol/TProtocol.h> |
| 36 | #include <transport/TTransport.h> |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 37 | #include \"${source}_types.h\" |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 38 | |
| 39 | ${namespacePrefix} |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 40 | """) |
| 41 | |
| 42 | CPP_SERVICES_FOOTER = Template(""" |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 43 | ${namespaceSuffix} |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 44 | #endif // !defined(${source}_h_)""") |
| 45 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 46 | CPP_IMPL_HEADER = Template(HEADER_COMMENT+""" |
| 47 | #include \"${source}.h\" |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 48 | |
| 49 | ${namespacePrefix} |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 50 | """) |
| 51 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 52 | CPP_IMPL_FOOTER = Template(""" |
| 53 | ${namespaceSuffix} |
| 54 | """) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 55 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 56 | def cpp_debug(arg): |
| 57 | print(arg) |
| 58 | |
| 59 | class Indenter(object): |
| 60 | def __init__(self, level=0, step=4): |
| 61 | self.level = level |
| 62 | self.step = step |
| 63 | self.chunk = "" |
| 64 | for i in range(step): |
| 65 | self.chunk+= " " |
| 66 | self.prefix="" |
| 67 | |
| 68 | def inc(self): |
| 69 | self.level+= self.step |
| 70 | self.prefix += self.chunk |
| 71 | |
| 72 | def dec(self): |
| 73 | self.level-= self.step |
| 74 | if(self.level < 0): |
| 75 | raise Exception, "Illegal indent level" |
| 76 | self.prefix = self.prefix[:self.level] |
| 77 | |
| 78 | def __call__(self): |
| 79 | return self.prefix |
| 80 | |
| 81 | class CFile(file): |
| 82 | |
| 83 | def __init__(self, name, flags): |
| 84 | file.__init__(self, name, flags) |
| 85 | self.indent = Indenter() |
| 86 | self.newline = True |
| 87 | |
| 88 | def rwrite(self, value): |
| 89 | file.write(self, value) |
| 90 | |
| 91 | def write(self, value=""): |
| 92 | if self.newline: |
| 93 | self.rwrite(self.indent()) |
| 94 | self.newline = False |
| 95 | self.rwrite(value) |
| 96 | |
| 97 | def writeln(self, value=""): |
| 98 | self.write(value+"\n") |
| 99 | self.newline = True |
| 100 | |
| 101 | def beginBlock(self): |
| 102 | self.writeln("{") |
| 103 | self.indent.inc(); |
| 104 | |
| 105 | def endBlock(self, suffix=""): |
| 106 | self.indent.dec(); |
| 107 | self.writeln("}"+suffix) |
| 108 | |
| 109 | CPP_PRIMITIVE_MAP = { |
| 110 | "void" : "void", |
| 111 | "bool" : "bool", |
| 112 | "string": "std::string", |
| 113 | "utf7": "std::string", |
| 114 | "utf8": "std::wstring", |
| 115 | "utf16": "std::utf16", |
| 116 | "byte" : "uint8_t", |
| 117 | "i08": "int8_t", |
| 118 | "i16": "int16_t", |
| 119 | "i32": "int32_t", |
| 120 | "i64": "int64_t", |
| 121 | "u08": "uint8_t", |
| 122 | "u16": "uint16_t", |
| 123 | "u32": "uint32_t", |
| 124 | "u64": "uint64_t", |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 125 | "float": "float", |
| 126 | "double": "double" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | CPP_CONTAINER_MAP = { |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 130 | MapType : "std::map", |
| 131 | ListType: "std::list", |
| 132 | SetType : "std::set", |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 135 | def toCNamespacePrefix(namespace): |
| 136 | if not namespace: |
| 137 | return "" |
| 138 | else: |
| 139 | return string.join(["namespace "+token + " {" for token in string.split(namespace, ".")], " ") |
| 140 | |
| 141 | def toCNamespaceSuffix(namespace): |
| 142 | if not namespace: |
| 143 | return "" |
| 144 | else: |
| 145 | return string.join(["}" for token in string.split(namespace, ".")], "") |
| 146 | |
| 147 | def toCTypeDeclaration(ttype): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 148 | """ Converts the thrift IDL type to the corresponding C/C++ type. Note that if ttype is FieldType, this function c |
| 149 | converts to type declaration followed by field name, i.e. \"string string_thing\"""" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 150 | |
| 151 | if isinstance(ttype, PrimitiveType): |
| 152 | return CPP_PRIMITIVE_MAP[ttype.name] |
| 153 | |
| 154 | elif isinstance(ttype, CollectionType): |
| 155 | |
| 156 | result = CPP_CONTAINER_MAP[type(ttype)]+"<" |
| 157 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 158 | if isinstance(ttype, MapType): |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 159 | result+= toCTypeDeclaration(ttype.keyType)+", "+ toCTypeDeclaration(ttype.valueType) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 160 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 161 | elif isinstance(ttype, SetType) or isinstance(ttype, ListType): |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 162 | result+= toCTypeDeclaration(ttype.valueType) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 163 | |
| 164 | else: |
| 165 | raise Exception, "Unknown Collection Type "+str(ttype) |
| 166 | |
| 167 | result+= "> " |
| 168 | |
| 169 | return result |
| 170 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 171 | elif isinstance(ttype, StructType): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 172 | return "struct "+ttype.name |
| 173 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 174 | elif isinstance(ttype, TypedefType): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 175 | return ttype.name; |
| 176 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 177 | elif isinstance(ttype, EnumType): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 178 | return ttype.name; |
| 179 | |
| 180 | elif isinstance(ttype, Function): |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 181 | result = toCTypeDeclaration(ttype.returnType())+ " "+ttype.name+"("+string.join([toCTypeDeclaration(arg) for arg in ttype.args()], ", ")+")" |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 182 | if len(ttype.exceptions()): |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 183 | result+= " throw("+string.join([toCTypeDeclaration(exceptions.type) for exceptions in ttype.exceptions()], ", ")+")" |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 184 | return result |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 185 | |
| 186 | elif isinstance(ttype, Field): |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 187 | return toCTypeDeclaration(ttype.type)+ " "+ttype.name |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 188 | |
| 189 | else: |
| 190 | raise Exception, "Unknown type "+str(ttype) |
| 191 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 192 | def toTypeDefDefinition(typedef): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 193 | """ Converts a thrift typedef to a C/C++ typedef """ |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 194 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 195 | return "typedef "+toCTypeDeclaration(typedef.definitionType)+" "+typedef.name+";" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 196 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 197 | def toEnumDefinition(enum): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 198 | """ Converts a thrift enum to a C/C++ enum """ |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 199 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 200 | result = "enum "+enum.name+" {\n" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 201 | |
| 202 | first = True |
| 203 | |
| 204 | for ed in enum.enumDefs: |
| 205 | if first: |
| 206 | first = False |
| 207 | else: |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 208 | result+= ",\n" |
| 209 | result+= " "+ed.name+" = "+str(ed.id) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 210 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 211 | result+= "\n};\n" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 212 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 213 | return result |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 214 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 215 | def toStructDefinition(struct): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 216 | """Converts a thrift struct to a C/C++ struct""" |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 217 | |
| 218 | result = "struct "+struct.name+" {\n" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 219 | |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 220 | # Create constructor defaults for primitive types |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 221 | |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 222 | ctorValues = string.join([field.name+"(0)" for field in struct.fieldList if isinstance(toCanonicalType(field.type), PrimitiveType) and toCanonicalType(field.type) not in [STRING_TYPE, UTF8_TYPE, UTF16_TYPE, VOID_TYPE]], ", ") |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 223 | |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 224 | if len(ctorValues) > 0: |
| 225 | result+= " "+struct.name+"() : "+ctorValues+ " {}\n" |
| 226 | |
| 227 | # Field declarations |
| 228 | |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 229 | result+= string.join([" "+toCTypeDeclaration(field)+";\n" for field in struct.fieldList if not isVoidType(field.type)], "") |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 230 | |
| 231 | # is-field-set struct and ctor |
| 232 | |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 233 | ctorValues = string.join([field.name+"(false)" for field in struct.fieldList if not isVoidType(field.type)], ", ") |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 234 | |
| 235 | if len(ctorValues) > 0: |
| 236 | result+= " struct __isset {\n" |
| 237 | result+= " __isset() : "+ctorValues+" {}\n" |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 238 | result+= string.join([" bool "+field.name+";\n" for field in struct.fieldList if not isVoidType(field.type)], "") |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 239 | result+= " } __isset;\n" |
| 240 | |
| 241 | # bring it on home |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 242 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 243 | result+= "};\n" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 244 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 245 | return result |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 246 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 247 | CPP_DEFINITION_MAP = { |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 248 | TypedefType : toTypeDefDefinition, |
| 249 | EnumType : toEnumDefinition, |
| 250 | StructType : toStructDefinition, |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 251 | ExceptionType : toStructDefinition, |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 252 | Service : None |
| 253 | } |
| 254 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 255 | def toDefinitions(definitions): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 256 | """Converts an arbitrafy thrift grammatical unit definition to the corresponding C/C++ definition""" |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 257 | |
| 258 | result = "" |
| 259 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 260 | for definition in definitions: |
| 261 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 262 | writer = CPP_DEFINITION_MAP[type(definition)] |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 263 | |
| 264 | if writer: |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 265 | result+= writer(definition)+"\n" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 266 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 267 | return result |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 268 | |
| 269 | CPP_THRIFT_NS = "facebook::thrift" |
| 270 | |
| 271 | CPP_INTERFACE_FUNCTION_DECLARATION = Template(""" virtual ${functionDeclaration} = 0; |
| 272 | """) |
| 273 | |
| 274 | CPP_INTERFACE_DECLARATION = Template(""" |
| 275 | class ${service}If { |
| 276 | public: |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 277 | virtual ~${service}If() {} |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 278 | ${functionDeclarations}}; |
| 279 | """) |
| 280 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 281 | def toServiceInterfaceDeclaration(service, debugp=None): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 282 | """Converts a thrift service definition into a C++ abstract base class""" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 283 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 284 | functionDeclarations = string.join([CPP_INTERFACE_FUNCTION_DECLARATION.substitute(service=service.name, functionDeclaration=toCTypeDeclaration(function)) for function in service.functionList], "") |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 285 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 286 | return CPP_INTERFACE_DECLARATION.substitute(service=service.name, functionDeclarations=functionDeclarations) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 287 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 288 | CPP_EXCEPTION = CPP_THRIFT_NS+"::Exception" |
| 289 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 290 | CPP_SP = Template("boost::shared_ptr<${klass}> ") |
| 291 | |
| 292 | CPP_PROCESSOR = CPP_THRIFT_NS+"::TProcessor" |
| 293 | CPP_PROCESSORP = CPP_SP.substitute(klass=CPP_PROCESSOR) |
| 294 | |
| 295 | CPP_PROTOCOL_NS = CPP_THRIFT_NS+"::protocol" |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 296 | CPP_PROTOCOL = CPP_PROTOCOL_NS+"::TProtocol" |
| 297 | CPP_PROTOCOLP = CPP_SP.substitute(klass="const "+CPP_PROTOCOL) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 298 | |
| 299 | |
| 300 | CPP_TRANSPORT_NS = CPP_THRIFT_NS+"::transport" |
| 301 | CPP_TRANSPORT = CPP_TRANSPORT_NS+"::TTransport" |
| 302 | CPP_TRANSPORTP = CPP_SP.substitute(klass=CPP_TRANSPORT) |
| 303 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 304 | CPP_PROTOCOL_TSTOP = CPP_PROTOCOL_NS+"::T_STOP" |
| 305 | CPP_PROTOCOL_TTYPE = CPP_PROTOCOL_NS+"::TType" |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 306 | CPP_PROTOCOL_MESSAGE_TYPE = CPP_PROTOCOL_NS+"::TMessageType" |
| 307 | CPP_PROTOCOL_CALL = CPP_PROTOCOL_NS+"::T_CALL" |
| 308 | CPP_PROTOCOL_REPLY = CPP_PROTOCOL_NS+"::T_REPLY" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 309 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 310 | CPP_TTYPE_MAP = { |
| 311 | STOP_TYPE : CPP_PROTOCOL_NS+"::T_STOP", |
| 312 | VOID_TYPE : CPP_PROTOCOL_NS+"::T_VOID", |
| 313 | BOOL_TYPE : CPP_PROTOCOL_NS+"::T_BOOL", |
| 314 | UTF7_TYPE : CPP_PROTOCOL_NS+"::T_UTF7", |
| 315 | UTF7_TYPE : CPP_PROTOCOL_NS+"::T_UTF7", |
| 316 | UTF8_TYPE : CPP_PROTOCOL_NS+"::T_UTF8", |
| 317 | UTF16_TYPE : CPP_PROTOCOL_NS+"::T_UTF16", |
| 318 | U08_TYPE : CPP_PROTOCOL_NS+"::T_U08", |
| 319 | I08_TYPE : CPP_PROTOCOL_NS+"::T_I08", |
| 320 | I16_TYPE : CPP_PROTOCOL_NS+"::T_I16", |
| 321 | I32_TYPE : CPP_PROTOCOL_NS+"::T_I32", |
| 322 | I64_TYPE : CPP_PROTOCOL_NS+"::T_I64", |
| 323 | U08_TYPE : CPP_PROTOCOL_NS+"::T_U08", |
| 324 | U16_TYPE : CPP_PROTOCOL_NS+"::T_U16", |
| 325 | U32_TYPE : CPP_PROTOCOL_NS+"::T_U32", |
| 326 | U64_TYPE : CPP_PROTOCOL_NS+"::T_U64", |
| 327 | FLOAT_TYPE : CPP_PROTOCOL_NS+"::T_FLOAT", |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 328 | DOUBLE_TYPE : CPP_PROTOCOL_NS+"::T_DOUBLE", |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 329 | StructType : CPP_PROTOCOL_NS+"::T_STRUCT", |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 330 | ExceptionType : CPP_PROTOCOL_NS+"::T_STRUCT", |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 331 | ListType : CPP_PROTOCOL_NS+"::T_LIST", |
| 332 | MapType : CPP_PROTOCOL_NS+"::T_MAP", |
| 333 | SetType : CPP_PROTOCOL_NS+"::T_SET" |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Marc Slemko | 91f6748 | 2006-08-11 23:58:57 +0000 | [diff] [blame] | 336 | |
| 337 | CPP_SERVER_FUNCTION_DECLARATION = Template(""" void process_${function}(uint32_t seqid, """+CPP_TRANSPORTP+""" itrans, """+CPP_TRANSPORTP+""" otrans); |
| 338 | """) |
| 339 | |
| 340 | CPP_SERVER_FUNCTION_DEFINITION = Template(""" |
| 341 | void ${service}ServerIf::process_${function}(uint32_t seqid, """+CPP_TRANSPORTP+""" itrans, """+CPP_TRANSPORTP+""" otrans) { |
| 342 | |
| 343 | uint32_t xfer = 0; |
| 344 | |
| 345 | ${argsStructDeclaration}; |
| 346 | |
| 347 | ${argsStructReader}; |
| 348 | |
| 349 | _iprot->readMessageEnd(itrans); |
| 350 | |
Marc Slemko | 91f6748 | 2006-08-11 23:58:57 +0000 | [diff] [blame] | 351 | ${resultStructDeclaration}; |
| 352 | |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 353 | ${functionCall} |
Marc Slemko | 91f6748 | 2006-08-11 23:58:57 +0000 | [diff] [blame] | 354 | |
| 355 | _oprot->writeMessageBegin(otrans, \"${function}\", """+CPP_PROTOCOL_REPLY+""", seqid); |
| 356 | |
| 357 | ${resultStructWriter}; |
| 358 | |
| 359 | _oprot->writeMessageEnd(otrans); |
| 360 | |
| 361 | otrans->flush(); |
| 362 | } |
| 363 | """) |
| 364 | |
| 365 | CPP_SERVER_PROCESS_DEFINITION = Template(""" |
| 366 | bool ${service}ServerIf::process("""+CPP_TRANSPORTP+""" itrans, """+CPP_TRANSPORTP+""" otrans) { |
| 367 | |
Marc Slemko | 91f6748 | 2006-08-11 23:58:57 +0000 | [diff] [blame] | 368 | std::string name; |
| 369 | |
| 370 | """+CPP_PROTOCOL_MESSAGE_TYPE+""" messageType; |
| 371 | |
| 372 | uint32_t seqid; |
| 373 | |
| 374 | _iprot->readMessageBegin(itrans, name, messageType, seqid); |
| 375 | |
| 376 | if(messageType == """+CPP_PROTOCOL_CALL+""") { |
| 377 | ${callProcessSwitch} |
| 378 | } else { |
| 379 | throw """+CPP_EXCEPTION+"""(\"Unexpected message type\"); |
| 380 | } |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 381 | |
| 382 | return true; |
Marc Slemko | 91f6748 | 2006-08-11 23:58:57 +0000 | [diff] [blame] | 383 | } |
| 384 | """) |
| 385 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 386 | def toWireType(ttype): |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 387 | """Converts a thrift type to the corresponding wire type. This differs from toCTypeDeclaration in that it reduces typedefs |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 388 | to their canonical form and converts enums to signedf 32 bit integers""" |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 389 | |
| 390 | if isinstance(ttype, PrimitiveType): |
| 391 | return CPP_TTYPE_MAP[ttype] |
| 392 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 393 | elif isinstance(ttype, EnumType): |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 394 | return CPP_TTYPE_MAP[I32_TYPE] |
| 395 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 396 | elif isinstance(ttype, TypedefType): |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 397 | return toWireType(toCanonicalType(ttype)) |
| 398 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 399 | elif isinstance(ttype, StructType) or isinstance(ttype, CollectionType): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 400 | return CPP_TTYPE_MAP[type(ttype)] |
| 401 | |
| 402 | else: |
| 403 | raise Exception, "No wire type for thrift type: "+str(ttype) |
| 404 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 405 | CPP_SERVER_DECLARATION = Template(""" |
| 406 | class ${service}ServerIf : public ${service}If, public """+CPP_PROCESSOR+""" { |
| 407 | public: |
| 408 | ${service}ServerIf("""+CPP_PROTOCOLP+""" protocol): _iprot(protocol), _oprot(protocol) {} |
| 409 | ${service}ServerIf("""+CPP_PROTOCOLP+""" iprot, """+CPP_PROTOCOLP+""" oprot) : _iprot(iprot), _oprot(oprot) {} |
| 410 | virtual ~${service}ServerIf() {} |
| 411 | bool process("""+CPP_TRANSPORTP+""" _itrans,"""+CPP_TRANSPORTP+""" _otrans); |
| 412 | protected: |
| 413 | """+CPP_PROTOCOLP+""" _iprot; |
| 414 | """+CPP_PROTOCOLP+""" _oprot; |
| 415 | private: |
| 416 | ${functionDeclarations}}; |
| 417 | """) |
| 418 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 419 | def toServerDeclaration(service, debugp=None): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 420 | """Converts a thrift service definition to the Server skeleton class declaration.""" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 421 | |
| 422 | functionDeclarations = string.join([CPP_SERVER_FUNCTION_DECLARATION.substitute(function=function.name) for function in service.functionList], "") |
| 423 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 424 | return CPP_SERVER_DECLARATION.substitute(service=service.name, functionDeclarations=functionDeclarations) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 425 | |
| 426 | CPP_CLIENT_FUNCTION_DECLARATION = Template(""" ${functionDeclaration}; |
| 427 | """) |
| 428 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 429 | |
| 430 | CPP_CLIENT_FUNCTION_DEFINITION = Template(""" |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 431 | ${returnDeclaration} ${service}Client::${function}(${argsDeclaration}) ${exceptionDeclaration} { |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 432 | |
| 433 | uint32_t xfer = 0; |
Marc Slemko | 91f6748 | 2006-08-11 23:58:57 +0000 | [diff] [blame] | 434 | std::string name; |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 435 | """+CPP_PROTOCOL_MESSAGE_TYPE+""" messageType; |
| 436 | uint32_t cseqid = 0; |
| 437 | uint32_t rseqid = 0; |
| 438 | |
Marc Slemko | 91f6748 | 2006-08-11 23:58:57 +0000 | [diff] [blame] | 439 | _oprot->writeMessageBegin(_otrans, \"${function}\", """+CPP_PROTOCOL_CALL+""", cseqid); |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 440 | |
| 441 | ${argsStructDeclaration}; |
| 442 | |
| 443 | ${argsToStruct}; |
| 444 | |
| 445 | ${argsStructWriter}; |
| 446 | |
| 447 | _otrans->flush(); |
| 448 | |
Marc Slemko | 91f6748 | 2006-08-11 23:58:57 +0000 | [diff] [blame] | 449 | _iprot->readMessageBegin(_itrans, name, messageType, rseqid); |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 450 | |
| 451 | if(messageType != """+CPP_PROTOCOL_REPLY+""" || |
| 452 | rseqid != cseqid) { |
| 453 | throw """+CPP_EXCEPTION+"""(\"unexpected message type or id\"); |
| 454 | } |
| 455 | |
| 456 | ${resultStructDeclaration}; |
| 457 | |
| 458 | ${resultStructReader}; |
| 459 | |
| 460 | _iprot->readMessageEnd(_itrans); |
| 461 | |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 462 | ${returnResult} |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 463 | throw """+CPP_EXCEPTION+"""(\"${function} failed: unknown result\"); |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 464 | } |
| 465 | """) |
| 466 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 467 | CPP_CLIENT_DECLARATION = Template(""" |
| 468 | class ${service}Client : public ${service}If { |
| 469 | |
| 470 | public: |
| 471 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 472 | ${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] | 473 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 474 | ${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] | 475 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 476 | ${functionDeclarations} |
| 477 | private: |
| 478 | """+CPP_TRANSPORTP+""" _itrans; |
| 479 | """+CPP_TRANSPORTP+""" _otrans; |
| 480 | """+CPP_PROTOCOLP+""" _iprot; |
| 481 | """+CPP_PROTOCOLP+""" _oprot; |
| 482 | };""") |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 483 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 484 | def toServerFunctionDefinition(servicePrefix, function, debugp=None): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 485 | """Converts a thrift service method declaration into a server method-call processoror function""" |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 486 | result = "" |
| 487 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 488 | argsStructDeclaration = toCTypeDeclaration(function.argsStruct)+" __args" |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 489 | |
| 490 | argsStructReader = toReaderCall("__args", function.argsStruct, "_iprot") |
| 491 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 492 | resultStructDeclaration = toCTypeDeclaration(function.resultStruct)+" __result" |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 493 | |
| 494 | resultStructWriter = toWriterCall("__result", function.resultStruct, "_oprot") |
| 495 | |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 496 | if not isVoidType(function.returnType()): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 497 | functionCallPrefix= "__result.success = " |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 498 | functionCallSuffix = "\n __result.__isset.success = true;" |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 499 | else: |
| 500 | functionCallPrefix = "" |
| 501 | functionCallSuffix = "" |
| 502 | |
| 503 | functionCall= function.name+"("+string.join(["__args."+arg.name for arg in function.args()], ", ")+");" |
| 504 | |
| 505 | exceptions = function.exceptions() |
| 506 | |
| 507 | if len(exceptions) > 0: |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 508 | functionCallPrefix= "try {\n "+functionCallPrefix |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 509 | |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 510 | functionCallSuffix+= "\n }"+string.join([" catch("+toCTypeDeclaration(exceptions[ix].type)+"& e"+str(ix)+") {\n __result."+exceptions[ix].name+" = e"+str(ix)+";\n __result.__isset."+exceptions[ix].name+" = true;\n }" |
| 511 | for ix in range(len(exceptions))], "") |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 512 | |
| 513 | functionCall = functionCallPrefix+functionCall+functionCallSuffix |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 514 | |
| 515 | result+= CPP_SERVER_FUNCTION_DEFINITION.substitute(service=servicePrefix, function=function.name, |
| 516 | argsStructDeclaration=argsStructDeclaration, |
| 517 | argsStructReader=argsStructReader, |
| 518 | functionCall=functionCall, |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 519 | resultStructDeclaration=resultStructDeclaration, |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 520 | resultStructWriter=resultStructWriter) |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 521 | return result |
| 522 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 523 | def toServerServiceDefinition(service, debugp=None): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 524 | """Converts a thrift service definiton to a server skeleton implementation""" |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 525 | |
| 526 | result = "" |
| 527 | |
| 528 | for function in service.functionList: |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 529 | |
| 530 | result+= toServerFunctionDefinition(service.name, function, debugp) |
Marc Slemko | 66d67d8 | 2006-08-11 23:33:08 +0000 | [diff] [blame] | 531 | |
Marc Slemko | 53027fc | 2006-08-17 01:12:11 +0000 | [diff] [blame] | 532 | callProcessSwitch = " if"+string.join(["(name.compare(\""+function.name+"\") == 0) {\n process_"+function.name+"(seqid, itrans, otrans);\n }" for function in service.functionList], " else if")+" else {\n throw "+CPP_EXCEPTION+"(\"Unknown function name \\\"\"+name+\"\\\"\");\n }" |
Marc Slemko | 66d67d8 | 2006-08-11 23:33:08 +0000 | [diff] [blame] | 533 | |
Marc Slemko | 91f6748 | 2006-08-11 23:58:57 +0000 | [diff] [blame] | 534 | result+= CPP_SERVER_PROCESS_DEFINITION.substitute(service=service.name, callProcessSwitch=callProcessSwitch) |
Marc Slemko | 66d67d8 | 2006-08-11 23:33:08 +0000 | [diff] [blame] | 535 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 536 | return result |
| 537 | |
| 538 | def toServerDefinition(program, debugp=None): |
| 539 | |
| 540 | return string.join([toServerServiceDefinition(service) for service in program.serviceMap.values()], "\n") |
| 541 | |
| 542 | def toClientDeclaration(service, debugp=None): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 543 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 544 | functionDeclarations = string.join([CPP_CLIENT_FUNCTION_DECLARATION.substitute(functionDeclaration=toCTypeDeclaration(function)) for function in service.functionList], "") |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 545 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 546 | return CPP_CLIENT_DECLARATION.substitute(service=service.name, functionDeclarations=functionDeclarations)+"\n" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 547 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 548 | def toClientFunctionDefinition(servicePrefix, function, debugp=None): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 549 | """Converts a thrift service method declaration to a client stub implementation""" |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 550 | |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 551 | isVoid = isVoidType(function.returnType()) |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 552 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 553 | returnDeclaration = toCTypeDeclaration(function.returnType()) |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 554 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 555 | argsDeclaration = string.join([toCTypeDeclaration(function.args()[ix].type)+" __arg"+str(ix) for ix in range(len(function.args()))], ", ") |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 556 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 557 | exceptionDeclaration = string.join([toCTypeDeclaration(exception.type) for exception in function.exceptions()], ", ") |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 558 | |
| 559 | if len(exceptionDeclaration)> 0: |
| 560 | exceptionDeclaration = "throw("+exceptionDeclaration+")" |
| 561 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 562 | argsStructDeclaration = toCTypeDeclaration(function.argsStruct)+" __args" |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 563 | |
| 564 | argsStructWriter = toWriterCall("__args", function.argsStruct, "_oprot", "_otrans") |
| 565 | |
| 566 | argsToStruct= string.join([" __args."+function.args()[ix].name+" = __arg"+str(ix) for ix in range(len(function.args()))], ";\n") |
| 567 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 568 | resultStructDeclaration = toCTypeDeclaration(function.resultStruct)+" __result" |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 569 | |
| 570 | resultStructReader = toReaderCall("__result", function.resultStruct, "_iprot", "_itrans") |
| 571 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 572 | exceptions = function.exceptions() |
| 573 | |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 574 | """ void return and non-void returns require very different arrangments. For void returns, we don't actually expect |
| 575 | anything to have been sent from the remote side, therefore we need to check for explicit exception returns first, |
| 576 | then, if none were encountered, return. For void we test for success first - since this is the most likey result - |
| 577 | and then check for exceptions. In both cases, we need to handle the case where there are no specified exceptions. """ |
| 578 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 579 | if len(exceptions) > 0: |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 580 | errors= ["if(__result.__isset."+exception.name+") {\n throw __result."+exception.name+";\n }" for exception in exceptions] |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 581 | else: |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 582 | errors = [] |
| 583 | |
| 584 | if not isVoid: |
| 585 | returnResult = " if(__result.__isset.success) {\n return __result.success;\n}" |
| 586 | if len(errors) > 0: |
| 587 | returnResult+= " else "+string.join(errors, " else ") |
| 588 | else: |
| 589 | if len(errors) > 0: |
| 590 | returnResult= " "+string.join(errors, " else ")+" else {\n return;\n }\n" |
| 591 | else: |
| 592 | returnResult=" return;\n" |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 593 | |
| 594 | return CPP_CLIENT_FUNCTION_DEFINITION.substitute(service=servicePrefix, |
| 595 | function=function.name, |
| 596 | returnDeclaration=returnDeclaration, |
| 597 | argsDeclaration=argsDeclaration, |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 598 | exceptionDeclaration=exceptionDeclaration, |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 599 | argsStructDeclaration=argsStructDeclaration, |
| 600 | argsStructWriter=argsStructWriter, |
| 601 | argsToStruct=argsToStruct, |
| 602 | resultStructDeclaration=resultStructDeclaration, |
| 603 | resultStructReader=resultStructReader, |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 604 | returnResult=returnResult) |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 605 | |
| 606 | def toClientServiceDefinition(service, debugp=None): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 607 | """Converts a thrift service definition to a client stub implementation""" |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 608 | |
| 609 | result = "" |
| 610 | |
| 611 | for function in service.functionList: |
| 612 | |
| 613 | result+= toClientFunctionDefinition(service.name, function) |
| 614 | |
| 615 | return result |
| 616 | |
| 617 | def toClientDefinition(program, debugp=None): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 618 | """Converts all services in a thrift program to client stub implementations""" |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 619 | |
| 620 | return string.join([toClientServiceDefinition(service) for service in program.serviceMap.values()], "\n") |
| 621 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 622 | def toServiceDeclaration(service, debugp=None): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 623 | """Converts all services in a thrift program to service interface or abstract base class declarations""" |
| 624 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 625 | return toServiceInterfaceDeclaration(service, debugp) + toServerDeclaration(service, debugp) + toClientDeclaration(service, debugp) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 626 | |
Marc Slemko | d97eb61 | 2006-08-24 23:37:36 +0000 | [diff] [blame] | 627 | def toGenDir(filename, suffix="gen-cpp", debugp=None): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 628 | """creates a generated-code subdirectory for C++ code based on filename of thrift source file and optional suffix""" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 629 | |
| 630 | result = os.path.join(os.path.split(filename)[0], suffix) |
| 631 | |
| 632 | if not os.path.exists(result): |
| 633 | os.mkdir(result) |
| 634 | |
| 635 | return result |
| 636 | |
| 637 | def toBasename(filename, debugp=None): |
| 638 | """ Take the filename minus the path and\".thrift\" extension if present """ |
| 639 | |
| 640 | basename = os.path.split(filename)[1] |
| 641 | |
| 642 | tokens = os.path.splitext(basename) |
| 643 | |
| 644 | if tokens[1].lower() == ".thrift": |
| 645 | basename = tokens[0] |
| 646 | |
| 647 | if debugp: |
| 648 | debugp("toBasename("+str(filename)+") => "+str(basename)) |
| 649 | |
| 650 | return basename |
| 651 | |
| 652 | def toDefinitionHeaderName(filename, genDir=None, debugp=None): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 653 | """Creates a file name for the public thrift data types based on filename of thrift source file and optional suffix""" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 654 | |
| 655 | if not genDir: |
| 656 | genDir = toGenDir(filename) |
| 657 | |
| 658 | basename = toBasename(filename) |
| 659 | |
| 660 | result = os.path.join(genDir, basename+"_types.h") |
| 661 | |
| 662 | if debugp: |
| 663 | debugp("toDefinitionHeaderName("+str(filename)+", "+str(genDir)+") => "+str(basename)) |
| 664 | |
| 665 | return result |
| 666 | |
| 667 | def writeDefinitionHeader(program, filename, genDir=None, debugp=None): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 668 | """Writes public thrift data types defined in program into a public data types header file. Uses the name of the original |
| 669 | thrift source, filename, and the optional generated C++ code directory, genDir, to determine the name and location of header file""" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 670 | |
| 671 | definitionHeader = toDefinitionHeaderName(filename, genDir) |
| 672 | |
| 673 | if debugp: |
| 674 | debugp("definitionHeader: "+str(definitionHeader)) |
| 675 | |
| 676 | cfile = CFile(definitionHeader, "w") |
| 677 | |
| 678 | basename = toBasename(filename) |
| 679 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 680 | cfile.writeln(CPP_TYPES_HEADER.substitute(source=basename, date=time.ctime(), namespacePrefix=toCNamespacePrefix(program.namespace))) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 681 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 682 | cfile.write(toDefinitions(program.definitions)) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 683 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 684 | cfile.writeln(CPP_TYPES_FOOTER.substitute(source=basename, namespaceSuffix=toCNamespaceSuffix(program.namespace))) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 685 | |
| 686 | cfile.close() |
| 687 | |
| 688 | def toServicesHeaderName(filename, genDir=None, debugp=None): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 689 | """Creates a file name for the public thrift services based on filename of thrift source file and optional suffix""" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 690 | |
| 691 | if not genDir: |
| 692 | genDir = toGenDir(filename) |
| 693 | |
| 694 | basename = toBasename(filename) |
| 695 | |
| 696 | result = os.path.join(genDir, basename+".h") |
| 697 | |
| 698 | if debugp: |
| 699 | debugp("toDefinitionHeaderName("+str(filename)+", "+str(genDir)+") => "+str(basename)) |
| 700 | |
| 701 | return result |
| 702 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 703 | def writeServicesHeader(program, filename, genDir=None, debugp=None): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 704 | """Writes public thrift service abstract base class, client class, and server class for all services defined in program into a public services header file. Uses the name of the original thrift source, filename, and the optional generated C++ code directory, genDir, to determine the name and location of header file""" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 705 | |
| 706 | servicesHeader = toServicesHeaderName(filename, genDir) |
| 707 | |
| 708 | if debugp: |
| 709 | debugp("servicesHeader: "+str(servicesHeader)) |
| 710 | |
| 711 | cfile = CFile(servicesHeader, "w") |
| 712 | |
| 713 | basename = toBasename(filename) |
| 714 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 715 | cfile.writeln(CPP_SERVICES_HEADER.substitute(source=basename, date=time.ctime(), namespacePrefix=toCNamespacePrefix(program.namespace))) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 716 | |
| 717 | services = [] |
| 718 | |
| 719 | # Build orderered list of service definitions by scanning definitions list for services |
| 720 | |
| 721 | for definition in program.definitions: |
| 722 | if isinstance(definition, Service) and definition.name in program.serviceMap: |
| 723 | services.append(definition) |
| 724 | |
| 725 | for service in services: |
| 726 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 727 | cfile.write(toServiceDeclaration(service)) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 728 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 729 | cfile.writeln(CPP_SERVICES_FOOTER.substitute(source=basename, namespaceSuffix=toCNamespaceSuffix(program.namespace))) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 730 | |
| 731 | cfile.close() |
| 732 | |
| 733 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 734 | CPP_STRUCT_READ = Template(""" |
| 735 | uint32_t read${name}Struct("""+CPP_PROTOCOLP+""" _iprot, """+CPP_TRANSPORTP+""" itrans, ${declaration}& value) { |
| 736 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 737 | std::string name; |
| 738 | uint32_t id; |
| 739 | uint32_t type; |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 740 | uint32_t xfer = 0; |
| 741 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 742 | while(true) { |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 743 | xfer+= _iprot->readFieldBegin(_itrans, name, type, id); |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 744 | if(type == """+CPP_PROTOCOL_TSTOP+""") { |
| 745 | break; |
| 746 | } |
| 747 | switch(id) { |
| 748 | ${readFieldListSwitch} |
| 749 | } |
| 750 | } |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 751 | |
| 752 | xfer+= _iprot->readStructEnd(_itrans); |
| 753 | |
| 754 | return xfer; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 755 | } |
| 756 | """) |
| 757 | |
| 758 | CPP_PRIMITIVE_TYPE_IO_METHOD_SUFFIX_MAP = { |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 759 | "void" :"Void", |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 760 | "bool" : "Bool", |
| 761 | "string": "String", |
| 762 | "utf7": "String", |
| 763 | "utf8": "String", |
| 764 | "utf16": "String", |
| 765 | "i08": "Byte", |
| 766 | "i16": "I16", |
| 767 | "i32": "I32", |
| 768 | "i64": "I64", |
| 769 | "u08": "Byte", |
| 770 | "u16": "U16", |
| 771 | "u32": "U32", |
| 772 | "u64": "U64", |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 773 | "float": "Float", |
| 774 | "double": "Double" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | CPP_COLLECTION_TYPE_IO_METHOD_SUFFIX_MAP = { |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 778 | MapType : "map", |
| 779 | ListType : "list", |
| 780 | SetType : "set" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | def typeToIOMethodSuffix(ttype): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 784 | """Converts type to a name suitable as a suffix with TProtocol primitive read and write methods or with a generated read or write |
| 785 | method for a complex type""" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 786 | |
| 787 | if isinstance(ttype, PrimitiveType): |
| 788 | return CPP_PRIMITIVE_TYPE_IO_METHOD_SUFFIX_MAP[ttype.name] |
| 789 | |
| 790 | elif isinstance(ttype, CollectionType): |
| 791 | |
| 792 | result = CPP_COLLECTION_TYPE_IO_METHOD_SUFFIX_MAP[type(ttype)]+"_" |
| 793 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 794 | if isinstance(ttype, MapType): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 795 | result+= "k_"+typeToIOMethodSuffix(ttype.keyType)+"_" |
| 796 | |
| 797 | result += "v_"+typeToIOMethodSuffix(ttype.valueType) |
| 798 | |
| 799 | return result |
| 800 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 801 | elif isinstance(ttype, StructType): |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 802 | return "struct_"+ttype.name |
| 803 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 804 | elif isinstance(ttype, TypedefType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 805 | return ttype.name |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 806 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 807 | elif isinstance(ttype, EnumType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 808 | return ttype.name |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 809 | |
| 810 | else: |
| 811 | raise Exception, "Unknown type "+str(ttype) |
| 812 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 813 | def toReaderCall(value, ttype, reader="iprot", transport="itrans"): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 814 | """Converts type to a name suitable as a suffix with TProtocol primitive read methods or with a generated read |
| 815 | method for a complex type""" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 816 | |
| 817 | suffix = typeToIOMethodSuffix(ttype) |
| 818 | |
| 819 | if isinstance(ttype, PrimitiveType): |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 820 | if ttype != VOID_TYPE: |
| 821 | return "xfer += "+reader+"->read"+suffix+"("+transport+", "+value+")" |
| 822 | else: |
| 823 | return "" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 824 | |
| 825 | elif isinstance(ttype, CollectionType): |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 826 | return "xfer+= read_"+suffix+"("+reader+", "+transport+", "+value+")" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 827 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 828 | elif isinstance(ttype, StructType): |
| 829 | return "xfer+= read_"+suffix+"("+reader+", "+transport+", "+value+")" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 830 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 831 | elif isinstance(ttype, TypedefType): |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 832 | return toReaderCall("reinterpret_cast<"+toCTypeDeclaration(ttype.definitionType)+"&>("+value+")", ttype.definitionType, reader) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 833 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 834 | elif isinstance(ttype, EnumType): |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 835 | return toReaderCall("reinterpret_cast<"+toCTypeDeclaration(I32_TYPE)+"&>("+value+")", I32_TYPE, reader) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 836 | |
| 837 | else: |
| 838 | raise Exception, "Unknown type "+str(ttype) |
| 839 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 840 | def toWriterCall(value, ttype, writer="oprot", transport="otrans"): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 841 | """Converts type to a name suitable as a suffix with TProtocol primitive write methods or with a generated write |
| 842 | method for a complex type""" |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 843 | |
| 844 | suffix = typeToIOMethodSuffix(ttype) |
| 845 | |
| 846 | if isinstance(ttype, PrimitiveType): |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 847 | if ttype != VOID_TYPE: |
| 848 | return "xfer+= "+writer+"->write"+suffix+"("+transport+", "+value+")" |
| 849 | else: |
| 850 | return "" |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 851 | |
| 852 | elif isinstance(ttype, CollectionType): |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 853 | return "xfer+= write_"+suffix+"("+writer+", "+transport+", "+value+")" |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 854 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 855 | elif isinstance(ttype, StructType): |
| 856 | return "xfer+= write_"+suffix+"("+writer+", "+transport+", "+value+")" |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 857 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 858 | elif isinstance(ttype, TypedefType): |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 859 | return toWriterCall("reinterpret_cast<const "+toCTypeDeclaration(ttype.definitionType)+"&>("+value+")", ttype.definitionType, writer) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 860 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 861 | elif isinstance(ttype, EnumType): |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 862 | return toWriterCall("reinterpret_cast<const "+toCTypeDeclaration(I32_TYPE)+"&>("+value+")", I32_TYPE, writer) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 863 | |
| 864 | else: |
| 865 | raise Exception, "Unknown type "+str(ttype) |
| 866 | |
| 867 | CPP_READ_MAP_DEFINITION = Template(""" |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 868 | uint32_t read_${suffix}("""+CPP_PROTOCOLP+""" iprot, """+CPP_TRANSPORTP+""" itrans, ${declaration}& value) { |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 869 | |
| 870 | uint32_t count; |
| 871 | ${keyType} key; |
| 872 | ${valueType} elem; |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 873 | uint32_t xfer = 0; |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 874 | """+CPP_PROTOCOL_TTYPE+""" keyType; |
| 875 | """+CPP_PROTOCOL_TTYPE+""" valueType; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 876 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 877 | xfer += iprot->readMapBegin(itrans, keyType, valueType, count); |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 878 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 879 | /* XXX |
| 880 | Should we assert that read key and value type are correct? */ |
| 881 | |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 882 | for(uint32_t ix = 0; ix < count; ix++) { |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 883 | ${keyReaderCall}; |
| 884 | ${valueReaderCall}; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 885 | value.insert(std::make_pair(key, elem)); |
| 886 | } |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 887 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 888 | xfer += iprot->readMapEnd(itrans); |
| 889 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 890 | return xfer; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 891 | } |
| 892 | """) |
| 893 | |
| 894 | CPP_WRITE_MAP_DEFINITION = Template(""" |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 895 | 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] | 896 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 897 | uint32_t xfer = 0; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 898 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 899 | xfer += oprot->writeMapBegin(otrans, ${keyWireType}, ${valueWireType}, value.size()); |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 900 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 901 | for(${declaration}::const_iterator ix = value.begin(); ix != value.end(); ++ix) { |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 902 | ${keyWriterCall}; |
| 903 | ${valueWriterCall}; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 904 | } |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 905 | |
| 906 | xfer += oprot->writeMapEnd(otrans); |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 907 | return xfer; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 908 | } |
| 909 | """) |
| 910 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 911 | CPP_READ_LIST_DEFINITION = Template(""" |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 912 | uint32_t read_${suffix}("""+CPP_PROTOCOLP+""" iprot, """+CPP_TRANSPORTP+""" itrans, ${declaration}& value) { |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 913 | |
| 914 | uint32_t count; |
| 915 | ${valueType} elem; |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 916 | uint32_t xfer = 0; |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 917 | """+CPP_PROTOCOL_TTYPE+""" valueType; |
| 918 | |
| 919 | xfer += iprot->read${protocolSuffix}Begin(itrans, valueType, count); |
| 920 | |
| 921 | /* XXX |
| 922 | Should we assert that read value type is correct? */ |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 923 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 924 | xfer+= iprot->readU32(itrans, count); |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 925 | |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 926 | for(uint32_t ix = 0; ix < count; ix++) { |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 927 | ${valueReaderCall}; |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 928 | value.${insert}(elem); |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 929 | } |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 930 | |
| 931 | xfer += iprot->read${protocolSuffix}End(itrans); |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 932 | return xfer; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 933 | } |
| 934 | """) |
| 935 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 936 | CPP_WRITE_LIST_DEFINITION = Template(""" |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 937 | 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] | 938 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 939 | uint32_t xfer = 0; |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 940 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 941 | xfer+= oprot->write${protocolSuffix}Begin(otrans, ${valueWireType}, value.size()); |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 942 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 943 | for(${declaration}::const_iterator ix = value.begin(); ix != value.end(); ++ix) { |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 944 | ${valueWriterCall}; |
| 945 | } |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 946 | |
| 947 | xfer+= oprot->write${protocolSuffix}End(otrans); |
| 948 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 949 | return xfer; |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 950 | } |
| 951 | """) |
| 952 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 953 | def toCollectionReaderDefinition(collection): |
| 954 | """Converts collection type to reader function definition""" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 955 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 956 | suffix = typeToIOMethodSuffix(collection) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 957 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 958 | if isinstance(collection, MapType): |
| 959 | keyReaderCall = toReaderCall("key", collection.keyType) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 960 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 961 | valueReaderCall= toReaderCall("elem", collection.valueType) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 962 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 963 | if isinstance(collection, MapType): |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 964 | return CPP_READ_MAP_DEFINITION.substitute(suffix=suffix, declaration=toCTypeDeclaration(collection), |
| 965 | keyType=toCTypeDeclaration(collection.keyType), |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 966 | keyReaderCall=keyReaderCall, |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 967 | valueType=toCTypeDeclaration(collection.valueType), |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 968 | valueReaderCall=valueReaderCall) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 969 | |
| 970 | else: |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 971 | if isinstance(collection, ListType): |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 972 | protocolSuffix="List" |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 973 | insert="push_back" |
| 974 | else: |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 975 | protocolSuffix="Set" |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 976 | insert="insert" |
| 977 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 978 | return CPP_READ_LIST_DEFINITION.substitute(suffix=suffix, declaration=toCTypeDeclaration(collection), |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 979 | protocolSuffix=protocolSuffix, |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 980 | valueReaderCall=valueReaderCall, |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 981 | valueType=toCTypeDeclaration(collection.valueType), |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 982 | insert=insert) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 983 | |
| 984 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 985 | def toCollectionWriterDefinition(collection): |
| 986 | """Converts collection type to writer function definition""" |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 987 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 988 | suffix = typeToIOMethodSuffix(collection) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 989 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 990 | if isinstance(collection, MapType): |
| 991 | keyWriterCall = toWriterCall("ix->first", collection.keyType) |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 992 | keyWireType = toWireType(collection.keyType) |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 993 | valueWriterCall = toWriterCall("ix->second", collection.valueType) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 994 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 995 | else: |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 996 | valueWriterCall= toWriterCall("*ix", collection.valueType) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 997 | |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 998 | valueWireType = toWireType(collection.valueType) |
| 999 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 1000 | if isinstance(collection, MapType): |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 1001 | return CPP_WRITE_MAP_DEFINITION.substitute(suffix=suffix, declaration=toCTypeDeclaration(collection), |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 1002 | keyType=toCTypeDeclaration(collection.keyType), |
| 1003 | keyWireType=keyWireType, |
| 1004 | keyWriterCall=keyWriterCall, |
| 1005 | valueType=toCTypeDeclaration(collection.valueType), |
| 1006 | valueWireType=valueWireType, |
| 1007 | valueWriterCall=valueWriterCall) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1008 | |
| 1009 | else: |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 1010 | if isinstance(collection, ListType): |
| 1011 | protocolSuffix = "List" |
| 1012 | elif isinstance(collection, SetType): |
| 1013 | protocolSuffix = "Set" |
| 1014 | else: |
| 1015 | raise Exception, "Unknown collection type "+str(type(collection))+":"+str(collection) |
| 1016 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 1017 | return CPP_WRITE_LIST_DEFINITION.substitute(suffix=suffix, declaration=toCTypeDeclaration(collection), |
Marc Slemko | b09f588 | 2006-08-23 22:03:34 +0000 | [diff] [blame] | 1018 | protocolSuffix=protocolSuffix, |
| 1019 | valueWireType=valueWireType, |
| 1020 | valueWriterCall=valueWriterCall, |
| 1021 | valueType=toCTypeDeclaration(collection.valueType)) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1022 | |
| 1023 | |
| 1024 | CPP_READ_STRUCT_DEFINITION = Template(""" |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 1025 | uint32_t read_${suffix}("""+CPP_PROTOCOLP+""" iprot, """+CPP_TRANSPORTP+""" itrans, ${declaration}& value) { |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1026 | |
| 1027 | std::string name; |
| 1028 | """+CPP_PROTOCOL_TTYPE+""" type; |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 1029 | int16_t id; |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 1030 | uint32_t xfer = 0; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1031 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1032 | xfer+= iprot->readStructBegin(itrans, name); |
| 1033 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1034 | while(true) { |
| 1035 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 1036 | xfer+= iprot->readFieldBegin(itrans, name, type, id); |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1037 | |
| 1038 | if(type == """+CPP_PROTOCOL_TSTOP+""") {break;} |
| 1039 | |
| 1040 | switch(id) { |
| 1041 | ${fieldSwitch} |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1042 | default: xfer += iprot->skip(itrans, type); break; |
| 1043 | } |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1044 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 1045 | xfer+= iprot->readFieldEnd(itrans); |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1046 | } |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1047 | |
| 1048 | xfer+= iprot->readStructEnd(itrans); |
| 1049 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 1050 | return xfer; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1051 | } |
| 1052 | """) |
| 1053 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 1054 | CPP_WRITE_FIELD_DEFINITION = Template("""oprot->writeFieldBegin(otrans, \"${name}\", ${type}, ${id}); ${fieldWriterCall}; oprot->writeFieldEnd(otrans)""") |
| 1055 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1056 | CPP_WRITE_STRUCT_DEFINITION = Template(""" |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 1057 | 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] | 1058 | |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 1059 | uint32_t xfer = 0; |
| 1060 | |
| 1061 | xfer+= oprot->writeStructBegin(otrans, \"${name}\"); |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1062 | ${fieldWriterCalls} |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 1063 | xfer+= oprot->writeFieldStop(otrans); |
| 1064 | xfer += oprot->writeStructEnd(otrans); |
| 1065 | return xfer; |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1066 | } |
| 1067 | """) |
| 1068 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 1069 | def toStructReaderDefinition(struct): |
| 1070 | """Converts struct type to reader function definition""" |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1071 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 1072 | suffix = typeToIOMethodSuffix(struct) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1073 | |
| 1074 | # Sort field list in order of increasing ids |
| 1075 | |
| 1076 | fieldList = [] |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 1077 | fieldList+= struct.fieldList |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1078 | |
| 1079 | fieldList.sort(lambda a,b: a.id - b.id) |
| 1080 | |
| 1081 | fieldSwitch="" |
| 1082 | |
| 1083 | for field in fieldList: |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 1084 | if isVoidType(field.type): |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 1085 | continue; |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1086 | fieldSwitch+= " case "+str(field.id)+": " |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1087 | 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] | 1088 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 1089 | return CPP_READ_STRUCT_DEFINITION.substitute(suffix=suffix, declaration=toCTypeDeclaration(struct), fieldSwitch=fieldSwitch) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1090 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1091 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 1092 | def toStructWriterDefinition(struct): |
| 1093 | """Converts struct type to writer function definition""" |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1094 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 1095 | suffix = typeToIOMethodSuffix(struct) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1096 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 1097 | fieldWriterCalls = [] |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1098 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 1099 | fieldWriterCalls = [CPP_WRITE_FIELD_DEFINITION.substitute(name=field.name, |
| 1100 | type=toWireType(toCanonicalType(field.type)), |
| 1101 | id=field.id, |
| 1102 | fieldWriterCall=toWriterCall("value."+field.name, field.type))+";" |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 1103 | for field in struct.fieldList if not isVoidType(field.type) |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 1104 | ] |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 1105 | |
| 1106 | fieldWriterCalls = " "+string.join(fieldWriterCalls, "\n ") |
| 1107 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 1108 | return CPP_WRITE_STRUCT_DEFINITION.substitute(name=struct.name, suffix=suffix, declaration=toCTypeDeclaration(struct), fieldWriterCalls=fieldWriterCalls) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1109 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 1110 | CPP_WRITE_RESULT_STRUCT_DEFINITION = Template(""" |
| 1111 | uint32_t write_${suffix}("""+CPP_PROTOCOLP+""" oprot, """+CPP_TRANSPORTP+""" otrans, const ${declaration}& value) { |
| 1112 | |
| 1113 | uint32_t xfer = 0; |
| 1114 | |
| 1115 | xfer+= oprot->writeStructBegin(otrans, \"${name}\"); |
| 1116 | if(${value}.__isset.x) |
| 1117 | ${fieldWriterCalls} |
| 1118 | xfer+= oprot->writeFieldStop(otrans); |
| 1119 | xfer += oprot->writeStructEnd(otrans); |
| 1120 | return xfer; |
| 1121 | } |
| 1122 | """) |
| 1123 | |
| 1124 | def toResultStructReaderDefinition(struct): |
| 1125 | """Converts internal results struct to a reader function definition""" |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 1126 | |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 1127 | return toStructReaderDefinition(struct) |
| 1128 | |
| 1129 | def toResultStructWriterDefinition(struct): |
| 1130 | """Converts internal results struct to a reader function definition. The difference between this function and toStructWriterDefinition is that this only sends one field, either success or an exception field, depending on which field is set""" |
| 1131 | |
| 1132 | suffix = typeToIOMethodSuffix(struct) |
| 1133 | |
Marc Slemko | bf4fd19 | 2006-08-15 21:29:39 +0000 | [diff] [blame] | 1134 | fieldWriterCalls = ["if(value.__isset."+field.name+") { "+ |
| 1135 | CPP_WRITE_FIELD_DEFINITION.substitute(name=field.name, |
| 1136 | type=toWireType(toCanonicalType(field.type)), |
| 1137 | id=field.id, |
| 1138 | fieldWriterCall=toWriterCall("value."+field.name, field.type))+";}" |
Marc Slemko | c693640 | 2006-08-23 02:15:31 +0000 | [diff] [blame] | 1139 | for field in struct.fieldList if not isVoidType(field.type)] |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 1140 | |
| 1141 | fieldWriterCalls = " "+string.join(fieldWriterCalls, "\n else ") |
| 1142 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 1143 | return CPP_WRITE_STRUCT_DEFINITION.substitute(name=struct.name, suffix=suffix, declaration=toCTypeDeclaration(struct), fieldWriterCalls=fieldWriterCalls) |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 1144 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1145 | def toReaderDefinition(ttype): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 1146 | """Converts thrift type to a reader function definition""" |
| 1147 | |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1148 | if isinstance(ttype, CollectionType): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1149 | return toCollectionReaderDefinition(ttype) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1150 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1151 | elif isinstance(ttype, StructType): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1152 | return toStructReaderDefinition(ttype) |
| 1153 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1154 | elif isinstance(ttype, TypedefType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 1155 | return "" |
| 1156 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1157 | elif isinstance(ttype, EnumType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 1158 | return "" |
| 1159 | |
| 1160 | else: |
| 1161 | raise Exception, "Unsupported type: "+str(ttype) |
| 1162 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1163 | def toWriterDefinition(ttype): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 1164 | """Converts thrift type to a writer function definition""" |
| 1165 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1166 | if isinstance(ttype, CollectionType): |
| 1167 | return toCollectionWriterDefinition(ttype) |
| 1168 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1169 | elif isinstance(ttype, StructType): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1170 | return toStructWriterDefinition(ttype) |
| 1171 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1172 | elif isinstance(ttype, TypedefType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 1173 | return "" |
| 1174 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1175 | elif isinstance(ttype, EnumType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 1176 | return "" |
| 1177 | |
| 1178 | else: |
| 1179 | raise Exception, "Unsupported type: "+str(ttype) |
| 1180 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1181 | def toOrderedIOList(ttype, result=None): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 1182 | """Builds a list of types ordered by doing a depth first traverse of all thrift type definitions. This gives us a list from which we can |
| 1183 | generate read/write methods without making forward references.""" |
| 1184 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1185 | if not result: |
| 1186 | result = [] |
| 1187 | |
| 1188 | if ttype in result: |
| 1189 | return result |
| 1190 | |
| 1191 | elif isinstance(ttype, PrimitiveType): |
| 1192 | return result |
| 1193 | |
| 1194 | elif isinstance(ttype, CollectionType): |
| 1195 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1196 | if isinstance(ttype, MapType): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1197 | result = toOrderedIOList(ttype.keyType, result) |
| 1198 | |
| 1199 | result = toOrderedIOList(ttype.valueType, result) |
| 1200 | |
| 1201 | result.append(ttype) |
| 1202 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1203 | elif isinstance(ttype, StructType): |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1204 | for field in ttype.fieldList: |
| 1205 | result = toOrderedIOList(field.type, result) |
| 1206 | result.append(ttype) |
| 1207 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1208 | elif isinstance(ttype, TypedefType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 1209 | result.append(ttype) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1210 | return result |
| 1211 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1212 | elif isinstance(ttype, EnumType): |
Marc Slemko | c4eb9e8 | 2006-08-10 03:29:29 +0000 | [diff] [blame] | 1213 | result.append(ttype) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1214 | return result |
| 1215 | |
| 1216 | elif isinstance(ttype, Program): |
| 1217 | |
| 1218 | for struct in ttype.structMap.values(): |
| 1219 | result = toOrderedIOList(struct, result) |
| 1220 | |
| 1221 | for service in ttype.serviceMap.values(): |
| 1222 | result = toOrderedIOList(service, result) |
| 1223 | |
| 1224 | elif isinstance(ttype, Service): |
| 1225 | for function in ttype.functionList: |
| 1226 | result = toOrderedIOList(function, result) |
| 1227 | |
| 1228 | elif isinstance(ttype, Function): |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1229 | result = toOrderedIOList(ttype.returnType(), result) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1230 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 1231 | # skip the args struct itself and just order the arguments themselves |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1232 | # 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] | 1233 | # inline those struct definitions with the implementation, not in the types header |
| 1234 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1235 | for field in ttype.args(): |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 1236 | result = toOrderedIOList(field.type, result) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1237 | |
| 1238 | else: |
| 1239 | raise Exception, "Unsupported thrift type: "+str(ttype) |
| 1240 | |
| 1241 | return result |
| 1242 | |
| 1243 | def toIOMethodImplementations(program): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 1244 | """Generates read and write methods for all non-primitive types in a thrift program as well as for the internal argStruct and resultStruct types for |
| 1245 | all service functions""" |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1246 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 1247 | # get ordered list of all types that need marshallers: |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1248 | |
| 1249 | iolist = toOrderedIOList(program) |
| 1250 | |
| 1251 | result = "" |
| 1252 | |
| 1253 | for ttype in iolist: |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1254 | result+= toReaderDefinition(ttype) |
| 1255 | result+= toWriterDefinition(ttype) |
| 1256 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1257 | # For all function argument lists, we need to create both struct definitions |
| 1258 | # and io methods. We keep the struct definitions local, since they aren't part of the service API |
| 1259 | # |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 1260 | # Note that we don't need to do a depth-first traverse of arg structs since they can only include fields |
| 1261 | # we've already seen |
| 1262 | |
| 1263 | for service in program.serviceMap.values(): |
| 1264 | for function in service.functionList: |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 1265 | result+= toStructDefinition(function.argsStruct) |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1266 | result+= toReaderDefinition(function.argsStruct) |
| 1267 | result+= toWriterDefinition(function.argsStruct) |
| 1268 | result+= toStructDefinition(function.resultStruct) |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 1269 | result+= toResultStructReaderDefinition(function.resultStruct) |
| 1270 | result+= toResultStructWriterDefinition(function.resultStruct) |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 1271 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1272 | return result; |
| 1273 | |
| 1274 | def toImplementationSourceName(filename, genDir=None, debugp=None): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 1275 | """Creates a file name for the implementation of client stubs, server skeletons, and non-primitive read/write methods.""" |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1276 | |
| 1277 | if not genDir: |
| 1278 | genDir = toGenDir(filename) |
| 1279 | |
| 1280 | basename = toBasename(filename) |
| 1281 | |
| 1282 | result = os.path.join(genDir, basename+".cc") |
| 1283 | |
| 1284 | if debugp: |
| 1285 | debugp("toDefinitionHeaderName("+str(filename)+", "+str(genDir)+") => "+str(basename)) |
| 1286 | |
| 1287 | return result |
| 1288 | |
| 1289 | def writeImplementationSource(program, filename, genDir=None, debugp=None): |
Marc Slemko | d8b1051 | 2006-08-14 23:30:37 +0000 | [diff] [blame] | 1290 | """Writes client stub, server skeleton, and non-primitive type I/O functions for all servciesf defined in program into a C/C++ source file. Uses the name of the original |
| 1291 | thrift source, filename, and the optional generated C++ code directory, genDir, to determine the name and location of header file""" |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1292 | |
| 1293 | implementationSource = toImplementationSourceName(filename, genDir) |
| 1294 | |
| 1295 | if debugp: |
| 1296 | debugp("implementationSource: "+str(implementationSource)) |
| 1297 | |
| 1298 | cfile = CFile(implementationSource, "w") |
| 1299 | |
| 1300 | basename = toBasename(filename) |
| 1301 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 1302 | cfile.writeln(CPP_IMPL_HEADER.substitute(source=basename, date=time.ctime(), namespacePrefix=toCNamespacePrefix(program.namespace))) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1303 | |
| 1304 | cfile.write(toIOMethodImplementations(program)) |
| 1305 | |
Marc Slemko | 0b4ffa9 | 2006-08-11 02:49:29 +0000 | [diff] [blame] | 1306 | cfile.write(toServerDefinition(program)) |
| 1307 | |
Marc Slemko | 5b126d6 | 2006-08-11 23:03:42 +0000 | [diff] [blame] | 1308 | cfile.write(toClientDefinition(program)) |
| 1309 | |
Marc Slemko | 1785985 | 2006-08-15 00:21:31 +0000 | [diff] [blame] | 1310 | cfile.writeln(CPP_IMPL_FOOTER.substitute(source=basename, namespaceSuffix=toCNamespaceSuffix(program.namespace))) |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1311 | |
| 1312 | cfile.close() |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1313 | |
| 1314 | class CPPGenerator(Generator): |
| 1315 | |
| 1316 | def __call__(self, program, filename, genDir=None, debugp=None): |
| 1317 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1318 | writeDefinitionHeader(program, filename, genDir, debugp) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1319 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1320 | writeServicesHeader(program, filename, genDir, debugp) |
Marc Slemko | b2039e7 | 2006-08-09 01:00:17 +0000 | [diff] [blame] | 1321 | |
Marc Slemko | db14e17 | 2006-08-09 23:36:18 +0000 | [diff] [blame] | 1322 | writeImplementationSource(program, filename, genDir, debugp) |