You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by dr...@apache.org on 2010/10/06 19:10:15 UTC

svn commit: r1005143 - in /incubator/thrift/trunk: compiler/cpp/src/generate/t_cpp_generator.cc lib/cpp/src/Thrift.h

Author: dreiss
Date: Wed Oct  6 17:10:15 2010
New Revision: 1005143

URL: http://svn.apache.org/viewvc?rev=1005143&view=rev
Log:
THRIFT-925. cpp: Add _VALUES_TO_NAMES enum map

Modified:
    incubator/thrift/trunk/compiler/cpp/src/generate/t_cpp_generator.cc
    incubator/thrift/trunk/lib/cpp/src/Thrift.h

Modified: incubator/thrift/trunk/compiler/cpp/src/generate/t_cpp_generator.cc
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/compiler/cpp/src/generate/t_cpp_generator.cc?rev=1005143&r1=1005142&r2=1005143&view=diff
==============================================================================
--- incubator/thrift/trunk/compiler/cpp/src/generate/t_cpp_generator.cc (original)
+++ incubator/thrift/trunk/compiler/cpp/src/generate/t_cpp_generator.cc Wed Oct  6 17:10:15 2010
@@ -198,6 +198,12 @@ class t_cpp_generator : public t_oop_gen
   std::string type_to_enum(t_type* ttype);
   std::string local_reflection_name(const char*, t_type* ttype, bool external=false);
 
+  void generate_enum_constant_list(std::ofstream& f,
+                                   const vector<t_enum_value*>& constants,
+                                   const char* prefix,
+                                   const char* suffix,
+                                   bool include_values);
+
   // These handles checking gen_dense_ and checking for duplicates.
   void generate_local_reflection(std::ofstream& out, t_type* ttype, bool is_definition);
   void generate_local_reflection_pointer(std::ofstream& out, t_type* ttype);
@@ -441,6 +447,35 @@ void t_cpp_generator::generate_typedef(t
     endl;
 }
 
+
+void t_cpp_generator::generate_enum_constant_list(std::ofstream& f,
+                                                  const vector<t_enum_value*>& constants,
+                                                  const char* prefix,
+                                                  const char* suffix,
+                                                  bool include_values) {
+  f << " {" << endl;
+  indent_up();
+
+  vector<t_enum_value*>::const_iterator c_iter;
+  bool first = true;
+  for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
+    if (first) {
+      first = false;
+    } else {
+      f << "," << endl;
+    }
+    indent(f)
+      << prefix << (*c_iter)->get_name() << suffix;
+    if (include_values && (*c_iter)->has_value()) {
+      f << " = " << (*c_iter)->get_value();
+    }
+  }
+
+  f << endl;
+  indent_down();
+  indent(f) << "};" << endl;
+}
+
 /**
  * Generates code for an enumerated type. In C++, this is essentially the same
  * as the thrift definition itself, using the enum keyword in C++.
@@ -448,6 +483,8 @@ void t_cpp_generator::generate_typedef(t
  * @param tenum The enumeration
  */
 void t_cpp_generator::generate_enum(t_enum* tenum) {
+  vector<t_enum_value*> constants = tenum->get_constants();
+
   std::string enum_name = tenum->get_name();
   if (!gen_pure_enums_) {
     enum_name = "type";
@@ -456,28 +493,9 @@ void t_cpp_generator::generate_enum(t_en
     indent_up();
   }
   f_types_ <<
-    indent() << "enum " << enum_name << " {" << endl;
-  indent_up();
-
-  vector<t_enum_value*> constants = tenum->get_constants();
-  vector<t_enum_value*>::iterator c_iter;
-  bool first = true;
-  for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
-    if (first) {
-      first = false;
-    } else {
-      f_types_ <<
-        "," << endl;
-    }
-    f_types_ <<
-      indent() << (*c_iter)->get_name();
-    f_types_ <<
-      " = " << (*c_iter)->get_value();
-  }
+    indent() << "enum " << enum_name;
 
-  indent_down();
-  f_types_ << endl;
-  indent(f_types_) << "};" << endl;
+  generate_enum_constant_list(f_types_, constants, "", "", true);
 
   if (!gen_pure_enums_) {
     indent_down();
@@ -486,6 +504,29 @@ void t_cpp_generator::generate_enum(t_en
 
   f_types_ << endl;
 
+  /**
+     Generate a character array of enum names for debugging purposes.
+  */
+  std::string prefix = tenum->get_name() + "::";
+  f_types_impl_ <<
+    indent() << "int _k" << tenum->get_name() << "Values[] =";
+  generate_enum_constant_list(f_types_impl_, constants, prefix.c_str(), "", false);
+
+  f_types_impl_ <<
+    indent() << "const char* _k" << tenum->get_name() << "Names[] =";
+  generate_enum_constant_list(f_types_impl_, constants, "\"", "\"", false);
+
+  f_types_ <<
+    indent() << "extern const std::map<int, const char*> _" <<
+    tenum->get_name() << "_VALUES_TO_NAMES;" << endl << endl;
+
+  f_types_impl_ <<
+    indent() << "const std::map<int, const char*> _" << tenum->get_name() <<
+    "_VALUES_TO_NAMES(::apache::thrift::TEnumIterator(" << constants.size() <<
+    ", _k" << tenum->get_name() << "Values" <<
+    ", _k" << tenum->get_name() << "Names), " <<
+    "::apache::thrift::TEnumIterator(-1, NULL, NULL));" << endl << endl;
+
   generate_local_reflection(f_types_, tenum, false);
   generate_local_reflection(f_types_impl_, tenum, true);
 }

Modified: incubator/thrift/trunk/lib/cpp/src/Thrift.h
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/cpp/src/Thrift.h?rev=1005143&r1=1005142&r2=1005143&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/cpp/src/Thrift.h (original)
+++ incubator/thrift/trunk/lib/cpp/src/Thrift.h Wed Oct  6 17:10:15 2010
@@ -24,6 +24,7 @@
 #include "config.h"
 #endif
 #include <stdio.h>
+#include <assert.h>
 
 #include <sys/types.h>
 #include <netinet/in.h>
@@ -42,6 +43,34 @@
 
 namespace apache { namespace thrift {
 
+class TEnumIterator {
+ public:
+  TEnumIterator(int n,
+                int* enums,
+                const char** names) :
+      ii_(0), n_(n), enums_(enums), names_(names) {
+  }
+
+  int operator ++() {
+    return ++ii_;
+  }
+
+  bool operator !=(const TEnumIterator& end) {
+    assert(end.n_ == -1);
+    return (ii_ != n_);
+  }
+
+  std::pair<int, const char*> operator*() const {
+    return std::make_pair(enums_[ii_], names_[ii_]);
+  }
+
+ private:
+  int ii_;
+  const int n_;
+  int* enums_;
+  const char** names_;
+};
+
 class TOutput {
  public:
   TOutput() : f_(&errorTimeWrapper) {}