MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
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
33
34
35// ------------------------------------------------------------------
39// ------------------------------------------------------------------
40template <class BASE_DERIVED_CLASS> class MLBASEEXPORT CopyBase : public Engine
41{
42
43public:
46
48 ~CopyBase() override;
49
51 void copyObject();
52
55
57 typedef enum {
58 AutoUpdateModeDoNothing = 0,
61 AutoUpdateModeCount
62 } AutoUpdateMode;
63
65 static const char* autoUpdateModeNames[];
66
67protected:
70
73
76
79
82
86
89
92
96
98
101
104
107
110
111private:
112 //------------------------------------------------------------------------------------
115 //------------------------------------------------------------------------------------
116 CopyBase(const CopyBase&)
117 : Engine(0, 0)
118 {
119 ML_PRINT_FATAL_ERROR("CopyBase::CopyBase(const CopyBase &)", ML_PROGRAMMING_ERROR,
120 "Usage of copy constructor of CopyBase is not supported.");
121 }
122
123 //------------------------------------------------------------------------------------
126 //------------------------------------------------------------------------------------
127 CopyBase& operator=(const CopyBase&)
128 {
129 ML_PRINT_FATAL_ERROR("CopyBase& CopyBase::operator=(const CopyBase &)", ML_PROGRAMMING_ERROR,
130 "Usage of assignment operator of CopyBase is not supported.");
131 return *this;
132 }
133
135 int _lockNotification;
136
138 // ML_MODULE_CLASS_HEADER(CopyBase);
139};
140
141
142//------------------------------------------------------------------------------------------
143
145class MLBASEEXPORT CopyList : public CopyBase<ListBase>
146{
147
149 CopyList()
151 {}
152
153 // TODO: Add support for event copying (instead of always copying the entire list)
154private:
157};
158
159
160//------------------------------------------------------------------------------------------
161// implementation follows here in header because of problems with template class implementations in cpp files
162// in VC 6.0
163
164//------------------------------------------------------------------------------------------
165// CLASS CopyBase<>
166//------------------------------------------------------------------------------------------
167
168//------------------------------------------------------------------------------------------
169template <class BASE_DERIVED_CLASS>
170const char* CopyBase<BASE_DERIVED_CLASS>::autoUpdateModeNames[] = {"Off", "AutoClear", "AutoUpdate"};
171
172
173//------------------------------------------------------------------------------------------
175template <class BASE_DERIVED_CLASS> CopyBase<BASE_DERIVED_CLASS>::CopyBase()
176 : Engine()
177 , _outputObject(nullptr)
178 , _lockNotification(1)
179{
180 // add fields and assign default values:
181
182 _fldInputBase = addBase("inObject"); //)->setBaseValue(NULL);
183 (_fldOutputBase = addBase("outObject"))->setBaseValue(nullptr);
185 ->setEnumValue(AutoUpdateModeAutoUpdate);
186 (_fldStatusString = addString("status"))->setStringValue("ok.");
187
188 (_fldUpToDate = addBool("upToDate"))->setBoolValue(true);
189 (_fldObjectType = addString("objectType"))->setStringValue("(NULL)");
190 _fldUpdate = addNotify("update");
191 _fldClear = addNotify("clear");
192 _lockNotification = 0;
193}
194
195
196//------------------------------------------------------------------------------------------
198template <class BASE_DERIVED_CLASS> CopyBase<BASE_DERIVED_CLASS>::~CopyBase()
199{
200 deleteObject();
201}
202
203//------------------------------------------------------------------------------------------
205template <class BASE_DERIVED_CLASS>
207{
208
209 // check if object is present
210 if (!inputObject) {
211 _fldObjectType->setStringValue("(NULL)");
212 _fldStatusString->setStringValue("No input object detected.");
213 return nullptr;
214 }
215
216 // check type
217 const RuntimeType* inType = inputObject->getTypeId();
218 if (!inType || !inType->canCreateInstance() || !inType->isDerivedFrom(BASE_DERIVED_CLASS::getClassTypeId())) {
219 _fldObjectType->setStringValue("(INVALID TYPE)");
220 _fldStatusString->setStringValue("Error: Invalid input object type.");
221 return nullptr;
222 }
223
224 _fldObjectType->setStringValue(inType->getName());
225 _fldStatusString->setStringValue("Input Object Valid.");
226
227 return inType;
228}
229
230//------------------------------------------------------------------------------------------
232template <class BASE_DERIVED_CLASS> void CopyBase<BASE_DERIVED_CLASS>::copyObject()
233{
234 // first delete any existing object:
235 deleteObject();
236
237 Base* inputObject = _fldInputBase->getBaseValue();
239
240 // check if object is present and has correct type:
241 if (!checkObjectType(inputObject)) {
242 setNewOutputObject(nullptr);
243 _fldUpToDate->setBoolValue(inputObject == nullptr);
244 return;
245 }
246
247 // copy object
248 outputObject = static_cast<BASE_DERIVED_CLASS*>(inputObject)->clone();
249
250 // update output
251 setNewOutputObject(outputObject);
252 _fldUpToDate->setBoolValue(true);
253 _fldStatusString->setStringValue("ok.");
254}
255
256
257//------------------------------------------------------------------------------------------
259template <class BASE_DERIVED_CLASS>
261{
262 _outputObject = newOutObject;
263 _fldOutputBase->setBaseValue(_outputObject);
264}
265
266
267//------------------------------------------------------------------------------------------
269template <class BASE_DERIVED_CLASS> void CopyBase<BASE_DERIVED_CLASS>::deleteObject()
270{
271 if (_outputObject) {
272 _fldOutputBase->setBaseValue(nullptr);
273
274 delete _outputObject;
275
276 _outputObject = nullptr;
277 }
278}
279
280
281//------------------------------------------------------------------------------------------
283template <class BASE_DERIVED_CLASS> void CopyBase<BASE_DERIVED_CLASS>::handleNotification(Field* field)
284{
285 if (!_lockNotification) {
286 ++_lockNotification;
287
288 // output touched, output object possibly modified
289 if (field == _fldOutputBase) {
290 _fldUpToDate->setBoolValue(false);
291 _fldStatusString->setStringValue("Output externally touched, possibly modified.");
292 }
293
294 // input touched
295 if (field == _fldInputBase) {
296 switch (_fldAutoUpdateMode->getEnumValue()) {
297 case AutoUpdateModeDoNothing:
298 checkObjectType(_fldInputBase->getBaseValue());
299 _fldUpToDate->setBoolValue(false);
300 break;
301 case AutoUpdateModeAutoClear:
302 checkObjectType(_fldInputBase->getBaseValue());
303 // initiate clear
304 field = _fldClear;
305 break;
306 case AutoUpdateModeAutoUpdate:
307 // initiate update
308 field = _fldUpdate;
309 break;
310 default:
311 ML_PRINT_FATAL_ERROR("void CopyBase<BASE_DERIVED_CLASS>::handleNotification(Field* field)", ML_BAD_PARAMETER,
312 "Clearing output!");
313 field = _fldClear;
314 break;
315 }
316 }
317
318
319 // clear button pressed
320 if (field == _fldClear) {
321 deleteObject();
322 _fldStatusString->setStringValue("Output object deleted.");
323 _fldUpToDate->setBoolValue(_fldInputBase->getBaseValue() == nullptr);
324 }
325
326 // update button pressed
327 if (field == _fldUpdate) {
328 copyObject();
329 }
330
331 --_lockNotification;
332 }
333}
334
335
337
338#endif // __mlCopyBase_H
Field to encapsulate a pointer to an ML base object.
Definition mlFields.h:813
Class representing general ML objects that support import/export via strings (setPersistentState() an...
Definition mlBase.h:63
Field to encapsulate a boolean value.
Definition mlFields.h:56
Engine template module CopyBase to copy any Base-derived object that provides a virtual assignment op...
Definition mlCopyBase.h:41
void copyObject()
Copies the input object to _outputObject and updates the base output.
Definition mlCopyBase.h:232
static const char * autoUpdateModeNames[]
auto update mode enum tokens
Definition mlCopyBase.h:65
@ AutoUpdateModeAutoUpdate
Definition mlCopyBase.h:60
@ AutoUpdateModeAutoClear
Definition mlCopyBase.h:59
@ AutoUpdateModeCount
Definition mlCopyBase.h:61
StringField * _fldStatusString
String with object type name.
Definition mlCopyBase.h:81
NotifyField * _fldClear
delete the output object now Also sets upToDate to (input == NULL).
Definition mlCopyBase.h:95
BaseField * _fldInputBase
Base input.
Definition mlCopyBase.h:72
void setNewOutputObject(BASE_DERIVED_CLASS *newOutObject)
Sets output object to newOutObject, touches the output object.
Definition mlCopyBase.h:260
BaseField * _fldOutputBase
Base output.
Definition mlCopyBase.h:75
Base * _outputObject
Copied object:
Definition mlCopyBase.h:109
BoolField * _fldUpToDate
Is the current output update consistent with the input object? Becomes false if the input or output o...
Definition mlCopyBase.h:85
void deleteObject()
Deletes the output object.
Definition mlCopyBase.h:269
NotifyField * _fldUpdate
Delete output object, then update output object and set upToDateFld to true.
Definition mlCopyBase.h:91
const RuntimeType * checkObjectType(const Base *inputObject) const
Returns the ml-runtime type of the input object if valid, NULL otherwise.
Definition mlCopyBase.h:206
void handleNotification(Field *field) override
handle field changes
Definition mlCopyBase.h:283
CopyBase()
Constructor.
Definition mlCopyBase.h:175
~CopyBase() override
Destructor, deleting the _outputObject.
Definition mlCopyBase.h:198
StringField * _fldObjectType
String with object type name.
Definition mlCopyBase.h:78
EnumField * _fldAutoUpdateMode
Possible actions at input touch: DoNothing, AutoClear, AutoUpdate.
Definition mlCopyBase.h:88
Engine module class for copying ListBase derived classes, which is okay because lists have an assignm...
Definition mlCopyBase.h:146
Base class for all ML Engines that are derived from Module, and have no inputs or outputs but impleme...
Definition mlEngine.h:30
Field to encapsulate an enumerated value.
Definition mlFields.h:185
EnumField * addEnum(const char *name, const char *const *enumerationItemNames, MLint numEnumerationItems)
Creates an EnumField field with name and adds it to the container.
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.
Base class for all fields used in the ML.
Definition mlField.h:73
Field without value for notifications.
Definition mlFields.h:687
RuntimeType contains type and inheritance information of a class and a static dictionary with informa...
Field to encapsulate a string value.
Definition mlFields.h:634
#define ML_MODULE_CLASS_HEADER(className)
Similar to ML_CLASS_HEADER for the usage of derived classes from Module.
#define ML_PROGRAMMING_ERROR
A case occurred that should not appear and there are a variety of reasons, typically it is a programm...
Definition mlTypeDefs.h:844
#define ML_BAD_PARAMETER
A bad/invalid parameter (or even an inappropriate image) has been passed to a module or an algorithm,...
Definition mlTypeDefs.h:879
#define ML_PRINT_FATAL_ERROR(FUNC_NAME, REASON, HANDLING)
Like ML_PRINT_FATAL_ERROR_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ) without a runtime object to be du...
#define MLBASEEXPORT
defined Header file mlBaseInit.h
Definition mlBaseInit.h:22
Target mlrange_cast(Source arg)
Generic version of checked ML casts.