OSDN Git Service

8ef7f5f1a277ac5395ab8e2a235f0b05d8e2c4bd
[dennco/dennco.git] / Source / TKCell.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 12/11/2011.
18 //
19
20 #include "TKCell.h"
21 #include "TKReceptor.h"
22 #include "TKContainer.h"
23 #include "TKCellCode.h"
24 #include "TKAxon.h"
25 #include "TKCellCodeInstance.h"
26 #include "TKLog.h"
27 #include <string>
28
29 TKCell::TKCell(TKContainer *container, std::string location, std::string name, bool canIntarface)
30 : mName(name), mLocation(location), mContainer(container), mAxon(NULL), mCellCodeInstance(NULL), mCanInterface(canIntarface)
31 {
32     mAxon = new TKAxon(this);
33 }
34
35 TKCell::~TKCell()
36 {
37     if (mCellCodeInstance)
38     {
39         delete mCellCodeInstance;
40         mCellCodeInstance = NULL;
41     }
42
43         for ( TKReceptorMap::iterator it = mReceptors.begin(); it != mReceptors.end(); ++it ) {
44                 delete it->second;
45         }
46     mReceptors.clear();
47
48     if (mAxon)
49     {
50         delete mAxon;
51         mAxon = NULL;
52     }
53 }
54
55 float TKCell::getAxonValue()
56 {
57     if (!mAxon)
58         return 0.0;
59     return mAxon->getValue();
60 }
61
62 void  TKCell::setAxonValue(float value)
63 {
64     if (!mAxon)
65         return;
66     mAxon->setValue(value);
67 }
68
69 bool TKCell::setCellCode(TKCellCode *code, const void *data)
70 {
71     mCellCodeInstance = code->createCellCodeInstance(this, data);
72     return mCellCodeInstance != NULL;
73 }
74
75 bool TKCell::connectTo(std::string connectionName, TKCell *targetCell)
76 {
77     TKReceptor *receptor = targetCell->createReceptor(connectionName);    
78     if (receptor)
79     {
80         TKAxonTerminal *terminal = mAxon->addTerminal();
81         receptor->setTarget(terminal);
82     
83         return true;
84     }
85     else
86     {
87         return false;
88     }
89 }
90
91 TKReceptor* TKCell::createReceptor(std::string name)
92 {
93     TKReceptor *receptor;
94     TKReceptorMap::iterator it = mReceptors.find(name);
95     if (it != mReceptors.end())
96     {
97         receptor = NULL;
98     }
99     else
100     {
101         receptor = new TKReceptor(this);
102         if (receptor)
103         {
104             mReceptors.insert( TKReceptorMap::value_type( name, receptor ) );
105         }
106     }
107     
108     return receptor;
109 }
110