在命令数据结构中使用联合
Use of union in a command data structure
我正在研究一个项目,在该项目中,我们将命令从上层进行下层。
1。一次
处理单个命令我们使用以下数据结构。
struct Command{
//Different command types==> SEND / CREATE_TRANSACTION etc.
CommandType type;
//Associated parameters with every command
union{
struct{
string remote_ip;
uint16_t remote_port;
}address;
struct{
string transaction_id;
string transaction_details;
}tsx;
.
.
.
}params;
};
我们传递不同命令的不同参数。联合使其有效。
是否有更好的方法(或设计模式)在C ?
中进行另一个。
2。在单个命令对象中处理多个命令。
我可以这样做:
struct Command{
uint64_t flag; //ORed value of different command types
//parameters
struct{
string remote_ip;
uint16_t remote_port;
}address;
struct{
string transaction_id;
string transaction details;
}tsx;
};
但是,这不是内存有效的。
是否有更好的方法在单个对象(在C )中创建多个命令?
您想要std::variant
或boost::variant
。变体是类型安全的歧视工会。
struct address {
string remote_ip;
uint16_t remote_port;
};
struct tsx {
string transaction_id;
string transaction details;
};
using command = std::variant<address, tsx>;
示例用法:
command c0{tsx{/* ... */}};
std::visit(
overload(
[](const address&){ /* handle address case */ },
[](const tsx&) { /* handle tsx case */ }
),
c0
);
为了学习如何实施overload
和类似的模式匹配实用程序,请参阅我的Accu 2017 Talk:"使用lambdas实施变体访问"
您可以使用std ::变体和访问者模式。根据命令类型,您的访问者会做出不同的反应以处理有效载荷数据。
http://en.cppreference.com/w/cpp/utility/variant/visit
这提供了工会不提供的类型安全
使用工会作为"变体",尤其是出于"保存记忆"的目的,几乎总是表明设计差的迹象。您需要质疑设计/规范是否有意义。所以是的,有更好的方法:
如果这两个结构彼此无关,则将它们保留为2个单独的结构(或类)。与保存10-20字节的内存相比,无需紧密耦合的程序更重要。
否则,如果这两个结构确实有一些共同点,则将"参数"制作为一个抽象基类,其中包含结构的共同点。然后让"地址"answers" TSX"继承该基类。
相关文章:
- 链表,反向函数,数据结构
- 如何使用set实现无序数据结构?
- 我们可以将数据永久保存为数据结构吗?
- C++中的可变长度数组/数据结构
- 用于存储由空格分隔的字符串的 C++/C 数据结构
- 通过 NIF 从C++返回自定义数据结构
- 编译器上的策略数据结构不起作用
- 尝试构建"lock-free"数据结构C++
- 设计将引用元素移动到开头的数据结构.C++
- 在学习数据结构之前对STL有一个了解是好的吗?
- 如何解析表示树状数据结构的字符串
- 我对数据结构、双向链表有一些问题
- googletest:测试太大的数据结构
- C++中deque数据结构的大O是什么?
- 我可以使用哪种数据结构来处理这种方式
- 将文本文件解析为树状数据结构
- C++ 中具有 O(1) 搜索时间复杂度的数据结构
- 表示命令数据包格式的数据结构
- 在命令数据结构中使用联合
- 在解释来自结构整齐的用户命令的数据时使用的最干净的数据结构(在c++中)