www.developer.com/tech/article.php/3602441
|
April 28, 2006 Further Improvements: Generic Programming Techniques (Improved_crtp_double_interface.h)Static interfaces8 might provide the "missing link" in your endeavor. As a short recap, a static interface is a non-instantiable façade for all the classes having methods with the same signature as the methods exposed by the static interface. The static interface "hides" the real type of the wrapped class, doesn't use virtual methods, and is very useful in interface-based dispatching.
namespace improved_crtp_double_interface
{
template <class TAX_PAYER>
struct TaxPayer
{
TaxPayer() : strategy_(0), fun_(0) { }
typedef void (*FUN)(void*, void*);
template <class STRATEGY>
TaxPayer( STRATEGY* strategy) : strategy_(strategy),
fun_(fun<STRATEGY>::payTaxIsFun) { }
template <class STRATEGY>
void setStrategy( STRATEGY* strategy)
{
strategy_ = strategy;
fun_ = fun<STRATEGY>::payTaxIsFun;
}
void payTax()
{
assert(strategy_);
assert(fun_);
fun_(strategy_, this);
}
private:
template <class F>
struct fun
{
static void payTaxIsFun(void* a, void* p)
{
TAX_PAYER* tp = static_cast< TAX_PAYER*>(p);
F* strategy = static_cast< F*>(a);
strategy->payTax(tp);
}
};
private:
void* strategy_;
FUN fun_;
};
struct Company : TaxPayer<Company >
{
template <class T>
Company( T* strategy) : TaxPayer<Company>(strategy){ }
void only4companies() const {};
};
struct Employee : TaxPayer<Employee >
{
template <class T>
Employee( T* strategy) : TaxPayer<Employee>(strategy){ }
void only4employees() const {};
};
struct TaxStrategy
{
template <class TAX_PAYER>
void payTax(const TAX_PAYER* e)
{
std::cout << "generic payTax n" ;
}
};
template <>
void TaxStrategy::payTax(const Employee* e)
{
e->only4employees();
std::cout << "payTax Employeen" ;
}
template<>
void TaxStrategy::payTax(const Company* e)
{
e->only4companies();
std::cout << "payTax Companyn" ;
}
struct TaxStrategy1
{
template <class TAX_PAYER>
void payTax(const TAX_PAYER* e)
{
std::cout << "generic payTax 1 n" ;
}
};
template <>
void TaxStrategy1::payTax(const Employee* e)
{
e->only4employees();
std::cout << "payTax Employee 1n" ;
}
template<>
void TaxStrategy1::payTax(const Company* e)
{
e->only4companies();
std::cout << "payTax Company 1n" ;
}
class ITaxPayer
{
private:
typedef void (*FP1)(void* );
public:
template <class T>
ITaxPayer(T& x)
: p_(&x),
pf1_(&functions<T>::payTax)
{}
void payTax()
{
pf1_(p_);
}
private:
template <class TT>
struct functions
{
static void payTax( void* a )
{
static_cast<TT*>(a)->payTax();
}
};
void* p_;
FP1 pf1_;
};
void test()
{
TaxStrategy1 ts1;
TaxStrategy ts;
TaxPayer<Employee> e(&ts);
ITaxPayer i(e);
i.payTax();
std::vector<ITaxPayer> taxPayers;
taxPayers.push_back(i);
TaxPayer<Company> c;
c.setStrategy(&ts1);
i=c;
i.payTax();
taxPayers.push_back(i);
for (std::vector<ITaxPayer>::iterator it = taxPayers.begin();
it != taxPayers.end(); ++it)
{
it->payTax();
}
}
}
The previous example uses static interfaces twice:
Note: All classes exposing a method named payTax with the signature void (*)(void) can be manipulated through ITaxPayer. TaxPayer is more complicated; is not only an interface but it also hides the type of strategy as well as the mechanism of dispatching the call to the strategy. A strategy can be pushed into a TaxPayer at any time via setStrategy(). Another interesting addition is the way TaxStrategy is now factored as a single, non-parametric class exposing one template member function: template <class TAX_PAYER> void payTax(const TAX_PAYER* e); The class offers the generic, non-specialized version of the function. Specialization has to be done outside the class (template member specialization rule) making class extension a natural process. Supplementary CodeIn this section, you see some additional code—variations considered too interesting not to be at least mentioned.
ConclusionAfter an initial mechanical reconstruction in C++ of a Strategy with Generics, you've seen the opportunity of improving the design in a more generic way, potentially increasing the code's performance. Different versions of code were demonstrated and even more cases presented in the accompanying code snippets; some of them exemplify possible traps and design failures. Download the CodeYou can download the code that accompanies this article here. References
|