Click here to Skip to main content
11,416,194 members (71,087 online)
Click here to Skip to main content
Add your own
alternative version

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

, 24 Feb 2011 CPOL
Module to dynamically assign, store, and call with handlers different functional objects: functors, functions, member functions.
// DynamicActionSequenceWithRetVal.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "RetTypeSpecificator.hpp"
#include "DynamicActionSequenceHolder.hpp"
#include <boost/bind.hpp>
#include "DataClass.h"

/////////////////////////////////////////////////////////    FUNCTORS
template<typename T>
class A
{
public:
	T operator ()(T a, T b)
	{
		cout<<"Inside class A T operator ()(T a, T b) ret = "<<a * b<<endl;
		return a * b;
	}

};

class B
{
public:
	void  operator ()()
	{
		cout<<"Inside class B void  operator ()()"<<endl;
	}

};

double Func(int a, const char * pStr)
{
	cout<<"Inside void Func(int a, char * pStr) "<<a<<" "<<pStr<<endl;
	return a / 100.0;
}

class C
{
public:
	void MemberFunction(int iCount,const char * pStr)
	{
		for(int i = 0; i < iCount; ++i)
			cout<<pStr;
		cout<<endl;
	}
};

class D
{
	public:
	DataClass * operator ()(int a, double d)
	{
		cout<<"Inside class D DataClass * operator ()(int a, double d) "<<endl;
		return new DataClass(a, d);
	}	

};
//////////////////////////////////////////////////////////////////   HANDLERS

//:ref_1
class IntHandler
{
public:
	void operator()(int iVal)
	{
		cout<<"Handler operator()(int iVal) == "<<iVal<<endl;
	}
};

class DoubleHandler
{
public:
	void operator()(double dVal)
	{
		cout<<"DoubleHandler operator()(double dVal) == "<<dVal<<endl;
	}
};

///////////////////////////////////////////////////////////////////////////


int _tmain(int argc, _TCHAR* argv[])
{
	{
		DynamicActionSequenceHolder dash;

		IntHandler ih;
		dash.AddActionToSequence<int>(boost::bind<int>(A<int>(), 2,3), &ih);		
		dash.AddActionToSequence<void>(B());
		DoubleHandler dh;
		int iIndex = dash.AddActionToSequence<double>(boost::bind<double>(Func, 55, "String"), &dh);
		C c;
		dash.AddActionToSequence<void>(boost::bind<void>(&C::MemberFunction, boost::ref(c), 3, " ! "));
		int iDataClassInd = dash.AddActionToSequence<DataClass*>(boost::bind<DataClass*>(D(), 7, 2.654));

		dash.ActionSequence();

		// get double
		Base_Value_Storage * pBase = dash.GetRetValue_OnHolderIndex(iIndex);
		double d;
		if(ExtractValue(pBase, d))
			cout<<"Third operation has returned "<<d<<endl;

		// get DataClass 
		Base_Value_Storage * pBaseD = dash.GetRetValue_OnHolderIndex(iDataClassInd);
		DataClass * pData;
		if(ExtractValue(pBaseD, pData))
		{
			cout<<" =========   Fifth operation has returned ========= "<<endl;
			pData->Show();
		}

		// error modeling 
		int a;
		if(ExtractValue(pBase, a))
			cout<<"Third operation has returned "<<a<<endl;
		else
			cout<<"The type of [out] param does not correspond to stored type!!! "<<endl;
		
		delete pBase;
		delete pBaseD;
		delete pData;

	}

	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)

Share

About the Author

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150427.4 | Last Updated 24 Feb 2011
Article Copyright 2011 by Andriy Padalka
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid