在Thrift中,有没有一种方法可以避免生成C ++ setter?

时间:2019-01-26 00:53:32

标签: c++ thrift

当我使用此.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);

为了紧凑起见,有什么办法可以避免生成设置器吗?

使用Thrift版本0.11.0

1 个答案:

答案 0 :(得分:1)

否,Thrift编译器当前无法实现。如果看一下代码,您会发现设置器的声明是由该代码(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)

同样,设置器的定义是由此函数(link)生成的,其中setters参数控制是否应生成设置器函数:

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

如您所见,pointers的{​​{1}}参数被硬编码为generate_struct_declaration,而false的{​​{1}}参数被硬编码为setters。因此,当前没有选择可以绕过这些功能的生成。