Permalink
Browse files

Improve custom entity loading. Plugins doesn't have to manage entitie…

…s created before they are loaded
  • Loading branch information...
feragon committed Jul 15, 2017
1 parent 9cb2673 commit b2ad53c71835f385cd703b0b437bf194f09f84f3
@@ -14,6 +14,7 @@ set(lcluascript_srcs
lclua.cpp
primitive/customentity.cpp
builders/customentity.cpp
managers/luacustomentitymanager.cpp
)
# HEADER FILES
@@ -24,6 +25,7 @@ set(lcluascript_hdrs
lclua.h
primitive/customentity.h
builders/customentity.h
managers/luacustomentitymanager.h
)
View
@@ -1,6 +1,7 @@
#include <cad/document/document.h>
#include "lclua.h"
#include <utils/timer.h>
#include <managers/luacustomentitymanager.h>
using namespace lc;
using namespace LuaIntf;
@@ -20,6 +21,11 @@ LCLua::LCLua(lua_State* L) :
_L(L),
_f_openFileDialog(nullptr) {
LuaBinding(L)
.beginClass<LuaCustomEntityManager>("LuaCustomEntityManager")
.addStaticFunction("getInstance", &LuaCustomEntityManager::getInstance)
.addFunction("registerPlugin", &LuaCustomEntityManager::registerPlugin)
.endClass();
}
void LCLua::addLuaLibs() {
View
@@ -12,6 +12,7 @@ extern "C" {
namespace LuaIntf {
LUA_USING_SHARED_PTR_TYPE(std::shared_ptr)
LUA_USING_LIST_TYPE(std::vector)
LUA_USING_LIST_TYPE(std::unordered_set)
LUA_USING_MAP_TYPE(std::map)
}
@@ -0,0 +1,42 @@
#include <cad/events/newwaitingcustomentityevent.h>
#include <cad/dochelpers/documentlist.h>
#include <cad/meta/customentitystorage.h>
#include <cad/primitive/insert.h>
#include "luacustomentitymanager.h"
lc::LuaCustomEntityManager::LuaCustomEntityManager() {
DocumentList::getInstance().newWaitingCustomEntityEvent().connect<LuaCustomEntityManager, &LuaCustomEntityManager::onNewWaitingEntity>(this);
}
lc::LuaCustomEntityManager::~LuaCustomEntityManager() {
DocumentList::getInstance().newWaitingCustomEntityEvent().disconnect<LuaCustomEntityManager, &LuaCustomEntityManager::onNewWaitingEntity>(this);
}
void lc::LuaCustomEntityManager::onNewWaitingEntity(const lc::NewWaitingCustomEntityEvent& event) {
auto block = event.insert()->displayBlock();
auto ces = std::static_pointer_cast<const lc::CustomEntityStorage>(block);
if(!ces) {
return;
}
auto it = _plugins.find(ces->pluginName());
if(it == _plugins.end()) {
return;
}
it->second(event.insert());
}
void lc::LuaCustomEntityManager::registerPlugin(const std::string& name, LuaIntf::LuaRef onNewWaitingEntityFunction) {
if(!onNewWaitingEntityFunction.isValid() || !onNewWaitingEntityFunction.isFunction()) {
return;
}
_plugins[name] = onNewWaitingEntityFunction;
for(auto entity : DocumentList::getInstance().waitingCustomEntities(name)) {
onNewWaitingEntityFunction(entity);
}
}
@@ -0,0 +1,32 @@
#pragma once
#include <lclua.h>
namespace lc {
class LuaCustomEntityManager {
public:
static LuaCustomEntityManager& getInstance() {
static LuaCustomEntityManager _instance;
return _instance;
}
LuaCustomEntityManager(LuaCustomEntityManager const&) = delete;
void operator=(LuaCustomEntityManager const&) = delete;
virtual ~LuaCustomEntityManager();
/**
* @brief Register a new plugin which handle custom entities
* @param name Name of the plugin
* @param onNewWaitingEntityFunction Function called when there are entities which needs to be recreated by the plugin
*/
void registerPlugin(const std::string& name, LuaIntf::LuaRef onNewWaitingEntityFunction);
private:
LuaCustomEntityManager();
void onNewWaitingEntity(const lc::NewWaitingCustomEntityEvent& event);
std::map<std::string, LuaIntf::LuaRef> _plugins;
};
}
View
@@ -75,6 +75,7 @@ cad/operations/blockops.cpp
cad/operations/builder.cpp
cad/primitive/customentity.cpp
cad/interface/snapconstrain.cpp
cad/dochelpers/documentlist.cpp
)
# HEADER FILES
@@ -171,6 +172,8 @@ cad/operations/blockops.h
cad/operations/builder.h
cad/base/visitor.h
cad/primitive/customentity.h
cad/events/newwaitingcustomentityevent.h
cad/dochelpers/documentlist.h
)
# LOG4CXX
@@ -50,7 +50,10 @@ void DocumentImpl::insertEntity(const entity::CADEntity_CSPtr cadEntity) {
auto ces = std::dynamic_pointer_cast<const CustomEntityStorage>(insert->displayBlock());
if(ces != nullptr) {
_waitingCustomEntities[ces->pluginName()][insert->id()] = insert;
_waitingCustomEntities[ces->pluginName()].insert(insert);
NewWaitingCustomEntityEvent customEntityEvent(insert);
newWaitingCustomEntityEvent()(customEntityEvent);
}
}
}
@@ -64,7 +67,7 @@ void DocumentImpl::removeEntity(const entity::CADEntity_CSPtr entity) {
if(insert != nullptr && std::dynamic_pointer_cast<const entity::CustomEntity>(entity) == nullptr) {
auto ces = std::dynamic_pointer_cast<const CustomEntityStorage>(insert->displayBlock());
if(ces != nullptr) {
_waitingCustomEntities[ces->pluginName()].erase(insert->id());
_waitingCustomEntities[ces->pluginName()].erase(insert);
}
}
}
@@ -170,6 +173,6 @@ std::vector<Block_CSPtr> DocumentImpl::blocks() const {
return _storageManager->metaTypes<const Block>();
}
std::map<ID_DATATYPE, entity::Insert_CSPtr> DocumentImpl::waitingCustomEntities(const std::string& pluginName) {
std::unordered_set<entity::Insert_CSPtr> DocumentImpl::waitingCustomEntities(const std::string& pluginName) {
return _waitingCustomEntities[pluginName];
}
@@ -64,7 +64,7 @@ namespace lc {
virtual StorageManager_SPtr storageManager() const;
public:
std::map<ID_DATATYPE, entity::Insert_CSPtr> waitingCustomEntities(const std::string& pluginName) override;
std::unordered_set<entity::Insert_CSPtr> waitingCustomEntities(const std::string& pluginName) override;
std::vector<Block_CSPtr> blocks() const override;
@@ -73,7 +73,7 @@ namespace lc {
// AI am considering remove the shared_ptr from this one so we can never get a shared object from it
StorageManager_SPtr _storageManager;
std::map<std::string, std::map<ID_DATATYPE, entity::Insert_CSPtr>> _waitingCustomEntities;
std::map<std::string, std::unordered_set<entity::Insert_CSPtr>> _waitingCustomEntities;
};
}
@@ -0,0 +1,36 @@
#include "documentlist.h"
using namespace lc;
DocumentList::DocumentList() {
}
void DocumentList::addDocument(lc::Document* document) {
_documents.insert(document);
document->newWaitingCustomEntityEvent().connect<DocumentList, &DocumentList::onNewWaitingCustomEntity>(this);
}
void DocumentList::removeDocument(lc::Document* document) {
document->newWaitingCustomEntityEvent().disconnect<DocumentList, &DocumentList::onNewWaitingCustomEntity>(this);
_documents.erase(document);
}
void DocumentList::onNewWaitingCustomEntity(const NewWaitingCustomEntityEvent& event) {
_newWaitingCustomEntityEvent(event);
}
Nano::Signal<void(const NewWaitingCustomEntityEvent&)>& DocumentList::newWaitingCustomEntityEvent() {
return _newWaitingCustomEntityEvent;
}
std::unordered_set<entity::Insert_CSPtr> DocumentList::waitingCustomEntities(const std::string& pluginName) {
std::unordered_set<entity::Insert_CSPtr> result;
for(auto document : _documents) {
auto entities = document->waitingCustomEntities(pluginName);
result.insert(entities.begin(), entities.end());
}
return result;
}
@@ -0,0 +1,38 @@
#pragma once
#include <cad/document/document.h>
#include <unordered_set>
namespace lc {
class DocumentList {
friend class Document;
public:
static DocumentList& getInstance() {
static DocumentList _instance;
return _instance;
}
DocumentList(DocumentList const&) = delete;
void operator=(DocumentList const&) = delete;
/**
* @brief Event called when an unmanaged entity is added to a document
*/
Nano::Signal<void(const NewWaitingCustomEntityEvent&)>& newWaitingCustomEntityEvent();
std::unordered_set<entity::Insert_CSPtr> waitingCustomEntities(const std::string& pluginName);
private:
DocumentList();
void addDocument(Document* document);
void removeDocument(Document* document);
void onNewWaitingCustomEntity(const NewWaitingCustomEntityEvent& event);
std::unordered_set<Document*> _documents;
Nano::Signal<void(const lc::NewWaitingCustomEntityEvent&)> _newWaitingCustomEntityEvent;
};
}
@@ -1,3 +1,4 @@
#include <cad/dochelpers/documentlist.h>
#include "document.h"
#include "cad/operations/documentoperation.h"
@@ -9,6 +10,14 @@
using namespace lc;
Document::Document() {
DocumentList::getInstance().addDocument(this);
}
Document::~Document() {
DocumentList::getInstance().removeDocument(this);
}
void Document::operationStart(operation::DocumentOperation_SPtr operation) {
operation->start();
}
@@ -60,4 +69,8 @@ Nano::Signal<void(const lc::AddLinePatternEvent&)>& Document::addLinePatternEven
Nano::Signal<void(const lc::ReplaceLinePatternEvent&)>& Document::replaceLinePatternEvent() {
return this->_replaceLinePatternEvent;
}
}
Nano::Signal<void(const NewWaitingCustomEntityEvent&)>& Document::newWaitingCustomEntityEvent() {
return _newWaitingCustomEntityEvent;
}
@@ -11,6 +11,8 @@
#include <cad/events/addlayerevent.h>
#include <cad/events/removelayerevent.h>
#include <cad/events/replacelayerevent.h>
#include <cad/events/newwaitingcustomentityevent.h>
#include <unordered_set>
#include "cad/meta/dxflinepattern.h"
#include "cad/events/beginprocessevent.h"
@@ -36,8 +38,10 @@ namespace lc {
}
class Document {
public:
Document();
virtual ~Document();
/*!
* \brief begins a Process Event
*/
@@ -92,6 +96,11 @@ namespace lc {
*/
virtual Nano::Signal<void(const lc::ReplaceLinePatternEvent&)>& replaceLinePatternEvent();
/**
* @brief Event called when an unmanaged entity is added to the document
*/
virtual Nano::Signal<void(const NewWaitingCustomEntityEvent&)>& newWaitingCustomEntityEvent();
protected:
/*!
* \brief execute
@@ -214,7 +223,7 @@ namespace lc {
/**
* @return all the custom entities which aren't managed by a plugin
*/
virtual std::map<ID_DATATYPE, entity::Insert_CSPtr> waitingCustomEntities(const std::string& pluginName) = 0;
virtual std::unordered_set<entity::Insert_CSPtr> waitingCustomEntities(const std::string& pluginName) = 0;
public:
friend class lc::operation::DocumentOperation;
@@ -234,6 +243,8 @@ namespace lc {
Nano::Signal<void(const lc::AddLinePatternEvent&)> _addLinePatternEvent;
Nano::Signal<void(const lc::ReplaceLinePatternEvent&)> _replaceLinePatternEvent;
Nano::Signal<void(const lc::RemoveLinePatternEvent&)> _removeLinePatternEvent;
Nano::Signal<void(const lc::NewWaitingCustomEntityEvent&)> _newWaitingCustomEntityEvent;
};
DECLARE_SHORT_SHARED_PTR(Document);
@@ -0,0 +1,20 @@
#pragma once
#include "cad/operations/documentoperation.h"
namespace lc {
class NewWaitingCustomEntityEvent {
public:
NewWaitingCustomEntityEvent(const entity::Insert_CSPtr& insert) :
_insert(insert) {
}
const entity::Insert_CSPtr& insert() const {
return _insert;
}
private:
entity::Insert_CSPtr _insert;
};
}
View
@@ -44,6 +44,7 @@ lcviewernoqt/testselection.cpp
lckernel/meta/customentitystorage.cpp
lckernel/operations/blocksopstest.cpp
lckernel/operations/buildertest.cpp
lckernel/dochelpers/documentlist.cpp
)
set(hdrs
@@ -84,7 +85,7 @@ if(WITH_QT_UI)
ui/testcommandline.cpp
ui/testcolorselect.cpp
ui/testlinewidthselect.cpp
)
)
include_directories("${CMAKE_SOURCE_DIR}/lcUI")
include_directories("${PROJECT_BINARY_DIR}/../lcUI") #Include ui_* files
Oops, something went wrong.

0 comments on commit b2ad53c

Please sign in to comment.