mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-23 15:25:35 +08:00
committed by
Mathew Sutcliffe
parent
c6da7b0d35
commit
ded6fc012e
@@ -7,7 +7,7 @@
|
||||
#define BLACKMISC_AVIOBASE_H
|
||||
|
||||
// QtGlobal is required for asserts
|
||||
#include "blackmisc/streamable.h"
|
||||
#include "blackmisc/valueobject.h"
|
||||
#include "blackmisc/pqconstants.h"
|
||||
#include <QtGlobal>
|
||||
|
||||
@@ -16,10 +16,10 @@ namespace BlackMisc
|
||||
namespace Aviation
|
||||
{
|
||||
|
||||
class CAvionicsBase : public BlackMisc::CStreamable
|
||||
/*!
|
||||
* \brief Base class for avionics
|
||||
*/
|
||||
class CAvionicsBase : public BlackMisc::CValueObject
|
||||
{
|
||||
protected:
|
||||
QString m_name; //!< name of the unit
|
||||
|
||||
@@ -55,11 +55,10 @@ namespace BlackMisc
|
||||
|
||||
};
|
||||
|
||||
|
||||
/*!
|
||||
* \brief Geodetic coordinate
|
||||
*/
|
||||
class CCoordinateGeodetic : public CStreamable
|
||||
class CCoordinateGeodetic : public CValueObject
|
||||
{
|
||||
private:
|
||||
BlackMisc::Geo::CLatitude m_latitude; //!< Latitude
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#ifndef BLACKMISC_MATHMATRIXBASE_H
|
||||
#define BLACKMISC_MATHMATRIXBASE_H
|
||||
|
||||
#include "blackmisc/streamable.h"
|
||||
#include "blackmisc/valueobject.h"
|
||||
#include "blackmisc/mathvector3dbase.h"
|
||||
#include <QGenericMatrix>
|
||||
#include <QDBusMetaType>
|
||||
@@ -19,7 +19,7 @@ namespace BlackMisc
|
||||
/*!
|
||||
* \brief Base functionality of a matrix
|
||||
*/
|
||||
template<class ImplMatrix, int Rows, int Columns> class CMatrixBase : public BlackMisc::CStreamable
|
||||
template<class ImplMatrix, int Rows, int Columns> class CMatrixBase : public BlackMisc::CValueObject
|
||||
{
|
||||
private:
|
||||
/*!
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#ifndef BLACKMISC_MATHVECTOR3DBASE_H
|
||||
#define BLACKMISC_MATHVECTOR3DBASE_H
|
||||
|
||||
#include "blackmisc/streamable.h"
|
||||
#include "blackmisc/valueobject.h"
|
||||
#include "blackmisc/mathematics.h"
|
||||
|
||||
namespace BlackMisc
|
||||
@@ -19,7 +19,7 @@ namespace BlackMisc
|
||||
/*!
|
||||
* \brief 3D vector base (x, y, z)
|
||||
*/
|
||||
template <class ImplVector> class CVector3DBase : public CStreamable
|
||||
template <class ImplVector> class CVector3DBase : public CValueObject
|
||||
{
|
||||
private:
|
||||
/*!
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
#ifndef BLACKMISC_PQBASE_H
|
||||
#define BLACKMISC_PQBASE_H
|
||||
|
||||
#include "blackmisc/streamable.h"
|
||||
#include "blackmisc/valueobject.h"
|
||||
#include "blackmisc/debug.h"
|
||||
#include "blackmisc/mathematics.h"
|
||||
#include <QCoreApplication>
|
||||
@@ -24,7 +24,7 @@ namespace BlackMisc
|
||||
/*!
|
||||
* \brief Base class for all units, such as meter, hertz.
|
||||
*/
|
||||
class CMeasurementUnit : public CStreamable
|
||||
class CMeasurementUnit : public CValueObject
|
||||
{
|
||||
protected:
|
||||
/*!
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace BlackMisc
|
||||
/*!
|
||||
* \brief A physical quantity such as "5m", "20s", "1500ft/s"
|
||||
*/
|
||||
template <class MU, class PQ> class CPhysicalQuantity : public BlackMisc::CStreamable
|
||||
template <class MU, class PQ> class CPhysicalQuantity : public BlackMisc::CValueObject
|
||||
{
|
||||
|
||||
private:
|
||||
|
||||
@@ -1,201 +0,0 @@
|
||||
#ifndef BLACKMISC_STREAMABLE_H
|
||||
#define BLACKMISC_STREAMABLE_H
|
||||
|
||||
#include "blackmisc/debug.h"
|
||||
#include <QtDBus/QDBusMetaType>
|
||||
#include <QString>
|
||||
#include <QtGlobal>
|
||||
#include <QDataStream>
|
||||
#include <QDebug>
|
||||
#include <iostream>
|
||||
|
||||
namespace BlackMisc {
|
||||
|
||||
/*!
|
||||
* \brief Base class for streamable value objects.
|
||||
* Public non-virtual interface with protected virtual implementation.
|
||||
*/
|
||||
// Virtual operators: http://stackoverflow.com/a/4571634/356726
|
||||
class CStreamable
|
||||
{
|
||||
/*!
|
||||
* \brief Stream << overload to be used in debugging messages
|
||||
* \param debug
|
||||
* \param uc
|
||||
* \return
|
||||
*/
|
||||
friend QDebug operator<<(QDebug debug, const CStreamable &uc)
|
||||
{
|
||||
debug << uc.stringForStreaming();
|
||||
return debug;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operator << based on text stream
|
||||
* \param textStream
|
||||
* \param uc
|
||||
* \return
|
||||
*/
|
||||
friend QTextStream &operator<<(QTextStream &textStream, const CStreamable &uc)
|
||||
{
|
||||
textStream << uc.stringForStreaming();
|
||||
return textStream;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operator << when there is no debug stream
|
||||
* \param nodebug
|
||||
* \param uc
|
||||
* \return
|
||||
*/
|
||||
friend QNoDebug operator<<(QNoDebug nodebug, const CStreamable & /* uc */)
|
||||
{
|
||||
return nodebug;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Stream operator << for QDataStream
|
||||
* \param stream
|
||||
* \param uc
|
||||
* \return
|
||||
*/
|
||||
friend QDataStream &operator<<(QDataStream &stream, const CStreamable &uc)
|
||||
{
|
||||
stream << uc.stringForStreaming();
|
||||
return stream;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Stream operator << for log messages
|
||||
* \param log
|
||||
* \param uc
|
||||
* \return
|
||||
*/
|
||||
friend CLogMessage operator<<(CLogMessage log, const CStreamable &uc)
|
||||
{
|
||||
log << uc.stringForStreaming();
|
||||
return log;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Stream operator << for std::cout
|
||||
* \param ostr
|
||||
* \param uc
|
||||
* \return
|
||||
*/
|
||||
friend std::ostream &operator<<(std::ostream &ostr, const CStreamable &uc)
|
||||
{
|
||||
ostr << uc.stringForStreaming().toStdString();
|
||||
return ostr;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Unmarshalling operator >>, DBus to object
|
||||
* \param argument
|
||||
* \param uc
|
||||
* \return
|
||||
*/
|
||||
friend const QDBusArgument &operator>>(const QDBusArgument &argument, CStreamable &uc)
|
||||
{
|
||||
argument.beginStructure();
|
||||
uc.unmarshallFromDbus(argument);
|
||||
argument.endStructure();
|
||||
return argument;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Marshalling operator <<, object to DBus
|
||||
* \param argument
|
||||
* \param pq
|
||||
* \return
|
||||
*/
|
||||
friend QDBusArgument &operator<<(QDBusArgument &argument, const CStreamable &uc)
|
||||
{
|
||||
argument.beginStructure();
|
||||
uc.marshallToDbus(argument);
|
||||
argument.endStructure();
|
||||
return argument;
|
||||
}
|
||||
|
||||
public:
|
||||
/*!
|
||||
* \brief Virtual destructor
|
||||
*/
|
||||
virtual ~CStreamable() {}
|
||||
|
||||
/*!
|
||||
* \brief Cast as QString
|
||||
* \bool i18n
|
||||
*/
|
||||
QString toQString(bool i18n = false) const
|
||||
{
|
||||
return this->convertToQString(i18n);
|
||||
}
|
||||
|
||||
protected:
|
||||
/*!
|
||||
* \brief Default constructor
|
||||
*/
|
||||
CStreamable() {}
|
||||
|
||||
/*!
|
||||
* \brief String for streaming operators
|
||||
* \return
|
||||
*/
|
||||
virtual QString stringForStreaming() const
|
||||
{
|
||||
// simplest default implementation requires only one method
|
||||
return this->convertToQString();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief String for QString conversion
|
||||
* \param i18n
|
||||
* \return
|
||||
*/
|
||||
virtual QString convertToQString(bool i18n = false) const = 0;
|
||||
|
||||
/*!
|
||||
* \brief Stream to DBus
|
||||
* \param argument
|
||||
*/
|
||||
virtual void marshallToDbus(QDBusArgument &) const = 0;
|
||||
|
||||
/*!
|
||||
* \brief Stream from DBus
|
||||
* \param argument
|
||||
*/
|
||||
virtual void unmarshallFromDbus(const QDBusArgument &) = 0;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Non-member non-friend operator for streaming T objects to QDBusArgument.
|
||||
* Needed because we can't rely on the friend operator in some cases due to
|
||||
* an unrelated template for streaming Container<T> in QtDBus/qdbusargument.h
|
||||
* which matches more types than it can actually handle.
|
||||
* \param argument
|
||||
* \param uc
|
||||
*/
|
||||
template <class T> typename std::enable_if<std::is_base_of<CStreamable, T>::value, QDBusArgument>::type const&
|
||||
operator>>(const QDBusArgument &argument, T &uc)
|
||||
{
|
||||
return argument >> static_cast<CStreamable&>(uc);
|
||||
}
|
||||
|
||||
/*!
|
||||
* Non-member non-friend operator for streaming T objects from QDBusArgument.
|
||||
* Needed because we can't rely on the friend operator in some cases due to
|
||||
* an unrelated template for streaming Container<T> in QtDBus/qdbusargument.h
|
||||
* which matches more types than it can actually handle.
|
||||
* \param argument
|
||||
* \param uc
|
||||
*/
|
||||
template <class T> typename std::enable_if<std::is_base_of<CStreamable, T>::value, QDBusArgument>::type&
|
||||
operator<<(QDBusArgument &argument, T &uc)
|
||||
{
|
||||
return argument << static_cast<CStreamable const&>(uc);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // guard
|
||||
67
src/blackmisc/valueobject.cpp
Normal file
67
src/blackmisc/valueobject.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
#include "valueobject.h"
|
||||
#include "valuemap.h"
|
||||
#include "blackmiscfreefunctions.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
/*
|
||||
* Constructor
|
||||
*/
|
||||
CValueObject::CValueObject() {}
|
||||
|
||||
/*
|
||||
* Stringify
|
||||
*/
|
||||
QString CValueObject::toQString(bool i18n) const
|
||||
{
|
||||
return this->convertToQString(i18n);
|
||||
}
|
||||
|
||||
/*
|
||||
* Stringify
|
||||
*/
|
||||
QString CValueObject::toFormattedQString(bool i18n) const
|
||||
{
|
||||
return this->toQString(i18n);
|
||||
}
|
||||
|
||||
/*
|
||||
* Stringify
|
||||
*/
|
||||
std::string CValueObject::toStdString(bool i18n) const
|
||||
{
|
||||
return this->convertToQString(i18n).toStdString();
|
||||
}
|
||||
|
||||
/*
|
||||
* Streaming
|
||||
*/
|
||||
QString CValueObject::stringForStreaming() const
|
||||
{
|
||||
// simplest default implementation requires only one method
|
||||
return this->convertToQString();
|
||||
}
|
||||
|
||||
/*
|
||||
* from DBus
|
||||
*/
|
||||
const QDBusArgument &operator>>(const QDBusArgument &argument, CValueObject &uc)
|
||||
{
|
||||
argument.beginStructure();
|
||||
uc.unmarshallFromDbus(argument);
|
||||
argument.endStructure();
|
||||
return argument;
|
||||
}
|
||||
|
||||
/*
|
||||
* to DBus
|
||||
*/
|
||||
QDBusArgument &operator<<(QDBusArgument &argument, const CValueObject &uc)
|
||||
{
|
||||
argument.beginStructure();
|
||||
uc.marshallToDbus(argument);
|
||||
argument.endStructure();
|
||||
return argument;
|
||||
}
|
||||
}
|
||||
209
src/blackmisc/valueobject.h
Normal file
209
src/blackmisc/valueobject.h
Normal file
@@ -0,0 +1,209 @@
|
||||
#ifndef BLACKMISC_VALUEOBJECT_H
|
||||
#define BLACKMISC_VALUEOBJECT_H
|
||||
|
||||
#include "blackmisc/debug.h"
|
||||
#include <QtDBus/QDBusMetaType>
|
||||
#include <QString>
|
||||
#include <QtGlobal>
|
||||
#include <QDataStream>
|
||||
#include <QDebug>
|
||||
#include <type_traits>
|
||||
#include <iostream>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
/*!
|
||||
* \brief Base class for value objects.
|
||||
* Public non-virtual interface with protected virtual implementation.
|
||||
*/
|
||||
class CValueObject
|
||||
{
|
||||
|
||||
/*!
|
||||
* \brief Stream << overload to be used in debugging messages
|
||||
* \param debug
|
||||
* \param uc
|
||||
* \return
|
||||
*/
|
||||
friend QDebug operator<<(QDebug debug, const CValueObject &uc)
|
||||
{
|
||||
debug << uc.stringForStreaming();
|
||||
return debug;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operator << based on text stream
|
||||
* \param textStream
|
||||
* \param uc
|
||||
* \return
|
||||
*/
|
||||
friend QTextStream &operator<<(QTextStream &textStream, const CValueObject &uc)
|
||||
{
|
||||
textStream << uc.stringForStreaming();
|
||||
return textStream;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operator << when there is no debug stream
|
||||
* \param nodebug
|
||||
* \param uc
|
||||
* \return
|
||||
*/
|
||||
friend QNoDebug operator<<(QNoDebug nodebug, const CValueObject & /* uc */)
|
||||
{
|
||||
return nodebug;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Stream operator << for QDataStream
|
||||
* \param stream
|
||||
* \param uc
|
||||
* \return
|
||||
*/
|
||||
friend QDataStream &operator<<(QDataStream &stream, const CValueObject &uc)
|
||||
{
|
||||
stream << uc.stringForStreaming();
|
||||
return stream;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Stream operator << for log messages
|
||||
* \param log
|
||||
* \param uc
|
||||
* \return
|
||||
*/
|
||||
friend CLogMessage operator<<(CLogMessage log, const CValueObject &uc)
|
||||
{
|
||||
log << uc.stringForStreaming();
|
||||
return log;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Stream operator << for std::cout
|
||||
* \param ostr
|
||||
* \param uc
|
||||
* \return
|
||||
*/
|
||||
friend std::ostream &operator<<(std::ostream &ostr, const CValueObject &uc)
|
||||
{
|
||||
ostr << uc.stringForStreaming().toStdString();
|
||||
return ostr;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Unmarshalling operator >>, DBus to object
|
||||
* \param argument
|
||||
* \param uc
|
||||
* \return
|
||||
*/
|
||||
friend const QDBusArgument &operator>>(const QDBusArgument &argument, CValueObject &uc);
|
||||
|
||||
/*!
|
||||
* \brief Marshalling operator <<, object to DBus
|
||||
* \param argument
|
||||
* \param pq
|
||||
* \return
|
||||
*/
|
||||
friend QDBusArgument &operator<<(QDBusArgument &argument, const CValueObject &uc);
|
||||
|
||||
public:
|
||||
/*!
|
||||
* \brief Virtual destructor
|
||||
*/
|
||||
virtual ~CValueObject() {}
|
||||
|
||||
/*!
|
||||
* \brief Cast as QString
|
||||
* \bool i18n
|
||||
*/
|
||||
QString toQString(bool i18n = false) const;
|
||||
|
||||
/*!
|
||||
* \brief Cast to pretty-printed QString
|
||||
* \return
|
||||
*/
|
||||
virtual QString toFormattedQString(bool i18n = false) const;
|
||||
|
||||
/*!
|
||||
* \brief To std string
|
||||
* \param i18n
|
||||
* \return
|
||||
*/
|
||||
std::string toStdString(bool i18n = false) const;
|
||||
|
||||
|
||||
protected:
|
||||
/*!
|
||||
* \brief Default constructor
|
||||
*/
|
||||
CValueObject();
|
||||
|
||||
/*!
|
||||
* \brief String for streaming operators
|
||||
* \return
|
||||
*/
|
||||
virtual QString stringForStreaming() const;
|
||||
|
||||
/*!
|
||||
* \brief String for QString conversion
|
||||
* \param i18n
|
||||
* \return
|
||||
*/
|
||||
virtual QString convertToQString(bool i18n = false) const = 0;
|
||||
|
||||
/*!
|
||||
* \brief Marshall to DBus
|
||||
* \param argument
|
||||
*/
|
||||
virtual void marshallToDbus(QDBusArgument &) const = 0;
|
||||
|
||||
/*!
|
||||
* \brief Unmarshall from DBus
|
||||
* \param argument
|
||||
*/
|
||||
virtual void unmarshallFromDbus(const QDBusArgument &) = 0;
|
||||
|
||||
};
|
||||
|
||||
/*!
|
||||
* Non-member non-friend operator for streaming T objects to QDBusArgument.
|
||||
* Needed because we can't rely on the friend operator in some cases due to
|
||||
* an unrelated template for streaming Container<T> in QtDBus/qdbusargument.h
|
||||
* which matches more types than it can actually handle.
|
||||
*
|
||||
* <a href="https://dev.vatsim-germany.org/boards/15/topics/26?r=891#message-891">Forum</a>
|
||||
* <a href="https://dev.vatsim-germany.org/boards/15/topics/26?r=865#message-865">Forum</a>
|
||||
*
|
||||
* \param argument
|
||||
* \param uc
|
||||
* \return
|
||||
*/
|
||||
template <class T> typename std::enable_if<std::is_base_of<CValueObject, T>::value, QDBusArgument>::type const &
|
||||
operator>>(const QDBusArgument &argument, T &uc)
|
||||
{
|
||||
return argument >> static_cast<CValueObject &>(uc);
|
||||
}
|
||||
|
||||
/*!
|
||||
* Non-member non-friend operator for streaming T objects from QDBusArgument.
|
||||
* Needed because we can't rely on the friend operator in some cases due to
|
||||
* an unrelated template for streaming Container<T> in QtDBus/qdbusargument.h
|
||||
* which matches more types than it can actually handle.
|
||||
*
|
||||
* <a href="https://dev.vatsim-germany.org/boards/15/topics/26?r=891#message-891">Forum</a>
|
||||
* <a href="https://dev.vatsim-germany.org/boards/15/topics/26?r=865#message-865">Forum</a>
|
||||
*
|
||||
* \param argument
|
||||
* \param uc
|
||||
* \return
|
||||
*/
|
||||
template <class T> typename std::enable_if<std::is_base_of<CValueObject, T>::value, QDBusArgument>::type &
|
||||
operator<<(QDBusArgument &argument, const T &uc)
|
||||
{
|
||||
return argument << static_cast<CValueObject const &>(uc);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // guard
|
||||
Reference in New Issue
Block a user