好的,以下内容对我来说在VC10和GCC 4.5.1上编译通过(在ideone.com上)。我认为这需要C++1x的全部是<tuple>
,这应该也可在旧编译器中使用(作为std::tr1::tuple
)。
它仍然需要您为每个成员键入一些代码,但那是非常少量的代码。(请参见我在结尾处的解释。)
#include <iostream>
#include <tuple>
typedef unsigned char uint8_t;
typedef unsigned char byte_t;
struct MsgData {
uint8_t num;
float x;
uint8_t elevation;
static const std::size_t buffer_size = sizeof(uint8_t)
+ sizeof(float)
+ sizeof(uint8_t);
std::tuple<uint8_t&,float&,uint8_t&> get_tied_tuple()
{return std::tie(num, x, elevation);}
std::tuple<const uint8_t&,const float&,const uint8_t&> get_tied_tuple() const
{return std::tie(num, x, elevation);}
};
inline std::ostream& operator<<(std::ostream& os, const MsgData& msgData)
{
os << '[' << static_cast<int>(msgData.num) << ' '
<< msgData.x << ' ' << static_cast<int>(msgData.elevation) << ']';
return os;
}
namespace detail {
template<typename T>
const byte_t* read_value(const byte_t* bin, T& val)
{
val = *reinterpret_cast<const T*>(bin);
return bin + sizeof(T)/sizeof(byte_t);
}
template<typename T>
byte_t* write_value(byte_t* bin, const T& val)
{
*reinterpret_cast<T*>(bin) = val;
return bin + sizeof(T)/sizeof(byte_t);
}
template< typename MsgTuple, unsigned int Size = std::tuple_size<MsgTuple>::value >
struct msg_serializer;
template< typename MsgTuple >
struct msg_serializer<MsgTuple,0> {
static const byte_t* read(const byte_t* bin, MsgTuple&) {return bin;}
static byte_t* write(byte_t* bin, const MsgTuple&) {return bin;}
};
template< typename MsgTuple, unsigned int Size >
struct msg_serializer {
static const byte_t* read(const byte_t* bin, MsgTuple& msg)
{
return read_value( msg_serializer<MsgTuple,Size-1>::read(bin, msg)
, std::get<Size-1>(msg) );
}
static byte_t* write(byte_t* bin, const MsgTuple& msg)
{
return write_value( msg_serializer<MsgTuple,Size-1>::write(bin, msg)
, std::get<Size-1>(msg) );
}
};
template< class MsgTuple >
inline const byte_t* do_read_msg(const byte_t* bin, MsgTuple msg)
{
return msg_serializer<MsgTuple>::read(bin, msg);
}
template< class MsgTuple >
inline byte_t* do_write_msg(byte_t* bin, const MsgTuple& msg)
{
return msg_serializer<MsgTuple>::write(bin, msg);
}
}
template< class Msg >
inline const byte_t* read_msg(const byte_t* bin, Msg& msg)
{
return detail::do_read_msg(bin, msg.get_tied_tuple());
}
template< class Msg >
inline const byte_t* write_msg(byte_t* bin, const Msg& msg)
{
return detail::do_write_msg(bin, msg.get_tied_tuple());
}
int main()
{
byte_t buffer[MsgData::buffer_size];
std::cout << "buffer size is " << MsgData::buffer_size << '\n';
MsgData msgData;
std::cout << "initializing data...";
msgData.num = 42;
msgData.x = 1.7f;
msgData.elevation = 17;
std::cout << "data is now " << msgData << '\n';
write_msg(buffer, msgData);
std::cout << "clearing data...";
msgData = MsgData();
std::cout << "data is now " << msgData << '\n';
std::cout << "reading data...";
read_msg(buffer, msgData);
std::cout << "data is now " << msgData << '\n';
return 0;
}
对我来说,这会打印出
缓冲区大小为6
正在初始化数据...数据现在为[0x2a 1.7 0x11]
正在清除数据...数据现在为[0x0 0 0x0]
正在读取数据...数据现在为[0x2a 1.7 0x11]
(我将您的MsgData
类型缩短为仅包含三个数据成员,但这只是为了测试。)
对于每种消息类型,您需要定义其buffer_size
静态常量和两个get_tied_tuple()
成员函数,一个是const
的,另一个是非const
的,两者实现方式相同。(当然,这些也可以是非成员,但我试图让它们保持接近它们绑定的数据成员列表。)
对于某些类型(如std::string
),您需要添加这些detail::read_value()
和detail::write_value()
函数的特殊重载。
其余的机制对于所有消息类型都保持不变。
有了完整的C++1x支持,您可能能够摆脱必须完全输入get_tied_tuple()
成员函数的显式返回类型,但我实际上还没有尝试过。
#pragma pack(1)
应该即使在其他平台上也可以工作。打包是通过位移和掩码实现的,而不是操作系统对齐修复。 - Billy ONealunpack
函数。 - 9dan