Skip to content
Snippets Groups Projects
Commit b630c760 authored by Cyrille Berger's avatar Cyrille Berger
Browse files

add contains function that also check the datatype

parent f3e4e9f8
No related branches found
No related tags found
No related merge requests found
......@@ -60,6 +60,33 @@ bool ValueHash::contains(const QString& _key) const
return d->values.contains(_key);
}
bool ValueHash::contains(const QString& _key, const knowCore::Uri& _datatype) const
{
auto it = d->values.find(_key);
if(it != d->values.end())
{
return it.value().canConvert(_datatype);
} else {
return false;
}
}
knowCore::ReturnVoid ValueHash::ensureContains(const QString& _key, const knowCore::Uri& _datatype) const
{
auto it = d->values.find(_key);
if(it != d->values.end())
{
if(it.value().canConvert(_datatype))
{
return kCrvSuccess();
} else {
return kCrvError("Cannot convert '{}' to '{}'", it.value(), _datatype);
}
} else {
return kCrvError("Missing '{}' in value hash.", _key);
}
}
bool ValueHash::operator==(const ValueHash& _rhs) const
{
return d->values == _rhs.d->values;
......
......@@ -48,6 +48,24 @@ namespace knowCore
* @return if the value is contained in the hash.
*/
bool contains(const QString& _key) const;
/**
* @return if the value is contained in the hash and has the given \p _datatype.
*/
bool contains(const QString& _key, const knowCore::Uri& _datatype) const;
/**
* @return if the value is contained in the hash and is of type \p _T_.
*/
template<typename _T_>
bool contains(const QString& _key) const;
/**
* @return if the value is contained in the hash and has the given \p _datatype.
*/
knowCore::ReturnVoid ensureContains(const QString& _key, const knowCore::Uri& _datatype) const;
/**
* @return if the value is contained in the hash and is of type \p _T_.
*/
template<typename _T_>
knowCore::ReturnVoid ensureContains(const QString& _key) const;
/**
* @return attempt to return as a hash of a specific type @arg _T_ if they are convertible to @arg _T_
*/
......@@ -156,6 +174,17 @@ namespace knowCore
struct Private;
QSharedDataPointer<Private> d;
};
template<typename _T_>
bool ValueHash::contains(const QString& _key) const
{
return contains(_key, MetaTypeInformation<_T_>::uri());
}
template<typename _T_>
knowCore::ReturnVoid ValueHash::ensureContains(const QString& _key) const
{
return ensureContains(_key, MetaTypeInformation<_T_>::uri());
}
inline ReturnValue<ValueHash> ValueHash::fromVariantMap(const QVariantMap& _map)
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment