/Users/eddyxu/work/quiplib/ace/srcs/Callbacks.h

Go to the documentation of this file.
00001 //-*- C++ -*-
00002 //--------------------------------------------------------------
00013 // $Id: Callbacks.h 14 2007-06-08 14:40:22Z eddyxu $
00014 
00015 #ifndef QLIB_ACE_CALLBACKS_H
00016 #define QLIB_ACE_CALLBACKS_H
00017 
00018 #include <map>
00019 #include <boost/call_traits.hpp>
00020 
00021 #include "Command.h"
00022 #include "Functors.h"
00023 
00024 namespace qlib {
00025         
00026         namespace ace {
00027                 using boost::call_traits;
00028 
00034                 template<
00035                         typename Result 
00036                         >
00037                 struct Callback : public Command<Result> {
00038                 public:
00039                         typedef Result result_type;
00040                         typedef Result* result_pointer;
00041                 public:
00042                         virtual ~Callback() {};
00043                 public: 
00052                         virtual int exec(void) = 0;
00053 
00061                         virtual int exec(result_pointer result) = 0;
00062 
00064                         virtual int operator() (void) { return this->exec(); }
00065 
00067                         virtual int operator() (result_pointer result) { return this->exec(result);}
00068                 public:
00069 
00070                         virtual int cancel(void) {};
00071                         /*
00072                         virtual int timeout(void) {};
00073                         virtual int failed(void) {};
00074                         */
00075                 };
00076 
00082                 template<
00083                         typename Seq,
00084                         typename CallBack 
00085                         >
00086                 class Callback_Manager {
00087                         typedef typename call_traits<Seq>::param_type param_type;
00088                 public: 
00089                         enum { ADD_REPLACE, ADD_WARNING };
00090                         typedef Seq sequence_type;
00091 
00092                         typedef CallBack callback_type;
00093                         typedef callback_type* callback_pointer;
00094                         typedef callback_type& callback_reference;
00095 
00096                         //typedef Ctn container_type;
00097                         typedef typename std::map<Seq, callback_pointer> container_type;
00098                 public:
00100                         Callback_Manager(void) {};
00102                         ~Callback_Manager(void) { 
00103                                 for_each(callbacks_.begin(), callbacks_.end(), Delete_Second_Ptr()); 
00104                         }
00105                 public:
00120                         int add(param_type seq_id, callback_pointer callback, int option = ADD_REPLACE);
00121 
00132                         int callback(param_type seq_id);
00133                 
00139                         int callback(param_type seq_id, typename callback_type::result_type *result);
00140 
00151                         int cancel(param_type seq_id);
00152 
00160                         callback_pointer find(param_type seq_id){
00161                                 typename container_type::iterator it = callbacks_.find(seq_id); 
00162                                 if(it == callbacks_.end())
00163                                         return NULL;
00164 
00165                                 return it->second;
00166                         }
00167 
00169                         typename container_type::size_type size(void) { return callbacks_.size(); }
00170                 protected:
00171                         int dispose(param_type seq_id, int return_value);
00172                 private:
00173                         container_type callbacks_;
00174                 };
00175         
00176         } // ace
00177 
00178 }// qlib
00179 
00180 #include "Callbacks.inl"
00181 
00182 #endif /* QLIB_ACE_CALLBACKS_H */
00183 

Generated on Fri Jun 8 22:49:19 2007 for qlib::ace by  doxygen 1.5.1