Skip to content

Instantly share code, notes, and snippets.

@micjabbour
micjabbour / main.cpp
Created January 7, 2017 21:51
QSettings sqlite format
#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());
@micjabbour
micjabbour / main.cpp
Created January 17, 2017 14:02
Worker QObjects - QThread::quit() while there are events in the event queue test
#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(); }
};
@micjabbour
micjabbour / main.cpp
Created February 17, 2017 17:04
Tcp Socket with heartbeats
#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,
@micjabbour
micjabbour / main.cpp
Last active April 13, 2017 16:24
Some useful QThread utilities, that can be used to call functors in other threads (and possibly grab their return value)
#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:
@micjabbour
micjabbour / datastream_enum.h
Last active February 24, 2023 11:27
(de)serialize enums into QDataStream
#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>
@micjabbour
micjabbour / main.cpp
Last active October 25, 2021 01:45
C++ time profiling example
//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
@micjabbour
micjabbour / main.cpp
Last active March 21, 2021 22:31
C++ healp-allocated memory usage measurement example
//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);
@micjabbour
micjabbour / receiver_main.cpp
Created September 16, 2017 12:58
toying around with QDataStream and QVariant serialization
#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;
@micjabbour
micjabbour / main.cpp
Last active October 17, 2017 14:57
QSqlQueryModel eager loading example
//based on https://doc.qt.io/qt-5/qsqlquerymodel.html#fetchMore
#include <QtWidgets>
#include <QtSql>
#include <type_traits>
//comment out the next line to disable eager loading
#define QSQLMODEL_EAGER_LODING 1
//a class template that can be used to disable lazy loading
//in any QAbstractTableModel subclass
@micjabbour
micjabbour / main.cpp
Last active November 5, 2023 16:02
C++ WinAPI - get first physical drive serial number
#include <windows.h>
#include <memory>
#include <string>
//returns the serial number of the first physical drive in a std::string or an empty std::string in case of failure
//based on http://codexpert.ro/blog/2013/10/26/get-physical-drive-serial-number-part-1/
std::string getFirstHddSerialNumber() {
//get a handle to the first physical drive
HANDLE h = CreateFileW(L"\\\\.\\PhysicalDrive0", 0, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
if(h == INVALID_HANDLE_VALUE) return {};