在单个结构中处理不同的数据类型 [英] Handling different datatypes in a single structure

查看:196
本文介绍了在单个结构中处理不同的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在VxWorks消息队列上发送一些信息。要发送的信息在运行时决定,并且可以是不同的数据类型。我使用的结构为这 -

  struct structData 
{
char m_chType; //变量表示数据类型 - long,float或string
long m_lData; //变量保存长值
float m_fData; // variable to hold float value
string m_strData; // variable to hold string value
};

我目前正在通过消息队列发送structData数组。

  structData arrStruct [MAX_SIZE]; 

这里的问题是结构中只有一个变量是有用的,无用。因此,消息队列是不必要的重载。
我不能使用联合,因为数据类型和值是必需的。
我尝试使用模板,但它不解决问题。我只能一次发送一个数据类型的结构数组。

  template< typename T> 
struct structData
{
char m_chType;
T m_Data;
}

structData< int> arrStruct [MAX_SIZE];

有标准的方式来存储这些信息吗?

解决方案

有很多方法可以处理不同的数据类型。除了联合解决方案,你可以使用一个通用结构,如:

  typedef struct 
{
char m_type;
void * m_data;
}
structData;

这种方式你知道类型,你可以将void *指针转换成正确的类型。
这就像联合解决方案一样比C ++方式做的事情。
C ++方法将是使用继承的东西。你定义一个基类Data类和一个使用继承来专门化数据。如果需要,您可以使用RTTI检查类型。



但是正如您所说,您需要通过VxWork队列发送您的数据。我没有专家,但如果这些队列是操作系统实时队列,所有以前的解决方案不是好的。你的问题是,你的数据有可变长度(特别是字符串),你需要发送他们通过一个队列,可能要求类似固定长度的数据结构和这个数据结构的实际长度。



在我的经验中,正确的处理方式是将数据序列化为类似于缓冲区类/结构体。这种方式你可以优化大小(你只需要序列化你需要的),你可以通过你的队列发送缓冲区。



要序列化你可以使用像1字节然后是数据。要处理可变长度的数据,可以使用1到n个字节对数据长度进行编码,以便对数据进行反序列化。



对于字符串:
1 byte代码类型(0x01 = string,...)
2字节来编码字符串长度(如果你需要少于65536字节)
n数据字节



因此,字符串Hello将被序列化为:

  0x00 0x00 0x07 0x65 0x48 0x6c 0x6c 

您需要一个缓冲区类和一个序列化/解串器类。然后你可以这样做:

 序列化数据
将序列化数据发送到队列
 接收数据
反序列化数据

我希望它有帮助,我没有误解你的问题。如果VxWorks队列不是我想要的...,序列化部分是过度的。


I need to send some information on a VxWorks message queue. The information to be sent is decided at runtime and may be of different data types. I am using a structure for this -

struct structData
{
  char m_chType;    // variable to indicate the data type - long, float or string
  long m_lData; 	// variable to hold long value
  float m_fData;    // variable to hold float value
  string m_strData; // variable to hold string value
};

I am currently sending an array of structData over the message queue.

structData arrStruct[MAX_SIZE];

The problem here is that only one variable in the structure is useful at a time, the other two are useless. The message queue is therefore unneccessarily overloaded. I can't use unions because the datatype and the value are required. I tried using templates, but it doesn't solve the problem.I can only send an array of structures of one datatype at a time.

template <typename T>
struct structData
{
  char m_chType;
  T m_Data;
}

structData<int> arrStruct[MAX_SIZE];

Is there a standard way to hold such information?

解决方案

There are many ways to handle different datatypes. Besides the union solution you can use a generic struct like :

typedef struct
{
    char m_type;
    void* m_data;
} 
structData;

This way you know the type and you can cast the void* pointer into the right type. This is like the union solution a more C than C++ way of doing things. The C++ way would be something using inheritance. You define a base "Data" class an use inheritance to specialize the data. You can use RTTI to check for type if needed.

But as you stated, you need to send your data over a VxWork queue. I'm no specialist but if those queues are OS realtime queue, all the previous solutions are not good ones. Your problem is that your data have variable length (in particular string) and you need to send them through a queue that probably ask for something like a fixed length datastruct and the actual length of this datastruct.

In my experience, the right way to handle this is to serialize the data into something like a buffer class/struct. This way you can optimize the size (you only serialize what you need) and you can send your buffer through your queue.

To serialize you can use something like 1 byte for type then data. To handle variable length data, you can use 1 to n bytes to encode data length, so you can deserialize the data.

For a string : 1 byte to code the type (0x01 = string, ...) 2 bytes to code the string length (if you need less than 65536 bytes) n data bytes

So the string "Hello" will be serialized as :

0x00 0x00 0x07 0x65 0x48 0x6c 0x6c

You need a buffer class and a serializer/deserializer class. Then you do something like :

serialize data
send serialized data into queue

and on the other side

receive data
deserialize data

I hope it helps and that I have not misunderstood your problem. The serialization part is overkill if the VxWorks queues are not what I think ...

这篇关于在单个结构中处理不同的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆