3 Commits

Author SHA1 Message Date
Allen Winter
52611ec317 Standardize project to KDAB copyright policy
For individual files: use year of file creation
For entire project:
  remove year from the statement
  use the © where feasible

reference:
https://matija.suklje.name/how-and-why-to-properly-write-copyright-statements-in-your-code
2024-01-19 15:51:29 -05:00
Allen Winter
c2f948d3d5 reuse headers
Change-Id: I2d7bd3dc68dc99a89a9d0a1ad5fe50257dc2773d
Reviewed-on: https://codereview.kdab.com/c/kdab/KDToolBox/+/132633
Reviewed-by: Allen Winter <allen@kdab.com>
2023-10-22 17:10:22 +02:00
Giuseppe D'Angelo
78ea947edc Long Live propagate_const
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>
2023-01-17 16:31:46 +01:00