C ++ cola método vinculado (administrador de tareas / programador?)
Pregunta
¿Hay un método / patrón / biblioteca para hacer algo por el estilo (en pseudo-código):
task_queue.push_back(ObjectType object1, method1);
task_queue.push_back(OtherObjectType object2, method2);
para que pudiera hacer el algo como:
for(int i=0; i<task_queue.size(); i++) {
task_queue[i].object -> method();
}
para que se llamaría:
obj1.method1();
obj2.method2();
O es un sueño imposible?
Y si hay una manera de añadir una serie de parámetros para llamar -. Eso sería la mejor
Doug T. consulte este Excelente respuesta!
Dave Van den Eynde 's versión funciona bien también.
Solución
Si usted quiere combinar boost :: bind y impulsar :: funciones su propia poderosa cosas.
Esta versión ahora compila, gracias a Slava!
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <vector>
class CClass1
{
public:
void AMethod(int i, float f) { std::cout << "CClass1::AMethod(" << i <<");\n"; }
};
class CClass2
{
public:
void AnotherMethod(int i) { std::cout << "CClass2::AnotherMethod(" << i <<");\n"; }
};
int main() {
boost::function< void (int) > method1, method2;
CClass1 class1instance;
CClass2 class2instance;
method1 = boost::bind(&CClass1::AMethod, class1instance, _1, 6.0) ;
method2 = boost::bind(&CClass2::AnotherMethod, class2instance, _1) ;
// does class1instance.AMethod(5, 6.0)
method1(5);
// does class2instance.AMethod(5)
method2(5);
// stored in a vector of functions...
std::vector< boost::function<void(int)> > functionVec;
functionVec.push_back(method1);
functionVec.push_back(method2);
for ( int i = 0; i < functionVec.size(); ++i)
{
functionVec[i]( 5);
};
return 0;
};
Otros consejos
Como C ++ no soporta contenedores heterogéneos, sus objetos tendrán que tener una base común (por lo que puede llegar a tener un contenedor para los punteros a esta clase base).
class shared_base {
public:
virtual void method() = 0; // force subclasses to do something about it
};
typedef std::list<shared_base*> obj_list;
class object : public shared_base {
public:
virtual void method() { methodx(); }
private:
int methodx();
};
// ...
list.insert(new object);
// ...
std::for_each(list.begin(), list.end(), std::mem_fun(&shared_base::method));
¿Estás tratando de poner en práctica el principio Hollywood conocido de otra manera como Inversion-de-control ( y también, de poorman manejo de errores)?
Busque tanto el href="http://en.wikipedia.org/wiki/Observer_pattern" rel="nofollow noreferrer"> rel patrones de los visitantes nofollow noreferrer wikipedia.org/wiki/Visitor_pattern -. que podrían ser de interés
Azoté algo.
#include <vector>
#include <algorithm>
#include <iostream>
template <typename ARG>
class TaskSystem
{
private:
class DelegateBase
{
public:
virtual ~DelegateBase() { }
virtual void Invoke(ARG arg) = 0;
};
template <typename T>
class Delegate : public DelegateBase
{
public:
typedef void (T::*Func)(ARG arg);
private:
Func m_func;
T* m_object;
public:
Delegate(T* object, Func func)
: m_object(object), m_func(func)
{ }
virtual void Invoke(ARG arg)
{
((*m_object).*(m_func))(arg);
}
};
typedef std::vector<DelegateBase*> Delegates;
Delegates m_delegates;
public:
~TaskSystem()
{
Clear();
}
void Clear()
{
Delegates::iterator item = m_delegates.begin();
for (; item != m_delegates.end(); ++item)
{
delete *item;
}
m_delegates.clear();
}
template <typename T>
void AddDelegate(T& object, typename Delegate<T>::Func func)
{
DelegateBase* delegate = new Delegate<T>(&object, func);
m_delegates.push_back(delegate);
}
void Invoke(ARG arg)
{
Delegates::iterator item = m_delegates.begin();
for (; item != m_delegates.end(); ++item)
{
(*item)->Invoke(arg);
}
}
};
class TaskObject1
{
public:
void CallOne(const wchar_t* value)
{
std::wcout << L"CallOne(): " << value << std::endl;
}
void CallTwo(const wchar_t* value)
{
std::wcout << L"CallTwo(): " << value << std::endl;
}
};
class TaskObject2
{
public:
void CallThree(const wchar_t* value)
{
std::wcout << L"CallThree(): " << value << std::endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
TaskSystem<const wchar_t*> tasks;
TaskObject1 obj1;
TaskObject2 obj2;
tasks.AddDelegate(obj1, &TaskObject1::CallOne);
tasks.AddDelegate(obj1, &TaskObject1::CallTwo);
tasks.AddDelegate(obj2, &TaskObject2::CallThree);
tasks.Invoke(L"Hello, World!\n");
return 0;
}
Tal vez usted podría pensar de otra manera:
for(int i=0; i<task_queue.size(); i++) {
task_queue[i].method(task_queue[i].object);
}