deCONZ C++ API v2.6.1
|
A strong typed ZCL attribute identifier. More...
#include <zcl.h>
Public Member Functions | |
constexpr | ZclAttributeId_t () |
Default constructor, with value = 0. | |
constexpr | ZclAttributeId_t (quint16 id) |
Explicit constructor to init by value. | |
constexpr | operator quint16 () const |
Explicit quint16 conversion operator, needs static_cast<quint16>() . | |
template<class T > | |
ZclAttributeId_t (T)=delete | |
Don't allow construction via implicit integer conversion. | |
constexpr bool | operator== (ZclAttributeId_t other) const |
Equal operator, requires strong type. | |
constexpr bool | operator!= (ZclAttributeId_t other) const |
Not equal operator, requires strong type. | |
Related Symbols | |
(Note that these are not member symbols.) | |
DECONZ_DLLSPEC QDataStream & | operator<< (QDataStream &ds, ZclAttributeId_t id) |
Writes ZclAttributeId_t as quint16 to the QDataStream. | |
DECONZ_DLLSPEC QDataStream & | operator>> (QDataStream &ds, ZclAttributeId_t &id) |
Reades ZclAttributeId_t as quint16 from the QDataStream. | |
A strong typed ZCL attribute identifier.
This wrapper around quint16
data type as ZCL attribute identifier for code correctness. It helps to prevent common hard to catch programming errors. The class is very strict and enforces a type safe programming style.
Following issues where observed in the past and are hard to spot and debug.
The strong typed ZclAttributeId_t catches all those errors at compile time.
Important: Always use C++ static_cast<>
instead C style cast to catch invalid conversions at compile time. Note when using ZclAttributeId_t properly, casts aren't needed at all.
In the following example (1) and (2) do the same and (1) seems easier to reason about at first.
at2
looks verbose and ugly and it should be! When using numbers directly, it's a code smell for magic values. Searching for 0x0005
when looking for a certain attribute is not going well.
Better: Use strong typed defines or C++11 enum classes with type quint16 or the C++ literal <number>_atid
.