Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef TMP_HPP
- #define TMP_HPP
- // Teamplate Meta-Programming tricks
- namespace tmp
- {
- // Compile-time increment functor
- template <typename T, T Val> struct increment
- {
- // Result
- static const T value = ++Val;
- };
- // Compile-time increment functor
- template <typename T, T Val> struct decrement
- {
- // Result
- static const T value = --Val;
- };
- // Compile-time addition functor
- template <typename T, T Lhs, T Rhs> struct plus
- {
- // Result
- static const T value = Lhs + Rhs;
- };
- // Compile-time subtraction functor
- template <typename T, T Lhs, T Rhs> struct minus
- {
- // Result
- static const T value = Lhs - Rhs;
- };
- // Compile-time multiplication functor
- template <typename T, T Lhs, T Rhs> struct multiplies
- {
- // Result
- static const T value = Lhs * Rhs;
- };
- // Compile-time division functor
- template <typename T, T Lhs, T Rhs> struct divides
- {
- // Result
- static const T value = Lhs / Rhs;
- };
- // Compile-time modulo functor
- template <typename T, T Lhs, T Rhs> struct modulus
- {
- // Result
- static const T value = Lhs % Rhs;
- };
- // Compile-time relation function
- template <typename T, T Lhs, T Rhs> struct equal_to
- {
- // Helper class
- template <typename TT, TT a, TT b, bool c> struct helper;
- template <typename TT, TT a, TT b> struct helper<TT, a, b, true >{ static const bool value = true; };
- template <typename TT, TT a, TT b> struct helper<TT, a, b, false>{ static const bool value = false; };
- // Result
- static const bool value = helper<T, Lhs, Rhs, Lhs == Rhs>::value;
- };
- // Compile-time relation function
- template <typename T, T Lhs, T Rhs> struct not_equal_to
- {
- // Helper class
- template <typename TT, TT a, TT b, bool c> struct helper;
- template <typename TT, TT a, TT b> struct helper<TT, a, b, true >{ static const bool value = true; };
- template <typename TT, TT a, TT b> struct helper<TT, a, b, false>{ static const bool value = false; };
- // Result
- static const bool value = helper<T, Lhs, Rhs, Lhs != Rhs>::value;
- };
- // Compile-time relation function
- template <typename T, T Lhs, T Rhs> struct greater
- {
- // Helper class
- template <typename TT, TT a, TT b, bool c> struct helper;
- template <typename TT, TT a, TT b> struct helper<TT, a, b, true >{ static const bool value = true; };
- template <typename TT, TT a, TT b> struct helper<TT, a, b, false>{ static const bool value = false; };
- // Result
- static const bool value = helper<T, Lhs, Rhs, Lhs > Rhs>::value;
- };
- // Compile-time relation function
- template <typename T, T Lhs, T Rhs> struct less
- {
- // Helper class
- template <typename TT, TT a, TT b, bool c> struct helper;
- template <typename TT, TT a, TT b> struct helper<TT, a, b, true >{ static const bool value = true; };
- template <typename TT, TT a, TT b> struct helper<TT, a, b, false>{ static const bool value = false; };
- // Result
- static const bool value = helper<T, Lhs, Rhs, Lhs < Rhs>::value;
- };
- // Compile-time relation function
- template <typename T, T Lhs, T Rhs> struct greater_equal
- {
- // Helper class
- template <typename TT, TT a, TT b, bool c> struct helper;
- template <typename TT, TT a, TT b> struct helper<TT, a, b, true >{ static const bool value = true; };
- template <typename TT, TT a, TT b> struct helper<TT, a, b, false>{ static const bool value = false; };
- // Result
- static const bool value = helper<T, Lhs, Rhs, Lhs >= Rhs>::value;
- };
- // Compile-time relation function
- template <typename T, T Lhs, T Rhs> struct less_equal
- {
- // Helper class
- template <typename TT, TT a, TT b, bool c> struct helper;
- template <typename TT, TT a, TT b> struct helper<TT, a, b, true >{ static const bool value = true; };
- template <typename TT, TT a, TT b> struct helper<TT, a, b, false>{ static const bool value = false; };
- // Result
- static const bool value = helper<T, Lhs, Rhs, Lhs <= Rhs>::value;
- };
- // Compile-time max function
- template <typename T, T Lhs, T Rhs> struct max
- {
- // Helper class
- template <typename TT, TT a, TT b, bool c> struct helper;
- template <typename TT, TT a, TT b> struct helper<TT, a, b, true >{ static const TT value = a; };
- template <typename TT, TT a, TT b> struct helper<TT, a, b, false>{ static const TT value = b; };
- // Result
- static const T value = helper<T, Lhs, Rhs, Lhs >= Rhs>::value;
- };
- // Compile-time min function
- template <typename T, T Lhs, T Rhs> struct min
- {
- // Helper class
- template <typename TT, TT a, TT b, bool c> struct helper;
- template <typename TT, TT a, TT b> struct helper<TT, a, b, true >{ static const TT value = a; };
- template <typename TT, TT a, TT b> struct helper<TT, a, b, false>{ static const TT value = b; };
- // Result
- static const T value = helper<T, Lhs, Rhs, Lhs <= Rhs>::value;
- };
- // Compile-time for cycle helpers
- template <bool B, size_t I, template <size_t PL, size_t PR> class P, template <size_t NL, size_t NR> class N, template <size_t, typename... FTArgs> class F, typename... Args> struct ForH;
- template <size_t I, template <size_t PL, size_t PR> class P, template <size_t NL, size_t NR> class N, template <size_t, typename... FTArgs> class F, typename... Args> struct ForH<false, I, P, N, F, Args...>
- {
- void operator()(F f, Args... args) {}
- };
- template <size_t I, template <size_t PL, size_t PR> class P, template <size_t NL, size_t NR> class N, template <size_t, typename... FTArgs> F, typename... Args> struct ForH<true, I, P, N, F, Args...>
- {
- void operator()(F f, Args... args)
- {
- // Call templated functor with arguments
- f<I>(args...);
- // Instantiate the helper with the Predicate for the Next value of I
- ForH<P<N<I, NR>::value, PR>::value, N<I, NR>::value, P<N<I, NR>::value, PR>, N<N<I, NR>::value, NR>, F, Args...>()(f, args...);
- }
- };
- // Compile-time for cycle
- template <
- size_t Initial,
- size_t Final,
- template <size_t, size_t> class Predicate,
- template <size_t, size_t NextRhs> class Next,
- template <size_t, typename... FTArgs> class Functor,
- typename... Args> void for_cycle(Functor<FTArgs> func, Args... args)
- {
- ForH<Predicate<Initial, Final>::value, Initial, Predicate<Initial, Final>, Next<Initial, NextRhs>, Functor<size_t, FTArgs>, Args...>()(func, args...);
- }
- }
- #endif // TMP_HPP
Advertisement
Add Comment
Please, Sign In to add comment