We help IT Professionals succeed at work.
Get Started

custom callback handlers

forums_mp
forums_mp asked
on
456 Views
Last Modified: 2012-09-13
Hello experts.  Consider the snippet below which compiles and runs.

# 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>


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 ; 

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 ;
    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() ;

}

Open in new window


The commented out constructors allows me to add support for Handler2 and Handler3, however is there a way to determine the handler that was passed to the constructor of the Worker class?   At present the member functions Metadata, Image and Dlink use the 'callBack' object.  I'll need to make a distinction if the user handler passed in another handler - say Handler2.

Worse case, I'm open to revising the code so by all means I'm open to an alternative (demonstrate with sample source).  My itent is to have 4 handlers clients could choose from but I'd like to know which handler was instatiated. and invoke the appropriate one.
Comment
Watch Question
Senior Software Engineer (Avast)
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 2 Answers and 9 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE