Jason H
2018-12-05 16:41:12 UTC
I've been doing a lot of Q_PROPERTY stuff again, and I waste too much time writing boiler plate code. Last time, I was reminded of the MEMBER modifier, which is what I thought I wanted. But after having worked with it some more, it's not what I want.
Given:
Q_PROPERTY (qreal scale READ scale WRITE setScale NOTIFY scaleChanged) // What I end up writing
Q_PROPERTY (qreal scale MEMBER _scale NOTIFY) // What I want to write
I want the member form to also declare/implement:
public:
qreal scale() { return _scale; }
void setScale(qreal scale) { if (scale != scale) { _scale = scale; emit scaleChanged(scale); } }
signal:
void scaleChanged(qreal scale);
Where T=type, N=name M=member
public:
T N() { return M; }
void setN(T N) { if (M != N) { M = N; emit NChanged(N); } }
signal:
void NChanged(T N);
I'm trying to think of how to do this, and this seems doable:
class X {
...
INCLUDE_AUTOMOC_DECLARATIONS
};
Where
INCLUDE_AUTOMOC_DECLARATIONS expands to:
#include "filename_X_automoc.h"
Where MOC has written the declarations. Similarly, there can be one for implementations as well.
Ideally though, all I should need to write:
Q_PROPERTY (qreal scale NOTIFY)
Epanding to:
private:
T _N;
public:
T N() { return _N; }
void setN(T N) { if (_N != N) { M = N; emit NChanged(N); } }
signal:
void NChanged(T N);
I know this might sound trivial but if I'm making 5 classes each with 10 properties, that's 1500 lines of boilerplate code that I'm writing (with code style applied).
Is there any way to get closer to my ideal?
Given:
Q_PROPERTY (qreal scale READ scale WRITE setScale NOTIFY scaleChanged) // What I end up writing
Q_PROPERTY (qreal scale MEMBER _scale NOTIFY) // What I want to write
I want the member form to also declare/implement:
public:
qreal scale() { return _scale; }
void setScale(qreal scale) { if (scale != scale) { _scale = scale; emit scaleChanged(scale); } }
signal:
void scaleChanged(qreal scale);
Where T=type, N=name M=member
public:
T N() { return M; }
void setN(T N) { if (M != N) { M = N; emit NChanged(N); } }
signal:
void NChanged(T N);
I'm trying to think of how to do this, and this seems doable:
class X {
...
INCLUDE_AUTOMOC_DECLARATIONS
};
Where
INCLUDE_AUTOMOC_DECLARATIONS expands to:
#include "filename_X_automoc.h"
Where MOC has written the declarations. Similarly, there can be one for implementations as well.
Ideally though, all I should need to write:
Q_PROPERTY (qreal scale NOTIFY)
Epanding to:
private:
T _N;
public:
T N() { return _N; }
void setN(T N) { if (_N != N) { M = N; emit NChanged(N); } }
signal:
void NChanged(T N);
I know this might sound trivial but if I'm making 5 classes each with 10 properties, that's 1500 lines of boilerplate code that I'm writing (with code style applied).
Is there any way to get closer to my ideal?