Here is my code in hpp:
template <size_t... Bits>
class TelemetryCompactDataStrategy : public TelemetryCommonDataStrategy
{
static_assert((Bits + ...) <= 32, "Bit count must not exceed 32.");
public:
static constexpr auto bitSize = (Bits + ...);
static constexpr auto dataSize = (bitSize + 7) / 8;
TelemetryCompactDataStrategy(QString... paths, QObject* parent = nullptr)
: TelemetryCommonDataStrategy({paths, ...}, dataSize, parent), m_args({paths, ...})
{
static_assert(sizeof...(Bits) == sizeof...(paths), "Number of bits and values must match.");
}
QByteArray toByteArray() override
{
uint32_t value = 0;
currentIndex = 0;
writeBits<0>(value, m_values);
QByteArray byteArray;
byteArray.append(reinterpret_cast<const char*>(&value), dataSize);
return (dataSize == m_dataSize) ? byteArray : QByteArray(m_dataSize, 0x00);
}
int validTelemetryDataCount() override { return m_args.size(); }
QVariantHash toVariantHash(QByteArray datas) override { return {}; }
protected:
void processedTelemetryValue() override
{
m_values = m_rawValues;
}
private:
std::array<QString, sizeof...(Bits)> m_args;
size_t currentIndex = 0;
template <size_t Index>
void writeBits(uint32_t& value, const QMap<QString, QVariant>& values)
{
constexpr size_t BitCount = BitsAtIndex<Index>();
if (currentIndex + BitCount > 32)
throw std::runtime_error("Bit count exceeds 32.");
const auto valueToAdd = values[m_args[Index]].toInt() & ((1 << BitCount) - 1);
value |= (valueToAdd << currentIndex);
currentIndex += BitCount;
if constexpr (Index + 1 < sizeof...(Bits))
writeBits<Index + 1>(value, values);
}
template <size_t Index>
static constexpr size_t BitsAtIndex()
{
if constexpr (Index < sizeof...(Bits))
return Bits + ... + (Index * 0); // Force evaluation of Bits at Index
else
return 0;
}
};
I want my class construct with several QString values, but when I instantiate a object like:
new TelemetryCompactDataStrategy<2, 1, 1, 2, 1>(getTelemetryDataInfo("LowBeamLight").path,
getTelemetryDataInfo("RainLight").path,
getTelemetryDataInfo("Wipers").path,
getTelemetryDataInfo("EngineIgnition").path,
getTelemetryDataInfo("TyreType").path,
this)
it seems that this does not work, how should I modify my hpp or how should I do to realise such a class to contain variety of bits like this class?
I've checked those documents on cppreference.com, It demonstrates using template<typename... Type>, I wonder if I can't use a real type like QString? this only supports templete type like func(T&& ...path)? Hope somebody save me!