summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Reiss <dreiss@apache.org>2010-09-10 23:00:40 +0000
committerDavid Reiss <dreiss@apache.org>2010-09-10 23:00:40 +0000
commitb6c50e56583d503ab7dcc843d4e09d99f8010ef3 (patch)
tree286e1ed8025f0cd60cfeb717b989d2ee097c77a6
parent13ad873d1815da49cf17f7a52c98895bfde011e1 (diff)
downloadthrift-b6c50e56583d503ab7dcc843d4e09d99f8010ef3.tar.gz
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
-rw-r--r--compiler/cpp/src/generate/t_cpp_generator.cc35
1 files 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 4e4e48a7b..8d35b6d13 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<std::string, std::string>::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());
@@ -204,6 +207,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.
*/
bool gen_dense_;
@@ -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<t_enum_value*> 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"
);