From 95fa5e84dfb943508f41c2e5aac357178133ee96 Mon Sep 17 00:00:00 2001 From: Roland Winklmeier Date: Sat, 26 Apr 2014 20:46:56 +0200 Subject: [PATCH] Removed IContext Network and voice samples work using them and have to allocate their own vatlib objects now. refs #221 --- samples/cli_client/client.cpp | 6 +- samples/cli_client/client.h | 3 +- samples/cli_client/main.cpp | 8 +- samples/voiceclient/client.cpp | 3 +- samples/voiceclient/main.cpp | 3 +- src/blackcore/voice.h | 1 - src/blackmisc/context.cpp | 69 ---------- src/blackmisc/context.h | 229 --------------------------------- 8 files changed, 8 insertions(+), 314 deletions(-) delete mode 100644 src/blackmisc/context.cpp delete mode 100644 src/blackmisc/context.h diff --git a/samples/cli_client/client.cpp b/samples/cli_client/client.cpp index f6fe45d7b..dcb08d333 100644 --- a/samples/cli_client/client.cpp +++ b/samples/cli_client/client.cpp @@ -4,12 +4,12 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "client.h" -#include "blackmisc/context.h" +#include "blackcore/network_vatlib.h" #include #include -Client::Client(BlackMisc::IContext &ctx) - : m_net(&ctx.getObject()) +Client::Client(QObject *parent) + : QObject(parent), m_net(new BlackCore::CNetworkVatlib(this)) { using namespace BlackCore; connect(m_net, &INetwork::atcPositionUpdate, this, &Client::atcPositionUpdate); diff --git a/samples/cli_client/client.h b/samples/cli_client/client.h index afd006574..d00675bbb 100644 --- a/samples/cli_client/client.h +++ b/samples/cli_client/client.h @@ -7,7 +7,6 @@ #define __BLACKSAMPLE_CLI_CLIENT_H__ #include "blackcore/network.h" -#include "blackmisc/context.h" #include #include #include @@ -18,7 +17,7 @@ class Client : public QObject Q_OBJECT public: - Client(BlackMisc::IContext &); + Client(QObject *parent = nullptr); signals: void quit(); diff --git a/samples/cli_client/main.cpp b/samples/cli_client/main.cpp index b9795dc4f..97aca8649 100644 --- a/samples/cli_client/main.cpp +++ b/samples/cli_client/main.cpp @@ -4,7 +4,6 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "blackcore/network_vatlib.h" -#include "blackmisc/context.h" #include "client.h" #include "reader.h" #include @@ -14,12 +13,7 @@ int main(int argc, char *argv[]) { QCoreApplication app (argc, argv); - BlackMisc::CApplicationContext ctx; - BlackMisc::IContext::setInstance(ctx); - BlackCore::CNetworkVatlib net; - ctx.setObject(net); - - Client client(ctx); + Client client(&app); LineReader reader; QObject::connect(&reader, SIGNAL(command(const QString&)), &client, SLOT(command(const QString&))); QObject::connect(&client, SIGNAL(quit()), &reader, SLOT(terminate())); diff --git a/samples/voiceclient/client.cpp b/samples/voiceclient/client.cpp index adf5f6751..b6b7f91aa 100644 --- a/samples/voiceclient/client.cpp +++ b/samples/voiceclient/client.cpp @@ -4,6 +4,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include "client.h" +#include "blackcore/voice_vatlib.h" #include "blackmisc/audiodevicelist.h" #include "blackmisc/avcallsignlist.h" @@ -16,7 +17,7 @@ using namespace BlackMisc::Aviation; */ Client::Client(QObject *parent) : QObject(parent), - m_voiceClient(&BlackMisc::IContext::getInstance().getObject()) + m_voiceClient(new BlackCore::CVoiceVatlib(this)) { using namespace BlackCore; connect(m_voiceClient, &IVoice::squelchTestFinished, this, &Client::onSquelchTestFinished); diff --git a/samples/voiceclient/main.cpp b/samples/voiceclient/main.cpp index 0497d47aa..ba117d576 100644 --- a/samples/voiceclient/main.cpp +++ b/samples/voiceclient/main.cpp @@ -16,9 +16,8 @@ using namespace BlackMisc::Audio; int main(int argc, char *argv[]) { QCoreApplication app (argc, argv); - BlackMisc::IContext::getInstance().setObject(*new BlackCore::CVoiceVatlib()); - Client client; + Client client(&app); LineReader reader; QObject::connect(&reader, SIGNAL(command(const QString &)), &client, SLOT(command(const QString &))); QObject::connect(&client, SIGNAL(quit()), &reader, SLOT(terminate())); diff --git a/src/blackcore/voice.h b/src/blackcore/voice.h index b6c8baa0c..eefe1c856 100644 --- a/src/blackcore/voice.h +++ b/src/blackcore/voice.h @@ -6,7 +6,6 @@ #ifndef BLACKCORE_VOICE_H #define BLACKCORE_VOICE_H -#include "../blackmisc/context.h" #include "../blackmisc/avcallsignlist.h" #include "../blackmisc/avselcal.h" #include "../blackmisc/nwuserlist.h" diff --git a/src/blackmisc/context.cpp b/src/blackmisc/context.cpp deleted file mode 100644 index b9b93690f..000000000 --- a/src/blackmisc/context.cpp +++ /dev/null @@ -1,69 +0,0 @@ -//! Copyright (C) 2013 Roland Winklmeier -//! 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/ - -#include "blackmisc/context.h" -#include -#include -#include - -#ifdef Q_OS_WIN -#ifndef NOMINMAX -#define NOMINMAX -#endif -#include -#endif - -namespace BlackMisc -{ - - IContext *&instancePointer() - { - static CApplicationContext context; - static IContext *ptr = &context; - return ptr; - } - - void IContext::setInstance(IContext &context) - { - instancePointer() = &context; - } - - IContext &IContext::getInstance() - { - return *instancePointer(); - } - - IContext::~IContext() - { - } - - CApplicationContext::CApplicationContext() - { - } - - const QObject *CApplicationContext::getQObjectNothrow(const QString &name) const - { - auto it = m_map.find(name); - return it == m_map.end() ? nullptr : it.value(); - } - - void CApplicationContext::setQObject(QString name, QObject &object) - { - m_map.insert(name, &object); - } - - void CApplicationContext::removeQObject(const QString &name) - { - m_map.remove(name); - } - - void CApplicationContext::setDefaultApplicationName() - { - // This part was not working before on linux. - // This class is depricated, but as long as it is not removed, if fixed it anyway. - setApplicationName(QFileInfo(QCoreApplication::applicationFilePath()).fileName()); - } - -} // namespace BlackMisc diff --git a/src/blackmisc/context.h b/src/blackmisc/context.h deleted file mode 100644 index d2b4f3d0b..000000000 --- a/src/blackmisc/context.h +++ /dev/null @@ -1,229 +0,0 @@ -//! Copyright (C) 2013 Roland Winklmeier -//! 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/ - -#ifndef BLACKMISC_CONTEXT_H -#define BLACKMISC_CONTEXT_H - -#include -#include -#include -#include - -namespace BlackMisc -{ - - /*! - * Provides centralized modular access to long-lived interface objects. - */ - class IContext - { - public: - /*! - * Returns a reference to a static global context singleton. - * \return - * \warning Do not use this in code which could be called from within a plugin. - * \deprecated Provided as a crutch to ease transition from singleton-based design. - */ - static IContext &getInstance(); - - /*! - * Allows for apps to set the static global context singleton. - * \deprecated Provided as a crutch to ease transition from singleton-based design. - */ - static void setInstance(IContext &); - - /*! - * Destructor. - */ - virtual ~IContext(); - - /*! - * Returns true if an object with the given name is present. - * You usually use the type-safe hasObject method instead. - * \param name - * \return - */ - bool hasQObject(const QString &name) - { - return getQObjectNothrow(name); - } - - /*! - * Set the object pointer with the type T. - * \tparam T A class derived from QObject using the Q_OBJECT macro. - * \param object - * \param name The name of the object; default is the name of its class, T. - */ - template void setObject(T &object, QString name = T::staticMetaObject.className()) - { - setQObject(name, object); - } - - /*! - * Remove the object pointer with the type T. - * \tparam T A class derived from QObject using the Q_OBJECT macro. - * \param name The name of the object; default is the name of its class T. - */ - template void removeObject(const QString &name = T::staticMetaObject.className()) - { - removeQObject(name); - } - - /*! - * Return an object pointer of the class T. - * \tparam T A class derived from QObject using the Q_OBJECT macro. - * \param name The name of the object; default is the name of its class, T. - * \return - * \throw std::logic_error The requested object is not present. - */ - template T &getObject(const QString &name = T::staticMetaObject.className()) - { - T *obj = qobject_cast(getQObjectNothrow(name)); - if (!obj) { throw std::logic_error("IContext: qobject_cast failed"); } - return *obj; - } - - /*! - * Return true if the requested object in present in the context. - * \tparam T A class derived from QObject using the Q_OBJECT macro. - * \param name The name of the object; default is the name of its class, T. - * \return - */ - template bool hasObject(const QString &name = T::staticMetaObject.className()) const - { - const QObject *qobj = getQObjectNothrow(name); - return qobj && qobject_cast(qobj); - } - - /*! - * Sets an object pointer, given by its name. - * You usually use the type-safe setObject method instead. - * \param object - * \param name - */ - virtual void setQObject(QString name, QObject &object) = 0; - - /*! - * Removes an object pointer, given by its name. - * You usually use the type-safe removeObject method instead. - * \param name - */ - virtual void removeQObject(const QString &name) = 0; - - /*! - * Like getQObject but returns nullptr instead of throwing an exception. - * \param name - * \return - */ - virtual const QObject *getQObjectNothrow(const QString &name) const = 0; - - /*! - * Like getQObject but returns nullptr instead of throwing an exception. - * \param name - * \return - */ - QObject *getQObjectNothrow(const QString &name) - { - return const_cast(static_cast(this)->getQObjectNothrow(name)); - } - - /*! - * Returns the pointer to an object by its name. - * You usually use the type-safe getObject method instead. - * \param name - * \return - * \throw std::logic_error The requested object is not present. - */ - QObject &getQObject(const QString &name) - { - QObject *qobj = getQObjectNothrow(name); - if (!qobj) { throw std::logic_error("IContext: named object not found"); } - return *qobj; - } - - /*! - * Set the global name of the application. - * \param appName - */ - virtual void setApplicationName(QString appName) = 0; - - /*! - * Set the application name to the default. - */ - virtual void setDefaultApplicationName() = 0; - - /*! - * Return the global name of the application. - * \return - */ - virtual const QString &getApplicationName() const = 0; - }; - - /*! - * Default implementation of the IContext interface. - */ - class CApplicationContext : public IContext - { - public: - /*! - * Constructor - */ - CApplicationContext(); - - virtual const QObject *getQObjectNothrow(const QString &) const override; - virtual void setQObject(QString, QObject &) override; - virtual void removeQObject(const QString &) override; - virtual void setApplicationName(QString appName) override { m_appName = appName; } - virtual void setDefaultApplicationName() override; - virtual const QString &getApplicationName() const override { return m_appName; } - - private: - QMap m_map; - QString m_appName; - }; - - /*! - This macros helps to create the body of a singletone class, - which registers itself with the application context. - \warning Singletons defined with this macro will not be accessible in plugins. - \deprecated Preferred way is, during application initialization, - construct would-be singletons and register them manually, - and when you want to access them, do it through the IContext. - */ -#define SINGLETON_CLASS_DECLARATION(className) \ -private:\ - /* Forbid copying */ \ - className (const className &); \ - className &operator= (const className &); \ - /* the local static pointer to the singleton instance */ \ - static className *m_instance; \ -public:\ - static className &getInstance() \ - { \ - if (m_instance == NULL) \ - { \ - /* Get the singleton object from the context, if there is one */ \ - if (BlackMisc::IContext::getInstance().hasQObject(#className)) \ - { \ - m_instance = static_cast(&BlackMisc::IContext::getInstance().getQObject(#className)); \ - } \ - else \ - { \ - /* We have no allocated object yet, so do it now. */ \ - m_instance = new className; \ - BlackMisc::IContext::getInstance().setQObject(#className, *m_instance); \ - } \ - } \ - return *m_instance; \ - } \ -private: - /* Put your class constructor directly after this makro and make sure it is private! */ - - -#define SINGLETON_CLASS_IMPLEMENTATION(className) className *className::m_instance = NULL; - -} // namespace BlackMisc - -#endif //BLACKMISC_CONTEXT_H