Reputation: 943
Considering this delegate class:
template <class DELEGATECLASS, class METHODPTRSPEC>
class CLDelegate
{
public:
CLDelegate();
CLDelegate( DELEGATECLASS* instancePtr, METHODPTRSPEC methodPtr );
CLDelegate( const CLDelegate& rhs );
DELEGATECLASS* pInstance_;
METHODPTRSPEC pMethod_;
};
//-----------------------------------------------------------------------------
//
template <class DELEGATECLASS, class METHODPTRSPEC>
CLDelegate<DELEGATECLASS, METHODPTRSPEC>::CLDelegate()
: pInstance_( nullptr )
, pMethod_( nullptr )
{}
//-----------------------------------------------------------------------------
//
template <class DELEGATECLASS, class METHODPTRSPEC>
CLDelegate<DELEGATECLASS, METHODPTRSPEC>::CLDelegate( DELEGATECLASS*
instancePtr, METHODPTRSPEC methodPtr )
: pInstance_( instancePtr )
, pMethod_( methodPtr )
{}
//-----------------------------------------------------------------------------
//
template <class DELEGATECLASS, class METHODPTRSPEC>
CLDelegate<DELEGATECLASS, METHODPTRSPEC>::CLDelegate( const CLDelegate& rhs )
: pInstance_( rhs.pInstance_ )
, pMethod_( rhs.pMethod_ )
{}
I would like to use this delegate within another class, but I would have to have template parameters for the whole class, which I do not want, like this:
template<class DELEGATECLASS, class METHODPTR>
class SomeClass
{
public:
IOVFUCam();
~IOVFUCam();
// Data Members
private:
...
const CLDelegate<DELEGATECLASS, METHODPTR>& del_;
};
Since I do not want to use template parameters on the whole class I wonder if it is possible to have a dummy delegate in my class and change its type (at compile time ?) :
struct EmptyType
{};
class SomeClass
{
public:
template<class DELEGATECLASS, class METHODPTR>
IOVFUCam( CLDelegate<DELEGATECLASS, METHODPTR> del )
: del_(del)
{};
~IOVFUCam();
// Data Members
private:
...
const CLDelegate<EmptyType, EmptyType>& del_;
};
FrameHandler handler;
CLDelegate<FrameHandler,
void(FrameHandler::*)( IOBuffer::Ptr )>
del( &handler, &FrameHandler::ProcessImage );
SomeClass obj( del );
I rapidly read something that Alexandrescu wrote about TypeTraits, and I wonder if this could be applied to my issue, or another technique ?
Thanks a lot !
Upvotes: 0
Views: 77
Reputation: 757
Have you seen std::allocator? It has a 'rebind' type that allows an std::allocator to give you an std::allocator at compile time.
template <class DELEGATECLASS, class METHODPTRSPEC>
class CLDelegate
{
public:
template<class D, class M>
struct rebind {
typedef CLDelegate<D, M> type;
};
CLDelegate();
CLDelegate( DELEGATECLASS* instancePtr, METHODPTRSPEC methodPtr );
CLDelegate( const CLDelegate& rhs );
DELEGATECLASS* pInstance_;
METHODPTRSPEC pMethod_;
};
This way if you have a CLDelegate type already you can use the following code to get a different CLDelegate type.
typename MyDelType::rebind<NewDel, NewSpec>::type
Is that what you were looking for?
Upvotes: 1