Skip to content

Commit

Permalink
Don´t wrap QVariant into QVariant
Browse files Browse the repository at this point in the history
  • Loading branch information
beckdave committed Apr 11, 2024
1 parent 542785a commit 53c4d0c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 13 deletions.
13 changes: 12 additions & 1 deletion inc/finalmq/metadata/MetaEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class SYMBOLEXP MetaEnum
return m_entries.size();
}

const std::string& getProperty(const std::string& key, const std::string& defaultValue = {}) 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())
Expand All @@ -79,6 +79,16 @@ class SYMBOLEXP MetaEnum
return defaultValue;
}

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

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

Expand All @@ -90,6 +100,7 @@ class SYMBOLEXP MetaEnum
std::unordered_map<int, std::shared_ptr<const MetaEnumEntry>> m_id2Entry{};
std::unordered_map<std::string, std::shared_ptr<const MetaEnumEntry>> m_name2Entry{};
std::unordered_map<std::string, std::shared_ptr<const MetaEnumEntry>> m_alias2Entry{};
const std::string EMPTY_STRING{};
};

} // namespace finalmq
13 changes: 12 additions & 1 deletion inc/finalmq/metadata/MetaField.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class MetaField
const MetaField& operator =(MetaField& rhs) = delete;
const MetaField& operator =(MetaField&& rhs) = delete;

const std::string& getProperty(const std::string& key, const std::string& defaultValue = {}) const
std::string getProperty(const std::string& key, const std::string& defaultValue) const
{
const auto it = properties.find(key);
if (it != properties.end())
Expand All @@ -114,6 +114,16 @@ class MetaField
return defaultValue;
}

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

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 Expand Up @@ -163,6 +173,7 @@ class MetaField
return properties;
}

const std::string EMPTY_STRING{};
mutable const MetaEnum* metaEnum = nullptr; ///< cache to find MetaEnum of typeName faster
mutable const MetaStruct* metaStruct = nullptr; ///< cache to find MetaStruct of typeName faster

Expand Down
13 changes: 12 additions & 1 deletion inc/finalmq/metadata/MetaStruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class SYMBOLEXP MetaStruct
return m_fields.size();
}

const std::string& getProperty(const std::string& key, const std::string& defaultValue = {}) 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())
Expand All @@ -72,6 +72,16 @@ class SYMBOLEXP MetaStruct
return defaultValue;
}

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

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

Expand All @@ -83,6 +93,7 @@ class SYMBOLEXP MetaStruct
const std::vector<std::string> m_attrs{};
const std::unordered_map<std::string, std::string> m_properties{};
std::unordered_map<std::string, std::shared_ptr<const MetaField>> m_name2Field{};
const std::string EMPTY_STRING{};
};

} // namespace finalmq
28 changes: 19 additions & 9 deletions src/serializeqt/ParserQt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ bool ParserQt::parseStruct(const std::string& typeName)
return res;
}

static const std::string KEY_QTTYPE = "qttype";
static const std::string QTTYPE_QVARIANT = "QVariant";

static const std::string QT_CODE = "qtcode";
static const std::string QT_CODE_BYTES = "bytes";

Expand Down Expand Up @@ -367,24 +370,31 @@ bool ParserQt::parseStructIntern(const MetaStruct& stru, bool wrappedByQVariant)
}
break;
case MetaTypeId::TYPE_STRUCT:
if (wrappedByQVariant)
{
ok = parseQVariantHeader(*field);
}
if (ok)
{
const MetaStruct* stru1 = MetaDataGlobal::instance().getStruct(field->typeName);
if (!stru1)
{
m_visitor.notifyError(reinterpret_cast<const char*>(m_ptr), "typename not found");
m_visitor.finished();
ok = false;
}
else
{
m_visitor.enterStruct(*field);
ok = parseStructIntern(*stru1, false);
m_visitor.exitStruct(*field);
if (wrappedByQVariant)
{
// parse QVariant header, if qttype not QVariant
const std::string& qttypeField = field->getProperty(KEY_QTTYPE);
if ((!qttypeField.empty() && (qttypeField != QTTYPE_QVARIANT)) ||
(qttypeField.empty() && (stru1->getProperty(KEY_QTTYPE) != QTTYPE_QVARIANT)))
{
ok = parseQVariantHeader(*field);
}
}
if (ok)
{
m_visitor.enterStruct(*field);
ok = parseStructIntern(*stru1, false);
m_visitor.exitStruct(*field);
}
}
}
break;
Expand Down
12 changes: 11 additions & 1 deletion src/serializeqt/SerializerQt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
namespace finalmq
{

static const std::string KEY_QTTYPE = "qttype";
static const std::string QTTYPE_QVARIANT = "QVariant";

static const std::string FIXED_ARRAY = "fixedarray";


Expand Down Expand Up @@ -92,7 +95,14 @@ void SerializerQt::Internal::enterStruct(const MetaField& field)
assert(field.typeId == MetaTypeId::TYPE_STRUCT);
if (isWrappedByQVariant())
{
serializeQVariantHeader(field);
// serialize QVariant header, if qttype not QVariant
const MetaStruct* stru = MetaDataGlobal::instance().getStruct(field);
const std::string& qttypeField = field.getProperty(KEY_QTTYPE);
if ((!qttypeField.empty() && (qttypeField != QTTYPE_QVARIANT)) ||
(qttypeField.empty() && ((stru == nullptr) || (stru->getProperty(KEY_QTTYPE) != QTTYPE_QVARIANT))))
{
serializeQVariantHeader(field);
}
}

if (levelState.arrayStructCounter >= 0)
Expand Down

0 comments on commit 53c4d0c

Please sign in to comment.