Guest User

Untitled

a guest
May 22nd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. struct not_found
  2. {
  3. };
  4.  
  5. template <typename TKey, typename TValue, TKey...>
  6. struct JumpTable;
  7.  
  8. template <typename TKey, typename TValue>
  9. struct JumpTable<TKey, TValue>
  10. {
  11. TValue& operator[]([[maybe_unused]] TKey needed)
  12. {
  13. throw not_found{};
  14. }
  15. };
  16.  
  17. template <typename TKey, typename TValue, TKey key, TKey... rest>
  18. struct JumpTable<TKey, TValue, key, rest...> : JumpTable<TKey, TValue, rest...>
  19. {
  20. TValue value;
  21. using super = JumpTable<TKey, TValue, rest...>;
  22.  
  23. TValue& operator[](TKey k)
  24. {
  25. return key == k ? value : super::operator[](k);
  26. }
  27. };
  28.  
  29.  
  30. int fn1(int x)
  31. {
  32. return x + 1;
  33. }
  34. int fn2(int x)
  35. {
  36. return x + 2;
  37. }
  38. int fn3(int x)
  39. {
  40. return x + 3;
  41. }
  42.  
  43. int main()
  44. {
  45. JumpTable<DispatchId,
  46. FuncType,
  47. DispatchId::Id1,
  48. DispatchId::Id2,
  49. DispatchId::Id3> jt;
  50.  
  51. jt[DispatchId::Id1] = fn1;
  52. jt[DispatchId::Id2] = fn2;
  53. jt[DispatchId::Id3] = fn3;
  54. }
Add Comment
Please, Sign In to add comment