Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 2.26 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to inspect the types of a function's parameters?
  2. template<typename Function, typename... Args>
  3. void marshal_and_apply(Function f, Args... args);
  4.  
  5. void func1(int x, int y);
  6. void func2(marshal_me<int> x, int y);
  7. void func3(marshal_me<int> x, marshal_me<int> y, marshal_me<int> z);
  8.  
  9. // this call would be equivalent to:
  10. // func1(7,13)
  11. marshal_and_apply(func1, 7, 13);
  12.  
  13. // this call would be equivalent to:
  14. // auto storage = my_allocator(sizeof(int));
  15. // auto x = marshal_me<int>(7, storage);
  16. // func2(x, 13);
  17. marshal_and_apply(func2, 7, 13);
  18.  
  19. // this call would be equivalent to:
  20. // auto storage = my_allocator(sizeof(int) + sizeof(int) + sizeof(int));
  21. // auto x = marshal_me<int>(7, storage);
  22. // auto y = marshal_me<int>(13, storage + sizeof(int));
  23. // auto z = marshal_me<int>(42, storage + sizeof(int) + sizeof(int));
  24. // func3(x,y,z);
  25. marshal_and_apply(func3, 7, 13, 42);
  26.        
  27. template<typename Function, typename... Args>
  28. void marshal_and_apply(Function f, Args &&... args)
  29. {
  30.     f(InspectAndModify<Args>::process(sizeof...(Args), std::forward<Args>(args))...);
  31. }
  32.        
  33. template <typename T> struct InspectAndModify
  34. {
  35.     static T&& process(unsigned int N, T && t)
  36.     {
  37.         return std::forward<T>(t);
  38.     }
  39. };
  40.  
  41. template <typename T> struct InspectAndModify<marshal_me<T>>
  42. {
  43.      static T&& process(unsigned int N, marshal_me<T> && m)
  44.      {
  45.          /* ... */
  46.      }
  47. };
  48.        
  49. template <typename T> struct marshal_me { marshal_me(T) { } };
  50.  
  51. template <typename To, typename From> struct static_transform;
  52.  
  53. template <typename T> struct static_transform<T, T>
  54. {
  55.   static T go(T t) { return t; }
  56. };
  57.  
  58. template <typename T> struct static_transform<T, T&>
  59. {
  60.   static T go(T & t) { return t; }
  61. };
  62.  
  63. template <typename T> struct static_transform<marshal_me<T>, T>
  64. {
  65.   static marshal_me<T> go(T && t) { return std::forward<T>(t); }
  66. };
  67.  
  68. template<typename T, typename... Args>
  69. struct marshal_impl
  70. {
  71.   template <typename ...Urgs>
  72.   static T go(T(*f)(Urgs...), Args &&... args)
  73.   {
  74.     return f(static_transform<Urgs, Args>::go(std::forward<Args>(args))...);
  75.   }
  76. };
  77.  
  78. template<typename Function, typename... Args>
  79. void marshal_and_apply(Function f, Args &&... args)
  80. {
  81.   marshal_impl<void, Args...>::go(static_cast<typename std::decay<Function>::type>(f),
  82.                                   std::forward<Args>(args)...);
  83. }