You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@thrift.apache.org by jeking3 <gi...@git.apache.org> on 2017/02/16 21:08:29 UTC

[GitHub] thrift pull request #1194: THRIFT-3921 cpp library- optional operator ostrea...

GitHub user jeking3 opened a pull request:

    https://github.com/apache/thrift/pull/1194

    THRIFT-3921 cpp library- optional operator ostream support for enumerations

    

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/jeking3/thrift THRIFT-3921

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/thrift/pull/1194.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #1194
    
----
commit 570717279501ab1c0184ab76b93b8b35abae2914
Author: Vivek Jain <vi...@vivekja.in>
Date:   2016-09-07T17:34:25Z

    THRIFT-3921 Add C++ ostream operator<< functions for enums

commit 05012da4996455330a58f8664341d3d38beba758
Author: James E. King, III <ji...@simplivity.com>
Date:   2017-02-16T20:23:42Z

    Merge branch 'THRIFT-3921' of https://github.com/viveksjain/thrift into THRIFT-3921

commit 777a98bf7506b26033f14976ff7cc1b63d1e1fde
Author: James E. King, III <ji...@simplivity.com>
Date:   2017-02-16T21:06:29Z

    THRIFT-3921: made initial implementation conform to THRIFT-4060 optional output and added unit tests

----


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] thrift pull request #1194: THRIFT-3921 cpp library- optional operator ostrea...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/thrift/pull/1194


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] thrift pull request #1194: THRIFT-3921 cpp library- optional operator ostrea...

Posted by viveksjain <gi...@git.apache.org>.
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] thrift pull request #1194: THRIFT-3921 cpp library- optional operator ostrea...

Posted by jeking3 <gi...@git.apache.org>.
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. :)


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---