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  FieldContainer* fields = getFieldContainer();
197 
198  // add fields and assign default values:
199 
200  _fldInputBase = fields->addBase("inObject");//)->setBaseValue(NULL);
201  (_fldOutputBase = fields->addBase("outObject"))->setBaseValue(nullptr);
203  (_fldStatusString = fields->addString("status"))->setStringValue("ok.");
204 
205  (_fldUpToDate = fields->addBool("upToDate"))->setBoolValue(true);
206  (_fldObjectType = fields->addString("objectType"))->setStringValue("(NULL)");
207  _fldUpdate = fields->addNotify("update");
208  _fldClear = fields->addNotify("clear");
209  _lockNotification = 0;
210 }
211 
212 
213 //------------------------------------------------------------------------------------------
215 template <class BASE_DERIVED_CLASS>
217 {
218  deleteObject();
219 }
220 
221 //------------------------------------------------------------------------------------------
223 template <class BASE_DERIVED_CLASS>
225 
226  // check if object is present
227  if (!inputObject) {
228  _fldObjectType->setStringValue("(NULL)");
229  _fldStatusString->setStringValue("No input object detected.");
230  return nullptr;
231  }
232 
233  // check type
234  const RuntimeType* inType = inputObject->getTypeId();
235  if (!inType || !inType->canCreateInstance() || !inType->isDerivedFrom(BASE_DERIVED_CLASS::getClassTypeId())) {
236  _fldObjectType->setStringValue("(INVALID TYPE)");
237  _fldStatusString->setStringValue("Error: Invalid input object type.");
238  return nullptr;
239  }
240 
241  _fldObjectType->setStringValue(inType->getName());
242  _fldStatusString->setStringValue("Input Object Valid.");
243 
244  return inType;
245 }
246 
247 //------------------------------------------------------------------------------------------
249 template <class BASE_DERIVED_CLASS>
251 {
252  // first delete any existing object:
253  deleteObject();
254 
255  Base* inputObject = _fldInputBase->getBaseValue();
256  BASE_DERIVED_CLASS* outputObject = nullptr;
257 
258  // check if object is present and has correct type:
259  if (!checkObjectType(inputObject)) {
260  setNewOutputObject(nullptr);
261  _fldUpToDate->setBoolValue(inputObject == nullptr);
262  return;
263  }
264 
265  // copy object
266  outputObject = static_cast<BASE_DERIVED_CLASS*>(inputObject)->clone();
267 
268  // update output
269  setNewOutputObject(outputObject);
270  _fldUpToDate->setBoolValue(true);
271  _fldStatusString->setStringValue("ok.");
272 }
273 
274 
275 
276 //------------------------------------------------------------------------------------------
278 template <class BASE_DERIVED_CLASS>
279 void CopyBase<BASE_DERIVED_CLASS>::setNewOutputObject(BASE_DERIVED_CLASS* newOutObject)
280 {
281  _outputObject = newOutObject;
282  _fldOutputBase->setBaseValue(_outputObject);
283 }
284 
285 
286 
287 //------------------------------------------------------------------------------------------
289 template <class BASE_DERIVED_CLASS>
291 {
292  if (_outputObject) {
293  _fldOutputBase->setBaseValue(nullptr);
294 
295  delete _outputObject;
296 
297  _outputObject = nullptr;
298  }
299 }
300 
301 
302 //------------------------------------------------------------------------------------------
304 template <class BASE_DERIVED_CLASS>
306 {
307  if (!_lockNotification) {
308  ++_lockNotification;
309 
310  // output touched, output object possibly modified
311  if (field == _fldOutputBase) {
312  _fldUpToDate->setBoolValue(false);
313  _fldStatusString->setStringValue("Output externally touched, possibly modified.");
314  }
315 
316  // input touched
317  if (field == _fldInputBase) {
318  switch (_fldAutoUpdateMode->getEnumValue()) {
319  case AutoUpdateModeDoNothing:
320  checkObjectType(_fldInputBase->getBaseValue());
321  _fldUpToDate->setBoolValue(false);
322  break;
323  case AutoUpdateModeAutoClear:
324  checkObjectType(_fldInputBase->getBaseValue());
325  // initiate clear
326  field = _fldClear;
327  break;
328  case AutoUpdateModeAutoUpdate:
329  // initiate update
330  field = _fldUpdate;
331  break;
332  default:
333  ML_PRINT_FATAL_ERROR("void CopyBase<BASE_DERIVED_CLASS>::handleNotification(Field* field)", ML_BAD_PARAMETER, "Clearing output!");
334  field = _fldClear;
335  break;
336  }
337  }
338 
339 
340  // clear button pressed
341  if (field == _fldClear) {
342  deleteObject();
343  _fldStatusString->setStringValue("Output object deleted.");
344  _fldUpToDate->setBoolValue(_fldInputBase->getBaseValue() == nullptr);
345 
346  }
347 
348  // update button pressed
349  if (field == _fldUpdate) {
350  copyObject();
351  }
352 
353  --_lockNotification;
354  }
355 }
356 
357 
358 ML_END_NAMESPACE
359 
360 #endif // __mlCopyBase_H
361 
Field to encapsulate a pointer to an ML base object.
Definition: mlFields.h:1187
Class representing general ML objects that support import/export via strings (setPersistentState() an...
Definition: mlBase.h:62
Field to encapsulate a boolean value.
Definition: mlFields.h:62
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:250
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:279
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:290
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:224
void handleNotification(Field *field) override
handle field changes
Definition: mlCopyBase.h:305
CopyBase()
Constructor.
Definition: mlCopyBase.h:191
~CopyBase() override
Destructor, deleting the _outputObject.
Definition: mlCopyBase.h:216
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 ok because lists have an assignmen...
Definition: mlCopyBase.h:155
Base class for all ML Engines which are derived from Module, which have no inputs or outputs and whic...
Definition: mlEngine.h:30
Field to encapsulate an enumerated value.
Definition: mlFields.h:363
Defines the class FieldContainer to encapsulate a vector of fields for (see class Field).
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
FieldContainer * getFieldContainer()
Returns a pointer to the container of all the module's fields.
Definition: mlModule.h:777
Field without value for notifications.
Definition: mlFields.h:1049
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:1000
#define ML_MODULE_CLASS_HEADER(className)
Like ML_CLASS_HEADER for the usage of derived classes from Module.
#define ML_PROGRAMMING_ERROR
A case occurred which should not appear and here are a variety of reasons, typically it is a programm...
Definition: mlTypeDefs.h:890
#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:925
#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