多个回调处理程序

时间:2012-08-28 02:25:25

标签: c++ boost boost-function

考虑一下代码段:

# include <iostream>

# include <boost/scoped_ptr.hpp>
# include <boost/shared_ptr.hpp>

# include <boost/function.hpp>
# include <boost/array.hpp>
# include <boost/asio.hpp>
# include <boost/thread.hpp>
# include <boost/thread/locks.hpp>
# include <boost/bind.hpp>
# include <boost/noncopyable.hpp>

# include <boost/variant.hpp>


class DataLink {};
class Metadata {};
class Image {};

 typedef boost::function<void( DataLink const&, Metadata const& , Image const& )> Handler ; 
 typedef boost::function<void( Metadata const& , Image const& )> Handler2 ; 
 typedef boost::function<void( Image const& )> Handler3 ; 

typedef boost::variant<DataLink, Metadata, Image> Variant; 



enum callbackHandler { link_enum, meta_enum, image_enum };
class Worker {
  Handler  callBack ;
  //Handler2 callBack2 ;
  //Handler3 callBack3 ;

  DataLink dlink;
  Metadata meta ;
  Image    img ;

  callbackHandler handlerEnum ;

public :
  Worker ( const Handler& handler ) 
  : callBack ( handler ) 
   {}
  //Worker ( const Handler2& handler ) 
  //: callBack2 ( handler ) 
  // {}
  //Worker ( const Handler3& handler ) 
  //: callBack3 ( handler ) 
  // {}

  void Image ( ) {
    // update the img object 
    // invoke call back 
    handlerEnum = image_enum ;
    //const std::type_info& xxx = callBack.target_type();
    //std::cout << xxx.raw_name() << std::endl;
    callBack ( dlink, meta, img ) ;
  }

  void Metadata ( ) {
    // update the meta object 
    // invoke call back 
    handlerEnum = meta_enum ;
    callBack ( dlink, meta, img ) ;
  }

  void Dlink ( ) {
    // update the link object 
    // invoke call back 
    handlerEnum = link_enum ;
    callBack ( dlink, meta, img ) ;
  }

  callbackHandler getHandlerType() { return handlerEnum ; }
};

class Foo {
  Worker *ptrWorker ;
public :
  Foo () 
  : ptrWorker( 0 ) 
  {}

  void callback ( DataLink const& d, Metadata const& m , Image const&  i ) {
    callbackHandler tt = ptrWorker->getHandlerType();
    if ( tt == image_enum ) {
      std::cout <<  "Image object " << std::endl;
    }
    if ( tt == meta_enum ) {
      std::cout <<  "Meta object " << std::endl;
    }
    if ( tt == link_enum ) {
      std::cout <<  "Link object " << std::endl;
    }

  }
  bool start () {
    ptrWorker = new ( std::nothrow ) Worker ( boost::bind ( &Foo::callback, this, _1, _2, _3 ) );
    if ( !ptrWorker ) {
      return - 1 ;
    }
  }
  void testWorker() {
    ptrWorker->Image() ;
    ptrWorker->Metadata() ;
    ptrWorker->Dlink() ;
  }
};

int main() {
  Foo f;
  f.start() ;
  f.testWorker() ;
  std::cin.get() ;
}

注释掉的构造函数允许我添加对Handler2和Handler3的支持,但有没有办法确定传递给Worker类的构造函数的处理程序?目前,成员函数Metadata,Image和Dlink使用'callBack'对象。如果用户处理程序在另一个处理程序中传递,我需要区分 - 比如Handler2

我需要使用枚举来实现,有效的是我自己的类型系统(在描述性联盟的行中 - 也就是变体),这也是设计需要一点点的确定标志在这种情况下我会这么想,我愿意重新设计。必须拥有N-1个虚拟处理程序(即,在任何时候只使用一个处理程序而其他人只是在那里做任何事情)在类中定义了混淆和低凝聚力的对象模型,但谁知道。

1 个答案:

答案 0 :(得分:0)

您可以使用从虚拟基类派生的仿函数来实现它:

struct CallbackBase
{
    // Dummy virtual functions that does nothing
    virtual operator()(DataLink const &, Metadata const &, Image const &) {}
    virtual operator()(Metadata const &, Image const &) {}
    virtual operator()(Image const &) {}
};

class Worker
{
    CallbackBase &callback_;

public:
    Worker(const CallbackBase &callback)
        : callback_(callback)
        { }

    void Image()
        {
            // Do something...

            callback_(img);
        }

    void Metadata()
        {
            // Do something...

            callback_(meta, img);
        }

    void Dlink()
        {
            // Do something...

            callback_(dlink, meta, img);
        }
};

struct MyCallback : public CallbackBase
{
    virtual operator()(Image const &)
        {
            // Do something useful here
        }
};

MyCallback my_callback;
Worker my_worker(my_callback);

由于基本回调类中的重载,Worker类可以在正确的位置调用它们中的任何一个,而您只实现派生回调类中实际需要的回调。