Advertisement
Guest User

Untitled

a guest
Jul 13th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. enum class TypeID {
  5.     String,
  6.     Float,
  7.     Integer
  8. };
  9.  
  10. template <TypeID>
  11. struct TypeIdentifier {
  12. };
  13.  
  14. template <>
  15. struct TypeIdentifier<TypeID::String> {
  16.     typedef std::string type;
  17. };
  18.  
  19. template <>
  20. struct TypeIdentifier<TypeID::Float> {
  21.     typedef float type;
  22. };
  23.  
  24. template <>
  25. struct TypeIdentifier<TypeID::Integer> {
  26.     typedef int32_t type;
  27. };
  28.  
  29. struct Data {
  30.     template <typename T>
  31.     std::vector<T> read();
  32. };
  33.  
  34.  
  35. template <>
  36. std::vector<std::string> Data::read()
  37. {
  38.     return std::vector<std::string>{ "Zero", "One", "Two" };
  39. }
  40.  
  41. template <>
  42. std::vector<float> Data::read()
  43. {
  44.     return std::vector<float>{ 0.1, 0.2, 0.3 };
  45. }
  46.  
  47. template <>
  48. std::vector<int32_t> Data::read()
  49. {
  50.     return std::vector<int32_t>{ 10, 20, 30 };
  51. }
  52.  
  53. template <TypeID ID>
  54. std::vector<typename TypeIdentifier<ID>::type> process(Data &d)
  55. {
  56.     return d.read<typename TypeIdentifier<ID>::type>();
  57. }
  58.  
  59. template <typename T>
  60. void print(const T &t)
  61. {
  62.     for (auto && i : t)
  63.         std::cout << i << " ";
  64.     std::cout << "\n";
  65. }
  66.  
  67. template <typename T>
  68. void printReverse(const T &t)
  69. {
  70.     for (auto it = t.rbegin(); it != t.rend(); it++)
  71.         std::cout << *it << " ";
  72.     std::cout << "\n";
  73. }
  74.  
  75. template <template <TypeID> class P>
  76. void dispatcher(const TypeID id, Data &d)
  77. {
  78.     switch (id) {
  79.     case TypeID::String:
  80.         P<TypeID::String>::call(d); break;
  81.     case TypeID::Float:
  82.         P<TypeID::Float>::call(d); break;
  83.     case TypeID::Integer:
  84.         P<TypeID::Integer>::call(d); break;
  85.     }
  86. }
  87.  
  88. template <TypeID>
  89. struct Proc {};
  90.  
  91. template <>
  92. struct Proc<TypeID::String> {
  93.     static void call(Data &d)
  94.     {
  95.         auto v = process<TypeID::String>(d);
  96.         print(v);
  97.     }
  98. };
  99.  
  100. template <>
  101. struct Proc<TypeID::Float> {
  102.     static void call(Data &d)
  103.     {
  104.         auto v = process<TypeID::Float>(d);
  105.         print(v);
  106.     }
  107. };
  108.  
  109. template <>
  110. struct Proc<TypeID::Integer> {
  111.     static void call(Data &d)
  112.     {
  113.         auto v = process<TypeID::Integer>(d);
  114.         print(v);
  115.     }
  116. };
  117.  
  118. template <TypeID>
  119. struct ProcTwo {};
  120.  
  121. template <>
  122. struct ProcTwo<TypeID::String> {
  123.     static void call(Data &d)
  124.     {
  125.         auto v = process<TypeID::String>(d);
  126.         printReverse(v);
  127.     }
  128. };
  129.  
  130. template <>
  131. struct ProcTwo<TypeID::Float> {
  132.     static void call(Data &d)
  133.     {
  134.         auto v = process<TypeID::Float>(d);
  135.         printReverse(v);
  136.     }
  137. };
  138.  
  139. template <>
  140. struct ProcTwo<TypeID::Integer> {
  141.     static void call(Data &d)
  142.     {
  143.         auto v = process<TypeID::Integer>(d);
  144.         printReverse(v);
  145.     }
  146. };
  147.  
  148. int main()
  149. {
  150.     Data d;
  151.  
  152.     dispatcher<Proc>(TypeID::String, d);
  153.     dispatcher<Proc>(TypeID::Float, d);
  154.     dispatcher<Proc>(TypeID::Integer, d);
  155.  
  156.     dispatcher<ProcTwo>(TypeID::String, d);
  157.     dispatcher<ProcTwo>(TypeID::Float, d);
  158.     dispatcher<ProcTwo>(TypeID::Integer, d);
  159.  
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement