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

雨燕双飞 提交于 2020-01-24 21:39:05

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/42056958/how-to-set-a-string-item-of-a-list-in-capnproto-c-generated-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!