Guest User

TMP.HPP

a guest
Oct 28th, 2014
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.95 KB | None | 0 0
  1. #ifndef TMP_HPP
  2. #define TMP_HPP
  3.  
  4.  
  5. // Teamplate Meta-Programming tricks
  6. namespace tmp
  7. {
  8.     // Compile-time increment functor
  9.     template <typename T, T Val> struct increment
  10.     {
  11.         // Result
  12.         static const T value = ++Val;
  13.     };
  14.  
  15.     // Compile-time increment functor
  16.     template <typename T, T Val> struct decrement
  17.     {
  18.         // Result
  19.         static const T value = --Val;
  20.     };
  21.  
  22.     // Compile-time addition functor
  23.     template <typename T, T Lhs, T Rhs> struct plus
  24.     {
  25.         // Result
  26.         static const T value = Lhs + Rhs;
  27.     };
  28.  
  29.     // Compile-time subtraction functor
  30.     template <typename T, T Lhs, T Rhs> struct minus
  31.     {
  32.         // Result
  33.         static const T value = Lhs - Rhs;
  34.     };
  35.  
  36.     // Compile-time multiplication functor
  37.     template <typename T, T Lhs, T Rhs> struct multiplies
  38.     {
  39.         // Result
  40.         static const T value = Lhs * Rhs;
  41.     };
  42.  
  43.     // Compile-time division functor
  44.     template <typename T, T Lhs, T Rhs> struct divides
  45.     {
  46.         // Result
  47.         static const T value = Lhs / Rhs;
  48.     };
  49.  
  50.     // Compile-time modulo functor
  51.     template <typename T, T Lhs, T Rhs> struct modulus
  52.     {
  53.         // Result
  54.         static const T value = Lhs % Rhs;
  55.     };
  56.  
  57.     // Compile-time relation function
  58.     template <typename T, T Lhs, T Rhs> struct equal_to
  59.     {
  60.         // Helper class
  61.         template <typename TT, TT a, TT b, bool c> struct helper;
  62.  
  63.         template <typename TT, TT a, TT b> struct helper<TT, a, b, true >{ static const bool value = true; };
  64.         template <typename TT, TT a, TT b> struct helper<TT, a, b, false>{ static const bool value = false; };
  65.  
  66.         // Result
  67.         static const bool value = helper<T, Lhs, Rhs, Lhs == Rhs>::value;
  68.     };
  69.  
  70.     // Compile-time relation function
  71.     template <typename T, T Lhs, T Rhs> struct not_equal_to
  72.     {
  73.         // Helper class
  74.         template <typename TT, TT a, TT b, bool c> struct helper;
  75.  
  76.         template <typename TT, TT a, TT b> struct helper<TT, a, b, true >{ static const bool value = true; };
  77.         template <typename TT, TT a, TT b> struct helper<TT, a, b, false>{ static const bool value = false; };
  78.  
  79.         // Result
  80.         static const bool value = helper<T, Lhs, Rhs, Lhs != Rhs>::value;
  81.     };
  82.  
  83.     // Compile-time relation function
  84.     template <typename T, T Lhs, T Rhs> struct greater
  85.     {
  86.         // Helper class
  87.         template <typename TT, TT a, TT b, bool c> struct helper;
  88.  
  89.         template <typename TT, TT a, TT b> struct helper<TT, a, b, true >{ static const bool value = true; };
  90.         template <typename TT, TT a, TT b> struct helper<TT, a, b, false>{ static const bool value = false; };
  91.  
  92.         // Result
  93.         static const bool value = helper<T, Lhs, Rhs, Lhs > Rhs>::value;
  94.     };
  95.  
  96.     // Compile-time relation function
  97.     template <typename T, T Lhs, T Rhs> struct less
  98.     {
  99.         // Helper class
  100.         template <typename TT, TT a, TT b, bool c> struct helper;
  101.  
  102.         template <typename TT, TT a, TT b> struct helper<TT, a, b, true >{ static const bool value = true; };
  103.         template <typename TT, TT a, TT b> struct helper<TT, a, b, false>{ static const bool value = false; };
  104.  
  105.         // Result
  106.         static const bool value = helper<T, Lhs, Rhs, Lhs < Rhs>::value;
  107.     };
  108.  
  109.     // Compile-time relation function
  110.     template <typename T, T Lhs, T Rhs> struct greater_equal
  111.     {
  112.         // Helper class
  113.         template <typename TT, TT a, TT b, bool c> struct helper;
  114.  
  115.         template <typename TT, TT a, TT b> struct helper<TT, a, b, true >{ static const bool value = true; };
  116.         template <typename TT, TT a, TT b> struct helper<TT, a, b, false>{ static const bool value = false; };
  117.  
  118.         // Result
  119.         static const bool value = helper<T, Lhs, Rhs, Lhs >= Rhs>::value;
  120.     };
  121.  
  122.     // Compile-time relation function
  123.     template <typename T, T Lhs, T Rhs> struct less_equal
  124.     {
  125.         // Helper class
  126.         template <typename TT, TT a, TT b, bool c> struct helper;
  127.  
  128.         template <typename TT, TT a, TT b> struct helper<TT, a, b, true >{ static const bool value = true; };
  129.         template <typename TT, TT a, TT b> struct helper<TT, a, b, false>{ static const bool value = false; };
  130.  
  131.         // Result
  132.         static const bool value = helper<T, Lhs, Rhs, Lhs <= Rhs>::value;
  133.     };
  134.  
  135.     // Compile-time max function
  136.     template <typename T, T Lhs, T Rhs> struct max
  137.     {
  138.         // Helper class
  139.         template <typename TT, TT a, TT b, bool c> struct helper;
  140.  
  141.         template <typename TT, TT a, TT b> struct helper<TT, a, b, true >{ static const TT value = a; };
  142.         template <typename TT, TT a, TT b> struct helper<TT, a, b, false>{ static const TT value = b; };
  143.  
  144.         // Result
  145.         static const T value = helper<T, Lhs, Rhs, Lhs >= Rhs>::value;
  146.     };
  147.  
  148.     // Compile-time min function
  149.     template <typename T, T Lhs, T Rhs> struct min
  150.     {
  151.         // Helper class
  152.         template <typename TT, TT a, TT b, bool c> struct helper;
  153.  
  154.         template <typename TT, TT a, TT b> struct helper<TT, a, b, true >{ static const TT value = a; };
  155.         template <typename TT, TT a, TT b> struct helper<TT, a, b, false>{ static const TT value = b; };
  156.  
  157.         // Result
  158.         static const T value = helper<T, Lhs, Rhs, Lhs <= Rhs>::value;
  159.     };
  160.    
  161.    
  162.     // Compile-time for cycle helpers
  163.     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;
  164.    
  165.     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...>
  166.     {
  167.         void operator()(F f, Args... args) {}
  168.     };
  169.     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...>
  170.     {
  171.         void operator()(F f, Args... args)
  172.         {
  173.             // Call templated functor with arguments
  174.             f<I>(args...);
  175.  
  176.             // Instantiate the helper with the Predicate for the Next value of I
  177.             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...);
  178.         }
  179.     };
  180.  
  181.     // Compile-time for cycle
  182.     template <
  183.         size_t Initial,
  184.         size_t Final,
  185.         template <size_t, size_t> class Predicate,
  186.         template <size_t, size_t NextRhs> class Next,
  187.         template <size_t, typename... FTArgs> class Functor,
  188.         typename... Args> void for_cycle(Functor<FTArgs> func, Args... args)
  189.     {
  190.         ForH<Predicate<Initial, Final>::value, Initial, Predicate<Initial, Final>, Next<Initial, NextRhs>, Functor<size_t, FTArgs>, Args...>()(func, args...);
  191.     }
  192. }
  193.  
  194. #endif // TMP_HPP
Advertisement
Add Comment
Please, Sign In to add comment