This file contains hidden or 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 <QtCore> | |
#include <QtNetwork> | |
//some custom data structure depending on QList | |
using DataStructure = QList<QPair<QString, QString>>; | |
//Q_DECLARE_METATYPE(DataStructure) | |
/*QDataStream& operator<<(QDataStream& stream, const DataStructure& ds) { | |
stream << (quint32) ds.size(); | |
for(auto e : ds) stream << e; |
This file contains hidden or 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
//from Howard Hinnant's answer https://stackoverflow.com/a/28003328 | |
#include <cstdlib> | |
#include <cstddef> | |
#include <iostream> | |
std::size_t allocated = 0; | |
void* operator new(std::size_t sz) { | |
void* p = std::malloc(sz); |
This file contains hidden or 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
//from Howard Hinnant's answer https://stackoverflow.com/a/18303787 | |
#include <random> | |
#include <chrono> | |
#include <iostream> | |
std::random_device eng; //can be used to generate random input for test cases | |
std::uniform_int_distribution<std::size_t> random(500, 1000); | |
//a function template that executes a given function object and returns |
This file contains hidden or 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
#ifndef DATASTREAM_ENUM_H | |
#define DATASTREAM_ENUM_H | |
#include <type_traits> | |
//a function that can serialize any enum into QDataStream | |
//it stores the enum in a qint64 | |
template<typename Enum, | |
typename = typename std::enable_if<std::is_enum<Enum>::value>::type> |
This file contains hidden or 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 "qthreadutils.h" | |
#include <QtCore> | |
//the following file shows an example usage for CallByWorker | |
//a thread that can be destroyed at any time | |
//see http://stackoverflow.com/a/25230470 | |
class SafeThread : public QThread{ | |
using QThread::run; | |
public: |
This file contains hidden or 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 <QtWidgets> | |
#include <QtNetwork> | |
class SyncTcpSocket : public QTcpSocket{ | |
Q_OBJECT | |
public: | |
explicit SyncTcpSocket(QObject* parent= nullptr):QTcpSocket(parent){ | |
setSocketOption(QAbstractSocket::LowDelayOption, 1); | |
heartbeatTimer.setInterval(100); | |
connect(&heartbeatTimer, &QTimer::timeout, |
This file contains hidden or 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 <QtWidgets> | |
//QThread wrapper for safe destruction | |
//see http://stackoverflow.com/a/19666329 | |
class Thread : public QThread { | |
using QThread::run; // final | |
public: | |
Thread(QObject * parent = 0) : QThread(parent) {} | |
~Thread() { quit(); wait(); } | |
}; |
This file contains hidden or 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 <QtWidgets> | |
#include <QtSql> | |
//to ensure the database connection is closed and removed at the end | |
//of the write/read func call | |
class SQLConnectionRAIIWrapper{ | |
public: | |
SQLConnectionRAIIWrapper(QString fileName){ | |
//use a unique connection name, to ensure thread safety | |
connectionName = QString("SQLSETTINGS%1").arg((int)QThread::currentThreadId()); |
NewerOlder