MeVisLab Toolbox Reference
mlCopyBase.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2007, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef ML_COPY_BASE_H
14#define ML_COPY_BASE_H
15
16
23
24// ML includes
25#include "mlModuleIncludes.h"
26#include "mlEngine.h"
27
28
29#include "mlBaseInit.h"
30#include "mlListBase.h"
31
32ML_START_NAMESPACE
33
34
35// ------------------------------------------------------------------
39// ------------------------------------------------------------------
40template <class BASE_DERIVED_CLASS>
42{
43
44public:
45
48
50 ~CopyBase() override;
51
53 void copyObject();
54
57
65
67 static const char* autoUpdateModeNames[];
68
69protected:
70
71
74
77
80
83
86
90
93
96
100
102
104 void handleNotification(Field* field) override;
105
107 void setNewOutputObject(BASE_DERIVED_CLASS* newOutObject);
108
110 const RuntimeType* checkObjectType(const Base* inputObject) const;
111
114
115private:
116
117 //------------------------------------------------------------------------------------
120 //------------------------------------------------------------------------------------
121 CopyBase(const CopyBase &) : Engine(0,0)
122 {
123 ML_PRINT_FATAL_ERROR("CopyBase::CopyBase(const CopyBase &)",
125 "Usage of copy constructor of CopyBase is not supported.");
126 }
127
128 //------------------------------------------------------------------------------------
131 //------------------------------------------------------------------------------------
132 CopyBase& operator=(const CopyBase &)
133 {
134 ML_PRINT_FATAL_ERROR("CopyBase& CopyBase::operator=(const CopyBase &)",
136 "Usage of assignment operator of CopyBase is not supported.");
137 return *this;
138 }
139
141 int _lockNotification;
142
144 //ML_MODULE_CLASS_HEADER(CopyBase);
145
146};
147
148
149
150
151//------------------------------------------------------------------------------------------
152
154class MLBASEEXPORT CopyList : public CopyBase<ListBase>
155{
156
158 CopyList() : CopyBase<ListBase>(){}
159
160 // TODO: Add support for event copying (instead of always copying the entire list)
161 private:
162
164 ML_MODULE_CLASS_HEADER(CopyList);
165};
166
167
168
169
170
171//------------------------------------------------------------------------------------------
172// implementation follows here in header because of problems with template class implementations in cpp files
173// in VC 6.0
174
175//------------------------------------------------------------------------------------------
176// CLASS CopyBase<>
177//------------------------------------------------------------------------------------------
178
179//------------------------------------------------------------------------------------------
180template <class BASE_DERIVED_CLASS>
182 "Off",
183 "AutoClear",
184 "AutoUpdate"
185};
186
187
188//------------------------------------------------------------------------------------------
190template <class BASE_DERIVED_CLASS>
192: Engine(),
193_outputObject(nullptr),
194_lockNotification(1)
195{
196 // add fields and assign default values:
197
198 _fldInputBase = addBase("inObject");//)->setBaseValue(NULL);
199 (_fldOutputBase = addBase("outObject"))->setBaseValue(nullptr);
201 (_fldStatusString = addString("status"))->setStringValue("ok.");
202
203 (_fldUpToDate = addBool("upToDate"))->setBoolValue(true);
204 (_fldObjectType = addString("objectType"))->setStringValue("(NULL)");
205 _fldUpdate = addNotify("update");
206 _fldClear = addNotify("clear");
207 _lockNotification = 0;
208}
209
210
211//------------------------------------------------------------------------------------------
213template <class BASE_DERIVED_CLASS>
218
219//------------------------------------------------------------------------------------------
221template <class BASE_DERIVED_CLASS>
223
224 // check if object is present
225 if (!inputObject) {
226 _fldObjectType->setStringValue("(NULL)");
227 _fldStatusString->setStringValue("No input object detected.");
228 return nullptr;
229 }
230
231 // check type
232 const RuntimeType* inType = inputObject->getTypeId();
233 if (!inType || !inType->canCreateInstance() || !inType->isDerivedFrom(BASE_DERIVED_CLASS::getClassTypeId())) {
234 _fldObjectType->setStringValue("(INVALID TYPE)");
235 _fldStatusString->setStringValue("Error: Invalid input object type.");
236 return nullptr;
237 }
238
239 _fldObjectType->setStringValue(inType->getName());
240 _fldStatusString->setStringValue("Input Object Valid.");
241
242 return inType;
243}
244
245//------------------------------------------------------------------------------------------
247template <class BASE_DERIVED_CLASS>
249{
250 // first delete any existing object:
251 deleteObject();
252
253 Base* inputObject = _fldInputBase->getBaseValue();
254 BASE_DERIVED_CLASS* outputObject = nullptr;
255
256 // check if object is present and has correct type:
257 if (!checkObjectType(inputObject)) {
258 setNewOutputObject(nullptr);
259 _fldUpToDate->setBoolValue(inputObject == nullptr);
260 return;
261 }
262
263 // copy object
264 outputObject = static_cast<BASE_DERIVED_CLASS*>(inputObject)->clone();
265
266 // update output
267 setNewOutputObject(outputObject);
268 _fldUpToDate->setBoolValue(true);
269 _fldStatusString->setStringValue("ok.");
270}
271
272
273
274//------------------------------------------------------------------------------------------
276template <class BASE_DERIVED_CLASS>
277void CopyBase<BASE_DERIVED_CLASS>::setNewOutputObject(BASE_DERIVED_CLASS* newOutObject)
278{
279 _outputObject = newOutObject;
280 _fldOutputBase->setBaseValue(_outputObject);
281}
282
283
284
285//------------------------------------------------------------------------------------------
287template <class BASE_DERIVED_CLASS>
289{
290 if (_outputObject) {
291 _fldOutputBase->setBaseValue(nullptr);
292
293 delete _outputObject;
294
295 _outputObject = nullptr;
296 }
297}
298
299
300//------------------------------------------------------------------------------------------
302template <class BASE_DERIVED_CLASS>
304{
305 if (!_lockNotification) {
306 ++_lockNotification;
307
308 // output touched, output object possibly modified
309 if (field == _fldOutputBase) {
310 _fldUpToDate->setBoolValue(false);
311 _fldStatusString->setStringValue("Output externally touched, possibly modified.");
312 }
313
314 // input touched
315 if (field == _fldInputBase) {
316 switch (_fldAutoUpdateMode->getEnumValue()) {
318 checkObjectType(_fldInputBase->getBaseValue());
319 _fldUpToDate->setBoolValue(false);
320 break;
322 checkObjectType(_fldInputBase->getBaseValue());
323 // initiate clear
324 field = _fldClear;
325 break;
327 // initiate update
328 field = _fldUpdate;
329 break;
330 default:
331 ML_PRINT_FATAL_ERROR("void CopyBase<BASE_DERIVED_CLASS>::handleNotification(Field* field)", ML_BAD_PARAMETER, "Clearing output!");
332 field = _fldClear;
333 break;
334 }
335 }
336
337
338 // clear button pressed
339 if (field == _fldClear) {
340 deleteObject();
341 _fldStatusString->setStringValue("Output object deleted.");
342 _fldUpToDate->setBoolValue(_fldInputBase->getBaseValue() == nullptr);
343
344 }
345
346 // update button pressed
347 if (field == _fldUpdate) {
348 copyObject();
349 }
350
351 --_lockNotification;
352 }
353}
354
355
356ML_END_NAMESPACE
357
358#endif // __mlCopyBase_H
359
void copyObject()
Copies the input object to _outputObject and updates the base output.
Definition mlCopyBase.h:248
@ AutoUpdateModeAutoUpdate
Definition mlCopyBase.h:62
@ AutoUpdateModeAutoClear
Definition mlCopyBase.h:61
@ AutoUpdateModeCount
Definition mlCopyBase.h:63
@ AutoUpdateModeDoNothing
Definition mlCopyBase.h:60
static const char * autoUpdateModeNames[]
auto update mode enum tokens
Definition mlCopyBase.h:67
StringField * _fldStatusString
String with object type name.
Definition mlCopyBase.h:85
NotifyField * _fldClear
Definition mlCopyBase.h:99
BaseField * _fldInputBase
Base input.
Definition mlCopyBase.h:76
void setNewOutputObject(BASE_DERIVED_CLASS *newOutObject)
Sets output object to newOutObject, touches the output object.
Definition mlCopyBase.h:277
BaseField * _fldOutputBase
Base output.
Definition mlCopyBase.h:79
Base * _outputObject
Copied object:
Definition mlCopyBase.h:113
BoolField * _fldUpToDate
Definition mlCopyBase.h:89
void deleteObject()
Deletes the output object.
Definition mlCopyBase.h:288
NotifyField * _fldUpdate
Delete output object, then update output object and set upToDateFld to true.
Definition mlCopyBase.h:95
const RuntimeType * checkObjectType(const Base *inputObject) const
Returns the ml-runtime type of the input object if valid, NULL otherwise.
Definition mlCopyBase.h:222
void handleNotification(Field *field) override
handle field changes
Definition mlCopyBase.h:303
CopyBase()
Constructor.
Definition mlCopyBase.h:191
~CopyBase() override
Destructor, deleting the _outputObject.
Definition mlCopyBase.h:214
StringField * _fldObjectType
String with object type name.
Definition mlCopyBase.h:82
EnumField * _fldAutoUpdateMode
Possible actions at input touch: DoNothing, AutoClear, AutoUpdate.
Definition mlCopyBase.h:92
Engine(int numInputImages=0, int numOutputImages=0)
Constructor.
Definition mlEngine.h:35
EnumField * addEnum(const char *name, const char *const *enumerationItemNames, MLint numEnumerationItems)
BaseField * addBase(const char *name)
Creates a Base field with name and adds it to the container. Default value is NULL.
BoolField * addBool(const char *name)
Creates a BoolField with name and adds it to the container. Default value is false.
NotifyField * addNotify(const char *name)
Creates a NotifyField field with name and adds it to the container.
StringField * addString(const char *name)
Creates a StringField with name and adds it to the container. Default value is empty string.
bool canCreateInstance() const
Returns true if this (runtime) type knows how to create an instance of the class.
ML_UTILS_EXPORT bool isDerivedFrom(const RuntimeType *runtimeType) const
const char * getName() const
Returns the NULL-terminated string name of the class. Returns 'BadType' on error.
#define ML_MODULE_CLASS_HEADER(className)
#define ML_PROGRAMMING_ERROR
Definition mlTypeDefs.h:787
#define ML_BAD_PARAMETER
Definition mlTypeDefs.h:822
#define ML_PRINT_FATAL_ERROR(FUNC_NAME, REASON, HANDLING)
#define MLBASEEXPORT
defined Header file mlBaseInit.h
Definition mlBaseInit.h:22