Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C++

How to build an in-memory state engine at runtime

Rate me:
Please Sign up or sign in to vote.
3.67/5 (9 votes)
28 Oct 2007CPOL3 min read 32.6K   174   20  
A C++ template for an efficient in-memory state engine.
//	Copyright � 2007, solosTec
//  All rights reserved.
//---------------------------------------------------------------------
//	License for redistribution is the GNU General Public License v2.
//	See the included readme.txt for details.
//---------------------------------------------------------------------
//	developed by solosTec
//	http://www.solostec.de
//---------------------------------------------------------------------
//
//	$Author: $
//	$Revision: $
/*	$Log: $
*/
//---------------------------------------------------------------------
/**
 *	calculating Base to the power of N.
 *	TODO: make more compatible with the BOOST mpl stuff.
 */
#pragma once
#include <boost/mpl/integral_c.hpp>
#include <boost/mpl/next_prior.hpp>

namespace turban {
namespace mpl {

//	+-----------------------------------------------------------------+
//	| power
//	+-----------------------------------------------------------------+
//
//	Usage: unsigned x = power < unsigned, 2, 8 >::value;
//	x is 256.
template < typename T, T base_, T x_ >
struct power
{ 
	template < T n_ >
	struct calculate
	{
		typedef boost::mpl::integral_c< T, n_ > counter_;
		typedef typename boost::mpl::prior< counter_ >::type prior_;
		typedef boost::mpl::integral_c< T, calculate< prior_::value >::result_::value * base_ > result_;
	};

	template < >
	struct calculate< 0 >
	{
		typedef boost::mpl::integral_c< T, 0 > result_;
	};

	typedef typename calculate< x_ >::result_ result;
	enum { value = result::value };

};
}	//	namespace mpl
}	//	namespace turban

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
osy
Software Developer (Senior)
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions