Github user viveksjain commented on a diff in the pull request:

    https://github.com/apache/thrift/pull/1194#discussion_r101694140
  
    --- 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 --
    
    I realized later there's a bug in my implementation - this should be `out 
<< static_cast<int>(val);` otherwise the code will infinite loop in this branch.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to