我正在尝试存储std::tuple不同数量的值,这些值稍后将用作调用与存储类型匹配的函数指针的参数.
我创建了一个简化的示例,显示了我正在努力解决的问题:
#include <iostream>
#include <tuple>
void f(int a, double b, void* c) {
std::cout << a << ":" << b << ":" << c << std::endl;
}
template <typename ...Args>
struct save_it_for_later {
std::tuple<Args...> params;
void (*func)(Args...);
void delayed_dispatch() {
// How can I "unpack" params to call func?
func(std::get<0>(params), std::get<1>(params), std::get<2>(params));
// But I *really* don't want to write 20 versions of dispatch so I'd rather
// write something like:
func(params...); // Not legal
}
}; …Run Code Online (Sandbox Code Playgroud) c++ function-pointers variadic-templates c++11 iterable-unpacking
在带有GCD的ObjC中,有一种在旋转事件循环的任何线程中执行lambda的方法.例如:
dispatch_sync(dispatch_get_main_queue(), ^{ /* do sth */ });
Run Code Online (Sandbox Code Playgroud)
要么:
dispatch_async(dispatch_get_main_queue(), ^{ /* do sth */ });
Run Code Online (Sandbox Code Playgroud)
它[]{ /* do sth */ }在主线程的队列中执行某些操作(相当于在C++中),阻塞或异步.
我怎么能在Qt中做同样的事情?
从我所读到的,我想解决方案将以某种方式向主线程的某个对象发送信号.但是什么对象呢?只是QApplication::instance()?(那是那时生活在主线程中的唯一对象.)什么信号?
从目前的答案和我目前的研究来看,似乎我需要一些虚拟对象来坐在主线程中,有一些插槽只是等待进入某些代码来执行.
所以,我决定使用子类QApplication来添加它.我目前的代码,但不起作用(但也许你可以帮忙):
#include <QApplication>
#include <QThread>
#include <QMetaMethod>
#include <functional>
#include <assert.h>
class App : public QApplication
{
Q_OBJECT
public:
App();
signals:
public slots:
void genericExec(std::function<void(void)> func) {
func();
}
private:
// cache this
QMetaMethod genericExec_method;
public:
void invokeGenericExec(std::function<void(void)> func, Qt::ConnectionType connType) {
if(!genericExec_method) {
QByteArray normalizedSignature = QMetaObject::normalizedSignature("genericExec(std::function<void(void)>)");
int …Run Code Online (Sandbox Code Playgroud) 假设我们在QObject-deriving类中编写了一个非const方法:
class MyClass : public QObject {
int x;
public:
void method(int a) {
x = a; // and possibly other things
};
};
Run Code Online (Sandbox Code Playgroud)
我们希望使该方法成为线程安全的:意味着从任意线程调用它,并且同时从多个线程调用它,不应该引入未定义的行为.
Qt提供了哪些机制/ API来帮助我们使该方法具有线程安全性?
Qt的哪些机制/ API可以在方法执行"其他事情"时使用?
是否可以对"其他事物"进行分类,以便为使用Qt特定的机制/ API提供信息?
非主题是C++标准本身提供的机制,以及确保线程安全的通用/非Qt特定方式.
我正试图调用这样的模板化函数:
typedef std::tuple<int, double, bool> InstrumentTuple;
Cache cache;
InstrumentTuple tuple = cache.get<InstrumentTuple>();
Run Code Online (Sandbox Code Playgroud)
我知道我可以"简单地"传递元组的类型.这就是我所知道的但是它非常麻烦,因为我对这个函数进行了很多调用,因为元组很长:
InstrumentTuple tuple = c.get<int, double, bool>(); // syntax I'd like to avoid
Run Code Online (Sandbox Code Playgroud)
所以我尝试了get方法的多个实现,但没有成功:
#include <tuple>
class Cache
{
private:
template<int I, typename T, typename = typename std::enable_if<I == std::tuple_size<T>::value>::type>
std::tuple<> get() // line 6
{
return std::tuple<>();
}
template<int I, typename T, typename = typename std::enable_if<I != std::tuple_size<T>::value>::type>
std::tuple<typename std::tuple_element<I,T>::type, decltype(get<I+1, T>())> get() // line 12
{
std::tuple<typename std::tuple_element<I,T>::type> value;
return std::tuple_cat(value, get<I+1, T>());
}
public: …Run Code Online (Sandbox Code Playgroud)