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 
32 ML_START_NAMESPACE
33 
34 
35 // ------------------------------------------------------------------
39 // ------------------------------------------------------------------
40 template <class BASE_DERIVED_CLASS>
42 {
43 
44 public:
45 
48 
50  ~CopyBase() override;
51 
53  void copyObject();
54 
56  void deleteObject();
57 
59  typedef enum {
60  AutoUpdateModeDoNothing = 0,
63  AutoUpdateModeCount
64  } AutoUpdateMode;
65 
67  static const char* autoUpdateModeNames[];
68 
69 protected:
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 
115 private:
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 
154 class MLBASEEXPORT CopyList : public CopyBase<ListBase>
155 {
156 
159 
160  // TODO: Add support for event copying (instead of always copying the entire list)
161  private:
162 
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 //------------------------------------------------------------------------------------------
180 template <class BASE_DERIVED_CLASS>
182  "Off",
183  "AutoClear",
184  "AutoUpdate"
185 };
186 
187 
188 //------------------------------------------------------------------------------------------
190 template <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 //------------------------------------------------------------------------------------------
213 template <class BASE_DERIVED_CLASS>
215 {
216  deleteObject();
217 }
218 
219 //------------------------------------------------------------------------------------------
221 template <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 //------------------------------------------------------------------------------------------
247 template <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 //------------------------------------------------------------------------------------------
276 template <class BASE_DERIVED_CLASS>
277 void CopyBase<BASE_DERIVED_CLASS>::setNewOutputObject(BASE_DERIVED_CLASS* newOutObject)
278 {
279  _outputObject = newOutObject;
280  _fldOutputBase->setBaseValue(_outputObject);
281 }
282 
283 
284 
285 //------------------------------------------------------------------------------------------
287 template <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 //------------------------------------------------------------------------------------------
302 template <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()) {
317  case AutoUpdateModeDoNothing:
318  checkObjectType(_fldInputBase->getBaseValue());
319  _fldUpToDate->setBoolValue(false);
320  break;
321  case AutoUpdateModeAutoClear:
322  checkObjectType(_fldInputBase->getBaseValue());
323  // initiate clear
324  field = _fldClear;
325  break;
326  case AutoUpdateModeAutoUpdate:
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 
356 ML_END_NAMESPACE
357 
358 #endif // __mlCopyBase_H
359 
Field to encapsulate a pointer to an ML base object.
Definition: mlFields.h:729
Class representing general ML objects that support import/export via strings (setPersistentState() an...
Definition: mlBase.h:59
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:42
void copyObject()
Copies the input object to _outputObject and updates the base output.
Definition: mlCopyBase.h:248
static const char * autoUpdateModeNames[]
auto update mode enum tokens
Definition: mlCopyBase.h:67
@ AutoUpdateModeAutoUpdate
Definition: mlCopyBase.h:62
@ AutoUpdateModeAutoClear
Definition: mlCopyBase.h:61
@ AutoUpdateModeCount
Definition: mlCopyBase.h:63
StringField * _fldStatusString
String with object type name.
Definition: mlCopyBase.h:85
NotifyField * _fldClear
delete the output object now Also sets upToDate to (input == NULL).
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
Is the current output update consistent with the input object? Becomes false if the input or output o...
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 module class for copying ListBase derived classes, which is okay because lists have an assignm...
Definition: mlCopyBase.h:155
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:173
EnumField * addEnum(const char *name, const char *const *enumerationItemNames, MLint numEnumerationItems)
Creates an EnumField field with name and adds it to the container.
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.
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.
Base class for all fields used in the ML.
Definition: mlField.h:73
Field without value for notifications.
Definition: mlFields.h:598
RuntimeType contains type and inheritance information of a class and a static dictionary with informa...
Definition: mlRuntimeType.h:53
bool canCreateInstance() const
Returns true if this (runtime) type knows how to create an instance of the class.
Definition: mlRuntimeType.h:94
ML_UTILS_EXPORT bool isDerivedFrom(const RuntimeType *runtimeType) const
Returns true if this (runtime) type is derived from the argument (runtime) type runtimeType.
const char * getName() const
Returns the NULL-terminated string name of the class. Returns 'BadType' on error.
Definition: mlRuntimeType.h:84
Field to encapsulate a string value.
Definition: mlFields.h:553
#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:788
#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:823
#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