Click here to Skip to main content
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.
#include "ActionHolder.hpp"


#pragma once


template <typename T>
class HandlerStub
{
public:
	void operator ()(const T & rt)
	{
	}
};
template <>
class HandlerStub<void>
{
public:
	/*void operator ()()
	{
	}*/
};



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];
	}
	// two overloaded member functions just for convenient usage - user may pass Handler as param and may not 
	template<typename ret_type, typename Action >
	int AddActionToSequence(Action act) 
	{
		m_ActionsArr.push_back(new ActionHolder<ret_type, Action, HandlerStub<ret_type> >(act, new HandlerStub<ret_type>(), true));
		return (int)m_ActionsArr.size() - 1;
	}

	template<typename ret_type, typename Action, typename Handler >
	int AddActionToSequence(Action act, Handler * pHandler) 
	{
		m_ActionsArr.push_back(new ActionHolder<ret_type, Action, Handler>(act, pHandler, false ));
		return (int)m_ActionsArr.size() - 1;
	}
	void ActionSequence()
	{
		int iSize = (int)m_ActionsArr.size();
		for(int i = 0; i < iSize; ++i)
			m_ActionsArr[i]->DoAction();
	}
	Base_Value_Storage *  GetRetValue_OnHolderIndex(int iIndex)
	{
		Base_Value_Storage * pBase = 0;
		int iSize = (int)m_ActionsArr.size();
		if(iIndex >= 0 && iIndex < iSize)
			pBase = m_ActionsArr[iIndex]->ReturnValue();
		return pBase;
	}
};

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
Web02 | 2.8.150123.1 | Last Updated 24 Feb 2011
Article Copyright 2011 by Andriy Padalka
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid