From b6c50e56583d503ab7dcc843d4e09d99f8010ef3 Mon Sep 17 00:00:00 2001 From: David Reiss Date: Fri, 10 Sep 2010 23:00:40 +0000 Subject: [PATCH] THRIFT-895. cpp: By default, generate enums as class-scoped enums Most of the other Thrift languages either have enum values that are scoped to the type or emulate enums in that way. Now C++ does the same by default. "enum Foo" in a .thrift file will be generated as Foo::type so the values can be called Foo::value1, etc. The "pure_enums" compiler option restores the old behavior. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@996015 13f79535-47bb-0310-9956-ffa450edef68 --- compiler/cpp/src/generate/t_cpp_generator.cc | 35 +++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/compiler/cpp/src/generate/t_cpp_generator.cc b/compiler/cpp/src/generate/t_cpp_generator.cc index 4e4e48a7..8d35b6d1 100644 --- a/compiler/cpp/src/generate/t_cpp_generator.cc +++ b/compiler/cpp/src/generate/t_cpp_generator.cc @@ -50,6 +50,9 @@ class t_cpp_generator : public t_oop_generator { { std::map::const_iterator iter; + iter = parsed_options.find("pure_enums"); + gen_pure_enums_ = (iter != parsed_options.end()); + iter = parsed_options.find("dense"); gen_dense_ = (iter != parsed_options.end()); @@ -203,6 +206,11 @@ class t_cpp_generator : public t_oop_generator { */ std::string get_include_prefix(const t_program& program) const; + /** + * True iff we should generate pure enums for Thrift enums, instead of wrapper classes. + */ + bool gen_pure_enums_; + /** * True iff we should generate local reflection metadata for TDenseProtocol. */ @@ -363,8 +371,15 @@ void t_cpp_generator::generate_typedef(t_typedef* ttypedef) { * @param tenum The enumeration */ void t_cpp_generator::generate_enum(t_enum* tenum) { + std::string enum_name = tenum->get_name(); + if (!gen_pure_enums_) { + enum_name = "type"; + f_types_ << + indent() << "struct " << tenum->get_name() << " {" << endl; + indent_up(); + } f_types_ << - indent() << "enum " << tenum->get_name() << " {" << endl; + indent() << "enum " << enum_name << " {" << endl; indent_up(); vector constants = tenum->get_constants(); @@ -386,10 +401,15 @@ void t_cpp_generator::generate_enum(t_enum* tenum) { } indent_down(); - f_types_ << - endl << - "};" << endl << - endl; + f_types_ << endl; + indent(f_types_) << "};" << endl; + + if (!gen_pure_enums_) { + indent_down(); + f_types_ << "};" << endl; + } + + f_types_ << endl; generate_local_reflection(f_types_, tenum, false); generate_local_reflection(f_types_impl_, tenum, true); @@ -2764,6 +2784,10 @@ string t_cpp_generator::type_name(t_type* ttype, bool in_typedef, bool arg) { pname = class_prefix + ttype->get_name(); } + if (ttype->is_enum() && !gen_pure_enums_) { + pname += "::type"; + } + if (arg) { if (is_complex_type(ttype)) { return "const " + pname + "&"; @@ -3016,6 +3040,7 @@ string t_cpp_generator::get_include_prefix(const t_program& program) const { THRIFT_REGISTER_GENERATOR(cpp, "C++", +" pure_enums: Generate pure enums instead of wrapper classes.\n" " dense: Generate type specifications for the dense protocol.\n" " include_prefix: Use full include paths in generated files.\n" ); -- 2.17.1