00001 /* 00002 * callbacks.h 00003 * 00004 * Created on: Oct 31, 2010 00005 * Author: ethan 00006 */ 00007 00008 #ifndef PANO_CALLBACKS_H_ 00009 #define PANO_CALLBACKS_H_ 00010 00011 #include <map> 00012 #include <opencv2/core/core.hpp> 00013 00014 namespace pano{ 00015 struct Callback{ 00016 virtual ~Callback(){} 00017 template<typename T> 00018 T& cast(){return dynamic_cast<T&>(*this);} 00019 }; 00020 00021 template<typename Data> 00022 struct DCallback : public Callback{ 00023 virtual ~DCallback(){} 00024 virtual void operator()(const Data& data) = 0; 00025 }; 00026 00027 template<typename Data, typename Function> 00028 struct GCallback:public DCallback<Data>{ 00029 Function f_; 00030 GCallback(const Function& f):f_(f){} 00031 virtual ~GCallback(){} 00032 virtual void operator()(const Data& data){ 00033 f_(data); 00034 } 00035 }; 00036 00037 class CallbackEngine{ 00038 public: 00039 template<typename Data, typename Function> 00040 void addCallback(int idx, const Function& f){ 00041 cbs_[idx] = cv::Ptr<Callback>(new GCallback<Data,Function>(f)); 00042 } 00043 template<typename Data> 00044 void callBack(const Data& data, int idx){ 00045 if(cbs_.count(idx)){ 00046 DCallback<Data>& dcb = cbs_[idx]->cast<DCallback<Data> >(); 00047 dcb(data); 00048 } 00049 } 00050 void clearCallback(int idx){ 00051 cbs_.erase(idx); 00052 } 00053 private: 00054 std::map<int,cv::Ptr<Callback> > cbs_; 00055 }; 00056 } 00057 00058 #endif /* PANO_CALLBACKS_H_ */