PeriDyno 1.2.1
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 mSingleSource = source;
50 }
51
53 {
54 return mSingleSource;
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 if (f->inputPolicy() == FInputPolicy::One)
114 f->setSource(this);
115 else
116 f->addSource(this);
117
118 Log::sendMessage(Log::Info, FormatConnectionInfo(this, f, true, true));
119
120 return true;
121 }
122
123 Log::sendMessage(Log::Info, FormatConnectionInfo(this, f, true, false));
124 return false;
125 }
126
128 {
129 auto it = std::find(mSinks.begin(), mSinks.end(), f);
130
131 if (it != mSinks.end())
132 {
133 mSinks.erase(it);
134
135 if (f->inputPolicy() == FInputPolicy::One)
136 f->setSource(nullptr);
137 else
138 f->removeSource(this);
139
140 Log::sendMessage(Log::Info, FormatConnectionInfo(this, f, false, true));
141
142 return true;
143 }
144
145 Log::sendMessage(Log::Info, FormatConnectionInfo(this, f, false, false));
146
147 return false;
148 }
149
151 {
152 auto it = std::find(mMultipleSources.begin(), mMultipleSources.end(), f);
153
154 if (it == mMultipleSources.end()) {
155 mMultipleSources.push_back(f);
156
157 Log::sendMessage(Log::Info, FormatConnectionInfo(this, f, true, true));
158
159 return true;
160 }
161
162 Log::sendMessage(Log::Info, FormatConnectionInfo(this, f, true, false));
163 return false;
164 }
165
167 {
168 auto it = std::find(mMultipleSources.begin(), mMultipleSources.end(), f);
169
170 if (it != mMultipleSources.end())
171 {
172 mMultipleSources.erase(it);
173
174 Log::sendMessage(Log::Info, FormatConnectionInfo(this, f, false, true));
175 return true;
176 }
177
178 Log::sendMessage(Log::Info, FormatConnectionInfo(this, f, false, false));
179 return false;
180 }
181
183 {
184 return m_derived;
185 }
186
188 {
189 return m_autoDestroyable;
190 }
191
192 void FBase::setAutoDestroy(bool autoDestroy)
193 {
194 m_autoDestroyable = autoDestroy;
195 }
196
197 void FBase::setDerived(bool derived)
198 {
199 m_derived = derived;
200 }
201
203 {
204 if (this->inputPolicy() == FInputPolicy::One)
205 {
206 return mSingleSource == nullptr ? 0 : 1;
207 }
208 else
209 return mMultipleSources.size();
210 }
211
212 void FBase::requestValidSources(std::vector<FBase*>& src)
213 {
214 src.clear();
215 if (this->inputPolicy() == FInputPolicy::One)
216 {
217 if (mSingleSource != nullptr) src.push_back(mSingleSource);
218 }
219 else
220 {
221 src.assign(mMultipleSources.begin(), mMultipleSources.end());
222 }
223 }
224
226 {
227 return (uint)mSinks.size();
228 }
229
231 {
232 if (dst->inputPolicy() == FInputPolicy::One)
233 {
234 if (dst->getSource() != nullptr && dst->getSource() != this) {
235 dst->getSource()->removeSink(dst);
236 }
237 }
238
239 //If state is connected to an input field of the other node, the field should be exported
240 Node* node = dynamic_cast<Node*>(dst->parent());
241 if (m_fType == FieldTypeEnum::State && node !=nullptr)
242 {
243 this->promoteOuput();
244 }
245
246 // fprintf(stderr,"%s ----> %s\n",this->m_name.c_str(), dst->m_name.c_str());
247 this->addSink(dst);
248
249 this->update();
250
251 return true;
252 }
253
255 {
256 return this->removeSink(dst);
257 }
258
260 {
261 return this->disconnectField(dst);
262 }
263
265 {
266 return mSingleSource == nullptr ? this : mSingleSource->getTopField();
267 }
268
270 {
271 if (!this->isEmpty())
272 {
273 for(auto func : mCallbackFunc)
274 {
275 func->update();
276 }
277 }
278
279 auto& sinks = this->getSinks();
280
281 for(auto var : sinks)
282 {
283 if (var != nullptr)
284 {
285 var->update();
286 }
287 }
288 }
289
290 void FBase::attach(std::shared_ptr<FCallBackFunc> func)
291 {
292 //Add the current field as one of the input to the callback function
293 func->addInput(this);
294
295 mCallbackFunc.push_back(func);
296 }
297
298 void FBase::detach(std::shared_ptr<FCallBackFunc> func)
299 {
300 if (func == nullptr || mCallbackFunc.size() <= 0)
301 return;
302
303 auto it = std::find(mCallbackFunc.begin(), mCallbackFunc.end(), func);
304
305 if (it != mCallbackFunc.end())
306 {
307 mCallbackFunc.erase(it);
308 }
309 }
310
312 {
313 if (this->inputPolicy() == FInputPolicy::One)
314 {
315 FBase* topField = this->getTopField();
316 return mTackTime < topField->mTickTime;
317 }
318 else
319 {
320 for (auto src : mMultipleSources)
321 {
322 FBase* topField = src->getTopField();
324 return true;
325 }
326 return false;
327 }
328 }
329
331 {
332 return mActive;
333 }
334
335 void FBase::setActive(bool b)
336 {
337 mActive = b;
338 }
339
341 {
342 FBase* topField = this->getTopField();
343
344 topField->mTickTime.mark();
345 }
346
348 {
349 this->mTackTime.mark();
350 }
351
353 {
354 return m_optional;
355 }
356
357 void FBase::tagOptional(bool optional)
358 {
359 m_optional = optional;
360 }
361
362 FBase::FBase(std::string name, std::string description, FieldTypeEnum type, OBase* parent)
363 {
364 m_name = name; m_description = description;
365 m_fType = type;
366 if (parent != nullptr)
367 {
368 parent->attachField(this, name, description, false);
369 }
370 }
371
373 {
374 //Before deallocating data, fields should be disconnected first
375 FBase* src = this->getSource();
376 if (src != nullptr) {
377 src->disconnectField(this);
378 }
379
380 while (!mSinks.empty()) {
381 auto sink = mSinks.back();
382 this->disconnectField(sink);
383 }
384
385 mCallbackFunc.clear();
386 }
387
389 {
390 return m_fType;
391 }
392
393}
394
bool mActive
Definition FBase.h:188
bool addSink(FBase *f)
Definition FBase.cpp:105
virtual bool disconnect(FBase *dst)
Definition FBase.cpp:259
void setSource(FBase *source)
Definition FBase.cpp:46
TimeStamp mTickTime
Definition FBase.h:203
FieldTypeEnum getFieldType()
Definition FBase.cpp:388
void tagOptional(bool optional)
Definition FBase.cpp:357
OBase * parent()
Definition FBase.cpp:41
FieldTypeEnum m_fType
Definition FBase.h:177
void tick()
Definition FBase.cpp:340
void tack()
Definition FBase.cpp:347
bool removeSource(FBase *f)
Definition FBase.cpp:166
FBase * getTopField()
Definition FBase.cpp:264
bool isDerived()
Definition FBase.cpp:182
bool m_autoDestroyable
Definition FBase.h:185
std::vector< FBase * > mMultipleSources
Definition FBase.h:199
FBase * getSource()
Definition FBase.cpp:52
void requestValidSources(std::vector< FBase * > &src)
Definition FBase.cpp:212
std::string m_description
Definition FBase.h:181
void attach(std::shared_ptr< FCallBackFunc > func)
Definition FBase.cpp:290
FBase * promoteOuput()
Display a state field as an ouput field.
Definition FBase.cpp:57
void setDerived(bool derived)
Definition FBase.cpp:197
FBase * mSingleSource
Definition FBase.h:198
virtual ~FBase()
Definition FBase.cpp:372
bool isAutoDestroyable()
Definition FBase.cpp:187
std::vector< std::shared_ptr< FCallBackFunc > > mCallbackFunc
Definition FBase.h:206
std::vector< FBase * > & getSinks()
Definition FBase.h:91
TimeStamp mTackTime
Definition FBase.h:204
void detach(std::shared_ptr< FCallBackFunc > func)
Definition FBase.cpp:298
uint sizeOfSinks()
Definition FBase.cpp:225
void setParent(OBase *owner)
Definition FBase.cpp:36
virtual bool isEmpty()=0
bool m_optional
Definition FBase.h:183
bool isOptional()
Definition FBase.cpp:352
FBase * demoteInput()
Hide a state field from inputs.
Definition FBase.cpp:93
bool isModified()
Definition FBase.cpp:311
std::vector< FBase * > mSinks
Definition FBase.h:201
bool addSource(FBase *f)
Definition FBase.cpp:150
bool m_derived
Definition FBase.h:186
void setAutoDestroy(bool autoDestroy)
Definition FBase.cpp:192
bool disconnectField(FBase *dst)
Definition FBase.cpp:254
virtual FInputPolicy inputPolicy()
Definition FBase.h:125
std::string m_name
Definition FBase.h:180
FBase * promoteInput()
Display a state field as an input field.
Definition FBase.cpp:69
void setActive(bool b)
Definition FBase.cpp:335
uint sizeOfValidSources()
Definition FBase.cpp:202
FBase * demoteOuput()
Hide a state field from outputs.
Definition FBase.cpp:81
bool isActive()
A variable to control the visibility of the field.
Definition FBase.cpp:330
bool removeSink(FBase *f)
Definition FBase.cpp:127
virtual void update()
Definition FBase.cpp:269
bool connectField(FBase *dst)
Definition FBase.cpp:230
OBase * mOwner
Definition FBase.h:193
@ 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
unsigned int uint
Definition VkProgram.h:14