[ 
https://issues.apache.org/jira/browse/THRIFT-539?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Nitay Joffe updated THRIFT-539:
-------------------------------

    Attachment: thrift-539-hash.patch

Attaching patch for adding hash portion of this JIRA.
This patch adds a boost::hash compatible hashing function to each struct in the 
IDL. It is optional to generate the function, because it adds a requirement on 
boost/functional/hash.hpp. The option to enable it is called boost_hash.

Here is an example of running the thrift compiler with this patch to generate 
the hash functions:

{noformat}
ni...@coeus ~/code/thrift/tutorial $ ../compiler/cpp/thrift -r --gen 
cpp:boost_hash tutorial.thrift
{noformat}

Here is the hashing code that is generated for the tutorial:

{noformat}
std::size_t hash_value(const Work &v) {
  std::size_t seed = 7;
  boost::hash_combine(seed, v.num1);
  boost::hash_combine(seed, v.num2);
  boost::hash_combine(seed, v.op);
  boost::hash_combine(seed, v.__isset.comment);
  if (v.__isset.comment) {
    boost::hash_combine(seed, v.comment);
  }
  return seed;
}
{noformat}

Note that the "comment" field is optional, so it is also optionally hashed.

Thoughts?

> struct hash and print methods for C++
> -------------------------------------
>
>                 Key: THRIFT-539
>                 URL: https://issues.apache.org/jira/browse/THRIFT-539
>             Project: Thrift
>          Issue Type: New Feature
>          Components: Compiler (C++), Library (C++)
>            Reporter: Nitay Joffe
>            Priority: Minor
>         Attachments: thrift-539-hash.patch
>
>
> The current thrift compiler for C++ generates methods for structs like 
> operator== and operator< since it uses maps,sets, etc but it does not 
> generate a hash() method or a human readable print() method.
> The idea of this issue is to add functionality to the C++ compiler to 
> generate:
> # A boost hash compatible hash_value() function. This should probably be an 
> option to the compiler as it adds a dependency in generated code on boost.
> # An operator<< function for printing structs in a nice form. This should be 
> safe to always do?

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to