From 607ac3bb0458340d58b22901c789d640ed7b93da Mon Sep 17 00:00:00 2001 From: tkawata Date: Sun, 17 Jun 2012 01:42:28 +0900 Subject: [PATCH] refactored for next development phase. --- Source/DNContainerBuilder.cpp | 6 +- Source/QtScript/dnqscontainer.cpp | 164 +++++++++----------------------------- Source/QtScript/dnqscontainer.h | 14 ++-- Source/TKAxon.cpp | 4 +- Source/TKCell.cpp | 21 +++-- Source/TKCell.h | 32 ++++---- Source/TKContainer.cpp | 151 +++++++++++++++++++++++++++++++++++ Source/TKContainer.h | 28 +++++-- 8 files changed, 253 insertions(+), 167 deletions(-) diff --git a/Source/DNContainerBuilder.cpp b/Source/DNContainerBuilder.cpp index 6bc7139..9360d2e 100644 --- a/Source/DNContainerBuilder.cpp +++ b/Source/DNContainerBuilder.cpp @@ -69,7 +69,7 @@ bool DNContainerBuilder::buildContainerFromXHTML(const char* docRoot) TKCellCode *cellCode = mContainer->getCellCode(cellInfo->cellCodeClassOrType); if (cellCode) { - newCell = mContainer->createCellWithCellCodeClass(cellInfo->location, cellInfo->cellName, cellCode, cellInfo->customScript); + newCell = mContainer->addCell(cellInfo->location, cellInfo->cellName, cellCode, cellInfo->customScript); } else { @@ -80,7 +80,7 @@ bool DNContainerBuilder::buildContainerFromXHTML(const char* docRoot) } else { - newCell = mContainer->createCellWithoutCellCodeClass(cellInfo->location, cellInfo->cellName, cellInfo->cellCodeClassOrType, cellInfo->customScript); + newCell = mContainer->addCell(cellInfo->location, cellInfo->cellName, cellInfo->cellCodeClassOrType, cellInfo->customScript); } if (newCell == NULL) @@ -152,7 +152,7 @@ void DNContainerBuilder::defineCellCodeClass(const char *path, std::string name, DEBUG_TRACE("\n===== Define cell code class ===== \nClass:%s\nAPI:%s\n%s\n", fqnName.c_str(), type.c_str(), cellScript.c_str()); - mContainer->createCellCode(fqnName, type, cellScript); + mContainer->addCellCode(fqnName, type, cellScript); } void DNContainerBuilder::defineConnection(const char *path, std::string origine, std::string destination, std::string name) diff --git a/Source/QtScript/dnqscontainer.cpp b/Source/QtScript/dnqscontainer.cpp index 5cc1d16..9820b05 100644 --- a/Source/QtScript/dnqscontainer.cpp +++ b/Source/QtScript/dnqscontainer.cpp @@ -20,6 +20,9 @@ #include "TKCell.h" #include "TKCellCode.h" +#include "TKAxon.h" +#include "TKReceptor.h" +#include "TKAxonTerminal.h" #include "TKContainer.h" #include "TKLog.h" #include "DNUtils.h" @@ -32,11 +35,6 @@ #include "dnqscellcode.h" #include "DNGlobal.h" -const std::string DNQSContainer::CELLTYPE_JSBASIC = "B"; -const std::string DNQSContainer::CELLTYPE_OUT = "O"; -const std::string DNQSContainer::CELLTYPE_IN = "I"; -const std::string DNQSContainer::CELLTYPE_BASICSTORAGE = "BS"; - static QScriptValue scriptPrint(QScriptContext *context, QScriptEngine *engine) { if (context->argumentCount() >= 1) @@ -66,7 +64,9 @@ static QScriptValue scriptFQN(QScriptContext *context, QScriptEngine *engine) //static TKContainer* TKContainer::createContainer() { - return new DNQSContainer; + TKContainer *container = new DNQSContainer(); + container->init(); + return container; } DNQSContainer::DNQSContainer() @@ -83,8 +83,6 @@ DNQSContainer::DNQSContainer() mQSCellContainer = mQSEngine->newObject(); mQSGlobalObject.setProperty("__private_DNCellContainer__",mQSCellContainer); - - mEmptyCellClass = createCellCode("_DNEMPTY",CELLTYPE_JSBASIC,""); } DNQSContainer::~DNQSContainer() @@ -92,154 +90,70 @@ DNQSContainer::~DNQSContainer() if (mQSEngine) delete mQSEngine; } -TKCell* DNQSContainer::createCellWithoutCellCodeClass(std::string theLocation, std::string theName, std::string type, std::string customScript) -{ - TKCell *cell = NULL; - bool noScript = false; - if (type == CELLTYPE_JSBASIC || type.length() == 0) - { - cell = new DNQSBasicCell(this, theLocation, theName, false); - } - else if (type == CELLTYPE_IN) - { - cell = new DNQSInputCell(this, theLocation, theName, true); - noScript = true; - } - else if (type == CELLTYPE_OUT) - { - cell = new DNQSOutputCell(this, theLocation, theName, true); - noScript = true; - } - else if (type == CELLTYPE_BASICSTORAGE) - { - cell = new DNQSBasicStorageCell(this, theLocation, theName, false); - } - else - { - std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName); - message.append("'\nThe cellcode requires type '").append(type).append("' but it's not a supported type."); - dnNotifyError("Initialization failed", message); - } +void DNQSContainer::setValue(std::string key, float value) +{ + mQSGlobalObject.setProperty(QString::fromStdString(key), QScriptValue(value)); +} - if (cell) +float DNQSContainer::getValue(std::string key) +{ + QScriptValue v = mQSGlobalObject.property(QString::fromStdString(key)); + if (v.isNumber()) { - std::string fqnName = getFQNString(theLocation.c_str(), theName.c_str()); - mCells.insert(TKCellMap::value_type(fqnName, cell)); - if (cell->isInterface()) - { - mInterfaceCells.insert(TKCellMap::value_type(fqnName, cell)); - } - if (!noScript) - { - cell->setCellCode(mEmptyCellClass,(const void*)customScript.c_str()); - } - else - { - if (customScript.length()>0) - { - std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName); - message.append("'\nThe cellcode is type '").append(type).append("'. This type doesn't support custom script."); - dnNotifyError("Initialization failed",message); - } - } + return v.toNumber(); } else { - std::string fqnName = getFQNString(theLocation.c_str(),theName.c_str()); - std::string message = "Failed to create a cell. "; - message += fqnName; - message += "\n"; - message += "Out of memory?"; - dnNotifyError("Initialization failed", message); + return 0; } - - return cell; } -TKCell* DNQSContainer::createCellWithCellCodeClass(std::string theLocation, std::string theName, TKCellCode *cellCode, std::string customScript) +TKCell* DNQSContainer::cellFactory(std::string location, std::string name, std::string type, bool canInterface) { TKCell *cell = NULL; - std::string type = cellCode->getCellAPIName(); - - if (type == CELLTYPE_JSBASIC) - { - cell = new DNQSBasicCell(this, theLocation, theName, false); - } - else if (type == CELLTYPE_BASICSTORAGE) - { - cell = new DNQSBasicStorageCell(this, theLocation, theName, false); - } - else if (type == CELLTYPE_OUT) + if (type == CELLTYPE_JSBASIC || type.length() == 0) { - std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName); - message.append("'\nThe cellcode type '").append(type).append("' doesn't support to have a CellCode class."); - dnNotifyError("Initialization failed", message); + cell = new DNQSBasicCell(this, location, name, canInterface); + cell->init(); } else if (type == CELLTYPE_IN) { - std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName); - message.append("'\nThe cellcode requires type '").append(type).append("' doesn't support to have a CellCode class."); - dnNotifyError("Initialization failed", message); - } - else - { - std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName); - message.append("'\nThe cellcode requires type '").append(type).append("' but it's not a supported type."); - dnNotifyError("Initialization failed",message); + cell = new DNQSInputCell(this, location, name, canInterface); + cell->init(); } - - - if (cell) + else if (type == CELLTYPE_OUT) { - std::string fqnName = getFQNString(theLocation.c_str(), theName.c_str()); - mCells.insert(TKCellMap::value_type(fqnName, cell)); - cell->setCellCode(cellCode,(const void*)customScript.c_str()); - - if (cell->isInterface()) - { - mInterfaceCells.insert(TKCellMap::value_type(fqnName, cell)); - } + cell = new DNQSOutputCell(this, location, name, canInterface); + cell->init(); } - else + else if (type == CELLTYPE_BASICSTORAGE) { - std::string fqnName = getFQNString(theLocation.c_str(),theName.c_str()); - std::string message = "Failed to create a cell. "; - message += fqnName; - message += "\n"; - message += "Out of memory?"; - dnNotifyError("Initialization failed", message); + cell = new DNQSBasicStorageCell(this, location, name, canInterface); + cell->init(); } return cell; } -TKCellCode* DNQSContainer::createCellCode(std::string theName, std::string theAPIType, std::string code) +TKAxon* DNQSContainer::axonFactory(TKCell *theOwner) { - DNQSCellCode *cellCode = new DNQSCellCode(theName, theAPIType, this, code); + return new TKAxon(theOwner); +} - if (cellCode) - { - mCellCodes.insert( std::map::value_type(theName, cellCode)); - } - return cellCode; +TKReceptor* DNQSContainer::receptorFactory(TKCell *theOwner) +{ + return new TKReceptor(theOwner); } -void DNQSContainer::setValue(std::string key, float value) +TKAxonTerminal* DNQSContainer::axonTerminalFactory(TKAxon *theOwner) { - mQSGlobalObject.setProperty(QString::fromStdString(key), QScriptValue(value)); + return new TKAxonTerminal(theOwner); } -float DNQSContainer::getValue(std::string key) +TKCellCode* DNQSContainer::cellCodeFactory(std::string name, std::string cellapi, std::string code) { - QScriptValue v = mQSGlobalObject.property(QString::fromStdString(key)); - if (v.isNumber()) - { - return v.toNumber(); - } - else - { - return 0; - } + return new DNQSCellCode(name, cellapi, this, code); } + diff --git a/Source/QtScript/dnqscontainer.h b/Source/QtScript/dnqscontainer.h index 6cd318d..b1da392 100644 --- a/Source/QtScript/dnqscontainer.h +++ b/Source/QtScript/dnqscontainer.h @@ -30,10 +30,6 @@ public: DNQSContainer(); virtual ~DNQSContainer(); - virtual TKCell* createCellWithoutCellCodeClass(std::string theLocation, std::string theName, std::string type, std::string customScript); - virtual TKCell* createCellWithCellCodeClass(std::string theLocation, std::string theName, TKCellCode *cellCode, std::string customScript); - virtual TKCellCode* createCellCode(std::string theName, std::string theAPIType, std::string code); - virtual void setValue(std::string key, float value); virtual float getValue(std::string key); @@ -41,16 +37,16 @@ public: inline QScriptValue getScriptGlobalObject() { return mQSGlobalObject; } inline QScriptValue getQSCellContainer() { return mQSCellContainer; } - static const std::string CELLTYPE_JSBASIC; - static const std::string CELLTYPE_OUT; - static const std::string CELLTYPE_IN; - static const std::string CELLTYPE_BASICSTORAGE; + virtual TKCell* cellFactory(std::string location, std::string name, std::string type, bool canInterface = true); + virtual TKAxon* axonFactory(TKCell *theOwner); + virtual TKReceptor* receptorFactory(TKCell *theOwner); + virtual TKAxonTerminal* axonTerminalFactory(TKAxon *theOwner); + virtual TKCellCode* cellCodeFactory(std::string name, std::string cellapi, std::string code); private: QScriptEngine *mQSEngine; QScriptValue mQSGlobalObject; QScriptValue mQSCellContainer; - TKCellCode *mEmptyCellClass; }; #endif // DNQSCONTAINER_H diff --git a/Source/TKAxon.cpp b/Source/TKAxon.cpp index 93ebe34..a0501e3 100644 --- a/Source/TKAxon.cpp +++ b/Source/TKAxon.cpp @@ -17,6 +17,8 @@ // Created by tkawata on 12/11/2011. // #include "TKAxon.h" +#include "TKCell.h" +#include "TKContainer.h" #include "TKAxonTerminal.h" #include "TKLock.h" #include "DNUtils.h" @@ -49,7 +51,7 @@ void TKAxon::setValue(float value) TKAxonTerminal* TKAxon::addTerminal() { - TKAxonTerminal *terminal = new TKAxonTerminal(this); + TKAxonTerminal *terminal = mOwner->getContainer()->axonTerminalFactory(this); if (terminal) { diff --git a/Source/TKCell.cpp b/Source/TKCell.cpp index 8ef7f5f..ed693d1 100644 --- a/Source/TKCell.cpp +++ b/Source/TKCell.cpp @@ -29,7 +29,6 @@ TKCell::TKCell(TKContainer *container, std::string location, std::string name, bool canIntarface) : mName(name), mLocation(location), mContainer(container), mAxon(NULL), mCellCodeInstance(NULL), mCanInterface(canIntarface) { - mAxon = new TKAxon(this); } TKCell::~TKCell() @@ -40,9 +39,9 @@ TKCell::~TKCell() mCellCodeInstance = NULL; } - for ( TKReceptorMap::iterator it = mReceptors.begin(); it != mReceptors.end(); ++it ) { - delete it->second; - } + for ( TKReceptorMap::iterator it = mReceptors.begin(); it != mReceptors.end(); ++it ) { + delete it->second; + } mReceptors.clear(); if (mAxon) @@ -52,6 +51,12 @@ TKCell::~TKCell() } } +void TKCell::init() +{ + if (!mAxon) + mAxon = mContainer->axonFactory(this); +} + float TKCell::getAxonValue() { if (!mAxon) @@ -74,12 +79,12 @@ bool TKCell::setCellCode(TKCellCode *code, const void *data) bool TKCell::connectTo(std::string connectionName, TKCell *targetCell) { - TKReceptor *receptor = targetCell->createReceptor(connectionName); + TKReceptor *receptor = targetCell->createReceptor(connectionName); if (receptor) { TKAxonTerminal *terminal = mAxon->addTerminal(); receptor->setTarget(terminal); - + return true; } else @@ -98,13 +103,13 @@ TKReceptor* TKCell::createReceptor(std::string name) } else { - receptor = new TKReceptor(this); + receptor = mContainer->receptorFactory(this); if (receptor) { mReceptors.insert( TKReceptorMap::value_type( name, receptor ) ); } } - + return receptor; } diff --git a/Source/TKCell.h b/Source/TKCell.h index 3be7326..8c4ed71 100644 --- a/Source/TKCell.h +++ b/Source/TKCell.h @@ -34,37 +34,39 @@ typedef std::map TKReceptorMap; class TKCell { public: - TKCell(TKContainer *container, std::string location, std::string name, bool canInterface); - virtual ~TKCell(); - + TKCell(TKContainer *container, std::string location, std::string name, bool canInterface); + virtual ~TKCell(); + + void init(); + std::string getName() { return mName; } float getAxonValue(); void setAxonValue(float value); - + const TKReceptorMap* getReceptors() { return &mReceptors; } - + virtual bool setCellCode(TKCellCode *code, const void *data); - - bool connectTo(std::string connectionName, TKCell *targetReceptor); - inline TKContainer* getContainer() { return mContainer; } + + bool connectTo(std::string connectionName, TKCell *targetReceptor); + inline TKContainer* getContainer() { return mContainer; } bool isInterface() { return mCanInterface; } - - virtual bool doTick(float time) = 0; + + virtual bool doTick(float time) = 0; virtual bool doInit() = 0; virtual bool doDestroy() = 0; protected: virtual TKReceptor* createReceptor(std::string name); - + std::string mName; std::string mLocation; - TKContainer *mContainer; - TKAxon *mAxon; - TKReceptorMap mReceptors; + TKContainer *mContainer; + TKAxon *mAxon; + TKReceptorMap mReceptors; TKCellCodeInstance *mCellCodeInstance; bool mCanInterface; }; #endif - + diff --git a/Source/TKContainer.cpp b/Source/TKContainer.cpp index 6b0ffcf..ae91abd 100644 --- a/Source/TKContainer.cpp +++ b/Source/TKContainer.cpp @@ -19,9 +19,26 @@ #include "TKContainer.h" #include "TKCell.h" +#include "TKCellCode.h" #include "DNStorage.h" #include "DNUtils.h" +#include + +const std::string TKContainer::CELLTYPE_JSBASIC = "B"; +const std::string TKContainer::CELLTYPE_OUT = "O"; +const std::string TKContainer::CELLTYPE_IN = "I"; +const std::string TKContainer::CELLTYPE_BASICSTORAGE = "BS"; + +TKContainer::TKContainer() : mStorage(NULL), mEmptyCellClass(NULL) +{ +} + +void TKContainer::init() +{ + mEmptyCellClass = addCellCode("_DNEMPTY",CELLTYPE_JSBASIC,""); +} + TKContainer::~TKContainer() { doDestroy(); @@ -65,6 +82,140 @@ bool TKContainer::doDestroy() return true; } +TKCell* TKContainer::addCell(std::string theLocation, std::string theName, std::string type, std::string customScript) +{ + TKCell *cell = NULL; + bool noScript = false; + + if (type == CELLTYPE_JSBASIC || type.length() == 0) + { + cell = cellFactory(theLocation, theName, type, false); + } + else if (type == CELLTYPE_IN) + { + cell = cellFactory(theLocation, theName, type, true); + noScript = true; + } + else if (type == CELLTYPE_OUT) + { + cell = cellFactory(theLocation, theName, type, true); + noScript = true; + } + else if (type == CELLTYPE_BASICSTORAGE) + { + cell = cellFactory(theLocation, theName, type, false); + } + else + { + std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName); + message.append("'\nThe cellcode requires type '").append(type).append("' but it's not a supported type."); + dnNotifyError("Initialization failed", message); + } + + if (cell) + { + std::string fqnName = getFQNString(theLocation.c_str(), theName.c_str()); + mCells.insert(TKCellMap::value_type(fqnName, cell)); + if (cell->isInterface()) + { + mInterfaceCells.insert(TKCellMap::value_type(fqnName, cell)); + } + if (!noScript) + { + cell->setCellCode(mEmptyCellClass,(const void*)customScript.c_str()); + } + else + { + if (customScript.length()>0) + { + std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName); + message.append("'\nThe cellcode is type '").append(type).append("'. This type doesn't support custom script."); + dnNotifyError("Initialization failed",message); + } + } + } + else + { + std::string fqnName = getFQNString(theLocation.c_str(),theName.c_str()); + std::string message = "Failed to create a cell. "; + message += fqnName; + message += "\n"; + message += "Out of memory?"; + dnNotifyError("Initialization failed", message); + } + + return cell; +} + +TKCell* TKContainer::addCell(std::string theLocation, std::string theName, TKCellCode *cellCode, std::string customScript) +{ + TKCell *cell = NULL; + + std::string type = cellCode->getCellAPIName(); + + if (type == CELLTYPE_JSBASIC) + { + cell = cellFactory(theLocation, theName, type, false); + } + else if (type == CELLTYPE_BASICSTORAGE) + { + cell = cellFactory(theLocation, theName, type, false); + } + else if (type == CELLTYPE_OUT) + { + std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName); + message.append("'\nThe cellcode type '").append(type).append("' doesn't support to have a CellCode class."); + dnNotifyError("Initialization failed", message); + } + else if (type == CELLTYPE_IN) + { + std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName); + message.append("'\nThe cellcode requires type '").append(type).append("' doesn't support to have a CellCode class."); + dnNotifyError("Initialization failed", message); + } + else + { + std::string message = std::string("Failed to construct cell '").append(theLocation).append("#").append(theName); + message.append("'\nThe cellcode requires type '").append(type).append("' but it's not a supported type."); + dnNotifyError("Initialization failed",message); + } + + + if (cell) + { + std::string fqnName = getFQNString(theLocation.c_str(), theName.c_str()); + mCells.insert(TKCellMap::value_type(fqnName, cell)); + cell->setCellCode(cellCode,(const void*)customScript.c_str()); + + if (cell->isInterface()) + { + mInterfaceCells.insert(TKCellMap::value_type(fqnName, cell)); + } + } + else + { + std::string fqnName = getFQNString(theLocation.c_str(),theName.c_str()); + std::string message = "Failed to create a cell. "; + message += fqnName; + message += "\n"; + message += "Out of memory?"; + dnNotifyError("Initialization failed", message); + } + + return cell; +} + +TKCellCode* TKContainer::addCellCode(std::string theName, std::string theAPIType, std::string code) +{ + TKCellCode *cellCode = cellCodeFactory(theName, theAPIType, code); + + if (cellCode) + { + mCellCodes.insert( std::map::value_type(theName, cellCode)); + } + return cellCode; +} + bool TKContainer::setDataStore(std::string storagePath) { mStorage = new DNStorage(storagePath.c_str()); diff --git a/Source/TKContainer.h b/Source/TKContainer.h index c44829d..bc2bb61 100644 --- a/Source/TKContainer.h +++ b/Source/TKContainer.h @@ -26,6 +26,9 @@ #include class TKCell; +class TKReceptor; +class TKAxon; +class TKAxonTerminal; class TKCellCode; class DNStorage; @@ -35,11 +38,17 @@ typedef std::map TKCellCodeMap; class TKContainer { public: + static const std::string CELLTYPE_JSBASIC; + static const std::string CELLTYPE_OUT; + static const std::string CELLTYPE_IN; + static const std::string CELLTYPE_BASICSTORAGE; + static TKContainer* createContainer(); + virtual void init(); virtual ~TKContainer(); - const TKCellMap* getCells() { return &mCells; } + const TKCellMap* getCells() { return &mCells; } const TKCellCodeMap* getCellCodes() { return &mCellCodes; } void setContentPath(std::string contentPath) { mContentPath = contentPath; } @@ -55,20 +64,27 @@ public: bool doTick(float time); bool doDestroy(); - virtual TKCell* createCellWithoutCellCodeClass(std::string theLocation, std::string theName, std::string type, std::string customScript) = 0; - virtual TKCell* createCellWithCellCodeClass(std::string theLocation, std::string theName, TKCellCode *cellCode, std::string customScript) = 0; - virtual TKCellCode* createCellCode(std::string theName, std::string theAPIType, std::string code) = 0; - + TKCell* addCell(std::string theLocation, std::string theName, std::string type, std::string customScript); + TKCell* addCell(std::string theLocation, std::string theName, TKCellCode *cellCode, std::string customScript); + TKCellCode* addCellCode(std::string theName, std::string theAPIType, std::string code); + virtual void setValue(std::string key, float value) = 0; inline virtual float getValue(std::string key) = 0; + virtual TKCell* cellFactory(std::string location, std::string name, std::string type, bool canInterface = true) = 0; + virtual TKAxon* axonFactory(TKCell *theOwner) = 0; + virtual TKReceptor* receptorFactory(TKCell *theOwner) = 0; + virtual TKAxonTerminal* axonTerminalFactory(TKAxon *theOwner) = 0; + virtual TKCellCode* cellCodeFactory(std::string name, std::string cellapi, std::string code) = 0; + protected: - TKContainer() : mStorage(NULL){} + TKContainer(); TKCellMap mCells; TKCellMap mInterfaceCells; TKCellCodeMap mCellCodes; std::string mContentPath; DNStorage *mStorage; + TKCellCode *mEmptyCellClass; private: TKLock mLock; -- 2.11.0