tomix wrote:
Hi,
I am trying to create a template which fill window list control with
items.
I would like to ask can i 'pass' to the template the actual function
which need
to be called on the item in order to get its name or description?
currently i assuming that the container is a map and the item have
GetName method:
template < class TControl,class TContainer>
class WindowLoaderFromMap
{
public:
static Load(TControl& wnd,TContainer& container)
{
for(TContainer::iterator
it=container.begin();it!=container.end();it++)
{
int index=wnd.AddString(it->second->GetName().c_str());
wnd.SetItemDataPtr(index,static_cast<void*>(it->second));
}
}
};
I usually use template specialization for this. (I call it the moral
equivalent to the come-from).
template <typename T>
const char * WindowLoaderGetName( T & i_thing )
{
return i_thing->GetName().c_str();
}
class OtherType;
template <>
const char * WindowLoaderGetName( const OtherType * & i_thing )
{
return i_thing->OtherName().c_str();
}
// you could use classes instead of functions
template < class TControl,class TContainer>
class WindowLoaderFromMap
{
public:
static Load(TControl& wnd,TContainer& container)
{
for(
TContainer::iterator it=container.begin();
it!=container.end();
it++
)
{
int index=wnd.AddString(WindowLoaderGetName( it->second ));
wnd.SetItemDataPtr(index,static_cast<void*>(it->second));
}
}
};