fork download
  1. #include <iostream>
  2. #include <functional>
  3. #include <type_traits>
  4.  
  5. // TODO: provide your definition of what this actually is
  6. // (sounds like you already have one)
  7. struct NATIVEVALUE {};
  8.  
  9.  
  10. // from your question
  11. typedef int (*EXTENSIONFUNCTION)(NATIVEVALUE args[]);
  12.  
  13.  
  14. // TODO: provide overloads for conversion of supported data
  15. // types to the NATIVEVALUE construction.
  16. NATIVEVALUE nv_conv(int value)
  17. {
  18. std::cout << __PRETTY_FUNCTION__ << '\n';
  19. NATIVEVALUE nv = {};
  20. return nv;
  21. }
  22.  
  23. NATIVEVALUE nv_conv(double dbl)
  24. {
  25. std::cout << __PRETTY_FUNCTION__ << '\n';
  26. NATIVEVALUE nv = {};
  27. return nv;
  28. }
  29.  
  30. NATIVEVALUE nv_conv(unsigned int ui)
  31. {
  32. std::cout << __PRETTY_FUNCTION__ << '\n';
  33. NATIVEVALUE nv = {};
  34. return nv;
  35. }
  36.  
  37. NATIVEVALUE nv_conv(char c)
  38. {
  39. std::cout << __PRETTY_FUNCTION__ << '\n';
  40. NATIVEVALUE nv = {};
  41. return nv;
  42. }
  43.  
  44. // object wrapper for holding the callback and provide call-operator
  45. // overloads (template and empty)
  46. struct Callable
  47. {
  48. EXTENSIONFUNCTION m_pfn;
  49.  
  50. Callable(EXTENSIONFUNCTION pfn) : m_pfn(pfn) {}
  51.  
  52. template<class Arg, class... Args>
  53. int operator()(Arg arg, Args... args)
  54. {
  55. std::cout << __PRETTY_FUNCTION__ << '\n';
  56. NATIVEVALUE nv_args[] = { nv_conv(arg), nv_conv(args)... };
  57. return m_pfn(nv_args);
  58. }
  59.  
  60. // no arg overload
  61. int operator()()
  62. {
  63. std::cout << __PRETTY_FUNCTION__ << '\n';
  64. return m_pfn(NULL);
  65. }
  66. };
  67.  
  68. int nv_callback(NATIVEVALUE args[])
  69. {
  70. std::cout << __PRETTY_FUNCTION__ << '\n';
  71. return 0;
  72. }
  73.  
  74.  
  75. int main()
  76. {
  77. auto fn = std::bind(Callable(nv_callback), 1, 2.0, 03, 4U, '5');
  78. fn();
  79.  
  80. auto fn2 = std::bind(Callable(nv_callback));
  81. fn2();
  82.  
  83. return 0;
  84. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
int Callable::operator()(Arg, Args ...) [with Arg = int; Args = {double, int, unsigned int, char}]
NATIVEVALUE nv_conv(int)
NATIVEVALUE nv_conv(double)
NATIVEVALUE nv_conv(int)
NATIVEVALUE nv_conv(unsigned int)
NATIVEVALUE nv_conv(char)
int nv_callback(NATIVEVALUE*)
int Callable::operator()()
int nv_callback(NATIVEVALUE*)