Click here to Skip to main content
11,647,600 members (63,938 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 15K 158 10
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
	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
	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
	void MemberFunction(int iCount,const char * pStr)
		for(int i = 0; i < iCount; ++i)

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

//////////////////////////////////////////////////////////////////   HANDLERS

class IntHandler
	void operator()(int iVal)
		cout<<"Handler operator()(int iVal) == "<<iVal<<endl;

class DoubleHandler
	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);		
		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));


		// 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;

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



	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.


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


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.

You may also be interested in...

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