A reimplementation of propagate_const from the Library Fundamentals TS.
Qt-based applications tend to use quite a lot of (raw) pointers as
class data members. This is just... habits, I guess; Qt encourages
this particular style. For instance
class SomeWidget : public QWidget {
QLineEdit *m_lineEdit;
QComboBox *m_comboBox;
...
};
But why pointers, rather than non-pointers? That is, why not doing
class SomeWidget : public QWidget {
QLineEdit m_lineEdit;
QComboBox m_comboBox;
...
};
?
A reason is lower the #include dependencies for SomeWidget.h (can just
forward declare), so better compile times. Another is that ownership of
QObjects is non-obvious; some of the objects may be owned by something
else, so we shouldn't destroy them directly when we destroy *this.
(Another one, that doesn't apply very often, is that some of the members
may be only optionally set.)
There is at least one _design_ difference between the pointers vs
non-pointers scenario: const correctness. If your SomeWidget has a
method
void update() const;
then you cannot modify the data members (if they're not pointers), but
you can happily do
void SomeWidget::update() const {
m_lineEdit->setText("hello");
}
which violates const correctness. This could be deliberate (but that
sounds silly); more likely, this will happen by accident -- you're in
a const method, and you end up doing something which mutates the
observable state of *this by manipulating a pointee.
Why does the above compile? What's const in there is the pointer itself,
not the object it points to; and pointers do not deep propagate
constness (they're "shallow const").
This is a almost universally bad situation
.
===
Linters aside, how can we prevent this sort of mistakes?
Enter propagate_const, a drop-in replacement for raw pointers as well as
smart pointers (which also suffer from the same issue), that deeply
propagates const:
class SomeWidget : public QWidget {
propagate_const<QLineEdit *> m_lineEdit;
propagate_const<QComboBox *> m_comboBox;
...
};
With this in place, we get back const correctness:
void SomeWidget::update() const {
// ERROR: m_lineEdit-> will yield a `const QLineEdit *`
m_lineEdit->setText("hello");
}
propagate_const has been proposed for the C++ Extensions for Library
Fundamentals version 3. Unfortunately
1) MSVC does not implement it,
2) both GCC and Clang have bugs in their implementations that make
them unsuitable for Qt development.
So here's a new reimplementation that works around those bugs.
Change-Id: Ief2051ba8f2ed8aa241359a2e75ecc9440ba2a60
Reviewed-on: https://codereview.kdab.com/c/kdab/KDToolBox/+/117692
Tested-by: Continuous Integration <build@kdab.com>
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
This is some code I have had in my Qt build for a while now.
Given the barrier to put this into QtCore is too high, maybe
it's useful to publish elsewhere, so here it is.
Change-Id: I519d360920539ee2048536b4e4eeb706fe03d64f
Reviewed-on: https://codereview.kdab.com/c/kdab/KDToolBox/+/94929
Tested-by: Continuous Integration <build@kdab.com>
Reviewed-by: Ivan Čukić <ivan.cukic@kdab.com>
DuplicateTracker transparently wraps std::pmr::unordered_map with a
std::pmr::monotonic_memory_resource in a non-copyable, non-movable
class with a narrowly-focussed API (basically, hasSeen()). If C++17
<memory_resource> isn't available, it falls back to the default
std::allocator.
This is the third iteration of this class. The first was
QDuplicateTracker in Qt, which suffers from too small a static buffer,
and a very restricted API.
The second iteration was developed in a customer project and got the
size of the static buffer (almost) right, as well as adding contains()
and set(). The latter was made possible because, unlike
QDuplicateTracker, this version always used std::unordered_set, just
with different allocators, so the interface and semantics of
decltype(set()) no longer varied wildly between C++17 and C++14
builds.
In this third iteration, I factored the preallocated storage into a
separate base class, to allow more implementation sharing between
DuplicateTracker<T, N>s with same T, but different N. It also adds
template arguments for the Hash and Equal template arguments (which is
important when you don't, yet, use Qt 5.15, because then you might
want to pass KDToolBox::QtHasher). Lastly, it exposes the
unordered_set(N) ctor, so we can control the bucket_count() already at
construction time, which means we no longer waste a default-sized
bucket list allocated from the monotonic_buffer_resource when
default-constructing a DuplicateTracker and then calling reserve().
Change-Id: Iaf7bd1d2291796e05beb36fef7172aed0c3e6c1f
Reviewed-on: https://codereview.kdab.com/c/kdab/KDToolBox/+/88299
Tested-by: Continuous Integration <build@kdab.com>
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
This is a back-port of C++20's Uniform Container Erasure, for vector
and the associative containers. It's easy to extend to all the other
container types, too, though, both in the implementation as well as in
the test.
This also lays the framework for more such components in the future
(k20::span?). See the README.md for more details.
Change-Id: I6d3d216c2d14910d09fa7438e97fe3ff6e3fe97a
Reviewed-on: https://codereview.kdab.com/c/kdab/KDToolBox/+/85864
Tested-by: Continuous Integration <build@kdab.com>
Reviewed-by: Milian Wolff <milian.wolff@kdab.com>