OSDN Git Service

[dennco] re-working the doTick process flow. The work is still in progress.
[dennco/dennco.git] / Source / layer3 / QtScript / dnqscontainer.cpp
1 //  Copyright (c) 2012 Dennco Project
2 //
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, either version 3 of the License, or
6 // (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 //
17 //  Created by tkawata on 2/22/2012.
18 //
19 #include "dnqscontainer.h"
20
21 #include "TKCell.h"
22 #include "TKCellCode.h"
23 #include "TKAxon.h"
24 #include "TKReceptor.h"
25 #include "TKAxonTerminal.h"
26 #include "TKContainer.h"
27 #include "TKLog.h"
28 #include "DNUtils.h"
29 #include "versioninfo.h"
30 #include "dnqsbasiccell.h"
31 #include "dnqsinputcell.h"
32 #include "dnqsoutputcell.h"
33 #include "dnqsbasicstoragecell.h"
34 #include "dnqscellcode.h"
35 #include "DNGlobal.h"
36
37 static QScriptValue scriptPrint(QScriptContext *context, QScriptEngine *engine)
38 {
39     if (context->argumentCount() >= 1)
40     {
41         QString value = context->argument(0).toString();
42         TKLog::printf(TKLog::NORMAL, "%s", value.toLocal8Bit().data());
43     }
44     return engine->nullValue();
45 }
46
47 static QScriptValue scriptFQN(QScriptContext *context, QScriptEngine *engine)
48 {
49     if (context->argumentCount() >= 2)
50     {
51         const char *location = context->argument(0).toString().toUtf8().constData();
52         const char *name = context->argument(1).toString().toUtf8().constData();
53
54         std::string fqnString = getFQNString(location, name);
55         return QScriptValue(fqnString.c_str());
56     }
57     else
58     {
59         return engine->nullValue();
60     }
61 }
62
63 //static
64 TKContainer* TKContainer::createContainer()
65 {
66     TKContainer *container = new DNQSContainer();
67     container->init();
68     return container;
69 }
70
71 DNQSContainer::DNQSContainer()
72 {
73     mQSEngine = new QScriptEngine();
74     mQSGlobalObject = mQSEngine->globalObject();
75
76     mQSGlobalObject.setProperty("print",mQSEngine->newFunction(scriptPrint, 1));
77     mQSGlobalObject.setProperty("FQN",mQSEngine->newFunction(scriptFQN, 2));
78
79     QScriptValue global = mQSEngine->newObject();
80     mQSGlobalObject.setProperty("global", global,QScriptValue::Undeletable);
81     global.setProperty("container_version", QScriptValue(CONTAINER_VERSION),QScriptValue::ReadOnly|QScriptValue::Undeletable);
82
83     mQSCellContainer = mQSEngine->newObject();
84     mQSGlobalObject.setProperty("__private_DNCellContainer__",mQSCellContainer);
85 }
86
87 DNQSContainer::~DNQSContainer()
88 {
89     if (mQSEngine)
90         delete mQSEngine;
91 }
92
93 void DNQSContainer::setValue(std::string key, float value)
94 {
95     mQSGlobalObject.setProperty(QString::fromStdString(key), QScriptValue(value));
96 }
97
98 float DNQSContainer::getValue(std::string key) const
99 {
100     QScriptValue v = mQSGlobalObject.property(QString::fromStdString(key));
101     if (v.isNumber())
102     {
103         return v.toNumber();
104     }
105     else
106     {
107         return 0;
108     }
109 }
110
111 TKCell* DNQSContainer::cellFactory(std::string location, std::string name, std::string type, bool canInterfaceIn, bool canInterfaceOut)
112 {
113     TKCell *cell = NULL;
114
115     if (type == CELLTYPE_JSBASIC || type.length() == 0)
116     {
117         cell = new DNQSBasicCell(this, location, name, canInterfaceIn, canInterfaceOut);
118         cell->init();
119     }
120     else if (type == CELLTYPE_IN)
121     {
122         cell = new DNQSInputCell(this, location, name);
123         cell->init();
124     }
125     else if (type == CELLTYPE_OUT)
126     {
127         cell = new DNQSOutputCell(this, location, name);
128         cell->init();
129     }
130     else if (type == CELLTYPE_BASICSTORAGE)
131     {
132         cell = new DNQSBasicStorageCell(this, location, name, canInterfaceIn, canInterfaceOut);
133         cell->init();
134     }
135
136     return cell;
137 }
138
139 TKAxon* DNQSContainer::axonFactory(TKCell *theOwner)
140 {
141     return new TKAxon(theOwner);
142 }
143
144 TKReceptor* DNQSContainer::receptorFactory(TKCell *theOwner)
145 {
146     return new TKReceptor(theOwner);
147 }
148
149 TKAxonTerminal* DNQSContainer::axonTerminalFactory(TKAxon *theOwner)
150 {
151     return new TKAxonTerminal(theOwner);
152 }
153
154 TKCellCode* DNQSContainer::cellCodeFactory(std::string name, std::string cellapi, std::string code)
155 {
156     return new DNQSCellCode(name, cellapi, this, code);
157 }
158