Boost模板函数的多个条件

Boost template function multiple conditions

本文关键字:条件 函数 Boost      更新时间:2023-10-16

我正在尝试跨网络序列化数据,并且在大多数情况下,模板化是值得的。我在下面的场景中遇到了麻烦。

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;
}