This is an automated email from the ASF dual-hosted git repository.

jking pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git


The following commit(s) were added to refs/heads/master by this push:
     new 6a61dfa  THRIFT-4830: Add to_string helper function for cpp generator 
(#1765)
6a61dfa is described below

commit 6a61dfabbf6ae2fa9fbbc3996590ebdbe38e569f
Author: soroshsabz <soorosh_...@hotmail.com>
AuthorDate: Tue Jul 2 04:43:54 2019 +0430

    THRIFT-4830: Add to_string helper function for cpp generator (#1765)
---
 .../cpp/src/thrift/generate/t_cpp_generator.cc     | 44 ++++++++++++++++++++++
 lib/cpp/test/EnumTest.cpp                          | 22 ++++++++++-
 2 files changed, 64 insertions(+), 2 deletions(-)

diff --git a/compiler/cpp/src/thrift/generate/t_cpp_generator.cc 
b/compiler/cpp/src/thrift/generate/t_cpp_generator.cc
index dca3167..d66b6e6 100644
--- a/compiler/cpp/src/thrift/generate/t_cpp_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_cpp_generator.cc
@@ -114,6 +114,8 @@ public:
   void generate_enum(t_enum* tenum) override;
   void generate_enum_ostream_operator_decl(std::ostream& out, t_enum* tenum);
   void generate_enum_ostream_operator(std::ostream& out, t_enum* tenum);
+  void generate_enum_to_string_helper_function_decl(std::ostream& out, t_enum* 
tenum);
+  void generate_enum_to_string_helper_function(std::ostream& out, t_enum* 
tenum);
   void generate_forward_declaration(t_struct* tstruct) override;
   void generate_struct(t_struct* tstruct) override { 
generate_cpp_struct(tstruct, false); }
   void generate_xception(t_struct* txception) override { 
generate_cpp_struct(txception, true); }
@@ -584,6 +586,9 @@ void t_cpp_generator::generate_enum(t_enum* tenum) {
 
   generate_enum_ostream_operator_decl(f_types_, tenum);
   generate_enum_ostream_operator(f_types_impl_, tenum);
+
+  generate_enum_to_string_helper_function_decl(f_types_, tenum);
+  generate_enum_to_string_helper_function(f_types_impl_, tenum);
 }
 
 void t_cpp_generator::generate_enum_ostream_operator_decl(std::ostream& out, 
t_enum* tenum) {
@@ -631,6 +636,45 @@ void 
t_cpp_generator::generate_enum_ostream_operator(std::ostream& out, t_enum*
   }
 }
 
+void 
t_cpp_generator::generate_enum_to_string_helper_function_decl(std::ostream& 
out, t_enum* tenum) {
+  out << "std::string to_string(const ";
+  if (gen_pure_enums_) {
+    out << tenum->get_name();
+  } else {
+    out << tenum->get_name() << "::type&";
+  }
+  out << " val);" << endl;
+  out << endl;
+}
+
+void t_cpp_generator::generate_enum_to_string_helper_function(std::ostream& 
out, t_enum* tenum) {
+  if (!has_custom_ostream(tenum)) {
+    out << "std::string to_string(const ";
+    if (gen_pure_enums_) {
+      out << tenum->get_name();
+    } else {
+      out << tenum->get_name() << "::type&";
+    }
+    out << " val) " ;
+       scope_up(out);
+
+    out << indent() << "std::map<int, const char*>::const_iterator it = _"
+             << tenum->get_name() << "_VALUES_TO_NAMES.find(val);" << endl;
+    out << indent() << "if (it != _" << tenum->get_name() << 
"_VALUES_TO_NAMES.end()) {" << endl;
+    indent_up();
+    out << indent() << "return std::string(it->second);" << endl;
+    indent_down();
+    out << indent() << "} else {" << endl;
+    indent_up();
+    out << indent() << "return std::to_string(static_cast<int>(val));" << endl;
+    indent_down();
+    out << indent() << "}" << endl;
+
+    scope_down(out);
+    out << endl;
+  }
+}
+
 /**
  * Generates a class that holds all the constants.
  */
diff --git a/lib/cpp/test/EnumTest.cpp b/lib/cpp/test/EnumTest.cpp
index 6487906..388abb7 100644
--- a/lib/cpp/test/EnumTest.cpp
+++ b/lib/cpp/test/EnumTest.cpp
@@ -26,6 +26,13 @@ std::ostream& operator <<(std::ostream& os, const 
MyEnumWithCustomOstream::type&
   return os;
 }
 
+std::string to_string(const MyEnumWithCustomOstream::type& val)
+{
+  std::ostringstream os;
+  os << val;
+  return os.str();
+}
+
 BOOST_AUTO_TEST_SUITE(EnumTest)
 
 BOOST_AUTO_TEST_CASE(test_enum_value) {
@@ -66,7 +73,6 @@ std::string EnumToString(_T e)
   return ss.str();
 }
 
-
 BOOST_AUTO_TEST_CASE(test_enum_ostream)
 {
   BOOST_CHECK_EQUAL(EnumToString(MyEnum1::ME1_0), "ME1_0");
@@ -75,10 +81,22 @@ BOOST_AUTO_TEST_CASE(test_enum_ostream)
   BOOST_CHECK_EQUAL(EnumToString(MyEnumWithCustomOstream::CustoM2), 
"{2:CUSTOM!}");
 
   // some invalid or unknown value
-  auto uut = (MyEnum5::type)44;
+  auto uut = static_cast<MyEnum5::type>(44);
   BOOST_CHECK_EQUAL(EnumToString(uut), "44");
 }
 
+BOOST_AUTO_TEST_CASE(test_enum_to_string)
+{
+  BOOST_CHECK_EQUAL(::to_string(MyEnum1::ME1_0), "ME1_0");
+  BOOST_CHECK_EQUAL(::to_string(MyEnum5::e2), "e2");
+  BOOST_CHECK_EQUAL(::to_string(MyEnum3::ME3_N1), "ME3_N1");
+  BOOST_CHECK_EQUAL(::to_string(MyEnumWithCustomOstream::CustoM2), 
"{2:CUSTOM!}");
+
+  // some invalid or unknown value
+  auto uut = static_cast<MyEnum5::type>(44);
+  BOOST_CHECK_EQUAL(::to_string(uut), "44");
+}
+
 BOOST_AUTO_TEST_CASE(test_enum_constant)
 {
   MyStruct ms;

Reply via email to