OSDN Git Service

0f807ea4f0edd088a82a6a4a093559da4f46f7c5
[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 : mContainer(container), mAxon(NULL), mCellCodeInstance(NULL), mLocation(location), mName(name), mCanInterface(canIntarface)
31 {
32     mAxon = new TKAxon(this);
33 }
34
35 TKCell::~TKCell()
36 {
37     if (mCellCodeInstance)
38     {
39         mCellCodeInstance->doDestroy();
40         delete mCellCodeInstance;
41         mCellCodeInstance = NULL;
42     }
43
44         for ( TKReceptorMap::iterator it = mReceptors.begin(); it != mReceptors.end(); ++it ) {
45                 delete it->second;
46         }
47     mReceptors.clear();
48
49     if (mAxon)
50     {
51         delete mAxon;
52         mAxon = NULL;
53     }
54 }
55
56 float TKCell::getAxonValue()
57 {
58     if (!mAxon)
59         return 0.0;
60     return mAxon->value;
61 }
62
63 void  TKCell::setAxonValue(float value)
64 {
65     if (!mAxon)
66         return;
67     mAxon->value = value;
68 }
69
70 bool TKCell::setCellCode(TKCellCode *code, const void *data)
71 {
72     mCellCodeInstance = code->createCellCodeInstance(this, data);
73     mCellCodeInstance->doInit();
74     
75     return true;
76 }
77
78 bool TKCell::connectTo(std::string connectionName, TKCell *targetCell)
79 {
80     TKReceptor *receptor = targetCell->createReceptor(connectionName);    
81     TKAxonTerminal *terminal = mAxon->addTerminal();
82     receptor->setTarget(terminal);
83     
84     return true;
85 }
86
87 TKReceptor* TKCell::createReceptor(std::string name)
88 {
89     TKReceptor *receptor;
90     TKReceptorMap::iterator it = mReceptors.find(name);
91     if (it != mReceptors.end())
92     {
93         receptor = it->second;
94     }
95     else
96     {
97         receptor = new TKReceptor(this);
98         if (receptor)
99         {
100             mReceptors.insert( TKReceptorMap::value_type( name, receptor ) );
101         }
102     }
103     
104     return receptor;
105 }
106