Ref T89, provider for ecosystem info

Also Ref T259, Ref T243
This commit is contained in:
Klaus Basan
2018-03-12 18:07:05 +01:00
parent 55534cbb09
commit 63cff1853e
4 changed files with 175 additions and 0 deletions

View File

@@ -22,6 +22,12 @@ namespace BlackMisc
return this->getSystemString();
}
const CEcosystem &CEcosystem::unspecified()
{
static const CEcosystem e(Unspecified);
return e;
}
const CEcosystem &CEcosystem::vatsim()
{
static const CEcosystem e(VATSIM);

View File

@@ -82,6 +82,9 @@ namespace BlackMisc
//! \copydoc BlackMisc::Mixin::String::toQString
QString convertToQString(bool i18n = false) const;
//! Unspecified
static const CEcosystem &unspecified();
//! VATSIM eco system
static const CEcosystem &vatsim();

View File

@@ -0,0 +1,79 @@
/* Copyright (C) 2018
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "ecosystemprovider.h"
#include <QReadLocker>
namespace BlackMisc
{
namespace Network
{
CEcosystem IEcosystemProvider::getCurrentEcosystem() const
{
QReadLocker l(&m_lockSystem);
return m_currentEcoSystem;
}
CEcosystem IEcosystemProvider::getLastEcosystem() const
{
QReadLocker l(&m_lockSystem);
return m_lastEcoSystem;
}
bool IEcosystemProvider::isCurrentEcosystem(const CEcosystem &system) const
{
return this->getCurrentEcosystem() == system;
}
bool IEcosystemProvider::isLastEcosystem(const CEcosystem &system) const
{
return this->getLastEcosystem() == system;
}
bool IEcosystemProvider::setCurrentEcosystem(const CEcosystem &ecosystem)
{
if (this->isCurrentEcosystem(ecosystem)) { return false; }
QReadLocker l(&m_lockSystem);
m_currentEcoSystem = ecosystem;
return true;
}
bool IEcosystemProvider::setLastEcosystem(const CEcosystem &ecosystem)
{
if (this->isLastEcosystem(ecosystem)) { return false; }
QReadLocker l(&m_lockSystem);
m_lastEcoSystem = ecosystem;
return true;
}
CEcosystem CEcosystemAware::getCurrentEcosystem() const
{
if (!this->hasProvider()) { return CEcosystem::unspecified(); }
return this->provider()->getCurrentEcosystem();
}
CEcosystem CEcosystemAware::getLastEcosystem() const
{
if (!this->hasProvider()) { return CEcosystem::unspecified(); }
return this->provider()->getLastEcosystem();
}
bool CEcosystemAware::isCurrentEcosystem(const CEcosystem &system) const
{
if (!this->hasProvider()) { return this->getCurrentEcosystem() == system; }
return this->provider()->isCurrentEcosystem(system);
}
bool CEcosystemAware::isLastEcosystem(const CEcosystem &system) const
{
if (!this->hasProvider()) { return this->getLastEcosystem() == system; }
return this->provider()->isLastEcosystem(system);
}
} // namespace
} // namespace

View File

@@ -0,0 +1,87 @@
/* Copyright (C) 2018
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
//! \file
#ifndef BLACKMISC_NETWORK_ECOSYSTEMPROVIDER_H
#define BLACKMISC_NETWORK_ECOSYSTEMPROVIDER_H
#include "ecosystem.h"
#include "blackmisc/provider.h"
#include <QReadWriteLock>
namespace BlackMisc
{
namespace Network
{
//! Direct threadsafe in memory access to current ecosystem
class BLACKMISC_EXPORT IEcosystemProvider : public IProvider
{
public:
//! Current ecosystem
//! \remark CEcosystem::Unspecified if not connected with any network
//! \threadsafe
CEcosystem getCurrentEcosystem() const;
//! Last known ecosystem
//! \threadsafe
CEcosystem getLastEcosystem() const;
//! Current ecosystem?
//! \threadsafe
bool isCurrentEcosystem(const CEcosystem &system) const;
//! Last known ecosystem?
//! \threadsafe
bool isLastEcosystem(const CEcosystem &system) const;
protected:
//! Set the current system
//! \threadsafe
bool setCurrentEcosystem(const CEcosystem &ecosystem);
//! Set the last known system
//! \threadsafe
bool setLastEcosystem(const CEcosystem &ecosystem);
private:
CEcosystem m_currentEcoSystem;
CEcosystem m_lastEcoSystem;
mutable QReadWriteLock m_lockSystem; //!< lock
};
//! Delegating class which can be directly used to access an \sa IEcosystemProvider instance
class BLACKMISC_EXPORT CEcosystemAware : public IProviderAware<IEcosystemProvider>
{
public:
//! Set the provider
void setEcosystemProvider(IEcosystemProvider *provider) { this->setProvider(provider); }
//! \copydoc IEcosystemProvider::getCurrentEcosystem
CEcosystem getCurrentEcosystem() const;
//! \copydoc IEcosystemProvider::getLastEcosystem
CEcosystem getLastEcosystem() const;
//! \copydoc IEcosystemProvider::isCurrentEcosystem
bool isCurrentEcosystem(const CEcosystem &system) const;
//! \copydoc IEcosystemProvider::isLastEcosystem
bool isLastEcosystem(const CEcosystem &system) const;
protected:
//! Constructor
CEcosystemAware(IEcosystemProvider *EcosystemProvider) : IProviderAware(EcosystemProvider) { Q_ASSERT(EcosystemProvider); }
};
} // namespace
} // namespace
Q_DECLARE_INTERFACE(BlackMisc::Network::IEcosystemProvider, "BlackMisc::Network::IEcosystemProvider")
#endif // guard