PeriDyno 1.0.0
Loading...
Searching...
No Matches
FBase.cpp
Go to the documentation of this file.
1#include "FBase.h"
2#include <algorithm>
3
4#include "Node.h"
5#include "Module.h"
6
7#include "FCallbackFunc.h"
8
9namespace dyno
10{
11 std::string FormatConnectionInfo(FBase* fin, FBase* fout, bool connecting, bool succeeded)
12 {
13 OBase* pIn = fin != nullptr ? fin->parent() : nullptr;
14 OBase* pOut = fout != nullptr ? fout->parent() : nullptr;
15
16 std::string capIn = pIn != nullptr ? pIn->caption() : "";
17 std::string capOut = pOut != nullptr ? pOut->caption() : "";
18
19 std::string nameIn = pIn != nullptr ? pIn->getName() : "";
20 std::string nameOut = pOut != nullptr ? pOut->getName() : "";
21
22 if (connecting)
23 {
24 std::string message1 = capIn + ":" + nameIn + " is connected to " + capOut + ":" + nameOut;
25 std::string message2 = capIn + ":" + nameIn + " cannot be connected to " + capOut + ":" + nameOut;
26 return succeeded ? message1 : message2;
27 }
28 else
29 {
30 std::string message1 = capIn + ":" + nameIn + " is disconnected from " + capOut + ":" + nameOut;
31 std::string message2 = capIn + ":" + nameIn + " cannot be disconnected from " + capOut + ":" + nameOut;
32 return succeeded ? message1 : message2;
33 }
34 }
35
37 {
38 mOwner = owner;
39 }
40
42 {
43 return mOwner;
44 }
45
46 void FBase::setSource(FBase* source)
47 {
48 m_derived = source == nullptr ? false : true;
49 mSource = source;
50 }
51
53 {
54 return mSource;
55 }
56
58 {
59 if (m_fType != FieldTypeEnum::State && mOwner == nullptr)
60 return nullptr;
61
62 if (!mOwner->findOutputField(this)) {
63 mOwner->addToOutput(this);
64 }
65
66 return this;
67 }
68
70 {
71 if (mOwner == nullptr)
72 return nullptr;
73
74 if (!mOwner->findInputField(this)) {
75 mOwner->addInputField(this);
76 }
77
78 return this;
79 }
80
82 {
83 if (m_fType != FieldTypeEnum::State && mOwner == nullptr)
84 return nullptr;
85
86 if (mOwner->findOutputField(this)) {
87 mOwner->removeFromOutput(this);
88 }
89
90 return this;
91 }
92
94 {
95 if (mOwner == nullptr)
96 return nullptr;
97
98 if (mOwner->findInputField(this)) {
99 mOwner->removeInputField(this);
100 }
101
102 return this;
103 }
104
106 {
107 auto it = std::find(mSinks.begin(), mSinks.end(), f);
108
109 if (it == mSinks.end())
110 {
111 mSinks.push_back(f);
112
113// f->setDerived(true);
114 f->setSource(this);
115
116 Log::sendMessage(Log::Info, FormatConnectionInfo(this, f, true, true));
117
118 return;
119 }
120
121 Log::sendMessage(Log::Info, FormatConnectionInfo(this, f, true, false));
122 }
123
125 {
126 auto it = std::find(mSinks.begin(), mSinks.end(), f);
127
128 if (it != mSinks.end())
129 {
130 mSinks.erase(it);
131
132// f->setDerived(false);
133 f->setSource(nullptr);
134
135 Log::sendMessage(Log::Info, FormatConnectionInfo(this, f, false, true));
136
137 return true;
138 }
139
140 Log::sendMessage(Log::Info, FormatConnectionInfo(this, f, false, false));
141
142 return false;
143 }
144
146 {
147 return m_derived;
148 }
149
151 {
152 return m_autoDestroyable;
153 }
154
155 void FBase::setAutoDestroy(bool autoDestroy)
156 {
157 m_autoDestroyable = autoDestroy;
158 }
159
160 void FBase::setDerived(bool derived)
161 {
162 m_derived = derived;
163 }
164
166 {
167 if (dst->getSource() != nullptr && dst->getSource() != this) {
168 dst->getSource()->removeSink(dst);
169 }
170
171 //If state is connected to an input field of the other node, the field should be exported
172 Node* node = dynamic_cast<Node*>(dst->parent());
173 if (m_fType == FieldTypeEnum::State && node !=nullptr)
174 {
175 this->promoteOuput();
176 }
177
178 // fprintf(stderr,"%s ----> %s\n",this->m_name.c_str(), dst->m_name.c_str());
179 this->addSink(dst);
180
181 this->update();
182
183 return true;
184 }
185
187 {
188 return this->removeSink(dst);
189 }
190
192 {
193 return this->disconnectField(dst);
194 }
195
197 {
198 return mSource == nullptr ? this : mSource->getTopField();
199 }
200
202 {
203 if (!this->isEmpty())
204 {
205 for(auto func : mCallbackFunc)
206 {
207 func->update();
208 }
209 }
210
211 auto& sinks = this->getSinks();
212
213 for(auto var : sinks)
214 {
215 if (var != nullptr)
216 {
217 var->update();
218 }
219 }
220 }
221
222 void FBase::attach(std::shared_ptr<FCallBackFunc> func)
223 {
224 //Add the current field as one of the input to the callback function
225 func->addInput(this);
226
227 mCallbackFunc.push_back(func);
228 }
229
230 void FBase::detach(std::shared_ptr<FCallBackFunc> func)
231 {
232 if (func == nullptr || mCallbackFunc.size() <= 0)
233 return;
234
235 auto it = std::find(mCallbackFunc.begin(), mCallbackFunc.end(), func);
236
237 if (it != mCallbackFunc.end())
238 {
239 mCallbackFunc.erase(it);
240 }
241 }
242
244 {
245 FBase* topField = this->getTopField();
246
247 return mTackTime < topField->mTickTime;
248 }
249
251 {
252 FBase* topField = this->getTopField();
253
254 topField->mTickTime.mark();
255 }
256
258 {
259 this->mTackTime.mark();
260 }
261
263 {
264 return m_optional;
265 }
266
267 void FBase::tagOptional(bool optional)
268 {
269 m_optional = optional;
270 }
271
272 FBase::FBase(std::string name, std::string description, FieldTypeEnum type, OBase* parent)
273 {
274 m_name = name; m_description = description;
275 m_fType = type;
276 if (parent != nullptr)
277 {
278 parent->attachField(this, name, description, false);
279 }
280 }
281
283 {
284 //Before deallocating data, fields should be disconnected first
285 FBase* src = this->getSource();
286 if (src != nullptr) {
287 src->disconnectField(this);
288 }
289
290 while (!mSinks.empty()) {
291 auto sink = mSinks.back();
292 sink->setSource(nullptr);
293
294 mSinks.pop_back();
295 }
296
297 mCallbackFunc.clear();
298 }
299
301 {
302 return m_fType;
303 }
304
305}
306
virtual bool disconnect(FBase *dst)
Definition FBase.cpp:191
void setSource(FBase *source)
Definition FBase.cpp:46
TimeStamp mTickTime
Definition FBase.h:164
FieldTypeEnum getFieldType()
Definition FBase.cpp:300
void tagOptional(bool optional)
Definition FBase.cpp:267
OBase * parent()
Definition FBase.cpp:41
FieldTypeEnum m_fType
Definition FBase.h:144
void tick()
Definition FBase.cpp:250
void tack()
Definition FBase.cpp:257
FBase * getTopField()
Definition FBase.cpp:196
bool isDerived()
Definition FBase.cpp:145
bool m_autoDestroyable
Definition FBase.h:152
FBase * getSource()
Definition FBase.cpp:52
std::string m_description
Definition FBase.h:148
void attach(std::shared_ptr< FCallBackFunc > func)
Definition FBase.cpp:222
FBase * promoteOuput()
Display a state field as an ouput field.
Definition FBase.cpp:57
void setDerived(bool derived)
Definition FBase.cpp:160
virtual ~FBase()
Definition FBase.cpp:282
bool isAutoDestroyable()
Definition FBase.cpp:150
std::vector< std::shared_ptr< FCallBackFunc > > mCallbackFunc
Definition FBase.h:167
std::vector< FBase * > & getSinks()
Definition FBase.h:71
TimeStamp mTackTime
Definition FBase.h:165
void detach(std::shared_ptr< FCallBackFunc > func)
Definition FBase.cpp:230
void setParent(OBase *owner)
Definition FBase.cpp:36
virtual bool isEmpty()=0
bool m_optional
Definition FBase.h:150
bool isOptional()
Definition FBase.cpp:262
FBase * demoteInput()
Hide a state field from inputs.
Definition FBase.cpp:93
bool isModified()
Definition FBase.cpp:243
std::vector< FBase * > mSinks
Definition FBase.h:162
void addSink(FBase *f)
Definition FBase.cpp:105
bool m_derived
Definition FBase.h:153
void setAutoDestroy(bool autoDestroy)
Definition FBase.cpp:155
FBase * mSource
Definition FBase.h:160
bool disconnectField(FBase *dst)
Definition FBase.cpp:186
std::string m_name
Definition FBase.h:147
FBase * promoteInput()
Display a state field as an input field.
Definition FBase.cpp:69
FBase * demoteOuput()
Hide a state field from outputs.
Definition FBase.cpp:81
bool removeSink(FBase *f)
Definition FBase.cpp:124
virtual void update()
Definition FBase.cpp:201
bool connectField(FBase *dst)
Definition FBase.cpp:165
OBase * mOwner
Definition FBase.h:158
@ Info
Information to user.
Definition Log.h:48
static void sendMessage(MessageType type, const std::string &text)
Add a new message to log.
Definition Log.cpp:41
virtual std::string caption()
Return the caption.
Definition OBase.cpp:19
virtual std::string getName()
Definition OBase.cpp:40
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
std::string FormatConnectionInfo(FBase *fin, FBase *fout, bool connecting, bool succeeded)
Definition FBase.cpp:11
FieldTypeEnum
Definition FBase.h:30
@ State
Definition FBase.h:35