我的 .proto 文件看起来像
message Cmd
{
int code = 1;
}
message CmdOne
{
required Cmd cmd = 1;
required int data = 2;
}
message CmdTwo
{
required Cmd cmd = 1;
required string data = 2;
}
在我的 cpp 文件中,我想声明 CmdOne 和 CmdTwo 的对象并设置 cmd 和 data 成员。但是生成的pb.h文件没有CmdOne和CmdTwo对象的cmd成员的set方法,而是有一个 方法。如何为每个对象设置 data 成员的 setcmd 的值?
我不想在 CmdOne 和 CmdTwo 消息中定义消息 Cmd。我想重用 Cmd 消息,因为我有 10 条消息 CmdOne 到 CmdTen。
最佳答案
您有几个不同的选择。您可以获得指向 cmd 字段的非常量指针,然后适本地分配值:
CmdOne cmd_one;
Cmd* cmd(cmd_one.mutable_cmd());
cmd->set_code(2);
// Previous 2 lines could be simplified to:
// cmd_one.mutable_cmd()->set_code(2);
或者,如果你想将 Cmd 的构造实例传递给 CmdOne,你可以这样做:
Cmd* cmd(new Cmd);
cmd->set_code(1);
CmdOne cmd_one;
cmd_one.set_allocated_cmd(cmd); // Takes ownership of cmd -
// you don't call 'delete cmd'
来自 the docs 的“单一嵌入式消息字段”部分:
Given the message type:
message Bar {}For either of these field definitions:
optional Bar foo = 1; required Bar foo = 1;The compiler will generate the following accessor methods:
...
Bar* mutable_foo()Returns a mutable pointer to the
Barobject that stores the field's value. If the field was not set prior to the call, then the returnedBarwill have none of its fields set (i.e. it will be identical to a newly-allocatedBar). After calling this,has_foo()will returntrueandfoo()will return a reference to the same instance ofBar. The pointer is invalidated by a call toClear()orclear_foo()....
void set_allocated_foo(Bar* bar)Sets the
Barobject to the field and frees the previous field value if it exists. If theBarpointer is notNULL, the message takes ownership of the allocatedBarobject andhas_foo()will returntrue. Otherwise, if theBarisNULL, the behavior is the same as callingclear_foo().
关于c++ - Protocol Buffer cpp 嵌入消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20395126/
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我的工作要求我为某些测试自动生成电子邮件。我一直在四处寻找,但未能找到可以快速实现的合理解决方案。它需要在outlook而不是其他邮件服务器中,因为我们有一些奇怪的身份验证规则,我们需要保存草稿而不是仅仅发送邮件的选项。显然win32ole可以做到这一点,但我找不到任何相当简单的例子。 最佳答案 假设存储了Outlook凭据并且您设置为自动登录到Outlook,WIN32OLE可以很好地完成此操作:require'win32ole'outlook=WIN32OLE.new('Outlook.Application')message=
我正在使用Ruby,我正在与一个网络端点通信,该端点在发送消息本身之前需要格式化“header”。header中的第一个字段必须是消息长度,它被定义为网络字节顺序中的2二进制字节消息长度。比如我的消息长度是1024。如何将1024表示为二进制双字节? 最佳答案 Ruby(以及Perl和Python等)中字节整理的标准工具是pack和unpack。ruby的packisinArray.您的长度应该是两个字节长,并且按网络字节顺序排列,这听起来像是n格式说明符的工作:n|Integer|16-bitunsigned,network(bi
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
如果我在模型中设置验证消息validates:name,:presence=>{:message=>'Thenamecantbeblank.'}我如何让该消息显示在闪光警报中,这是我迄今为止尝试过的方法defcreate@message=Message.new(params[:message])if@message.valid?ContactMailer.send_mail(@message).deliverredirect_to(root_path,:notice=>"Thanksforyourmessage,Iwillbeintouchsoon")elseflash[:error]
我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我
RSpec似乎按顺序匹配方法接收的消息。我不确定如何使以下代码工作:allow(a).toreceive(:f)expect(a).toreceive(:f).with(2)a.f(1)a.f(2)a.f(3)我问的原因是a.f的一些调用是由我的代码的上层控制的,所以我不能对这些方法调用添加期望。 最佳答案 RSpecspy是测试这种情况的一种方式。要监视一个方法,用allowstub,除了方法名称之外没有任何约束,调用该方法,然后expect确切的方法调用。例如:allow(a).toreceive(:f)a.f(2)a.f(1)
我有一个数组数组,想将元素附加到子数组。+=做我想做的,但我想了解为什么push不做。我期望的行为(并与+=一起工作):b=Array.new(3,[])b[0]+=["apple"]b[1]+=["orange"]b[2]+=["frog"]b=>[["苹果"],["橙子"],["Frog"]]通过推送,我将推送的元素附加到每个子数组(为什么?):a=Array.new(3,[])a[0].push("apple")a[1].push("orange")a[2].push("frog")a=>[[“苹果”、“橙子”、“Frog”]、[“苹果”、“橙子”、“Frog”]、[“苹果”、“
我以为它们存储在cookie中-但不,检查cookie没有任何结果。session也不存储它们。那么,我在哪里可以找到它们?我需要这个来直接设置它们(而不是通过flashhash)。 最佳答案 它们存储在inyoursessionstore.自rails2.0以来的默认设置是cookie存储,但请检查config/initializers/session_store.rb以检查您是否使用默认设置以外的东西。 关于ruby-on-rails-闪存消息存储在哪里?,我们在StackOverf