c++中实现回调矩阵的数据结构

Data structure for callback matrix implementation in C++

本文关键字:数据结构 回调 实现 c++      更新时间:2023-10-16

我正在寻找数据结构,这将有利于决策矩阵的实现,一方面是非pod类型的参数,另一方面是回调函数。

我特别想在集合/元组参数和回调函数之间使用某种一对一的对应关系。在这种情况下,存在一组特定的参数值将导致回调的明确定义,类似于:

template<typename t1, typename t2, ...>
(t1 arg1 == _1_1, t2 arg2 == _2_1, t3 arg3 == _3_1) -> void callback_func_1()
(t1 arg1 == _1_2, t2 arg2 == _2_2, t3 arg3 == _3_2) -> void callback_func_2()
(t1 arg1 == _1_3, t2 arg2 == _2_3, t3 arg3 == _3_3) -> void callback_func_3()
...
(t1 arg1 == _1_n, t2 arg2 == _2_n, t3 arg3 == _3_n) -> void callback_func_n^3()

应该有一个搜索方法来选择对应于参数集合的回调函数,这些参数集合的值等于给定的值(用类似c++的伪代码的术语来说):

template<typename t1, typename t2, ...>
void CallbackMatrix::SelectCallback(t1& arg1, t2& arg2, t3& arg3, ...)
{
    BOOST_FOREACH(const auto& item, Matrix)
    {
        if( arg1 == item.arg1 && arg2 == item.arg2 && ... )
        {
             item.function();
             break;
        }
    }
}

从我的角度来看,这个数据结构可能对许多开发人员有用,所以我正在寻找一个库实现(可能是,在Boost的某个地方?)。如果有人能提供这个数据结构的自己的版本,我会很感激。

谢谢。

我觉得你要找的东西很复杂。您确定不能重新设计程序以避免这种情况吗?

无论如何,让我们考虑您的非pod类型为类MyType

struct MyType
{
  int         i;
  double      d;
  std::string s;
  MyType(...) {...} //ctor
  bool operator<( const MyType& other) //define a 'lexicographical' order
  {
    if(      i < other.i 
        || ( i == other.i && d < other.d )
        || ( i == other.i && d == other.d && s.compare( other.s ) < 0 ) )
    {
      return true;
    }
    else
      return false;
  }
};

那么,让我们使用策略模式来代替回调。

class MyFunc
{
public:
  virtual void function( MyType& ) = 0;
  virtual ~MyFunc() = default;
};
class FirstImpl : public MyFunc
{
public:
  void function( MyType& t ) {...} // do something
};
class SecondImpl : public MyFunc
{
public:
  void function( MyType& t ) {...} // do something else
};

最后,使用键为MyType的map(这就是为什么我们需要重载操作符<(在MyType中)和值是(指向)MyFunc的派生对象。

std::map<MyType, MyFunc*> Matrix;
//feed you map
MyType t1( 42, 0., "hey" );
MyType t2( 7, 12.34, "cool" );
MyFunc *f1 = new FirstImpl;
MyFunc *f2 = new SecondImpl;
Matrix.insert( std::make_pair<MyType, MyFunc*>( t1, f1 ) ); // can also use the C++11 map::emplace
Matrix.insert( std::make_pair<MyType, MyFunc*>( t2, f2 ) );
然后,你可以调用select函数
template<typename t1, typename t2, ...>
void CallbackMatrix::SelectCallback(t1& i, t2& d, t3& s, ...)
{
    for_each(const auto& item : Matrix)
    {
        if( i == item.first.i && d == item.first.d && ... )
        {
             item.second->function( item.first );
             break;
        }
    }
}

这个解决方案适合你吗?

编辑-第二个解决方案

注意:下面是伪代码;我没有尝试编译它!但是思想在这里。

我们仍然需要一个MyType类来重载操作符<注意,MyType变成了POD。这是个问题吗?>

struct MyType
{
  std::vector< boost::any > myVec;
  bool operator<( const MyType& other)
  {
    if( myVec.size() != other.myVec.size() )
      return false;
    else
    {
      for( int i = 0; i < myVec.size(); ++i )
      {
        if( myVec[i] < other.myVec[i] ) // so types must be comparable
          return true;
        else if( myVec[i] > other.myVec[i] )
          return false;
      }
      return false; // meaning myVec and other.myVec are identical
    }
  }
};

然后,SelectCallback变成

void CallbackMatrix::SelectCallback( std::vector< boost::any > args )
{
  for_each(const auto& item : Matrix)
    if( args.size() == item.first.myVec.size() )
    {
      auto mismatch_pairs = std::mismatch( args.begin(), 
                                           args.end(), 
                                           item.first.myVec.begin() );
      if( mismatch_pairs.empty() ) // if no mismatch
      {
           item.second->function( item.first );
           break;
      }
    }
}

当然,用数据填充MyType对象将略有不同,如

MyType t1;
t1.myVec.push_back( 42 );
t1.myVec.push_back( 0. );
t1.myVec.push_back( static_cast<char const *>("hey") );