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
diff --git a/compiler/cpp/src/generate/t_cpp_generator.cc b/compiler/cpp/src/generate/t_cpp_generator.cc
index 4e4e48a..8d35b6d 100644
--- a/compiler/cpp/src/generate/t_cpp_generator.cc
+++ b/compiler/cpp/src/generate/t_cpp_generator.cc
@@ -50,6 +50,9 @@
   {
     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 @@
   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 @@
  * @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 @@
   }
 
   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 @@
     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 @@
 
 
 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"
 );