Moving things from the application specific DatabaseWindow to generic LMainWindow (Leon framework)
To achieve flexibility the getDatabase call on the context which was application specific to has been replaced with a type based object registry.
This commit is contained in:
parent
4a78330153
commit
fd603a7434
5 changed files with 137 additions and 99 deletions
|
|
@ -2,7 +2,9 @@
|
|||
#define IPLUGINCONTENTWIDGETCONTEXT_H
|
||||
|
||||
#include <QString>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <typeindex>
|
||||
#include "plugin_support/ModuleActionParameters.h"
|
||||
|
||||
class OpenDatabase;
|
||||
|
|
@ -13,6 +15,9 @@ class PluginContentWidget;
|
|||
*
|
||||
* It provides interface for operating on the context without needing to many details.
|
||||
* Actual default implementation is in PluginContentWidgetContextBase.
|
||||
*
|
||||
* objectRegistry implementation is from Igor Tandetnik answer to the following question
|
||||
* https://stackoverflow.com/questions/35413745/using-shared-ptr-with-a-generic-registry-or-shared-object-storage-or-not
|
||||
*/
|
||||
class IPluginContentWidgetContext {
|
||||
public:
|
||||
|
|
@ -32,11 +37,6 @@ public:
|
|||
*/
|
||||
virtual void setIcon(PluginContentWidget *content, const QString &iconname) = 0;
|
||||
|
||||
/** Returns an OpenDatabase object the widget can use to access
|
||||
* the database.
|
||||
*/
|
||||
virtual std::shared_ptr<OpenDatabase> getDatabase() = 0;
|
||||
|
||||
virtual void showStatusMessage(const QString &msg) = 0;
|
||||
|
||||
virtual void moduleAction(
|
||||
|
|
@ -46,6 +46,45 @@ public:
|
|||
) = 0;
|
||||
|
||||
virtual void addContentWidget(PluginContentWidget *widget) = 0;
|
||||
|
||||
template<typename T, class...Args>
|
||||
bool addObjects(Args&&...args);
|
||||
template<typename T>
|
||||
bool registerObject(std::shared_ptr<T> object);
|
||||
template<typename T>
|
||||
std::shared_ptr<T> getObject() const;
|
||||
private:
|
||||
std::map<std::type_index, std::shared_ptr<void> > m_objectRegistry;
|
||||
|
||||
};
|
||||
|
||||
template<typename T, class...Args>
|
||||
bool IPluginContentWidgetContext::addObjects(Args&&...args)
|
||||
{
|
||||
std::type_index key(typeid(T));
|
||||
if (!m_objectRegistry.count(key)) {
|
||||
auto p = std::make_shared<T>(std::forward<Args>(args)...);
|
||||
m_objectRegistry[key] = p;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool IPluginContentWidgetContext::registerObject(std::shared_ptr<T> object)
|
||||
{
|
||||
return m_objectRegistry.emplace(std::type_index(typeid(T)), object).second;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
std::shared_ptr<T> IPluginContentWidgetContext::getObject() const
|
||||
{
|
||||
auto it = m_objectRegistry.find(typeid(T));
|
||||
if (it == m_objectRegistry.end()) {
|
||||
return {};
|
||||
}
|
||||
return std::static_pointer_cast<T>(it->second);
|
||||
}
|
||||
|
||||
|
||||
#endif // IPLUGINCONTENTWIDGETCONTEXT_H
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue