如何在C++中设置两个相似消息的属性,减少重复的代码?

huangapple go评论71阅读模式
英文:

How to set two similar msg's attributes with less duplicate codes in C++?

问题

在C++中如何使用较少的重复代码来设置变量属性?

A.proto

syntax = "proto3";

package Test1;

message A{
    double x = 1;
    double y = 2;
    double z = 3;
    double theta = 4;
    double kappa = 5;
    float s = 6;
    float v = 7;
    float a = 8;
}

B.proto

syntax = "proto3";

package Test2;

message A{
    double x = 1;
    double y = 2;
    double z = 3;
    double theta = 4;
    double kappa = 5;
    float s = 6;
    float v = 7;
    float a = 8;
}

main

#include <iostream>
#include "A.pb.h"
#include "B.pb.h"

void fun(){
    Test1::A t1;
    Test2::A t2;
    // t1的赋值,无需关心
    t1.set_x(1.0);
    t1.set_y(2.0);
    t1.set_z(3.0);
    t1.set_theta(4.0);
    t1.set_kappa(5.0);
    t1.set_s(6.0);
    t1.set_v(7.0);
    t1.set_a(8.0);

    // t2 = static_cast<Test2::A>(t1); 不能进行类型转换

    // 通过t1的属性设置t2
    // 是否有其他方法通过t1的属性来设置t2?
    t2.set_x(t1.x());
    t2.set_y(t1.y());
    t2.set_z(t1.z());
    t2.set_theta(t1.theta());
    t2.set_kappa(t1.kappa());
    t2.set_s(t1.s());
    t2.set_v(t1.v());
    t2.set_a(t1.a());
    
    // 处理t2的其他操作....
    std::cout << t2.z() << std::endl;
}

int main(){
    fun();
}

以上是您提供的代码的翻译部分。

英文:

How to set variable properties use less duplicate code in C++?

A.proto

syntax = &quot;proto3&quot;;

package Test1;

message A{
    double x = 1;
    double y = 2;
    double z = 3;
    double theta = 4;
    double kappa = 5;
    float s = 6;
    float v = 7;
    float a = 8;
}

B.proto

syntax = &quot;proto3&quot;;

package Test2;

message A{
    double x = 1;
    double y = 2;
    double z = 3;
    double theta = 4;
    double kappa = 5;
    float s = 6;
    float v = 7;
    float a = 8;
}

main

#include &lt;iostream&gt;
#include &quot;A.pb.h&quot;
#include &quot;B.pb.h&quot;

void fun(){
    Test1::A t1;
    Test2::A t2;
    // t1&#39;s assignment, no need to care
    t1.set_x(1.0);
    t1.set_y(2.0);
    t1.set_z(3.0);
    t1.set_theta(4.0);
    t1.set_kappa(5.0);
    t1.set_s(6.0);
    t1.set_v(7.0);
    t1.set_a(8.0);

    // t2 = static_cast&lt;Test2::A&gt;(t1); can&#39;t cast

    // t2&#39;s assignment by t1
    // Is there any other ways set t2 by t1&#39;s attribute?
    t2.set_x(t1.x());
    t2.set_y(t1.y());
    t2.set_z(t1.z());
    t2.set_theta(t1.theta());
    t2.set_kappa(t1.kappa());
    t2.set_s(t1.s());
    t2.set_v(t1.v());
    t2.set_a(t1.a());
    
    // something about t2....
    std::cout &lt;&lt; t2.z() &lt;&lt; std::endl;
}

int main(){
    fun();
}

two msgs defined by proto above, their attributes are almost the same except NAMESPACE, due to some reasons,their namespace CANNOT be changed.So is there any other ways set t2 by t1's attribute?

答案1

得分: 3

最好的方法是重新设计你的 protos,并将这些共同的属性放入一个消息中,在 Test1::ATest2::A 中都使用 import 引用这个消息。

package util;

message Common {
   // 字段...
}

package Test1;
message A {
  util::Common attr = 1;
}

package Test2;
message A {
  util::Common attr = 1;
}

Test1::A t1;
// 设置 t1.attr.xxx

Test2::A t2;
// 复制公共属性
*(t2.mutable_attr()) = t1.attr();

由于 Test1::ATest2::A 完全相同,还有另一种性能较差的解决方案,而且你不需要更改 proto 定义:将 t1 序列化为字符串,然后使用序列化的字符串构造 t2

string s;
t1.SerializeToString(&s);
t2.ParseFromString(s);
英文:

The best way is to redesign your protos, and put those common attributes into a message, and refer this message in both Test1::A and Test2::A with import.

package util;

message Common {
   // fileds...
}

package Test1;
message A {
  util::Common attr = 1;
}

package Test2;
message A {
  util::Common attr = 1;
}

Test1::A t1;
// set t1.attr.xxx

Test2::A t2;
// copy common attr
*(t2.mutable_attr()) = t1.attr();

Since Test1::A and Test2::A is exactly the same, there's another solution which is less performant, and you don't need to change the proto definition: serialize t1 to string, and construct t2 with the serialized string.

string s;
t1.SerializeToString(&amp;s);
t2.ParseFromString(s);

huangapple
  • 本文由 发表于 2023年5月30日 10:35:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76361291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定