54 lines
1.6 KiB
C++
54 lines
1.6 KiB
C++
// tmp_policy
|
|
// Created by lucas on 22-7-26.
|
|
// Re-implementation of a typical policy architecture in C++ Template Meta-Programming.
|
|
|
|
#ifndef TMP_POLICY_ACCPOLICY_H
|
|
#define TMP_POLICY_ACCPOLICY_H
|
|
|
|
// Policy Major Class definition.
|
|
struct AccPolicy {
|
|
using MajorClass = AccPolicy;
|
|
|
|
struct AccuTypeCate {
|
|
struct Add;
|
|
struct Mul;
|
|
};
|
|
using AccuType = AccuTypeCate::Add;
|
|
|
|
struct IsAveValueCate;
|
|
static constexpr bool IsAveValue = false;
|
|
|
|
struct ResultTypeCate;
|
|
using ResultType = int;
|
|
|
|
};
|
|
|
|
// Auxiliary Macros Definition.
|
|
#define TypePolicyObj(TName, TMajor, TMinor, TType) \
|
|
struct TName: virtual public TMajor { \
|
|
using MinorClass = TMinor##TypeCate; \
|
|
using TMinor##Type = TMinor##TypeCate::TType; \
|
|
}
|
|
|
|
#define ValuePolicyObj(TName, TMajor, TMinor, TValueType, TValue) \
|
|
struct TName: virtual public TMajor { \
|
|
using MinorClass = TMinor##ValueCate; \
|
|
static constexpr TValueType TMinor##Value = TValue; \
|
|
}
|
|
|
|
#define TypePolicyTemplate(TName, TMajor, TMinor) \
|
|
template <typename T> \
|
|
struct TName: virtual public TMajor{ \
|
|
using MinorClass = TMinor##TypeCate; \
|
|
using TMinor##Type = T; \
|
|
}
|
|
|
|
// Policy objects defined with auxiliary macros.
|
|
TypePolicyObj(PAdd, AccPolicy, Accu, Add);
|
|
TypePolicyObj(PMul, AccPolicy, Accu, Mul);
|
|
ValuePolicyObj(PAve, AccPolicy, IsAve, bool, true);
|
|
ValuePolicyObj(PNoAve, AccPolicy, IsAve, bool, false);
|
|
TypePolicyTemplate(PResultTypeIs, AccPolicy, Result);
|
|
|
|
#endif //TMP_POLICY_ACCPOLICY_H
|