Advertisement
Guest User

Untitled

a guest
Jul 13th, 2018
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 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 <typename T>
  54. void print(const T &t)
  55. {
  56.     for (auto && i : t)
  57.         std::cout << i << " ";
  58.     std::cout << "\n";
  59. }
  60.  
  61. template <typename T>
  62. void printReverse(const T &t)
  63. {
  64.     for (auto it = t.rbegin(); it != t.rend(); it++)
  65.         std::cout << *it << " ";
  66.     std::cout << "\n";
  67. }
  68.  
  69. template <template <typename> class P>
  70. void dispatcher(const TypeID id, Data &d)
  71. {
  72.     switch (id) {
  73.     case TypeID::String:
  74.         P<TypeIdentifier<TypeID::String>::type>::call(d); break;
  75.     case TypeID::Float:
  76.         P<TypeIdentifier<TypeID::Float>::type>::call(d); break;
  77.     case TypeID::Integer:
  78.         P<TypeIdentifier<TypeID::Integer>::type>::call(d); break;
  79.     }
  80. }
  81.  
  82. template <typename T>
  83. struct Proc {
  84.     static void call(Data &d)
  85.     {
  86.         auto v = d.read<T>();
  87.         print(v);
  88.     }
  89. };
  90.  
  91. template <>
  92. struct Proc<int32_t> {
  93.     static void call(Data &d)
  94.     {
  95.         std::cout << "Specialization for int32_t\n";
  96.  
  97.         auto v = d.read<int32_t>();
  98.         print(v);
  99.     }
  100. };
  101.  
  102. template <typename T>
  103. struct ProcTwo {
  104.     static void call(Data &d)
  105.     {
  106.         auto v = d.read<T>();
  107.         printReverse(v);
  108.     }
  109. };
  110.  
  111. template <>
  112. struct ProcTwo<float> {
  113.     static void call(Data &d)
  114.     {
  115.         std::cout << "Specialization for float\n";
  116.  
  117.         auto v = d.read<float>();
  118.         printReverse(v);
  119.     }
  120. };
  121.  
  122. int main()
  123. {
  124.     Data d;
  125.  
  126.     dispatcher<Proc>(TypeID::String, d);
  127.     dispatcher<Proc>(TypeID::Float, d);
  128.     dispatcher<Proc>(TypeID::Integer, d);
  129.  
  130.     dispatcher<ProcTwo>(TypeID::String, d);
  131.     dispatcher<ProcTwo>(TypeID::Float, d);
  132.     dispatcher<ProcTwo>(TypeID::Integer, d);
  133.  
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement