Files
pilotclient/src/blackmisc/valuemap.h
Mathew Sutcliffe db4c05dd9f refs #84 removed the CValueObject::compare method and added a friend function BlackMisc::compare to replace it.
The new compare is implemented using "multimethods" described in the book Advanced C++ Programming Styles and Idioms by James Coplien.

First, the isA method is used to determine which of the values being compared is the most general. (For example, CLength is more general than CAltitude.)
Then the compareImpl method is called on the most general value, with the other value as an argument.
If there is not a direct inheritance relation between the two values (or they are the same class) then the comparison is invalid and a assert is triggered.
2014-01-17 01:38:27 +00:00

166 lines
3.7 KiB
C++

/* Copyright (C) 2013 VATSIM Community / authors
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*!
\file
*/
#include "valueobject.h"
#include <QVariantMap>
#include <QDBusArgument>
#ifndef BLACKMISC_VALUEMAP_H
#define BLACKMISC_VALUEMAP_H
namespace BlackMisc
{
/*!
* \brief Value map
*/
class CValueMap : public CValueObject
{
public:
/*!
* \brief Constructor
* \param wildcard when used in search, for setting values irrelevant
*/
CValueMap(bool wildcard = false);
/*!
* \brief Single value constructor
* \param index
* \param value
*/
CValueMap(int index, const QVariant &value);
/*!
* \brief QVariant, required for DBus QVariant lists
* \return
*/
virtual QVariant toQVariant() const
{
return QVariant::fromValue(*this);
}
/*!
* \brief Destructor
*/
virtual ~CValueMap() {}
/*!
* \brief Add a value
* \param index
* \param value
*/
void addValue(int index, const QVariant &value);
/*!
* \brief Add a value as non QVariant
* \param index
* \param value
*/
template<class T> void addValue(int index, const T &value)
{
this->m_values.insert(index, QVariant::fromValue(value));
}
/*!
* \brief Is empty
* \return
*/
bool isEmpty() const { return this->m_values.isEmpty(); }
/*!
* \brief Value
* \param index
* \return
*/
QVariant value(int index) const { return this->m_values.value(index); }
/*!
* \brief Indexes
* \return
*/
QList<int> indexes() const { return this->m_values.keys(); }
/*!
* \brief values
* \return
*/
QList<QVariant> values() const { return this->m_values.values(); }
/*!
* \brief Wildcard, only relevant when used in search
* \return
*/
bool isWildcard() const { return this->m_wildcard; }
/*!
* \brief clear
*/
void clear() { this->m_values.clear(); }
/*!
* \brief Map
* \return
*/
const QMap<int, QVariant> &map() const { return this->m_values; }
/*!
* \brief Value hash
*/
virtual uint getValueHash() const;
/*!
* \brief Metadata
*/
static void registerMetadata();
protected:
QMap<int, QVariant> m_values; /*!< values */
bool m_wildcard;
/*!
* \brief Meaningful string representation
* \param i18n
* \return
*/
virtual QString convertToQString(bool i18n = false) const;
/*!
* \copydoc CValueObject::getMetaTypeId
*/
virtual int getMetaTypeId() const;
/*!
* \copydoc CValueObject::isA
*/
virtual bool isA(int metaTypeId) const;
/*!
* \copydoc CValueObject::compareImpl
*/
virtual int compareImpl(const CValueObject &other) const;
/*!
* \brief Stream to DBus <<
* \param argument
*/
virtual void marshallToDbus(QDBusArgument &argument) const;
/*!
* \brief Stream from DBus >>
* \param argument
*/
virtual void unmarshallFromDbus(const QDBusArgument &argument);
};
}
Q_DECLARE_METATYPE(BlackMisc::CValueMap)
#endif // guard