www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Variable template parameters count (static for)

Hi all.

Maybe it is inappropriate to the current occasion to make proposals: I 
think my post will be lost in mutable / immutable / mutable / immutable 
/ notmutable posts but anyway... :)

Time after time as well in C++ as in D I have to do one very very 
tedious thing. This is hard to explain with words, so I'll try to 
explain with example:

inline MessageLayout _gl0()
{
	MessageLayout ret;
	return ret;
}

template <typename T0>
inline MessageLayout _gl1()
{
	MessageLayout ret;
	ret.Push<T0>();
	return ret;
}

template <typename T0, typename T1>
inline MessageLayout _gl2()
{
	MessageLayout ret;
	ret.Push<T0>();
	ret.Push<T1>();
	return ret;
}

....

template <typename T0, typename T1, typename T2, typename T3, typename 
T4, typename T5, typename T6, typename T7>
inline MessageLayout _gl8()
{
	MessageLayout ret;
	ret.Push<T0>();
	ret.Push<T1>();
	ret.Push<T2>();
	ret.Push<T3>();
	ret.Push<T4>();
	ret.Push<T5>();
	ret.Push<T6>();
	ret.Push<T7>();
	return ret;
}


////
	template <typename T0>
	void Handle(DWORD id, Closure<void(const ReceiverAgent&, T0)> handler)
	{
		if (id != m_title)
			return;
		
		if (m_content->Layout() != _gl1<T0>())
		{
			COMMON_ASSERT(0, "Incompatible message layout and handler signature");
			return;
		}
		
		TMessage1<T0>* content = reinterpret_cast<TMessage1<T0>*>(m_content);
		handler(m_agent, content->p0);
	}

...

template <typename T0, typename T1, typename T2, typename T3, typename 
T4, typename T5, typename T6, typename T7>
	void Handle(DWORD id, Closure<void(const ReceiverAgent&, T0, T1, T2, 
T3, T4, T5, T6, T7)> handler)
	{
		if (id != m_title)
			return;
		
		if (m_content->Layout() != _gl8<T0, T1, T2, T3, T4, T5, T6, T7>())
		{
			COMMON_ASSERT(0, "Incompatible message layout and handler signature");
			return;
		}
		
		TMessage8<T0, T1, T2, T3, T4, T5, T6, T7>* content = 
reinterpret_cast<TMessage8<T0, T1, T2, T3, T4, T5, T6, T7>*>(m_content);
		handler(m_agent, content->p0, content->p1, content->p2, content->p3, 
content->p4, content->p5, content->p6, content->p7);
	}

I think you found pattern. Thi is C++ just because I could copy/paste it 
and not to write example in D, but the essense is the same.

I have to generate nearly similiar classes with only difference in count 
of template parameters. So suggestion is to make, hm... `static for 
(;;)` and make ability to declare template a-la:

template (SomeT, AnotherT, T[] ...)

What can you say about this?


-- 
Victor (aka nail) Nakoryakov
nail-mail<at>mail<dot>ru

Krasnoznamensk, Moscow, Russia
Jul 02 2005