How to set a string item of a list in capnproto C+

2019-07-30 13:34发布

I have capnproto definition like this:

struct School {
  name @0 :Text;
  address @1 :Address;
  foundation @2 :Date;
  emailAddresses @3 :List(Text);
}

I would like to set the emailAddresses field in a builder with code similar to this (but this won't compile):

static School::Builder random_School() {
  capnp::MallocMessageBuilder msg;
  School::Builder result = msg.initRoot<School>();
  result.setName(rand_str(36));
  result.setAddress(random_Address());
  result.setFoundation(random_Date());
  result.initEmailAddresses(item_count);
  for (size_t i = 0; i < item_count; ++i) {
    result.getEmailAddresses()[i] = rand_str(37); // rand_str returns std::string
  }
  return result;
}

What is the correct way to do this?

标签: c++ capnproto
1条回答
Luminary・发光体
2楼-- · 2019-07-30 13:59

According to the capnproto documentation in the Lists section, you should use builder.set(index, value).

result.getEmailAddresses().set(i, rand_str(37));

I guess it should compile now.

查看更多
登录 后发表回答