I’m creating a DNS name resolver. While parsing the returned packet I read the RR type then pass it through switch to initialize a derived type of class Record. Record types is a large enum type. I map the enum to a struct using template specialization.
template < QueryType n >
struct Struct;
template <> struct Struct< DNS_Q_A > { typedef A Type; };
template <> struct Struct< DNS_Q_CNAME > { typedef CNAME Type; };
template < QueryType n > struct Struct { typedef UNKNOWN Type; };
Currently I have 4 switch statements which all do a very similar thing. Namely call operator new, placement new, for both copy and move c’tors. This is a lot of code to maintain. I would like to have just 1 switch statement where I could pass in some type of object containing the function to perform, return type and n number of parameters.
The switch statements are like this:
switch ( nType )
{
case DNS_Q_A:
pInstance = new ( &Container ) Struct< DNS_Q_A >::Type( dynamic_cast< const Struct< DNS_Q_A >::Type& >( Other ) );
break;
case DNS_Q_CNAME:
pInstance = new ( &Container ) Struct< DNS_Q_CNAME >::Type( dynamic_cast< const Struct< DNS_Q_CNAME >::Type& >( Other ) );
break;
}
As you can see, each case is identical other than the reliance on the struct type. This screams out ‘template’ to me but I can’t figure out how to pass in an object.
Am I limited to coding 4 switch’s or is there a way? Please don’t quote “Boost”, this code has to be independent of any other libraries.
Solution: (Thanks to Jan Hudec)
template< template < class > class Action >
typename Action< Struct< DNS_Q_A >::Type >::result_type
CreateRecord (
unsigned n,
typename Action< Struct< DNS_Q_A >::Type >::first_argument_type arg1,
typename Action< Struct< DNS_Q_A >::Type >::second_argument_type arg2 )
{
typedef typename typename Action< Struct< DNS_Q_A >::Type >::result_type ReturnType;
switch ( n )
{
case DNS_Q_A: return static_cast< ReturnType >( Action< Struct< DNS_Q_A >::Type >()( arg1, arg2 ) );
case DNS_Q_NS: return static_cast< ReturnType >( Action< Struct< DNS_Q_NS >::Type >()( arg1, arg2 ) );
/*...*/
}
}
with Action struct defined as:
template < typename T >
struct Copy : std::binary_function< storage_type&, const Record&, Record* >
{
Record* operator() ( storage_type& Storage, const Record& Obj )
{
return new ( &Storage ) T( dynamic_cast< const T& >( Obj ) );
}
};
template < typename T >
struct Move : std::binary_function< storage_type&, Record&&, Record* >
{
Record* operator() ( storage_type& Storage, const Record& Obj )
{
return new ( &Storage ) T( dynamic_cast< const T& >( std::move( Obj ) ) );
}
};
And Switch statements replaced with:
pInstance = CreateRecord< Copy >( nType, Container, Other );
You can create a function template that will take functor template to execute. Without C++11 variadic arguments all the functors need the same number of arguments, so you will have to either pack them in a structure or pass NULLs when they are irrelevant. With variadic templates (I haven’t use them yet, so I don’t remember exact syntax and won’t write them here) you can easily have different arguments in each form.
The idea is like (off the top of my head, so there might be some typos):
Now you write the individual functions like:
and combine together:
(and wrap in another function, that will insert the getter and/or pack arguments for the multi-argument forms like placement copy construction)
Though for copying the common way to do this is to have virtual Clone member method. But that won’t work for construction.
Edit: Note, that I defined the return and argument types within the inner template as typedefs (using the standard
unary_functionhelper), but they can alternatively be passed as separate template arguments, especially if the use ofRecordCallis going to be wrapped in another function. Perhaps the return type does not even have to be a template parameter as it’s going to beRecord *for all the cases mentioned in the question.