Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / C++

Dynamically assigned and stored action sequence with handlers of the returned value, using boost::bind

Rate me:
Please Sign up or sign in to vote.
4.17/5 (5 votes)
24 Feb 2011CPOL9 min read 33.2K   186   11  
Module to dynamically assign, store, and call with handlers different functional objects: functors, functions, member functions.
// 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Ukraine Ukraine
Now I'm working as a C++ developer and trainer and supporting
all my previously written applications.

Comments and Discussions