Slicer 5.9
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
vtkMRMLNodePropertyMacros.h
Go to the documentation of this file.
1/*=auto=========================================================================
2
3 Portions (c) Copyright 2005 Brigham and Women's Hospital (BWH) All Rights Reserved.
4
5 See COPYRIGHT.txt
6 or http://www.slicer.org/copyright/copyright.txt for details.
7
8=========================================================================auto=*/
9
10#ifndef __vtkMRMLNodePropertyMacros_h
11#define __vtkMRMLNodePropertyMacros_h
12
13#include <sstream> // needed for std::stringstream
14#include <vtksys/SystemTools.hxx> // needed for vtksys::SystemTools functions
15
17
18//----------------------------------------------------------------------------
26
29#define vtkMRMLWriteXMLBeginMacro(of) \
30 { \
31 ostream& xmlWriteOutputStream = of;
32
34#define vtkMRMLWriteXMLEndMacro() \
35 }
36
38#define vtkMRMLWriteXMLBooleanMacro(xmlAttributeName, propertyName) \
39 xmlWriteOutputStream << " " #xmlAttributeName "=\"" << (Get##propertyName() ? "true" : "false") << "\"";
40
43#define vtkMRMLWriteXMLStringMacro(xmlAttributeName, propertyName) \
44 if (Get##propertyName() != nullptr) \
45 { \
46 xmlWriteOutputStream << " " #xmlAttributeName "=\""; \
47 xmlWriteOutputStream << vtkMRMLNode::XMLAttributeEncodeString(Get##propertyName()); \
48 xmlWriteOutputStream << "\""; \
49 }
50
52#define vtkMRMLWriteXMLStdStringMacro(xmlAttributeName, propertyName) \
53 xmlWriteOutputStream << " " #xmlAttributeName "=\"" << vtkMRMLNode::XMLAttributeEncodeString(Get##propertyName().c_str()) << "\""; \
54
55
57#define vtkMRMLWriteXMLEnumMacro(xmlAttributeName, propertyName) \
58 xmlWriteOutputStream << " " #xmlAttributeName "=\""; \
59 if (Get##propertyName##AsString(Get##propertyName()) != nullptr) \
60 { \
61 xmlWriteOutputStream << vtkMRMLNode::XMLAttributeEncodeString(Get##propertyName##AsString(Get##propertyName())); \
62 } \
63 xmlWriteOutputStream << "\"";
64
66#define vtkMRMLWriteXMLIntMacro(xmlAttributeName, propertyName) \
67 xmlWriteOutputStream << " " #xmlAttributeName "=\"" << Get##propertyName() << "\"";
68
70#define vtkMRMLWriteXMLFloatMacro(xmlAttributeName, propertyName) \
71 xmlWriteOutputStream << " " #xmlAttributeName "=\"" << Get##propertyName() << "\"";
72
74#define vtkMRMLWriteXMLVectorMacro(xmlAttributeName, propertyName, vectorType, vectorSize) \
75 { \
76 xmlWriteOutputStream << " " #xmlAttributeName "=\""; \
77 vectorType* vectorPtr = Get##propertyName(); \
78 if (vectorPtr != nullptr) \
79 { \
80 for (int i=0; i<vectorSize; i++) \
81 { \
82 if (i > 0) \
83 { \
84 xmlWriteOutputStream << " "; \
85 } \
86 xmlWriteOutputStream << vectorPtr[i]; \
87 } \
88 } \
89 xmlWriteOutputStream << "\""; \
90 }
91
93#define vtkMRMLWriteXMLStdFloatVectorMacro(xmlAttributeName, propertyName, vectorType) \
94 { \
95 xmlWriteOutputStream << " " #xmlAttributeName "=\""; \
96 vectorType vector = Get##propertyName(); \
97 for (vectorType::iterator it=vector.begin(); it!=vector.end(); it++) \
98 { \
99 xmlWriteOutputStream << *it; \
100 xmlWriteOutputStream << " "; \
101 } \
102 xmlWriteOutputStream << "\""; \
103 }
104
106#define vtkMRMLWriteXMLStdIntVectorMacro(xmlAttributeName, propertyName, vectorType) \
107 { \
108 xmlWriteOutputStream << " " #xmlAttributeName "=\""; \
109 vectorType vector = Get##propertyName(); \
110 for (vectorType::iterator it=vector.begin(); it!=vector.end(); it++) \
111 { \
112 xmlWriteOutputStream << *it; \
113 xmlWriteOutputStream << " "; \
114 } \
115 xmlWriteOutputStream << "\""; \
116 }
117
119#define vtkMRMLWriteXMLStdStringVectorMacro(xmlAttributeName, propertyName, vectorType) \
120 { \
121 xmlWriteOutputStream << " " #xmlAttributeName "=\""; \
122 vectorType<std::string> vector = Get##propertyName(); \
123 for (vectorType<std::string>::iterator it=vector.begin(); it!=vector.end(); it++) \
124 { \
125 if (it!=vector.begin()) \
126 { \
127 xmlWriteOutputStream << ";"; \
128 } \
129 std::string attributeValue = *it; \
130 vtksys::SystemTools::ReplaceString(attributeValue, "%", "%25"); \
131 vtksys::SystemTools::ReplaceString(attributeValue, ";", "%3B"); \
132 xmlWriteOutputStream << vtkMRMLNode::XMLAttributeEncodeString(attributeValue); \
133 } \
134 xmlWriteOutputStream << "\""; \
135 }
136
137
139#define vtkMRMLWriteXMLMatrix4x4Macro(xmlAttributeName, propertyName) \
140 { \
141 xmlWriteOutputStream << " " #xmlAttributeName "=\""; \
142 vtkMatrix4x4* matrix = this->Get##propertyName(); \
143 for (int row = 0; row < 4; row++) \
144 { \
145 for (int col = 0; col < 4; col++) \
146 { \
147 of << matrix->GetElement(row, col); \
148 if (!(row == 3 && col == 3)) \
149 { \
150 of << " "; \
151 } \
152 } \
153 } \
154 xmlWriteOutputStream << "\""; \
155 }
156
158
159//----------------------------------------------------------------------------
167
170#define vtkMRMLReadXMLBeginMacro(atts) \
171 { \
172 const char* xmlReadAttName; \
173 const char* xmlReadAttValue; \
174 const char** xmlReadAtts = atts; \
175 while (*xmlReadAtts != nullptr) \
176 { \
177 xmlReadAttName = *(xmlReadAtts++); \
178 xmlReadAttValue = *(xmlReadAtts++); \
179 if (xmlReadAttValue == nullptr) \
180 { \
181 break; \
182 }
183
185#define vtkMRMLReadXMLEndMacro() \
186 }};
187
189#define vtkMRMLReadXMLBooleanMacro(xmlAttributeName, propertyName) \
190 if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
191 { \
192 this->Set##propertyName(strcmp(xmlReadAttValue,"true") ? false : true); \
193 }
194
197#define vtkMRMLReadXMLStringMacro(xmlAttributeName, propertyName) \
198 if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
199 { \
200 this->Set##propertyName(xmlReadAttValue); \
201 }
202
205#define vtkMRMLReadXMLStdStringMacro(xmlAttributeName, propertyName) \
206 if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
207 { \
208 this->Set##propertyName(xmlReadAttValue); \
209 }
210
214#define vtkMRMLReadXMLEnumMacro(xmlAttributeName, propertyName) \
215 if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
216 { \
217 int propertyValue = this->Get##propertyName##FromString(xmlReadAttValue); \
218 if (propertyValue >= 0) \
219 { \
220 this->Set##propertyName(propertyValue); \
221 } \
222 else \
223 { \
224 vtkErrorMacro("Failed to read " #xmlAttributeName " attribute value from string '" << xmlReadAttValue << "'"); \
225 } \
226 }
227
229#define vtkMRMLReadXMLIntMacro(xmlAttributeName, propertyName) \
230 if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
231 { \
232 vtkVariant variantValue(xmlReadAttValue); \
233 bool valid = false; \
234 int intValue = variantValue.ToInt(&valid); \
235 if (valid) \
236 { \
237 this->Set##propertyName(intValue); \
238 } \
239 else \
240 { \
241 vtkErrorMacro("Failed to read " #xmlAttributeName " attribute value from string '" << xmlReadAttValue << "': integer expected"); \
242 } \
243 }
244
246#define vtkMRMLReadXMLFloatMacro(xmlAttributeName, propertyName) \
247 if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
248 { \
249 vtkVariant variantValue(xmlReadAttValue); \
250 bool valid = false; \
251 double scalarValue = variantValue.ToDouble(&valid); \
252 if (valid) \
253 { \
254 this->Set##propertyName(scalarValue); \
255 } \
256 else \
257 { \
258 vtkErrorMacro("Failed to read " #xmlAttributeName " attribute value from string '" << xmlReadAttValue << "': float expected"); \
259 } \
260 }
261
263#define vtkMRMLReadXMLVectorMacro(xmlAttributeName, propertyName, vectorType, vectorSize) \
264 if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
265 { \
266 vectorType vectorValue[vectorSize] = {0}; \
267 std::stringstream ss; \
268 ss << xmlReadAttValue; \
269 for (int i=0; i<vectorSize; i++) \
270 { \
271 vectorType val; \
272 ss >> val; \
273 vectorValue[i] = val; \
274 } \
275 this->Set##propertyName(vectorValue); \
276 }
277
279#define vtkMRMLReadXMLStdFloatVectorMacro(xmlAttributeName, propertyName, vectorType) \
280 if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
281 { \
282 vectorType vector; \
283 std::string valueString(xmlReadAttValue); \
284 size_t separatorPosition = valueString.find(" "); \
285 while(separatorPosition != std::string::npos) \
286 { \
287 std::string attributeValue = valueString.substr(0, separatorPosition); \
288 vtkVariant variantValue(attributeValue); \
289 bool valid = false; \
290 vectorType::value_type scalarValue = variantValue.ToDouble(&valid); \
291 if (valid) \
292 { \
293 vector.insert(vector.end(), scalarValue); \
294 } \
295 valueString = valueString.substr(separatorPosition+1); \
296 separatorPosition = valueString.find(" "); \
297 } \
298 this->Set##propertyName(vector); \
299 }
300
302#define vtkMRMLReadXMLStdIntVectorMacro(xmlAttributeName, propertyName, vectorType) \
303 if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
304 { \
305 vectorType vector; \
306 std::string valueString(xmlReadAttValue); \
307 size_t separatorPosition = valueString.find(" "); \
308 while(separatorPosition != std::string::npos) \
309 { \
310 std::string attributeValue = valueString.substr(0, separatorPosition); \
311 vtkVariant variantValue(attributeValue); \
312 bool valid = false; \
313 vectorType::value_type scalarValue = variantValue.ToInt(&valid); \
314 if (valid) \
315 { \
316 vector.insert(vector.end(), scalarValue); \
317 } \
318 valueString = valueString.substr(separatorPosition+1); \
319 separatorPosition = valueString.find(" "); \
320 } \
321 this->Set##propertyName(vector); \
322 }
323
325#define vtkMRMLReadXMLStdStringVectorMacro(xmlAttributeName, propertyName, vectorType) \
326 if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
327 { \
328 vectorType<std::string> attributeValues; \
329 std::string valueString(xmlReadAttValue); \
330 std::vector<std::string> splitXmlReadAttValue = vtksys::SystemTools::SplitString(valueString, ';'); \
331 for (std::string attributeValue : splitXmlReadAttValue) \
332 { \
333 vtksys::SystemTools::ReplaceString(attributeValue, "%3B", ";"); \
334 vtksys::SystemTools::ReplaceString(attributeValue, "%25", "%"); \
335 attributeValues.emplace_back(attributeValue); \
336 } \
337 this->Set##propertyName(attributeValues); \
338 }
339
343#define vtkMRMLReadXMLOwnedMatrix4x4Macro(xmlAttributeName, propertyName) \
344 if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
345 { \
346 vtkNew<vtkMatrix4x4> matrix; \
347 std::stringstream ss; \
348 double val; \
349 ss << xmlReadAttValue; \
350 for (int row = 0; row < 4; row++) \
351 { \
352 for (int col = 0; col < 4; col++) \
353 { \
354 ss >> val; \
355 matrix->SetElement(row, col, val); \
356 } \
357 } \
358 this->Get##propertyName()->DeepCopy(matrix); \
359 }
360
362
363//----------------------------------------------------------------------------
370
373#define vtkMRMLCopyBeginMacro(sourceNode) \
374 { \
375 vtkMRMLNode* copySourceNode = this->SafeDownCast(sourceNode); \
376 if (copySourceNode != nullptr) \
377 {
378
380#define vtkMRMLCopyEndMacro() \
381 } \
382 else \
383 { \
384 vtkErrorMacro("Copy failed: invalid source node"); \
385 } \
386 }
387
389#define vtkMRMLCopyBooleanMacro(propertyName) \
390 this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
391
393#define vtkMRMLCopyStringMacro(propertyName) \
394 this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
395
397#define vtkMRMLCopyStdStringMacro(propertyName) \
398 this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
399
401#define vtkMRMLCopyIntMacro(propertyName) \
402 this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
403
405#define vtkMRMLCopyEnumMacro(propertyName) \
406 this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
407
409#define vtkMRMLCopyFloatMacro(propertyName) \
410 this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
411
413#define vtkMRMLCopyVectorMacro(propertyName, vectorType, vectorSize) \
414 { \
415 /* Currently, vectorType and vectorSize is not essential, but in the future */ \
416 /* this information may be used more. */ \
417 vectorType* sourceVector = this->SafeDownCast(copySourceNode)->Get##propertyName(); \
418 if (sourceVector != nullptr) \
419 { \
420 this->Set##propertyName(sourceVector); \
421 } \
422 else \
423 { \
424 vtkErrorMacro("Failed to copy " #propertyName " attribute value: source node returned NULL"); \
425 } \
426 }
427
429#define vtkMRMLCopyStdFloatVectorMacro(propertyName) \
430 this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
431
433#define vtkMRMLCopyStdIntVectorMacro(propertyName) \
434 this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
435
437#define vtkMRMLCopyStdStringVectorMacro(propertyName) \
438 this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
439
443#define vtkMRMLCopyOwnedMatrix4x4Macro(propertyName) \
444 this->Get##propertyName()->DeepCopy(this->SafeDownCast(copySourceNode)->Get##propertyName());
445
447
448//----------------------------------------------------------------------------
455
459#define vtkMRMLPrintBeginMacro(os, indent) \
460 { \
461 ostream& printOutputStream = os; \
462 vtkIndent printOutputIndent = indent;
463
465#define vtkMRMLPrintEndMacro() \
466 }
467
469#define vtkMRMLPrintBooleanMacro(propertyName) \
470 printOutputStream << printOutputIndent << #propertyName ": " << (this->Get##propertyName() ? "true" : "false") << "\n";
471
473#define vtkMRMLPrintStringMacro(propertyName) \
474 printOutputStream << printOutputIndent << #propertyName ": " << (this->Get##propertyName() != nullptr ? this->Get##propertyName() : "(none)") << "\n";
475
477#define vtkMRMLPrintStdStringMacro(propertyName) \
478 printOutputStream << printOutputIndent << #propertyName ": " << this->Get##propertyName() << "\n";
479
481#define vtkMRMLPrintEnumMacro(propertyName) \
482 printOutputStream << printOutputIndent << #propertyName ": " << (Get##propertyName##AsString(Get##propertyName())) << "\n";
483
485#define vtkMRMLPrintIntMacro(propertyName) \
486 printOutputStream << printOutputIndent << #propertyName ": " << this->Get##propertyName() << "\n";
487
489#define vtkMRMLPrintFloatMacro(propertyName) \
490 printOutputStream << printOutputIndent << #propertyName ": " << this->Get##propertyName() << "\n";
491
494#define vtkMRMLPrintVectorMacro(propertyName, vectorType, vectorSize) \
495 { \
496 printOutputStream << printOutputIndent << #propertyName ": ("; \
497 vectorType* vectorValue = this->Get##propertyName(); \
498 if (vectorValue) \
499 { \
500 for (int i=0; i<vectorSize; i++) \
501 { \
502 if (i > 0) \
503 { \
504 printOutputStream << ", "; \
505 } \
506 printOutputStream << vectorValue[i]; \
507 } \
508 printOutputStream << ")\n"; \
509 } \
510 }
511
514#define vtkMRMLPrintStdFloatVectorMacro(propertyName, vectorType) \
515 { \
516 printOutputStream << printOutputIndent << #propertyName " : ("; \
517 vectorType vector = this->Get##propertyName(); \
518 for (vectorType::iterator it=vector.begin(); it!=vector.end(); it++) \
519 { \
520 if (it != vector.begin()) \
521 { \
522 printOutputStream << ", "; \
523 } \
524 printOutputStream << *it; \
525 } \
526 printOutputStream << ")\n"; \
527 }
528
531#define vtkMRMLPrintStdIntVectorMacro(propertyName, vectorType) \
532 { \
533 printOutputStream << printOutputIndent << #propertyName " : ("; \
534 vectorType vector = this->Get##propertyName(); \
535 for (vectorType::iterator it=vector.begin(); it!=vector.end(); it++) \
536 { \
537 if (it != vector.begin()) \
538 { \
539 printOutputStream << ", "; \
540 } \
541 printOutputStream << *it; \
542 } \
543 printOutputStream << ")\n"; \
544 }
545
547#define vtkMRMLPrintStdStringVectorMacro(propertyName, vectorType) \
548 { \
549 printOutputStream << printOutputIndent << #propertyName " : (\""; \
550 vectorType<std::string> vector = this->Get##propertyName(); \
551 for (vectorType<std::string>::iterator it=vector.begin(); it!=vector.end(); it++) \
552 { \
553 if (it != vector.begin()) \
554 { \
555 printOutputStream << "\", \""; \
556 } \
557 printOutputStream << *it; \
558 } \
559 printOutputStream << "\")\n"; \
560 }
561
562#define vtkMRMLPrintMatrix4x4Macro(propertyName) \
563 { \
564 vtkMatrix4x4* matrix = this->Get##propertyName(); \
565 printOutputStream << printOutputIndent << #propertyName ":"; \
566 if (matrix) \
567 { \
568 printOutputStream << "\n"; \
569 matrix->PrintSelf(printOutputStream, printOutputIndent.GetNextIndent()); \
570 } \
571 else \
572 { \
573 printOutputStream << " (none)\n"; \
574 } \
575 }
576
577#define vtkMRMLPrintObjectMacro(propertyName) \
578 { \
579 vtkObject* obj = this->Get##propertyName(); \
580 printOutputStream << printOutputIndent << #propertyName ":"; \
581 if (obj) \
582 { \
583 printOutputStream << "\n"; \
584 obj->PrintSelf(printOutputStream, printOutputIndent.GetNextIndent()); \
585 } \
586 else \
587 { \
588 printOutputStream << " (none)\n"; \
589 } \
590 }
591
592
594
595#endif