在 Thrift 中,有没有办法避免生成 C++ setter?

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

当我使用这个 .thrift 文件时

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

并用--gen cpp:no_default_operators,no_skeleton编译它我得到:

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);

为了简洁起见,有没有办法避免生成 setter?

使用 Thrift 版本 0.11.0

不,Thrift 编译器目前无法做到这一点。如果您查看代码,您会发现 setter 的声明是由以下代码生成的 (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;
    }
  }

此代码包含在以下函数中:

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)

同样,setters的定义是由这个函数(link)生成的,setters参数控制是否应该生成setter函数:

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

这里调用了这两个函数(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);

如您所见,generate_struct_declarationpointers 参数硬编码为 falsegenerate_struct_definitionsetters 参数硬编码为编码为 true。所以目前没有绕过这些函数生成的选项。