- Kód: Vybrat vše
/***************************************************************************
* Copyright (C) 2006 by tulpik *
* gimperek@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef TUBOTHOOK_T_H
#define TUBOTHOOK_T_H
#include <string>
#include <vector>
#include <map>
using namespace std;
namespace TuBot {
/**
@short struktura hooku, udava pravidla kdy hookovat.
@author tulpik
*/
struct Hook_t {
/** @param host = pair host + nick */
pair<string, string> host;
/** @param body = definovany pole tela */
vector<string> body;
/** @param data = definovana msg */
string msg;
inline bool operator < (const Hook_t& h) const {
if( body == h.body )
if( msg == h.msg )
if( host.first == h.host.first )
if( host.second == h.host.second )
return true;
return false;
};
};
/**
@short Zakladni hook handler, podle classy hookuje
@param template = hook object
@author tulpik
*/
template<class T> class TuBotHook_t {
/**
@param HookHandle = ukazatel na clena tridy T
@param Hooks = datovej typ pro mapu uchovavajici handly
@param hooks = mapa uchovavjici hooky (handly)
*/
typedef int (T::*HookHandle)(Hook_t*, TuBotHook_t<T>* );
typedef multimap< Hook_t*, HookHandle > Hooks;
Hooks hooks;
public:
/**
@short konstruktor
@author tulpik
*/
TuBotHook_t( );
/**
@short destruktor
@author tulpik
*/
~TuBotHook_t( );
/**
@short Prida hook na text
@param hook = akce na kterou se ma hookovat (volat fce)
@param fce = ukazatel na clena tridy T
@author tulpik
*/
void addHook( Hook_t hook, HookHandle fce );
/**
@short Smaze hook, pokud existuje
@param hook = hook na ktery je nahookovano
@param return = pokud se to povede tak 1, jinak 0
@author tulpik
*/
int delHook( Hook_t hook );
/**
@short Pokusi se najit hook na text, pokud existuje vola hook
@param hook = text na ktery se ma hook volat
@author tulpik
*/
void call( Hook_t hook );
};
template<class T> TuBotHook_t<T>::TuBotHook_t( ) {
}
template<class T> TuBotHook_t<T>::~TuBotHook_t( ) {
}
template<class T> void TuBotHook_t<T>::addHook( Hook_t hook, HookHandle fce ) {
// hodim do seznamu hooku, text a funkci, ktera se ma na nej volat
hooks.insert( make_pair( &hook, fce ) );
}
template<class T> void TuBotHook_t<T>::call( Hook_t hook ) {
// definuju datovej typ
T Hook;
// definuju si iterator
typename Hooks::iterator HookIt;
// najdu si vsechny hodnoty klice, kterym je text
for( HookIt = hooks.lower_bound( &hook );
HookIt != hooks.upper_bound( &hook ); ++HookIt ) {
// zavolam hook
(Hook.*HookIt->second)( &hook, this );
}
}
template<class T> int TuBotHook_t<T>::delHook( Hook_t hook ) {
// definuju datovej typ
T Hook;
typename Hooks::iterator Test;
// definuju iterator
Test = hooks.find( hook );
// pokusim se najit text hooku
if( Test != hooks.end( ) ) {
// kdyz ho najdu, tak ho smazu
hooks.erase( hook );
// povedlo se smazat hook, vratim 1
return 1;
}
// hook nebyl smazanej, tak vratim 0
return 0;
}
}
#endif
Potreboval bych nejak zjistit zda byl se sablonou predanej ukazatel a podle toho volat bud .* nebo ->*. Dik
Upraveno pro cestinu.
//christian: poupraven název threadu