[ https://issues.apache.org/jira/browse/THRIFT-3921?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15871854#comment-15871854 ]
ASF GitHub Bot commented on THRIFT-3921: ---------------------------------------- Github user jeking3 commented on a diff in the pull request: https://github.com/apache/thrift/pull/1194#discussion_r101755742 --- Diff: compiler/cpp/src/thrift/generate/t_cpp_generator.cc --- @@ -566,6 +567,47 @@ void t_cpp_generator::generate_enum(t_enum* tenum) { << tenum->get_name() << "Values" << ", _k" << tenum->get_name() << "Names), " << "::apache::thrift::TEnumIterator(-1, NULL, NULL));" << endl << endl; + + generate_enum_ostream_operator(tenum); +} + +void t_cpp_generator::generate_enum_ostream_operator(t_enum* tenum) { + + // If we've been told the consuming application will provide an ostream + // operator definition then we only make a declaration: + + if (!has_custom_ostream(tenum)) { + f_types_ << "inline "; + } + + f_types_ << "std::ostream& operator<<(std::ostream& out, const "; + if (gen_pure_enums_) { + f_types_ << tenum->get_name(); + } else { + f_types_ << tenum->get_name() << "::type&"; + } + f_types_ << " val)"; + if (has_custom_ostream(tenum)) { + f_types_ << ";"; + } else { + scope_up(f_types_); + + f_types_ << indent() << "std::map<int, const char*>::const_iterator it = _" + << tenum->get_name() << "_VALUES_TO_NAMES.find(val);" << endl; + f_types_ << indent() << "if (it != _" << tenum->get_name() << "_VALUES_TO_NAMES.end()) {" << endl; + indent_up(); + f_types_ << indent() << "out << it->second;" << endl; + indent_down(); + f_types_ << indent() << "} else {" << endl; + indent_up(); + f_types_ << indent() << "out << val;" << endl; --- End diff -- It isn't an infinite loop as you run our of memory to hold the ostream that gets very large, but it is bad nonetheless. I fixed it and added a test for it. :) > C++ code should print enums as strings > -------------------------------------- > > Key: THRIFT-3921 > URL: https://issues.apache.org/jira/browse/THRIFT-3921 > Project: Thrift > Issue Type: Improvement > Components: C++ - Compiler > Reporter: Vivek Jain > Assignee: James E. King, III > > THRIFT-2067 added {{operator<<}} to the C++ generated code, which is great, > but enums are printed as numbers rather than their string representation. It > would be great if the generated code printed them as a string instead. There > might be some backwards-compatibility concerns (if users have already defined > their own versions, then thrift's version would cause compile errors), not > sure how important you guys think those are. > Other thoughts/concerns? I am willing to work on a patch if no one else is > able to work on it. -- This message was sent by Atlassian JIRA (v6.3.15#6346)