Boost模板函数的多个条件
Boost template function multiple conditions
我正在尝试跨网络序列化数据,并且在大多数情况下,模板化是值得的。我在下面的场景中遇到了麻烦。
template < typename type >
class SerializedVector
{
public:
bool SerializeIn( const U8* data, int& bufferOffset );
bool SerializeOut( U8* data, int& bufferOffset ) const;
vector< type > m_data;
};
在基本类型的情况下,序列化只是调用memcpy(实际上是htonl)的情况,但对于std::string,我序列化字节数,然后memcpy后面的缓冲区。所以我有一个基本类型的模板函数和一个std:string的特化函数。简单。
现在,我想支持类序列化自己在我的m_data成员…像这样:
struct TextEntry
{
bool SerializeIn( const U8* data, int& bufferOffset );
bool SerializeOut( U8* data, int& bufferOffset ) const;
string username;
string message;
};
class PacketTextHistoryResult : public BasePacket
{
public:
PacketTextHistoryResult (){}
bool SerializeIn( const U8* data, int& bufferOffset );
bool SerializeOut( U8* data, int& bufferOffset ) const;
SerializedVector< TextEntry > chat;
};
我已经尝试了很多事情,但这是我卡住的地方…有更好的主意吗?
template <typename type>
struct calls_member_serialize : boost::false_type { };
template <>
struct calls_member_serialize< std::string > : boost::false_type { };
template <typename type>
struct calls_member_serialize< boost::is_class< type > > : boost::true_type { };
template < typename type >
bool SerializedVector< type >::SerializeIn( const U8* data, int& bufferOffset )
{
int num = m_data.size();
Serialize::In( data, bufferOffset, num );
struct localScope
{
static void do_work( const U8* data, int& bufferOffset, type temp, boost::true_type const & )
{
temp.SerializeIn( data, bufferOffset ); <<<<<<<< See how I invoke the self-serialization here.
}
static void do_work( const U8* data, int& bufferOffset, type temp, boost::false_type const & )
{
Serialize::In( data, bufferOffset, temp ); // call the standard template function
}
};
for( int i=0; i<num; i++ )
{
type temp;
localScope::do_work( data, bufferOffset, temp, ( calls_member_serialize< type >() ) ); //boost::is_fundamental<type>() || boost::is_class< std::string, type >()
m_data.push_back( temp );
}
return true;
}
我不认为你的第三个calls_member_serialize
做你想做的事情。试试这个:
template <typename type>
struct calls_member_serialize : boost::is_class< type > { };
template <>
struct calls_member_serialize< std::string > : boost::false_type { };
这样,calls_member_serialize<int>
由boost::false_type
而来,calls_member_serialize<TextEntry>
由boost::true_type
而来。
第二个问题是struct localScope
不是模板类,因此编译器将尝试为每种类型实例化do_work函数的两个版本,导致像std::string
这样的类型的编译器错误。您还需要将localScope helper类作为模板。然而,模板类不能在函数范围内,所以它看起来像这样(未经测试):
namespace { // put in unnamed namespace to keep it local
template<typename localType>
struct localScope
{
static void do_work( const U8* data, int& bufferOffset, localType temp, boost::true_type const & )
{
temp.SerializeIn( data, bufferOffset );
}
static void do_work( const U8* data, int& bufferOffset, localType temp, boost::false_type const & )
{
Serialize::In( data, bufferOffset, temp ); // call the standard template function
}
};
}
template < typename type >
bool SerializedVector< type >::SerializeIn( const U8* data, int& bufferOffset )
{
int num = m_data.size();
Serialize::In( data, bufferOffset, num );
for( int i=0; i<num; i++ )
{
type temp;
localScope<type>::do_work( data, bufferOffset, temp, ( calls_member_serialize< type >() ) ); //boost::is_fundamental<type>() || boost::is_class< std::string, type >()
m_data.push_back( temp );
}
return true;
}
相关文章:
- 条件constexpr函数
- 如何编写一个使用n倍三元条件语句的C++布尔函数
- compare_exchange C++函数如何确定竞争条件?
- 具有专用化的模板类中的可靠条件复制和移动构造函数
- 函数指针与条件分支
- 在运行时有条件地删除类成员或跳过调用该成员对象的构造函数
- 如何将条件传递给 C++ 中的函数?
- 有条件地将默认参数传递给函数(使用"?"运算符)
- 如何使一个函数具有三种不同的输出条件?
- 为什么我不能将 size() 函数编写为 while 循环中的条件?
- C++成员函数的条件模板类定义
- 调用一个小函数两次(例如在if条件和主体中)比将结果存储在局部变量中更可取
- 智能指针析构函数争用条件
- 为什么当 while 循环中的 if 条件变为 false 时,我的函数不输出最后一条语句?
- Clang++ 6.0 内存清理器未报告返回值指示条件分支的函数中的未初始化局部变量
- 初始化数据成员取决于构造函数中的条件
- 使用模板调用条件函数
- 执行基于 Windows 操作系统的条件函数
- C/C++日志记录功能,支持根据日志级别进行的条件函数调用
- C++具有类型特征的条件函数调用