THRIFT-2494: Add enum toString helper function in c_glib
authorRoger Meier <roger@apache.org>
Sun, 4 May 2014 19:48:48 +0000 (21:48 +0200)
committerRoger Meier <roger@apache.org>
Sun, 4 May 2014 19:48:48 +0000 (21:48 +0200)
Client: c_glib
Patch: Xiaodong Ma

compiler/cpp/src/generate/t_c_glib_generator.cc

index 639f479..9e3ac3c 100644 (file)
@@ -319,6 +319,24 @@ void t_c_glib_generator::generate_enum(t_enum *tenum) {
     "};" << endl <<
     "typedef enum _" << this->nspace << name << " " << this->nspace << name << ";" << endl <<
     endl;
+
+  f_types_ << "/* return the name of the constant */" << endl;
+  f_types_ << "const char *" << endl;
+  f_types_ << "toString_"<<name<<"(int value); "<<endl<<endl;;
+  f_types_impl_ << "/* return the name of the constant */" << endl;
+  f_types_impl_ << "const char *" << endl;
+  f_types_impl_ << "toString_"<<name<<"(int value) "<<endl;
+  f_types_impl_ << "{" << endl;
+  f_types_impl_ << "  static __thread char buf[16] = {0};" << endl;
+  f_types_impl_ << "  switch(value) {" << endl;
+  for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
+    f_types_impl_ << "  case " << this->nspace_uc << name_uc << "_" << (*c_iter)->get_name()
+        << ":" << "return \"" << this->nspace_uc << name_uc << "_" << (*c_iter)->get_name()
+        << "\";"<<endl;
+  }
+  f_types_impl_ << "  default: g_snprintf(buf, 16, \"%d\", value); return buf;" << endl;
+  f_types_impl_ << "  }" << endl;
+  f_types_impl_ << "}" << endl << endl;
 }
 
 /**