Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtodd/1001141 to your computer and use it in GitHub Desktop.
Save mtodd/1001141 to your computer and use it in GitHub Desktop.
From 0236c4cf9b61787413090365a6e42b29aadcb02d Mon Sep 17 00:00:00 2001
From: David Reiss <dreiss@facebook.com>
Date: Sun, 12 Sep 2010 21:30:40 -0700
Subject: [PATCH 20/50] THRIFT-925. cpp: Add _VALUES_TO_NAMES enum map
---
compiler/cpp/src/generate/t_cpp_generator.cc | 85 +++++++++++++++++++-------
lib/cpp/src/Thrift.h | 29 +++++++++
2 files changed, 91 insertions(+), 23 deletions(-)
diff --git a/compiler/cpp/src/generate/t_cpp_generator.cc b/compiler/cpp/src/generate/t_cpp_generator.cc
index 7c7863a..55cb4ca 100644
--- a/compiler/cpp/src/generate/t_cpp_generator.cc
+++ b/compiler/cpp/src/generate/t_cpp_generator.cc
@@ -488,6 +504,32 @@ void t_cpp_generator::generate_enum(t_enum* tenum) {
f_types_ << endl;
+ /**
+ Generate a character array of enum names for debugging purposes.
+ */
+ if (!gen_pure_enums_) {
+ 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);
}
diff --git a/lib/cpp/src/Thrift.h b/lib/cpp/src/Thrift.h
index a8bf2a8..cf766f2 100644
--- a/lib/cpp/src/Thrift.h
+++ b/lib/cpp/src/Thrift.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) {}
--
1.7.0.4
assert
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment