diff --git a/src/blackmisc/network/ecosystem.cpp b/src/blackmisc/network/ecosystem.cpp index 711e2e640..98d747ce2 100644 --- a/src/blackmisc/network/ecosystem.cpp +++ b/src/blackmisc/network/ecosystem.cpp @@ -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); diff --git a/src/blackmisc/network/ecosystem.h b/src/blackmisc/network/ecosystem.h index 671ea1cef..93252efd4 100644 --- a/src/blackmisc/network/ecosystem.h +++ b/src/blackmisc/network/ecosystem.h @@ -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(); diff --git a/src/blackmisc/network/ecosystemprovider.cpp b/src/blackmisc/network/ecosystemprovider.cpp new file mode 100644 index 000000000..d9f6aa5c5 --- /dev/null +++ b/src/blackmisc/network/ecosystemprovider.cpp @@ -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 + +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 diff --git a/src/blackmisc/network/ecosystemprovider.h b/src/blackmisc/network/ecosystemprovider.h new file mode 100644 index 000000000..5c0a1dd31 --- /dev/null +++ b/src/blackmisc/network/ecosystemprovider.h @@ -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 + +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 + { + 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