Skip to content

Commit

Permalink
Qt enums with different bitsizes
Browse files Browse the repository at this point in the history
  • Loading branch information
beckdave committed Jan 4, 2024
1 parent e90c7ba commit a353f4d
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 25 deletions.
10 changes: 10 additions & 0 deletions inc/finalmq/metadata/MetaEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ class SYMBOLEXP MetaEnum
return m_entries.size();
}

const std::string& getProperty(const std::string& key, const std::string& defaultValue = {}) const
{
const auto it = m_properties.find(key);
if (it != m_properties.end())
{
return it->second;
}
return defaultValue;
}

private:
static std::unordered_map<std::string, std::string> generateProperties(const std::vector<std::string>& attrs);

Expand Down
10 changes: 10 additions & 0 deletions inc/finalmq/metadata/MetaField.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ class MetaField
}
}

const std::string& getProperty(const std::string& key, const std::string& defaultValue = {}) const
{
const auto it = properties.find(key);
if (it != properties.end())
{
return it->second;
}
return defaultValue;
}

const MetaTypeId typeId; ///< type id of the parameter
const std::string typeName; ///< is needed for struct and enum
const std::string typeNameWithoutNamespace; ///< is the typeName, but without the namespace
Expand Down
10 changes: 10 additions & 0 deletions inc/finalmq/metadata/MetaStruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ class SYMBOLEXP MetaStruct
return m_fields.size();
}

const std::string& getProperty(const std::string& key, const std::string& defaultValue = {}) const
{
const auto it = m_properties.find(key);
if (it != m_properties.end())
{
return it->second;
}
return defaultValue;
}

private:
static std::unordered_map<std::string, std::string> generateProperties(const std::vector<std::string>& attrs);

Expand Down
80 changes: 72 additions & 8 deletions src/serializeqt/ParserQt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ namespace finalmq {
return res;
}

static const std::string QT_ENUM_BITS = "qtenumbits";
static const std::string BITS_8 = "8";
static const std::string BITS_16 = "16";
static const std::string BITS_32 = "32";

bool ParserQt::parseStructIntern(const MetaStruct& stru, bool wrappedByQVariant)
{
Expand Down Expand Up @@ -328,11 +332,39 @@ namespace finalmq {
}
if (ok)
{
std::int32_t value = 0;
ok = parse(value);
if (ok)
const MetaEnum* en = MetaDataGlobal::instance().getEnum(*field);
if (en)
{
m_visitor.enterEnum(*field, value);
const std::string& bits = en->getProperty(QT_ENUM_BITS, BITS_32);
std::int32_t value = 0;
if (bits == BITS_32)
{
ok = parse(value);
}
else if (bits == BITS_8)
{
std::int8_t value8;
ok = parse(value8);
value = static_cast<std::int32_t>(value8);
}
else if (bits == BITS_16)
{
std::int16_t value16;
ok = parse(value16);
value = static_cast<std::int32_t>(value16);
}
else
{
ok = false;
}
if (ok)
{
m_visitor.enterEnum(*field, value);
}
}
else
{
ok = false;
}
}
break;
Expand Down Expand Up @@ -533,11 +565,43 @@ namespace finalmq {
}
if (ok)
{
std::vector<std::int32_t> value;
ok = parse(value);
if (ok)
const MetaEnum* en = MetaDataGlobal::instance().getEnum(*field);
if (en)
{
const std::string& bits = en->getProperty(QT_ENUM_BITS, BITS_32);
std::vector<std::int32_t> value;
if (bits == BITS_32)
{
ok = parse(value);
}
else if (bits == BITS_8)
{
std::vector<std::int8_t> value8;
ok = parse(value);
value.resize(value8.size());
for (size_t i = 0; i < value8.size(); ++i)
{
value[i] = static_cast<std::int32_t>(value8[i]);
}
}
else if (bits == BITS_16)
{
std::vector<std::int16_t> value16;
ok = parse(value);
value.resize(value16.size());
for (size_t i = 0; i < value16.size(); ++i)
{
value[i] = static_cast<std::int32_t>(value16[i]);
}
}
if (ok)
{
m_visitor.enterArrayEnum(*field, std::move(value));
}
}
else
{
m_visitor.enterArrayEnum(*field, std::move(value));
ok = false;
}
}
break;
Expand Down
64 changes: 47 additions & 17 deletions src/serializeqt/SerializerQt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ namespace finalmq {
serialize(value, size);
}

static const std::string QT_ENUM_BITS = "qtenumbits";
static const std::string BITS_8 = "8";
static const std::string BITS_16 = "16";
static const std::string BITS_32 = "32";

void SerializerQt::Internal::enterEnum(const MetaField& field, std::int32_t value)
{
assert(field.typeId == MetaTypeId::TYPE_ENUM);
Expand All @@ -287,7 +292,19 @@ namespace finalmq {
serializeQVariantHeader(field);
}
reserveSpace(sizeof(std::int32_t));
serialize(value);
const std::string& bits = field.getProperty(QT_ENUM_BITS, BITS_32);
if (bits == BITS_8)
{
serialize(static_cast<std::int8_t>(value));
}
else if (bits == BITS_16)
{
serialize(static_cast<std::int16_t>(value));
}
else
{
serialize(value);
}
}

void SerializerQt::Internal::enterEnum(const MetaField& field, std::string&& value)
Expand Down Expand Up @@ -517,7 +534,32 @@ namespace finalmq {
serializeQVariantHeader(field);
}
reserveSpace(sizeof(std::int32_t) + size * sizeof(std::int32_t));
serialize(value, size);

const std::string& bits = field.getProperty(QT_ENUM_BITS, BITS_32);
if (bits == BITS_8)
{
std::vector<std::int8_t> value8;
value8.resize(size);
for (ssize_t i = 0; i < size; ++i)
{
value8[i] = static_cast<std::int8_t>(value[i]);
}
serialize(value8.data(), size);
}
else if (bits == BITS_16)
{
std::vector<std::int16_t> value16;
value16.resize(size);
for (ssize_t i = 0; i < size; ++i)
{
value16[i] = static_cast<std::int16_t>(value[i]);
}
serialize(value16.data(), size);
}
else
{
serialize(value, size);
}
}

void SerializerQt::Internal::enterArrayEnumMove(const MetaField& field, std::vector<std::string>&& value)
Expand Down Expand Up @@ -925,11 +967,7 @@ namespace finalmq {
typeName.clear();

static const std::string KEY_QTTYPE = "qttype";
const auto it = field.properties.find(KEY_QTTYPE);
if (it != field.properties.end())
{
typeName = it->second;
}
typeName = field.getProperty(KEY_QTTYPE);

if (typeName.empty())
{
Expand All @@ -938,23 +976,15 @@ namespace finalmq {
const MetaStruct* stru = MetaDataGlobal::instance().getStruct(field);
if (stru)
{
const auto it = stru->getProperties().find(KEY_QTTYPE);
if (it != stru->getProperties().end())
{
typeName = it->second;
}
typeName = stru->getProperty(KEY_QTTYPE);
}
}
else if (field.typeId == MetaTypeId::TYPE_ENUM)
{
const MetaEnum* enu = MetaDataGlobal::instance().getEnum(field);
if (enu)
{
const auto it = enu->getProperties().find(KEY_QTTYPE);
if (it != enu->getProperties().end())
{
typeName = it->second;
}
typeName = enu->getProperty(KEY_QTTYPE);
}
}
}
Expand Down

0 comments on commit a353f4d

Please sign in to comment.