Guest User

Untitled

a guest
Nov 25th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. template <typename Function , typename ...Args>
  2. auto exceptionWrapper (Object *obj, Function&& function, Args&& ...args)-> decltype((obj->*function)(std::forward<Args>(args)...))
  3. {
  4. try
  5. {
  6. QScopedPointer<Object> tempObj(new Object(obj->getSomeData()));
  7. return (tempObj->*function)(std::forward<Args>(args)...);
  8. }
  9. catch(std::exception& e)
  10. {
  11. ...
  12. }
  13. }
  14.  
  15. template <typename Function , typename ...Args, typename CallBack>
  16. void Object::callAsync(Object *obj,Function&& query,CallBack&& callBack, Args&& ...args)
  17. {
  18.  
  19. using ReturnValue = decltype(exceptionWrapper(obj, query, std::forward<Args>(args)...));
  20. QFutureWatcher<ReturnValue> watcher;
  21. connect(&watcher, &QFutureWatcher <ReturnValue>::finished, callBack);
  22. QFuture<ReturnValue> futureValue = QtConcurrent::run(exceptionWrapper, obj, query, std::forward<Args>(args)...);
  23.  
  24. watcher.setFuture(futureValue);
  25. }
  26.  
  27. Object object = new Object();
  28. Object::callAsync(object, Object::objFcn, [](){qDebug()<<"qq";}, arg1, arg2);
  29.  
  30. ошибка: no matching function for call to 'run(<unresolved overloaded function type>, BOKZDatabase*&, AngleOrientData (BOKZDatabase::*&)(const QString&, const QString&, const QString&, const QString&, int), const QString&, const QString&, const QString&, const QString&, const int&)'
  31. QFuture<ReturnValue> futureValue = QtConcurrent::run(exceptionWrapper, db, query, std::forward<Args>(args)...);
  32. ^
  33.  
  34. template <typename Function , typename ...Args, typename CallBack>
  35. void Object::callAsync (Object *obj,Function&& query,CallBack&& callBack, Args&& ...args)
  36. {
  37.  
  38. using ReturnValue = decltype(exceptionWrapper(obj, query, args...));
  39. QFutureWatcher<ReturnValue> watcher;
  40. connect(&watcher, &QFutureWatcher <ReturnValue>::finished, callBack);
  41. QFuture<ReturnValue> futureValue = QtConcurrent::run(exceptionWrapper <Function, Args...>, obj, std::forward<Function>(query), std::forward<Args>(args)...);
  42. watcher.setFuture(futureValue);
  43. }
  44.  
  45. class Object
  46. {
  47. public:
  48. int sum(int a, int b)
  49. {
  50. return a+b;
  51. }
  52. };
  53.  
  54.  
  55. template <typename Function , typename ...Args>
  56. auto fcn (Object *object, Function&& function, Args&& ...args)-> decltype ((object->*function)(std::forward<Args>(args)...))
  57. {
  58. return (object->*function)(std::forward<Args>(args)...);
  59. }
  60.  
  61. template <typename Function , typename ...Args>
  62. void callAsync(Object *object,Function&& function, Args&& ...args)
  63. {
  64.  
  65. QtConcurrent::run(fcn <Function, std::forward<Args...>>, object, function, std::forward<Args>(args)...);
  66.  
  67. }
  68.  
  69.  
  70. int main(int argc, char *argv[])
  71. {
  72. QCoreApplication a(argc, argv);
  73. Object* obj = new Object();
  74.  
  75. callAsync(obj, Object::sum, 5, 6);
  76. return a.exec();
  77. }
  78.  
  79. QtConcurrent::run(exceptionWrapper<Function, Args...>, obj, std::forward<Function>(query), std::forward<Args>(args)...);
  80.  
  81. int var = 5;
  82. callAsync(obj, &Object::sum, 7, var);
  83.  
  84. template <typename Function , typename ...Args>
  85. auto fcn(Object *object, Function function, const Args& ...args)
  86. {
  87. return (object->*function)(args...);
  88. }
  89.  
  90. template <typename Function , typename ...Args>
  91. void callAsync(Object *object, Function&& function, Args&& ...args)
  92. {
  93. QtConcurrent::run(fcn<Function, Args...>, object, function,
  94. std::forward<Args>(args)...);
  95. }
Add Comment
Please, Sign In to add comment