C ++ 바운드 메소드 큐 (작업 관리자/스케줄러?)
문제
(의사 코드에서)와 같은 작업을 수행 할 수있는 메소드/패턴/라이브러리가 있습니까?
task_queue.push_back(ObjectType object1, method1);
task_queue.push_back(OtherObjectType object2, method2);
내가 할 수 있도록 무엇 처럼:
for(int i=0; i<task_queue.size(); i++) {
task_queue[i].object -> method();
}
전화를 걸도록 :
obj1.method1();
obj2.method2();
아니면 불가능한 꿈입니까?
그리고 호출 할 여러 매개 변수를 추가 할 수있는 방법이 있다면 가장 좋습니다.
Doug T. 이것을 참조하십시오 훌륭한 대답!
Dave van den Eynde의 버전도 잘 작동합니다.
해결책
예, 당신은 결합하고 싶을 것입니다 부스트 :: 바인드 그리고 부스트 :: 기능 매우 강력한 것들.
이 버전은 이제 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;
};
다른 팁
C ++는 이종 컨테이너를 지원하지 않기 때문에 객체는 공유베이스가 있어야합니다 (따라서이 기본 클래스에 포인터를위한 컨테이너가 있으면 도망 갈 수 있습니다).
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));
당신은 그것을 구현하려고합니까? 할리우드 원리 그렇지 않으면 대조 역전 (및 Poorman의 오류 처리)이라고 알려져 있습니까?
나는 뭔가를 채찍질했다.
#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;
}
어쩌면 당신은 다른 방식으로 생각할 수 있습니다 :
for(int i=0; i<task_queue.size(); i++) {
task_queue[i].method(task_queue[i].object);
}
제휴하지 않습니다 StackOverflow