Created
May 1, 2015 04:45
-
-
Save landersson/11ff00b40cedf5f8f60f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QApplication> | |
#include <QThread> | |
#include <memory> | |
#include <iostream> | |
#define USE_UNIQUE_PTR | |
template<typename T, typename ...Args> | |
std::unique_ptr<T> make_unique( Args&& ...args ) | |
{ | |
return std::unique_ptr<T>( new T( std::forward<Args>(args)... ) ); | |
} | |
struct Signal | |
{ | |
public: | |
Signal() : ptr(nullptr) {} | |
#ifdef USE_UNIQUE_PTR | |
std::unique_ptr<char> ptr; | |
#else | |
char *ptr; | |
#endif | |
}; | |
class Sender : public QThread | |
{ | |
Q_OBJECT | |
public: | |
void run() | |
{ | |
Signal s; | |
#ifdef USE_UNIQUE_PTR | |
s.ptr = make_unique<char>(); | |
#else | |
s.ptr = new char(); | |
#endif | |
*s.ptr = 'A'; | |
emit signal(s); | |
} | |
signals: | |
void signal(const Signal & sig); | |
}; | |
class Receiver : public QObject | |
{ | |
Q_OBJECT | |
public slots: | |
void signal(const Signal & sig) | |
{ | |
std::cout << "Got signal: *ptr = '" << *sig.ptr << "'" << std::endl; | |
} | |
}; | |
#include "test_move_signal.moc" | |
int main(int argc, char *argv[]) | |
{ | |
QApplication app(argc, argv); | |
Receiver receiver; | |
Sender sender; | |
qRegisterMetaType<Signal>("Signal"); | |
QObject::connect(&sender, &Sender::signal, &receiver, &Receiver::signal); | |
sender.start(); | |
app.exec(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment