MeVisLab Toolbox 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
32
33class Base;
34class SubImageBoxd;
35
36//---------------------------------------------------------------------------------
40//---------------------------------------------------------------------------------
41enum {
78
80};
81
82
83//---------------------------------------------------------------------------------
93//---------------------------------------------------------------------------------
95{
96
97public:
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
122protected:
123
126
127private:
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
157public:
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
240public:
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
301
303 virtual void addChild(const std::string& /*str*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(const std::string&)");
304
306 virtual void addChild (const Vector2& /*vec*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(Vector2)");
307
309 virtual void addChild (const Vector3& /*vec*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(Vector3)");
310
312 virtual void addChild (const Vector4& /*vec*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(Vector4)");
313
315 virtual void addChild (const Vector6& /*vec*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(Vector6)");
316
318 virtual void addChild (const ImageVector& /*vec*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(ImageVector)");
319
321 virtual void addChild (const Matrix3& /*mat*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(Matrix3)");
322
324 virtual void addChild (const Matrix4& /*mat*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(Matrix4)");
325
327 virtual void addChild (const SubImageBox& /*box*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(SubImageBox)");
328
330 virtual void addChild (const SubImageBoxd& /*box*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(SubImageBoxd)");
331
334 virtual void addChild (const void* const /*ptr*/, size_t /*noBytes*/, const char* /*name*/) VOID_IMPLEMENTATION("addChild(void*)");
335
337
340 virtual void addChild (const Base* const /*obj*/, const char* /*name*/, bool /*generic*/ = true) VOID_IMPLEMENTATION("addChild(Base*)");
341
343
346 virtual TreeNode* addChild (const char* /*name*/) VOID_IMPLEMENTATION_RET("TreeNode* addChild()", nullptr);
347
349
350
351
352
353 //--------------------------------------------------------------------------------------------
359 //--------------------------------------------------------------------------------------------
360
362
367 virtual bool hasChild (const char* /*tagName*/ = nullptr) VOID_IMPLEMENTATION_RET("TreeNode* hasChild()", false);
368
369
370
372 virtual void readChild (bool& val, const char* name = nullptr) READ_LONG_CHILD_FOR_BOOL;
373
375 virtual void readChild (char& val, const char* name = nullptr) READ_LONG_CHILD(char);
376
378 virtual void readChild (unsigned char& val, const char* name = nullptr) READ_ULONG_CHILD(unsigned char);
379
381 virtual void readChild (short& val, const char* name = nullptr) READ_LONG_CHILD(short);
382
384 virtual void readChild (unsigned short& val, const char* name = nullptr) READ_ULONG_CHILD(unsigned short);
385
387 virtual void readChild (int& val, const char* name = nullptr) READ_LONG_CHILD(int);
388
390 virtual void readChild (unsigned int& val, const char* name = nullptr) READ_ULONG_CHILD(unsigned int);
391
393 virtual void readChild (long& /*val*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(long&)");
394
396 virtual void readChild (unsigned long& /*val*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(unsigned long&)");
397
399 virtual void readChild (MLint64& /*val*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(MLint64&)");
400
402 virtual void readChild (MLuint64& /*val*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(MLuint64&)");
403
405 virtual void readChild (float& val, const char* name = nullptr) READ_LDOUBLE_CHILD(float);
406
408 virtual void readChild (double& val, const char* name = nullptr) READ_LDOUBLE_CHILD(double);
409
411 virtual void readChild (long double& /*val*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(long double&)");
412
414 virtual void readChild (Vector2& /*vec*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(Vector2&)");
415
417 virtual void readChild (Vector3& /*vec*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(Vector3&)");
418
420 virtual void readChild (Vector4& /*vec*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(Vector4&)");
421
423 virtual void readChild (Vector6& /*vec*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(Vector6&)");
424
426 virtual void readChild (ImageVector& /*vec*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(ImageVector&)");
427
429 virtual void readChild (Matrix3& /*mat*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(Matrix3&)");
430
432 virtual void readChild (Matrix4& /*mat*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(Matrix4&)");
433
435 virtual void readChild (SubImageBox& /*box*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(SubImageBox&)");
436
438 virtual void readChild (SubImageBoxd& /*box*/, const char* /*name*/ = nullptr) VOID_IMPLEMENTATION("readChild(SubImageBoxd&)");
439
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
460public:
461
463
464
465 //-----------------------------------------------------------------------------
470 //-----------------------------------------------------------------------------
471
473
476 virtual TreeNode* readContainerChild (const char* name = nullptr)
477 {
478 TreeNode* node = nullptr;
479 readChild(node, name);
480 return node;
481 }
482
483
485
490 virtual Base* readBaseChild (const char* name = nullptr)
491 {
492 Base* objP = nullptr;
493 readChild(objP, name);
494 return objP;
495 }
496
498
499 //------------------------------
501 //------------------------------
502
504
506 virtual void setVersion(const char* /*className*/, int /*version*/){ VOID_IMPLEMENTATION("setVersion()"); }
507
509 virtual int getVersion(const char* /*className*/) { VOID_IMPLEMENTATION_RET("getVersion()", 0); }
510
512 virtual const char* getLastReadChildName() const { VOID_IMPLEMENTATION_RET("getLastReadChildName()", nullptr); }
513
515 bool isRootNode() const { return _isRoot; }
516
519 virtual char* correctName(const char* /*name*/) const { VOID_IMPLEMENTATION_RET("correctName()", nullptr); }
520
522 virtual bool isCorrectName(const char* /*name*/) const { VOID_IMPLEMENTATION_RET("isCorrectName()", false); }
523
525
526protected:
527
528
529 //----------------------------------------------------------------------------------
535 //----------------------------------------------------------------------------------
537
541 TreeNode(ConstructionMode /*mode*/) :
543 {
544 }
545
548 TreeNode() :
550 {
551 }
552
554
559 std::vector<TreeNode*> &_getChildTreeNodes();
560
561 //---------------------------------------------
564 //---------------------------------------------
565
567
570 void _appendNewChild(TreeNode& newNode) { _childTreeNodes.push_back(&newNode); }
571
573
574 //------------------------------------------------
578 //------------------------------------------------
579
580private:
581
582
583 //------------------------
586 //------------------------
587
589
592 std::vector<TreeNode*> _childTreeNodes;
593
596 bool _isRoot;
597
599
603
604
605#undef VOID_IMPLEMENTATION
606#undef ADD_ULONG_CHILD
607#undef ADD_LONG_CHILD
608#undef ADD_LDOUBLE_CHILD
609#undef READ_ULONG_CHILD
610#undef READ_LONG_CHILD
611#undef READ_LDOUBLE_CHILD
612#undef READ_LONG_CHILD_FOR_BOOL
613
614}; // class TreeNode
615
616
617//------------------------
620//------------------------
621
623
626#define ML_READCHILD_OPTIONAL(obj, tagName, defaultVal) \
627if (parent->hasChild(tagName)) { \
628 parent->readChild(obj, tagName); \
629} else { \
630 obj = defaultVal; \
631}
632
633
635#define ML_ADDSTATE_VERSION(ThisClass) parent->setVersion("#ThisClass#", ThisClass::getAddStateVersion());
636
639#define ML_ADDSTATE_SUPER(SuperClass) { \
640TreeNode* superClassNode = parent->addChild("_" #SuperClass); \
641SuperClass::addStateToTree(superClassNode); \
642}
643
646#define ML_READSTATE_SUPER(SuperClass) { \
647TreeNode* superClassNode = parent->readContainerChild("_" #SuperClass); \
648SuperClass::readStateFromTree(superClassNode); \
649}
650
656
659#define ML_TREE_NODE_SUPPORT_VIA_PERSISTENT_STATE \
660virtual void addStateToTree(TreeNode* parent) const \
661{ \
662auto state = persistentState(); \
663if (state.empty()){ state = "Could not read object state."; } \
664parent->addChild(state, "State"); \
665} \
666\
667virtual void readStateFromTree(TreeNode* parent) \
668{ \
669std::string state; \
670parent->readChild(state, "State"); \
671setPersistentState(state); \
672}
674
676
677#endif // __mlTreeNode_H
678
679
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 ~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.
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:154
virtual void addChild(char val, const char *name) ADD_LONG_CHILD
Factory method 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 adding a child encapsulating a variable of type int.
virtual void addChild(MLint64, const char *) VOID_IMPLEMENTATION("addChild(MLint64)")
Factory method adding a child encapsulating a variable of type MLint64.
virtual void addChild(unsigned short val, const char *name) ADD_ULONG_CHILD
Factory method adding a child encapsulating a variable of type unsigned short.
virtual void addChild(double val, const char *name) ADD_LDOUBLE_CHILD
Factory method adding a child encapsulating a variable of type double.
virtual void addChild(short val, const char *name) ADD_LONG_CHILD
Factory method 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 adding a child encapsulating a variable of type unsigned int.
virtual void addChild(unsigned char val, const char *name) ADD_ULONG_CHILD
Factory method adding a child encapsulating a variable of type unsigned char.
virtual void addChild(long, const char *) VOID_IMPLEMENTATION("addChild(long)")
Factory method adding a child encapsulating a variable of type long.
virtual void addChild(bool val, const char *name) ADD_ULONG_CHILD
Factory method adding a child encapsulating a variable of type bool.
virtual void addChild(unsigned long, const char *) VOID_IMPLEMENTATION("addChild(unsigned long)")
Factory method adding a child encapsulating a variable of type long.
virtual void addChild(MLuint64, const char *) VOID_IMPLEMENTATION("addChild(MLuint64)")
Factory method 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 adding a child encapsulating a variable of type float.
virtual void addChild(long double, const char *) VOID_IMPLEMENTATION("addChild(long double)")
Factory method 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:171
#define READ_LDOUBLE_CHILD(DST_TYPE)
For each floating point type, readChild(LDouble) is called by default and the results is simply caste...
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 casted t...
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 casted t...
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 caste...
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
STL namespace.