refs #494 Value class CValueCachePacket is a packet of cache value changes with timestamps.

This commit is contained in:
Mathew Sutcliffe
2015-10-22 22:25:34 +01:00
parent a284de3acb
commit eb11b69c6d
6 changed files with 142 additions and 0 deletions

View File

@@ -17,6 +17,7 @@
#include "namevariantpairlist.h"
#include "variantlist.h"
#include "variantmap.h"
#include "valuecache.h"
#include "rgbcolor.h"
#include "countrylist.h"
#include "statusmessagelist.h"
@@ -71,6 +72,7 @@ void BlackMisc::registerMetadata()
CVariant::registerMetadata();
CVariantList::registerMetadata();
CVariantMap::registerMetadata();
CValueCachePacket::registerMetadata();
CPropertyIndex::registerMetadata();
CPropertyIndexList::registerMetadata();
CPropertyIndexVariantMap::registerMetadata();

View File

@@ -42,6 +42,8 @@ namespace BlackMisc
static QString stringify(double n, bool i18n) { return i18n ? QLocale().toString(n) : QString::number(n); }
//! Stringify QString
static QString stringify(QString str, bool /*i18n*/) { return str; }
//! Stringify pair
template <class A, class B> static QString stringify(const std::pair<A, B> &pair, bool i18n) { return stringify(pair.first, i18n) + ":" + stringify(pair.second, i18n); }
};
/*!

View File

@@ -123,4 +123,38 @@ const QDBusArgument &operator >>(const QDBusArgument &arg, E &value)
return arg;
}
/*!
* Operator for streaming pairs to QDBusArgument.
*/
template <class A, class B>
QDBusArgument &operator <<(QDBusArgument &arg, const std::pair<A, B> &pair)
{
arg.beginStructure();
arg << pair.first << pair.second;
arg.endStructure();
return arg;
}
/*!
* Operator for streaming pairs from QDBusArgument.
*/
template <class A, class B>
const QDBusArgument &operator >>(const QDBusArgument &arg, std::pair<A, B> &pair)
{
arg.beginStructure();
arg >> pair.first >> pair.second;
arg.endStructure();
return arg;
}
/*!
* Non member non-friend streaming for QPixmap
*/
const QDBusArgument &operator>>(const QDBusArgument &argument, QPixmap &pixmap);
/*!
* Non member non-friend streaming for QPixmap
*/
QDBusArgument &operator<<(QDBusArgument &argument, const QPixmap &pixmap);
#endif // guard

View File

@@ -89,6 +89,24 @@ const &operator>>(const QJsonValueRef &json, ENUM &value)
return json;
}
//! \brief Specialized JSON deserialization for pair
//! \ingroup JSON
template<class FIRST, class SECOND>
const QJsonValueRef &operator>>(const QJsonValueRef &json, std::pair<FIRST, SECOND> &pair)
{
json.toArray() >> pair.first >> pair.second;
return json;
}
//! \brief Specialized JSON serialization for pair
//! \ingroup JSON
template<class FIRST, class SECOND>
QJsonArray &operator<<(QJsonArray &json, const std::pair<FIRST, SECOND> &pair)
{
QJsonArray array;
return json << QJsonValue(array << pair.first << pair.second);
}
//! \name Streaming operators for QJsonArray (from value)
//! \ingroup JSON
//! @{

View File

@@ -25,6 +25,42 @@ namespace BlackMisc
bool isSafeToIncrement(const T &value) { return value < std::numeric_limits<T>::max(); }
////////////////////////////////
// CValueCachePacket
////////////////////////////////
CValueCachePacket::CValueCachePacket(const CVariantMap &values, qint64 timestamp)
{
for (auto it = values.cbegin(); it != values.cend(); ++it)
{
implementationOf(*this).insert(CDictionary::cend(), it.key(), std::make_pair(it.value(), timestamp));
}
}
void CValueCachePacket::insert(const QString &key, const CVariant &value, qint64 timestamp)
{
CDictionary::insert(key, std::make_pair(value, timestamp));
}
void CValueCachePacket::insert(const CVariantMap &values, qint64 timestamp)
{
for (auto it = values.cbegin(); it != values.cend(); ++it)
{
CDictionary::insert(it.key(), std::make_pair(it.value(), timestamp));
}
}
CVariantMap CValueCachePacket::toVariantMap() const
{
CVariantMap result;
for (auto it = cbegin(); it != cend(); ++it)
{
implementationOf(result).insert(result.cend(), it.key(), it.value());
}
return result;
}
////////////////////////////////
// CValueCache
////////////////////////////////

View File

@@ -17,6 +17,54 @@
namespace BlackMisc
{
/*!
* Value class used for signalling changed values in the cache.
*/
class BLACKMISC_EXPORT CValueCachePacket :
public CDictionary<QString, std::pair<CVariant, qint64>, QMap>,
public Mixin::MetaType<CValueCachePacket>
{
public:
//! Default constructor.
CValueCachePacket() {}
//! Construct from CVariantMap and a timestamp.
CValueCachePacket(const CVariantMap &values, qint64 timestamp);
//! Insert a key/value pair with a timestamp.
void insert(const QString &key, const CVariant &value, qint64 timestamp);
//! Insert a CVariantMap with a timestamp.
void insert(const CVariantMap &values, qint64 timestamp);
//! Insert values from another packet.
void insert(const CValueCachePacket &other) { CDictionary::insert(other); }
//! Discard timestamps and return as variant map.
CVariantMap toVariantMap() const;
//! \private Iterator behaves like a CVariantMap::const_iterator with an additional timestamp() method.
struct const_iterator : public CDictionary::const_iterator
{
using value_type = CVariant;
using pointer = const value_type *;
using reference = const value_type &;
const_iterator(CDictionary::const_iterator base) : CDictionary::const_iterator(base) {}
reference value() const { return CDictionary::const_iterator::value().first; }
reference operator *() const { return value(); }
pointer operator ->() const { return &value(); }
qint64 timestamp() const { return CDictionary::const_iterator::value().second; }
};
//! Iterators.
//! @{
const_iterator cbegin() const { return CDictionary::cbegin(); }
const_iterator cend() const { return CDictionary::cend(); }
const_iterator begin() const { return CDictionary::cbegin(); }
const_iterator end() const { return CDictionary::cend(); }
//! @}
};
/*!
* Manages a map of { QString, CVariant } pairs, which can be distributed among multiple processes.
*/
@@ -213,4 +261,6 @@ namespace BlackMisc
} // namespace
Q_DECLARE_METATYPE(BlackMisc::CValueCachePacket)
#endif