|
// DynamicActionSequence.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <crtdbg.h>
#include <boost/bind.hpp>
using namespace std;
class ActionInterface
{
public:
virtual ~ActionInterface(){}
virtual void DoAcion() = 0;
};
template<typename Action>
class ActionHolder : public ActionInterface
{
Action m_Action;
public:
ActionHolder(Action act) : m_Action(act)
{
}
virtual void DoAcion()
{
m_Action();
}
};
class DynamicActionSequenceHolder
{
vector<ActionInterface *> m_ActionsArr;
public :
//DynamicActionSequenceHolder(){}
~DynamicActionSequenceHolder()
{
int iSize = (int)m_ActionsArr.size();
for(int i = 0; i < iSize; ++i)
delete m_ActionsArr[i];
}
template<typename ret_type, typename Action>
void AddActionToSequence(Action act)
{
m_ActionsArr.push_back(new ActionHolder<Action>(act));
}
void ActionSequence()
{
int iSize = (int)m_ActionsArr.size();
for(int i = 0; i < iSize; ++i)
m_ActionsArr[i]->DoAcion();
}
};
template<typename T>
class A
{
public:
T operator ()(T a, T b)
{
cout<<"Inside class A T operator ()(T a, T b)"<<endl;
return a * b;
}
};
class B
{
public:
void operator ()()
{
cout<<"Inside class B void operator ()()"<<endl;
}
};
void Func(int a, const char * pStr)
{
cout<<"Inside void Func(int a, const char * pStr) "<<a<<" "<<pStr<<endl;
}
class C
{
public:
void MemberFunction(int iCount,const char * pStr)
{
for(int i = 0; i < iCount; ++i)
cout<<pStr;
cout<<endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
{
DynamicActionSequenceHolder dash;
dash.AddActionToSequence<int>(boost::bind<int>(A<int>(), 2,3));
dash.AddActionToSequence<void>(B());
dash.AddActionToSequence<void>(boost::bind<void>(Func, 55, "String"));
C c;
dash.AddActionToSequence<void>(boost::bind<void>(&C::MemberFunction, boost::ref(c), 3, " ! "));
dash.ActionSequence();
}
if(_CrtDumpMemoryLeaks())
cout<<"MemoryLeaks!!!"<<endl;
return 0;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.