ML Reference
mlTreeNode.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_TREE_NODE_H
14 #define ML_TREE_NODE_H
15 
18 
19 #include "mlImageVector.h"
20 #include "mlInitSystemML.h"
21 #include "mlRuntimeType.h"
22 #include "mlRuntimeSubClass.h"
23 #include "mlSubImageBox.h"
24 #include "mlVector2.h"
25 #include "mlVector3.h"
26 #include "mlVector4.h"
27 #include "mlVector6.h"
28 
29 #include <string>
30 
31 ML_START_NAMESPACE
32 
33 class Base;
34 class SubImageBoxd;
35 
36 //---------------------------------------------------------------------------------
40 //---------------------------------------------------------------------------------
41 enum {
78 
79  TNE_COUNT
80 };
81 
82 
83 //---------------------------------------------------------------------------------
93 //---------------------------------------------------------------------------------
95 {
96 
97 public:
98 
101 
105  TreeNodeException(int errorCode = 0, const char* msg = nullptr);
106 
108  virtual ~TreeNodeException() { }
109 
111  int getCode() const { return _errorCode; }
112 
114 
120  virtual const char* getMessage() const;
121 
122 protected:
123 
126 
127 private:
128 
129  std::string _message;
130 
133  static const char* _stdErrorMsg[];
134 
138 };
139 
140 
142 #define TREE_NODE_CHECK_THROW(x) { if (!(x)) throw TreeNodeException(TNE_UserDefined, #x); }
143 
144 
147 
155 
156 
157 public:
158 
159  // Define macros for default implementations:
160 
162 #define VOID_IMPLEMENTATION(which) { throw TreeNodeException(TNE_VoidImplementation, which); }
163 
168 #define VOID_IMPLEMENTATION_RET(which, R) { if (MLAlwaysTrue){ throw TreeNodeException(TNE_VoidImplementation, which); } return R; }
169 
171 #define ADD_LONG_CHILD { addChild(static_cast<long>(val), name); }
172 
174 #define ADD_ULONG_CHILD { addChild(static_cast<unsigned long>(val), name); }
175 
177 #define ADD_LDOUBLE_CHILD { addChild(static_cast<long double>(val), name); }
178 
181 #define READ_LONG_CHILD(DST_TYPE) { long lval=0; readChild(lval, name); val = static_cast<DST_TYPE>(lval); }
182 
185 #define READ_ULONG_CHILD(DST_TYPE) { unsigned long lval=0; readChild(lval, name); val = static_cast<DST_TYPE>(lval); }
186 
189 #define READ_LDOUBLE_CHILD(DST_TYPE){ long double ldval=0; readChild(ldval, name); val = static_cast<DST_TYPE>(ldval); }
190 
193 #define READ_LONG_CHILD_FOR_BOOL { long lval=0; readChild(lval, name); val = (lval!=0); }
194 
195 
197  typedef enum {
198 
201 
204 
206  CM_COUNT
207 
208  } ConstructionMode;
209 
210 
212  virtual ~TreeNode();
213 
214  //--------------------------------------------------------------
218  //--------------------------------------------------------------
219 
221 
223  virtual void writeToFile (const char* /*fileName*/) VOID_IMPLEMENTATION("writeToFile");
224 
226  virtual void readFromFile (const char* /*fileName*/) VOID_IMPLEMENTATION("readFromFile");
227 
229 
232  virtual void writeToString(std::string& /*str*/) VOID_IMPLEMENTATION("writeToString");
233 
235 
238  virtual void readFromString(const std::string& /*str*/) VOID_IMPLEMENTATION("readFromString");
239 
240 public:
241 
243 
244  //------------------------------------------------------------------------------------------
255  //------------------------------------------------------------------------------------------
256 
258 
260  virtual void addChild (bool val , const char* name ) ADD_ULONG_CHILD;
261 
263  virtual void addChild (unsigned char val , const char* name ) ADD_ULONG_CHILD;
264 
266  virtual void addChild (char val , const char* name ) ADD_LONG_CHILD;
267 
269  virtual void addChild (unsigned short val , const char* name ) ADD_ULONG_CHILD;
270 
272  virtual void addChild (short val , const char* name ) ADD_LONG_CHILD;
273 
275  virtual void addChild (unsigned int val , const char* name ) ADD_ULONG_CHILD;
276 
278  virtual void addChild (int val , const char* name ) ADD_LONG_CHILD;
279 
281  virtual void addChild (unsigned long /*val*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(unsigned long)");
282 
284  virtual void addChild (long /*val*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(long)");
285 
287  virtual void addChild (MLuint64 /*val*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(MLuint64)");
288 
290  virtual void addChild (MLint64 /*val*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(MLint64)");
291 
293  virtual void addChild (float val , const char* name ) ADD_LDOUBLE_CHILD;
294 
296  virtual void addChild (double val , const char* name ) ADD_LDOUBLE_CHILD;
297 
299  virtual void addChild (long double /*val*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(long double)");
300 
302  virtual void addChild(const std::string& /*str*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(const std::string&)");
303 
305  virtual void addChild (const Vector2& /*vec*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(Vector2)");
306 
308  virtual void addChild (const Vector3& /*vec*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(Vector3)");
309 
311  virtual void addChild (const Vector4& /*vec*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(Vector4)");
312 
314  virtual void addChild (const Vector6& /*vec*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(Vector6)");
315 
317  virtual void addChild (const ImageVector& /*vec*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(ImageVector)");
318 
320  virtual void addChild (const Matrix3& /*mat*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(Matrix3)");
321 
323  virtual void addChild (const Matrix4& /*mat*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(Matrix4)");
324 
326  virtual void addChild (const SubImageBox& /*box*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(SubImageBox)");
327 
329  virtual void addChild (const SubImageBoxd& /*box*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(SubImageBoxd)");
330 
333  virtual void addChild (const void* const /*ptr*/, size_t /*noBytes*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(void*)");
334 
336 
339  virtual void addChild (const Base* const /*obj*/, const char* /*name*/, bool /*generic*/ = true) VOID_IMPLEMENTATION("addChild(Base*)");
340 
342 
345  virtual TreeNode* addChild (const char* /*name*/) VOID_IMPLEMENTATION_RET("TreeNode* addChild()", nullptr);
346 
348 
349 
350 
351 
352  //--------------------------------------------------------------------------------------------
358  //--------------------------------------------------------------------------------------------
359 
361 
366  virtual bool hasChild (const char* /*tagName*/ = nullptr) VOID_IMPLEMENTATION_RET("TreeNode* hasChild()", false);
367 
368 
369 
371  virtual void readChild (bool& val, const char* name = nullptr) READ_LONG_CHILD_FOR_BOOL;
372 
374  virtual void readChild (char& val, const char* name = nullptr) READ_LONG_CHILD(char);
375 
377  virtual void readChild (unsigned char& val, const char* name = nullptr) READ_ULONG_CHILD(unsigned char);
378 
380  virtual void readChild (short& val, const char* name = nullptr) READ_LONG_CHILD(short);
381 
383  virtual void readChild (unsigned short& val, const char* name = nullptr) READ_ULONG_CHILD(unsigned short);
384 
386  virtual void readChild (int& val, const char* name = nullptr) READ_LONG_CHILD(int);
387 
389  virtual void readChild (unsigned int& val, const char* name = nullptr) READ_ULONG_CHILD(unsigned int);
390 
392  virtual void readChild (long& /*val*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(long&)");
393 
395  virtual void readChild (unsigned long& /*val*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(unsigned long&)");
396 
398  virtual void readChild (MLint64& /*val*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(MLint64&)");
399 
401  virtual void readChild (MLuint64& /*val*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(MLuint64&)");
402 
404  virtual void readChild (float& val, const char* name = nullptr) READ_LDOUBLE_CHILD(float);
405 
407  virtual void readChild (double& val, const char* name = nullptr) READ_LDOUBLE_CHILD(double);
408 
410  virtual void readChild (long double& /*val*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(long double&)");
411 
413  virtual void readChild (Vector2& /*vec*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(Vector2&)");
414 
416  virtual void readChild (Vector3& /*vec*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(Vector3&)");
417 
419  virtual void readChild (Vector4& /*vec*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(Vector4&)");
420 
422  virtual void readChild (Vector6& /*vec*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(Vector6&)");
423 
425  virtual void readChild (ImageVector& /*vec*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(ImageVector&)");
426 
428  virtual void readChild (Matrix3& /*mat*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(Matrix3&)");
429 
431  virtual void readChild (Matrix4& /*mat*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(Matrix4&)");
432 
434  virtual void readChild (SubImageBox& /*box*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(SubImageBox&)");
435 
437  virtual void readChild (SubImageBoxd& /*box*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(SubImageBoxd&)");
438 
442  virtual void readChild (void*& /*ptr*/, unsigned long& /*noBytes*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(char*&)");
443 
447  virtual void readChild (Base*& /*objP*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(Base*&)");
448 
452  virtual void readChild (Base& /*objP*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(Base&)");
453 
455  virtual void readChild (TreeNode*& /*node*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(TreeNode*&)");
456 
458  virtual void readChild(std::string& /*str*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(std::string&)");
459 
462  template <typename T> bool readOptionalChild(T& value, const char* name)
463  {
464  if (hasChild(name)) {
465  readChild(value, name);
466  return true;
467  }
468  return false;
469  }
470 
473  template <typename T> T getOptionalChildValue(const char* name, const T& defaultValue)
474  {
475  T value;
476  if (readOptionalChild(value, name)) {
477  return value;
478  }
479  return defaultValue;
480  }
481 
483 
484 
485  //-----------------------------------------------------------------------------
490  //-----------------------------------------------------------------------------
491 
493 
496  virtual TreeNode* readContainerChild (const char* name = nullptr)
497  {
498  TreeNode* node = nullptr;
499  readChild(node, name);
500  return node;
501  }
502 
503 
505 
512  virtual Base* readBaseChild (const char* name = nullptr)
513  {
514  Base* objP = nullptr;
515  readChild(objP, name);
516  return objP;
517  }
518 
520 
521  //------------------------------
523  //------------------------------
524 
526 
528  virtual void setVersion(const char* /*className*/, int /*version*/){ VOID_IMPLEMENTATION("setVersion()"); }
529 
531  virtual int getVersion(const char* /*className*/) { VOID_IMPLEMENTATION_RET("getVersion()", 0); }
532 
534  virtual const char* getLastReadChildName() const { VOID_IMPLEMENTATION_RET("getLastReadChildName()", nullptr); }
535 
537  bool isRootNode() const { return _isRoot; }
538 
541  virtual char* correctName(const char* /*name*/) const { VOID_IMPLEMENTATION_RET("correctName()", nullptr); }
542 
544  virtual bool isCorrectName(const char* /*name*/) const { VOID_IMPLEMENTATION_RET("isCorrectName()", false); }
545 
547 
549  virtual void deleteMemory(void* /*ptr*/){};
550 
551 protected:
552 
553 
554  //----------------------------------------------------------------------------------
560  //----------------------------------------------------------------------------------
562 
566  TreeNode(ConstructionMode /*mode*/) :
567  _isRoot(true)
568  {
569  }
570 
573  TreeNode() :
574  _isRoot(false)
575  {
576  }
577 
579 
584  std::vector<TreeNode*> &_getChildTreeNodes();
585 
586  //---------------------------------------------
589  //---------------------------------------------
590 
592 
595  void _appendNewChild(TreeNode& newNode) { _childTreeNodes.push_back(&newNode); }
596 
598 
599  //------------------------------------------------
603  //------------------------------------------------
604 
605 private:
606 
607 
608  //------------------------
611  //------------------------
612 
614 
617  std::vector<TreeNode*> _childTreeNodes;
618 
621  bool _isRoot;
622 
624 
628 
629 
630 #undef VOID_IMPLEMENTATION
631 #undef ADD_ULONG_CHILD
632 #undef ADD_LONG_CHILD
633 #undef ADD_LDOUBLE_CHILD
634 #undef READ_ULONG_CHILD
635 #undef READ_LONG_CHILD
636 #undef READ_LDOUBLE_CHILD
637 #undef READ_LONG_CHILD_FOR_BOOL
638 
639 }; // class TreeNode
640 
641 
642 //------------------------
645 //------------------------
646 
648 
651 #define ML_READCHILD_OPTIONAL(obj, tagName, defaultVal) \
652 if (parent->hasChild(tagName)) { \
653  parent->readChild(obj, tagName); \
654 } else { \
655  obj = defaultVal; \
656 }
657 
658 
660 #define ML_ADDSTATE_VERSION(ThisClass) parent->setVersion("#ThisClass#", ThisClass::getAddStateVersion());
661 
664 #define ML_ADDSTATE_SUPER(SuperClass) { \
665 TreeNode* superClassNode = parent->addChild("_" #SuperClass); \
666 SuperClass::addStateToTree(superClassNode); \
667 }
668 
671 #define ML_READSTATE_SUPER(SuperClass) { \
672 TreeNode* superClassNode = parent->readContainerChild("_" #SuperClass); \
673 SuperClass::readStateFromTree(superClassNode); \
674 }
675 
681 
684 #define ML_TREE_NODE_SUPPORT_VIA_PERSISTENT_STATE \
685 virtual void addStateToTree(TreeNode* parent) const \
686 { \
687 auto state = persistentState(); \
688 if (state.empty()){ state = "Could not read object state."; } \
689 parent->addChild(state, "State"); \
690 } \
691 \
692 virtual void readStateFromTree(TreeNode* parent) \
693 { \
694 std::string state; \
695 parent->readChild(state, "State"); \
696 setPersistentState(state); \
697 }
699 
700 ML_END_NAMESPACE
701 
702 #endif // __mlTreeNode_H
703 
704 
Class representing general ML objects that support import/export via strings (setPersistentState() an...
Definition: mlBase.h:59
SubImageBoxd - SubImageBox with coordinates of float data type.
The class TreeNodeException is the base class for all exceptions thrown by the class TreeNode and all...
Definition: mlTreeNode.h:95
virtual const char * getMessage() const
Returns the error string.
virtual ~TreeNodeException()
Destructor.
Definition: mlTreeNode.h:108
int getCode() const
Returns the error code.
Definition: mlTreeNode.h:111
int _errorCode
The error code of the exception.
Definition: mlTreeNode.h:125
TreeNodeException(int errorCode=0, const char *msg=nullptr)
Creates a new exception object with code errorCode and an optional error message.
The class TreeNode is the abstract base class for the import/export of ML objects.
Definition: mlTreeNode.h:154
virtual void addChild(char val, const char *name) ADD_LONG_CHILD
Factory method for adding a child encapsulating a variable of type char.
@ CM_readerRoot
Construct as root node for reading.
Definition: mlTreeNode.h:203
@ CM_writerRoot
Construct as root node for writing.
Definition: mlTreeNode.h:200
virtual ~TreeNode()
Destructor deleting all children of this node.
virtual void addChild(int val, const char *name) ADD_LONG_CHILD
Factory method for adding a child encapsulating a variable of type int.
virtual void addChild(MLint64, const char *) VOID_IMPLEMENTATION("addChild(MLint64)")
Factory method for adding a child encapsulating a variable of type MLint64.
virtual void addChild(unsigned short val, const char *name) ADD_ULONG_CHILD
Factory method for adding a child encapsulating a variable of type unsigned short.
virtual void addChild(double val, const char *name) ADD_LDOUBLE_CHILD
Factory method for adding a child encapsulating a variable of type double.
virtual void addChild(short val, const char *name) ADD_LONG_CHILD
Factory method for adding a child encapsulating a variable of type short.
virtual void writeToString(std::string &) VOID_IMPLEMENTATION("writeToString")
Generates a string representation of the subtree represented by this node to a file.
virtual void writeToFile(const char *) VOID_IMPLEMENTATION("writeToFile")
Writes the subtree represented by this node to a file.
virtual void addChild(unsigned int val, const char *name) ADD_ULONG_CHILD
Factory method for adding a child encapsulating a variable of type unsigned int.
virtual void addChild(unsigned char val, const char *name) ADD_ULONG_CHILD
Factory method for adding a child encapsulating a variable of type unsigned char.
virtual void addChild(long, const char *) VOID_IMPLEMENTATION("addChild(long)")
Factory method for adding a child encapsulating a variable of type long.
virtual void addChild(bool val, const char *name) ADD_ULONG_CHILD
Factory method for adding a child encapsulating a variable of type bool.
virtual void addChild(unsigned long, const char *) VOID_IMPLEMENTATION("addChild(unsigned long)")
Factory method for adding a child encapsulating a variable of type long.
virtual void addChild(MLuint64, const char *) VOID_IMPLEMENTATION("addChild(MLuint64)")
Factory method for adding a child encapsulating a variable of type MLuint64.
virtual void readFromFile(const char *) VOID_IMPLEMENTATION("readFromFile")
Reads the subtree represented by this node from a file.
virtual void addChild(float val, const char *name) ADD_LDOUBLE_CHILD
Factory method for adding a child encapsulating a variable of type float.
virtual void addChild(long double, const char *) VOID_IMPLEMENTATION("addChild(long double)")
Factory method for adding a child encapsulating a variable of type long double.
virtual void readFromString(const std::string &) VOID_IMPLEMENTATION("readFromString")
Reads the subtree represented by this node from a string.
#define MLEXPORT
To export symbols from a DLL/shared object, we need to mark them with the MLEXPORT symbol.
#define ML_ABSTRACT_ROOT_CLASS_HEADER(className)
#define ML_ROOT_CLASS_HEADER(className)
#define ADD_LONG_CHILD
For each signed integer type, addChild(long) is called by default.
Definition: mlTreeNode.h:171
#define READ_LDOUBLE_CHILD(DST_TYPE)
For each floating point type, readChild(LDouble) is called by default and the results is simply cast ...
Definition: mlTreeNode.h:189
#define VOID_IMPLEMENTATION(which)
Standard body for a method in this interface class.
Definition: mlTreeNode.h:162
#define VOID_IMPLEMENTATION_RET(which, R)
Standard body for a method in this interface class.
Definition: mlTreeNode.h:168
#define READ_LONG_CHILD_FOR_BOOL
For each signed integer type, readChild(Long) is called by default and the results is simply cast to ...
Definition: mlTreeNode.h:193
#define ADD_LDOUBLE_CHILD
For each floating point type, addChild(LDouble) is called by default.
Definition: mlTreeNode.h:177
#define READ_LONG_CHILD(DST_TYPE)
For each signed integer type, readChild(Long) is called by default and the results is simply cast to ...
Definition: mlTreeNode.h:181
#define READ_ULONG_CHILD(DST_TYPE)
For each unsigned integer type, readChild(ULong) is called by default and the results is simply cast ...
Definition: mlTreeNode.h:185
#define ADD_ULONG_CHILD
For each unsigned integer type, addChild(ulong) is called by default.
Definition: mlTreeNode.h:174
UINT64 MLuint64
Introduce platform-independent 64-bit unsigned integer type.
Definition: mlTypeDefs.h:425
INT64 MLint64
Include 64-bit integer support for Windows or Unix.
Definition: mlTypeDefs.h:412
@ TNE_ReadingBase
Definition: mlTreeNode.h:46
@ TNE_ReadingUInt
Definition: mlTreeNode.h:51
@ TNE_ReadingChar
Definition: mlTreeNode.h:48
@ TNE_ReadingMLuint64
Definition: mlTreeNode.h:77
@ TNE_ReadingVector6
Definition: mlTreeNode.h:62
@ TNE_ReadingUChar
Definition: mlTreeNode.h:47
@ TNE_ReadingInt
Definition: mlTreeNode.h:52
@ TNE_ReadingMatrix4
Definition: mlTreeNode.h:65
@ TNE_VoidImplementation
Definition: mlTreeNode.h:42
@ TNE_ReadingShort
Definition: mlTreeNode.h:50
@ TNE_ReadingUShort
Definition: mlTreeNode.h:49
@ TNE_ReadingString
Definition: mlTreeNode.h:58
@ TNE_InvalidReadNext
Definition: mlTreeNode.h:68
@ TNE_ReadingSubImageBox
Definition: mlTreeNode.h:66
@ TNE_ReadingDouble
Definition: mlTreeNode.h:56
@ TNE_ReadingVector4
Definition: mlTreeNode.h:61
@ TNE_ReadingLong
Definition: mlTreeNode.h:54
@ TNE_ReadingULong
Definition: mlTreeNode.h:53
@ TNE_COUNT
Definition: mlTreeNode.h:79
@ TNE_ReadingImageVector
Definition: mlTreeNode.h:63
@ TNE_ReadingFile
Definition: mlTreeNode.h:71
@ TNE_NotSupported
Definition: mlTreeNode.h:43
@ TNE_UnsupportedClassVersion
Definition: mlTreeNode.h:73
@ TNE_ReadingMatrix3
Definition: mlTreeNode.h:64
@ TNE_AddingBase
Definition: mlTreeNode.h:45
@ TNE_ReadingSubImageBoxd
Definition: mlTreeNode.h:67
@ TNE_ReadingMLint64
Definition: mlTreeNode.h:76
@ TNE_ReadingVector3
Definition: mlTreeNode.h:60
@ TNE_ReadingFloat
Definition: mlTreeNode.h:55
@ TNE_InvalidParentNode
Definition: mlTreeNode.h:69
@ TNE_UserDefined
Definition: mlTreeNode.h:74
@ TNE_ReadingVector2
Definition: mlTreeNode.h:59
@ TNE_ReadingLDouble
Definition: mlTreeNode.h:57
@ TNE_FileNotFound
Definition: mlTreeNode.h:70
@ TNE_Unknown
Definition: mlTreeNode.h:75
@ TNE_WritingFile
Definition: mlTreeNode.h:72
@ TNE_ChildNotFound
Definition: mlTreeNode.h:44