如何在协议缓冲区中设置嵌套消息的字段?

时间:2014-08-16 08:31:59

标签: c++ protocol-buffers

我有一个原型文件,如下所示:

message terminal_data
{
    required int32 type = 1;              //1-->trade 2-->order

    message trade_data
    {
        optional string client_id =   1;
        optional string strat_id =    2;
        optional string symbol_name = 3;
        optional int64  trade_id =    4;
        optional string expiry =      5;
        optional int64  quantity =    6;
        optional string time =        7;

    }

    message order_data
    {
        optional string client_id =    1;
        optional string strat_id =     2;
        optional string symbol_name =  3;
        optional int64  order_id =     4;
        optional string side =         5;
        optional int64  quantity =     6;
        optional string time =         7;
    }

}

现在要设置数据,我这样做:

 tData.trade_data.mutable_client_id();

它抱怨道:

:

 error: invalid use of 'data_model::terminal_data::trade_data'
     tData.trade_data.mutable_client_id();
           ^

设置嵌套邮件的正确方法是什么?

以下是生成的代码:

class terminal_data : public ::google::protobuf::Message {
 public:
  terminal_data();
  virtual ~terminal_data();

  terminal_data(const terminal_data& from);

  inline terminal_data& operator=(const terminal_data& from) {
    CopyFrom(from);
    return *this;
  }

  inline const ::google::protobuf::UnknownFieldSet& unknown_fields() const {
    return _unknown_fields_;
  }

  inline ::google::protobuf::UnknownFieldSet* mutable_unknown_fields() {
    return &_unknown_fields_;
  }

  static const ::google::protobuf::Descriptor* descriptor();
  static const terminal_data& default_instance();

  void Swap(terminal_data* other);

  // implements Message ----------------------------------------------

  terminal_data* New() const;
  void CopyFrom(const ::google::protobuf::Message& from);
  void MergeFrom(const ::google::protobuf::Message& from);
  void CopyFrom(const terminal_data& from);
  void MergeFrom(const terminal_data& from);
  void Clear();
  bool IsInitialized() const;

  int ByteSize() const;
  bool MergePartialFromCodedStream(
      ::google::protobuf::io::CodedInputStream* input);
  void SerializeWithCachedSizes(
      ::google::protobuf::io::CodedOutputStream* output) const;
  ::google::protobuf::uint8* SerializeWithCachedSizesToArray(::google::protobuf::uint8* output) const;
  int GetCachedSize() const { return _cached_size_; }
  private:
  void SharedCtor();
  void SharedDtor();
  void SetCachedSize(int size) const;
  public:

  ::google::protobuf::Metadata GetMetadata() const;

  // nested types ----------------------------------------------------

  typedef terminal_data_trade_data trade_data;
  typedef terminal_data_order_data order_data;

  // accessors -------------------------------------------------------

  // required int32 type = 1;
  inline bool has_type() const;
  inline void clear_type();
  static const int kTypeFieldNumber = 1;
  inline ::google::protobuf::int32 type() const;
  inline void set_type(::google::protobuf::int32 value);

  // @@protoc_insertion_point(class_scope:data_model.terminal_data)
 private:
  inline void set_has_type();
  inline void clear_has_type();

  ::google::protobuf::UnknownFieldSet _unknown_fields_;

  ::google::protobuf::int32 type_;

  mutable int _cached_size_;
  ::google::protobuf::uint32 _has_bits_[(1 + 31) / 32];

  friend void  protobuf_AddDesc_data_5fmodel_2eproto();
  friend void protobuf_AssignDesc_data_5fmodel_2eproto();
  friend void protobuf_ShutdownFile_data_5fmodel_2eproto();

  void InitAsDefaultInstance();
  static terminal_data* default_instance_;
};

1 个答案:

答案 0 :(得分:7)

terminal_data的原型定义定义了嵌套消息trade_data - 但它实际上并没有定义类型为该消息的字段。 terminal_data只有一个字段type。添加类似

的内容
optional trade_data trade = 2;
optional order_data order = 3;

就像在C ++中一样,你可以写

class Outer {
  class Inner {};
};

仅仅因为你已经定义了一个嵌套类,并不意味着外部类神奇地获得了一个类型是嵌套类的成员。