MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
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
32
33class Base;
34class SubImageBoxd;
35
36//---------------------------------------------------------------------------------
40//---------------------------------------------------------------------------------
41enum {
78
80};
81
82
83//---------------------------------------------------------------------------------
93//---------------------------------------------------------------------------------
95{
96
97public:
100
104 TreeNodeException(int errorCode = 0, const char* msg = nullptr);
105
108 {}
109
111 int getCode() const
112 {
113 return _errorCode;
114 }
115
117
123 virtual const char* getMessage() const;
124
125protected:
128
129private:
130 std::string _message;
131
134 static const char* _stdErrorMsg[];
135
139};
140
141
143#define TREE_NODE_CHECK_THROW(x) \
144 { \
145 if (!(x)) \
146 throw TreeNodeException(TNE_UserDefined, #x); \
147 }
148
149
152
160{
161
162
163public:
164 // Define macros for default implementations:
165
167#define VOID_IMPLEMENTATION(which) \
168 { \
169 throw TreeNodeException(TNE_VoidImplementation, which); \
170 }
171
176#define VOID_IMPLEMENTATION_RET(which, R) \
177 { \
178 if (MLAlwaysTrue) { \
179 throw TreeNodeException(TNE_VoidImplementation, which); \
180 } \
181 return R; \
182 }
183
185#define ADD_LONG_CHILD \
186 { \
187 addChild(static_cast<long>(val), name); \
188 }
189
191#define ADD_ULONG_CHILD \
192 { \
193 addChild(static_cast<unsigned long>(val), name); \
194 }
195
197#define ADD_LDOUBLE_CHILD \
198 { \
199 addChild(static_cast<long double>(val), name); \
200 }
201
204#define READ_LONG_CHILD(DST_TYPE) \
205 { \
206 long lval = 0; \
207 readChild(lval, name); \
208 val = static_cast<DST_TYPE>(lval); \
209 }
210
213#define READ_ULONG_CHILD(DST_TYPE) \
214 { \
215 unsigned long lval = 0; \
216 readChild(lval, name); \
217 val = static_cast<DST_TYPE>(lval); \
218 }
219
222#define READ_LDOUBLE_CHILD(DST_TYPE) \
223 { \
224 long double ldval = 0; \
225 readChild(ldval, name); \
226 val = static_cast<DST_TYPE>(ldval); \
227 }
228
231#define READ_LONG_CHILD_FOR_BOOL \
232 { \
233 long lval = 0; \
234 readChild(lval, name); \
235 val = (lval != 0); \
236 }
237
238
240 typedef enum {
241
244
247
249 CM_COUNT
250
251 } ConstructionMode;
252
253
255 virtual ~TreeNode();
256
257 //--------------------------------------------------------------
261 //--------------------------------------------------------------
262
264
266 virtual void writeToFile(const char* /*fileName*/) VOID_IMPLEMENTATION("writeToFile");
267
269 virtual void readFromFile(const char* /*fileName*/) VOID_IMPLEMENTATION("readFromFile");
270
272
275 virtual void writeToString(std::string& /*str*/) VOID_IMPLEMENTATION("writeToString");
276
278
281 virtual void readFromString(const std::string& /*str*/) VOID_IMPLEMENTATION("readFromString");
282
283public:
285
286 //------------------------------------------------------------------------------------------
297 //------------------------------------------------------------------------------------------
298
300
302 virtual void addChild(bool val, const char* name) ADD_ULONG_CHILD;
303
305 virtual void addChild(unsigned char val, const char* name) ADD_ULONG_CHILD;
306
308 virtual void addChild(char val, const char* name) ADD_LONG_CHILD;
309
311 virtual void addChild(unsigned short val, const char* name) ADD_ULONG_CHILD;
312
314 virtual void addChild(short val, const char* name) ADD_LONG_CHILD;
315
317 virtual void addChild(unsigned int val, const char* name) ADD_ULONG_CHILD;
318
320 virtual void addChild(int val, const char* name) ADD_LONG_CHILD;
321
323 virtual void addChild(unsigned long /*val*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(unsigned long)");
324
326 virtual void addChild(long /*val*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(long)");
327
329 virtual void addChild(MLuint64 /*val*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(MLuint64)");
330
332 virtual void addChild(MLint64 /*val*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(MLint64)");
333
335 virtual void addChild(float val, const char* name) ADD_LDOUBLE_CHILD;
336
338 virtual void addChild(double val, const char* name) ADD_LDOUBLE_CHILD;
339
341 virtual void addChild(long double /*val*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(long double)");
342
344 virtual void addChild(const std::string& /*str*/, const char* /*name*/)
345 VOID_IMPLEMENTATION("addChild(const std::string&)");
346
348 virtual void addChild(const Vector2& /*vec*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(Vector2)");
349
351 virtual void addChild(const Vector3& /*vec*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(Vector3)");
352
354 virtual void addChild(const Vector4& /*vec*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(Vector4)");
355
357 virtual void addChild(const Vector6& /*vec*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(Vector6)");
358
360 virtual void addChild(const ImageVector& /*vec*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(ImageVector)");
361
363 virtual void addChild(const Matrix3& /*mat*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(Matrix3)");
364
366 virtual void addChild(const Matrix4& /*mat*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(Matrix4)");
367
369 virtual void addChild(const SubImageBox& /*box*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(SubImageBox)");
370
372 virtual void addChild(const SubImageBoxd& /*box*/, const char* /*name*/)
373 VOID_IMPLEMENTATION("addChild(SubImageBoxd)");
374
377 virtual void addChild(const void* const /*ptr*/, size_t /*noBytes*/, const char* /*name*/)
378 VOID_IMPLEMENTATION("addChild(void*)");
379
381
384 virtual void addChild(const Base* const /*obj*/, const char* /*name*/, bool /*generic*/ = true)
385 VOID_IMPLEMENTATION("addChild(Base*)");
386
388
391 virtual TreeNode* addChild(const char* /*name*/) VOID_IMPLEMENTATION_RET("TreeNode* addChild()", nullptr);
392
394
395
396 //--------------------------------------------------------------------------------------------
402 //--------------------------------------------------------------------------------------------
403
405
410 virtual bool hasChild(const char* /*tagName*/ = nullptr) VOID_IMPLEMENTATION_RET("TreeNode* hasChild()", false);
411
412
414 virtual void readChild(bool& val, const char* name = nullptr) READ_LONG_CHILD_FOR_BOOL;
415
417 virtual void readChild(char& val, const char* name = nullptr) READ_LONG_CHILD(char);
418
420 virtual void readChild(unsigned char& val, const char* name = nullptr) READ_ULONG_CHILD(unsigned char);
421
423 virtual void readChild(short& val, const char* name = nullptr) READ_LONG_CHILD(short);
424
426 virtual void readChild(unsigned short& val, const char* name = nullptr) READ_ULONG_CHILD(unsigned short);
427
429 virtual void readChild(int& val, const char* name = nullptr) READ_LONG_CHILD(int);
430
432 virtual void readChild(unsigned int& val, const char* name = nullptr) READ_ULONG_CHILD(unsigned int);
433
435 virtual void readChild(long& /*val*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(long&)");
436
438 virtual void readChild(unsigned long& /*val*/, const char* /*name*/ = nullptr)
439 VOID_IMPLEMENTATION("readChild(unsigned long&)");
440
442 virtual void readChild(MLint64& /*val*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(MLint64&)");
443
445 virtual void readChild(MLuint64& /*val*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(MLuint64&)");
446
448 virtual void readChild(float& val, const char* name = nullptr) READ_LDOUBLE_CHILD(float);
449
451 virtual void readChild(double& val, const char* name = nullptr) READ_LDOUBLE_CHILD(double);
452
454 virtual void readChild(long double& /*val*/, const char* /*name*/ = nullptr)
455 VOID_IMPLEMENTATION("readChild(long double&)");
456
458 virtual void readChild(Vector2& /*vec*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(Vector2&)");
459
461 virtual void readChild(Vector3& /*vec*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(Vector3&)");
462
464 virtual void readChild(Vector4& /*vec*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(Vector4&)");
465
467 virtual void readChild(Vector6& /*vec*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(Vector6&)");
468
470 virtual void readChild(ImageVector& /*vec*/, const char* /*name*/ = nullptr)
471 VOID_IMPLEMENTATION("readChild(ImageVector&)");
472
474 virtual void readChild(Matrix3& /*mat*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(Matrix3&)");
475
477 virtual void readChild(Matrix4& /*mat*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(Matrix4&)");
478
480 virtual void readChild(SubImageBox& /*box*/, const char* /*name*/ = nullptr)
481 VOID_IMPLEMENTATION("readChild(SubImageBox&)");
482
484 virtual void readChild(SubImageBoxd& /*box*/, const char* /*name*/ = nullptr)
485 VOID_IMPLEMENTATION("readChild(SubImageBoxd&)");
486
490 virtual void readChild(void*& /*ptr*/, unsigned long& /*noBytes*/, const char* /*name*/ = nullptr)
491 VOID_IMPLEMENTATION("readChild(char*&)");
492
496 virtual void readChild(Base*& /*objP*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(Base*&)");
497
501 virtual void readChild(Base& /*objP*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(Base&)");
502
504 virtual void readChild(TreeNode*& /*node*/, const char* /*name*/ = nullptr)
505 VOID_IMPLEMENTATION("readChild(TreeNode*&)");
506
508 virtual void readChild(std::string& /*str*/, const char* /*name*/ = nullptr)
509 VOID_IMPLEMENTATION("readChild(std::string&)");
510
513 template <typename T> bool readOptionalChild(T& value, const char* name)
514 {
515 if (hasChild(name)) {
516 readChild(value, name);
517 return true;
518 }
519 return false;
520 }
521
524 template <typename T> T getOptionalChildValue(const char* name, const T& defaultValue)
525 {
526 T value;
527 if (readOptionalChild(value, name)) {
528 return value;
529 }
530 return defaultValue;
531 }
532
534
535
536 //-----------------------------------------------------------------------------
541 //-----------------------------------------------------------------------------
542
544
547 virtual TreeNode* readContainerChild(const char* name = nullptr)
548 {
549 TreeNode* node = nullptr;
550 readChild(node, name);
551 return node;
552 }
553
554
556
563 virtual Base* readBaseChild(const char* name = nullptr)
564 {
565 Base* objP = nullptr;
566 readChild(objP, name);
567 return objP;
568 }
569
571
572 //------------------------------
574 //------------------------------
575
577
579 virtual void setVersion(const char* /*className*/, int /*version*/)
580 {
581 VOID_IMPLEMENTATION("setVersion()");
582 }
583
585 virtual int getVersion(const char* /*className*/)
586 {
587 VOID_IMPLEMENTATION_RET("getVersion()", 0);
588 }
589
591 virtual const char* getLastReadChildName() const
592 {
593 VOID_IMPLEMENTATION_RET("getLastReadChildName()", nullptr);
594 }
595
597 bool isRootNode() const
598 {
599 return _isRoot;
600 }
601
604 virtual char* correctName(const char* /*name*/) const
605 {
606 VOID_IMPLEMENTATION_RET("correctName()", nullptr);
607 }
608
610 virtual bool isCorrectName(const char* /*name*/) const
611 {
612 VOID_IMPLEMENTATION_RET("isCorrectName()", false);
613 }
614
616
618 virtual void deleteMemory(void* /*ptr*/) {};
619
620protected:
621 //----------------------------------------------------------------------------------
627 //----------------------------------------------------------------------------------
629
633 TreeNode(ConstructionMode /*mode*/)
634 : _isRoot(true)
635 {}
636
639 TreeNode()
640 : _isRoot(false)
641 {}
642
644
649 std::vector<TreeNode*>& _getChildTreeNodes();
650
651 //---------------------------------------------
654 //---------------------------------------------
655
657
660 void _appendNewChild(TreeNode& newNode)
661 {
662 _childTreeNodes.push_back(&newNode);
663 }
664
666
667 //------------------------------------------------
671 //------------------------------------------------
672
673private:
674 //------------------------
677 //------------------------
678
680
683 std::vector<TreeNode*> _childTreeNodes;
684
687 bool _isRoot;
688
690
694
695
696#undef VOID_IMPLEMENTATION
697#undef ADD_ULONG_CHILD
698#undef ADD_LONG_CHILD
699#undef ADD_LDOUBLE_CHILD
700#undef READ_ULONG_CHILD
701#undef READ_LONG_CHILD
702#undef READ_LDOUBLE_CHILD
703#undef READ_LONG_CHILD_FOR_BOOL
704
705}; // class TreeNode
706
707
708//------------------------
711//------------------------
712
714
717#define ML_READCHILD_OPTIONAL(obj, tagName, defaultVal) \
718 if (parent->hasChild(tagName)) { \
719 parent->readChild(obj, tagName); \
720 } \
721 else { \
722 obj = defaultVal; \
723 }
724
725
727#define ML_ADDSTATE_VERSION(ThisClass) parent->setVersion("#ThisClass#", ThisClass::getAddStateVersion());
728
731#define ML_ADDSTATE_SUPER(SuperClass) \
732 { \
733 TreeNode* superClassNode = parent->addChild("_" #SuperClass); \
734 SuperClass::addStateToTree(superClassNode); \
735 }
736
739#define ML_READSTATE_SUPER(SuperClass) \
740 { \
741 TreeNode* superClassNode = parent->readContainerChild("_" #SuperClass); \
742 SuperClass::readStateFromTree(superClassNode); \
743 }
744
750
753#define ML_TREE_NODE_SUPPORT_VIA_PERSISTENT_STATE \
754 virtual void addStateToTree(TreeNode* parent) const \
755 { \
756 auto state = persistentState(); \
757 if (state.empty()) { \
758 state = "Could not read object state."; \
759 } \
760 parent->addChild(state, "State"); \
761 } \
762 \
763 virtual void readStateFromTree(TreeNode* parent) \
764 { \
765 std::string state; \
766 parent->readChild(state, "State"); \
767 setPersistentState(state); \
768 }
770
772
773#endif // __mlTreeNode_H
@ T
Class representing general ML objects that support import/export via strings (setPersistentState() an...
Definition mlBase.h:63
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 ~TreeNodeException()
Destructor.
Definition mlTreeNode.h:107
int getCode() const
Returns the error code.
Definition mlTreeNode.h:111
int _errorCode
The error code of the exception.
Definition mlTreeNode.h:127
TreeNodeException(int errorCode=0, const char *msg=nullptr)
Creates a new exception object with code errorCode and an optional error message.
virtual const char * getMessage() const
Returns the error string.
The class TreeNode is the abstract base class for the import/export of ML objects.
Definition mlTreeNode.h:160
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:246
@ CM_writerRoot
Construct as root node for writing.
Definition mlTreeNode.h:243
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.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
#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:185
#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:222
#define VOID_IMPLEMENTATION(which)
Standard body for a method in this interface class.
Definition mlTreeNode.h:167
#define VOID_IMPLEMENTATION_RET(which, R)
Standard body for a method in this interface class.
Definition mlTreeNode.h:176
#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:231
#define ADD_LDOUBLE_CHILD
For each floating point type, addChild(LDouble) is called by default.
Definition mlTreeNode.h:197
#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:204
#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:213
#define ADD_ULONG_CHILD
For each unsigned integer type, addChild(ulong) is called by default.
Definition mlTreeNode.h:191
UINT64 MLuint64
Introduce platform-independent 64-bit unsigned integer type.
Definition mlTypeDefs.h:424
INT64 MLint64
Include 64-bit integer support for Windows or Unix.
Definition mlTypeDefs.h:411
@ 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
STL namespace.