Skip to content

Instantly share code, notes, and snippets.

View jniemann66's full-sized avatar

Judd Niemann jniemann66

  • Melbourne
View GitHub Profile
@jniemann66
jniemann66 / lvalues-rvalues-N3092.md
Last active August 8, 2019 02:04
lvalues and rvalues

http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2010/n3092.pdf - 3.10

— An lvalue (so called, historically, because lvalues could appear on the left-hand side of an assignment expression) designates a function or an object. [ Example: If E is an expression of pointer type, then *E is an lvalue expression referring to the object or function to which E points. As another example, the result of calling a function whose return type is an lvalue reference is an lvalue. —end example ]

— An xvalue (an “eXpiring” value) also refers to an object, usually near the end of its lifetime (so that its resources may be moved, for example). An xvalue is the result of certain kinds of expressions involving rvalue references (8.3.2). [ Example: The result of calling a function whose return type is an rvalue reference is an xvalue. —end example ]

— A glvalue (“generalized” lvalue) is an lvalue or an xvalue.

— An rvalue (so called, historically, because rvalues could appear on the right-hand side of an assi

@jniemann66
jniemann66 / endian-swap.cpp
Created August 7, 2019 11:06
Byte swapping with built-in functions
#include <cstddef>
#include <cstdint>
// gcc >= 4.8.1
// clang >= 3.5
uint16_t a1 = 0x0102;
uint32_t a2 = 0x01020304;
uint64_t a3 = 0x0102030405060708ull;
auto b1 = __builtin_bswap16(a1);
auto b2 = __builtin_bswap32(a2);
auto b3 = __builtin_bswap64(a3);
@jniemann66
jniemann66 / icons2pixmap.cpp
Last active July 29, 2019 03:10
Qt getting standard Qt icons as QPixmaps
statusIndicator->setPixmap(style()->standardPixmap(QStyle::SP_DialogOkButton));
// see https://doc.qt.io/qt-5/qstyle.html#StandardPixmap-enum
@jniemann66
jniemann66 / foreach.md
Last active March 6, 2019 21:48
Qt foreach vs ranged for

https://www.kdab.com/goodbye-q_foreach/

Conclusion

Here’s why you’ll want to port away from Q_FOREACH, ideally to C++11 ranged for-loops:

Q_FOREACH is deprectaed since 5.7
It only works efficiently on (some) Qt containers; it performs prohibitively expensive on all std containers, QVarLengthArray, and doesn’t work at all for C arrays.
Even where it works as advertised, it typically costs ~100 bytes of text size more per loop than the C++11 ranged for-loop.

Its unconditionally taking a copy of the container makes it hard to reason about the loop.

@jniemann66
jniemann66 / convertsignalslot.cpp
Created February 6, 2019 22:51
Qt: converting to new signal / slot syntax and resolving overloads
// connect(nameCompleter_, SIGNAL(activated(QString)), this, SLOT(selected(QString)));
connect(nameCompleter_, QOverload<const QString &>::of(&QCompleter::activated), this, &ProspectFinder::selected);
@jniemann66
jniemann66 / calling-conventions-64bit.cpp
Last active November 5, 2018 11:11
64-bit Calling-conventions
#ifdef _MSVC
/* MSVC: 4 params in registers, remainder on stack) */
// x86-64 msvc (note:long = 32bit, long long = 64-bit)
long long add(int rcx, int rdx, int r8d, int r9d, int rsp40, int rsp48, int rsp56, int rsp64) {
return rcx + rdx + r8d + r9d + rsp40 + rsp48 + rsp56 + rsp64; // xmm0
}
@jniemann66
jniemann66 / qtgodbolt.md
Last active March 11, 2024 08:11
Running Qt on Godbolt compiler explorer
@jniemann66
jniemann66 / longpress.h
Last active June 4, 2020 06:01
Qt: Event Filter for Long Mouse Press (hold mouse for >= 1sec)
// LongPress: event filter Object for detecting Mouse pressed down for 1 second or more
// when triggered, it executes callback function supplied in the constructor
// usage:
/*
someButton->installEventFilter(new LongPress(this, [this] {
// do some stuff
}));
*/
@jniemann66
jniemann66 / printJson-old.cpp
Created September 23, 2018 22:47
Old printJson (using string insertion - slow)
QString printJSON(const QJsonValue& jsonValue, int maxLength, int indentLevel) {
static const QString indent{QStringLiteral(" ")};
static const QString cn{QStringLiteral(",\n")};
QString whiteSpace(indent.repeated(indentLevel));
switch(jsonValue.type()) {
case QJsonValue::Object:
{
QStringList values;
QJsonObject o = jsonValue.toObject();