From b324c6cc57a149204296e3d63c73f15c73629f98 Mon Sep 17 00:00:00 2001 From: "(no author)" <(no author)@41a61cd8-c433-0410-bb1c-e256eeef9e11> Date: Tue, 18 Dec 2007 03:42:29 +0000 Subject: [PATCH] r1417@opsdev009 (orig r74659): dreiss | 2007-12-17 19:37:30 -0800 Thrift: Optional support for new-style classes. Reviewed By: mcslee Test Plan: Thrifted ThriftTest.thrift and looked at the output. Revert Plan: ok Other Notes: Based on a patch from Jon Dugan. git-svn-id: http://svn.facebook.com/svnroot/thrift/trunk@727 41a61cd8-c433-0410-bb1c-e256eeef9e11 --- compiler/cpp/src/generate/t_py_generator.cc | 10 ++++++---- compiler/cpp/src/generate/t_py_generator.h | 11 +++++++++-- compiler/cpp/src/main.cc | 27 ++++++++++++--------------- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/compiler/cpp/src/generate/t_py_generator.cc b/compiler/cpp/src/generate/t_py_generator.cc index d682bee..7f3507f 100644 --- a/compiler/cpp/src/generate/t_py_generator.cc +++ b/compiler/cpp/src/generate/t_py_generator.cc @@ -149,7 +149,9 @@ void t_py_generator::generate_typedef(t_typedef* ttypedef) {} */ void t_py_generator::generate_enum(t_enum* tenum) { f_types_ << - "class " << tenum->get_name() << ":" << endl; + "class " << tenum->get_name() << + (gen_newstyle_ ? "(object)" : "") << + ":" << endl; indent_up(); vector constants = tenum->get_constants(); @@ -326,9 +328,7 @@ struct FieldKeyCompare { }; /** - * Generates a struct definition for a thrift data type. This is nothing in PHP - * where the objects are all just associative arrays (unless of course we - * decide to start using objects for them...) + * Generates a struct definition for a thrift data type. * * @param tstruct The struct definition */ @@ -346,6 +346,8 @@ void t_py_generator::generate_py_struct_definition(ofstream& out, "class " << tstruct->get_name(); if (is_exception) { out << "(Exception)"; + } else if (gen_newstyle_) { + out << "(object)"; } out << ":" << endl; diff --git a/compiler/cpp/src/generate/t_py_generator.h b/compiler/cpp/src/generate/t_py_generator.h index c2eed8d..4732561 100644 --- a/compiler/cpp/src/generate/t_py_generator.h +++ b/compiler/cpp/src/generate/t_py_generator.h @@ -21,8 +21,10 @@ */ class t_py_generator : public t_generator { public: - t_py_generator(t_program* program) : - t_generator(program) { + t_py_generator(t_program* program, bool gen_newstyle) : + t_generator(program), + gen_newstyle_(gen_newstyle) { + out_dir_base_ = "gen-py"; } @@ -147,6 +149,11 @@ class t_py_generator : public t_generator { private: /** + * True iff we should generate new-style classes. + */ + bool gen_newstyle_; + + /** * File streams */ diff --git a/compiler/cpp/src/main.cc b/compiler/cpp/src/main.cc index 0a47e53..ac5fa04 100644 --- a/compiler/cpp/src/main.cc +++ b/compiler/cpp/src/main.cc @@ -133,10 +133,12 @@ int g_doctext_lineno; * Flags to control code generation */ bool gen_cpp = false; +bool gen_dense = false; bool gen_java = false; bool gen_javabean = false; bool gen_rb = false; bool gen_py = false; +bool gen_py_newstyle = false; bool gen_xsd = false; bool gen_php = false; bool gen_phpi = false; @@ -150,7 +152,6 @@ bool gen_ocaml = false; bool gen_hs = false; bool gen_cocoa = false; bool gen_st = false; -bool gen_dense = false; bool gen_recurse = false; /** @@ -569,6 +570,7 @@ void usage() { fprintf(stderr, " -phpa Generate PHP with autoload (with -php)\n"); fprintf(stderr, " -phpo Generate PHP with object oriented subclasses (with -php)\n"); fprintf(stderr, " -py Generate Python output files\n"); + fprintf(stderr, " -pyns Generate Python new-style classes (with -py)\n"); fprintf(stderr, " -rb Generate Ruby output files\n"); fprintf(stderr, " -xsd Generate XSD output files\n"); fprintf(stderr, " -perl Generate Perl output files\n"); @@ -834,7 +836,7 @@ void generate(t_program* program) { if (gen_py) { pverbose("Generating Python\n"); - t_py_generator* py = new t_py_generator(program); + t_py_generator* py = new t_py_generator(program, gen_py_newstyle); py->generate_program(); delete py; } @@ -958,30 +960,25 @@ int main(int argc, char** argv) { } else if (strcmp(arg, "-phpi") == 0) { gen_phpi = true; } else if (strcmp(arg, "-phps") == 0) { - if (!gen_php) { - gen_php = true; - } + gen_php = true; gen_phps = true; } else if (strcmp(arg, "-phpl") == 0) { - if (!gen_php) { - gen_php = true; - } + gen_php = true; gen_phps = false; } else if (strcmp(arg, "-phpa") == 0) { - if (!gen_php) { - gen_php = true; - gen_phps = false; - } + gen_php = true; + gen_phps = false; gen_phpa = true; } else if (strcmp(arg, "-phpo") == 0) { - if (!gen_php) { - gen_php = true; - } + gen_php = true; gen_phpo = true; } else if (strcmp(arg, "-rest") == 0) { gen_rest = true; } else if (strcmp(arg, "-py") == 0) { gen_py = true; + } else if (strcmp(arg, "-pyns") == 0) { + gen_py = true; + gen_py_newstyle = true; } else if (strcmp(arg, "-rb") == 0) { gen_rb = true; } else if (strcmp(arg, "-xsd") == 0) { -- 2.11.4.GIT