Search code examples
c++thrift

In Thrift, is there a way to avoid generating C++ setters?


When I use this .thrift file

struct Character {
  1: required string firstname;
  2: required string lastname;
  3: required string nickname;
}

and compile it with --gen cpp:no_default_operators,no_skeleton I get:

class Character : public virtual ::apache::thrift::TBase {
 public:

  Character(const Character&);
  Character& operator=(const Character&);
  Character() : firstname(), lastname(), nickname() {
  }

  virtual ~Character() throw();
  std::string firstname;
  std::string lastname;
  std::string nickname;

  void __set_firstname(const std::string& val);

  void __set_lastname(const std::string& val);

  void __set_nickname(const std::string& val);

  uint32_t read(::apache::thrift::protocol::TProtocol* iprot);
  uint32_t write(::apache::thrift::protocol::TProtocol* oprot) const;

  virtual void printTo(std::ostream& out) const;
};

void swap(Character &a, Character &b);

std::ostream& operator<<(std::ostream& out, const Character& obj);

For the sake of compactness, is there a way I could avoid having the setters generated?

Using Thrift version 0.11.0


Solution

  • No, this is currently not possible with the Thrift compiler. If you look at the code, you can see that the declarations of the setters are generated by this code (link):

      // Create a setter function for each field
      for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
        if (pointers) {
          continue;
        }
        if (is_reference((*m_iter))) {
          out << endl << indent() << "void __set_" << (*m_iter)->get_name() << "(::std::shared_ptr<"
              << type_name((*m_iter)->get_type(), false, false) << ">";
          out << " val);" << endl;
        } else {
          out << endl << indent() << "void __set_" << (*m_iter)->get_name() << "("
              << type_name((*m_iter)->get_type(), false, true);
          out << " val);" << endl;
        }
      }
    

    This code is contained in the following function:

    void t_cpp_generator::generate_struct_declaration(ostream& out,
                                                      t_struct* tstruct,
                                                      bool is_exception,
                                                      bool pointers,
                                                      bool read,
                                                      bool write,
                                                      bool swap,
                                                      bool is_user_struct)
    

    Likewise, the definition of the setters is generated by this function (link), with the setters parameter controlling whether the setter function should be generated:

    void t_cpp_generator::generate_struct_definition(ostream& out,
                                                     ostream& force_cpp_out,
                                                     t_struct* tstruct,
                                                     bool setters,
                                                     bool is_user_struct)
    

    Both of these functions are called here (link):

    void t_cpp_generator::generate_cpp_struct(t_struct* tstruct, bool is_exception) {
      generate_struct_declaration(f_types_, tstruct, is_exception, false, true, true, true, true);
      generate_struct_definition(f_types_impl_, f_types_impl_, tstruct, true, true);
    

    As you can see, the pointers parameter for generate_struct_declaration is hard-coded to false and the setters parameter for generate_struct_definition is hard-coded to true. So there currently is no option for by-passing the generation of these functions.