Slicer 5.9
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
Loading...
Searching...
No Matches
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
37#define vtkMRMLWriteXMLBooleanMacro(xmlAttributeName, propertyName) xmlWriteOutputStream << " " #xmlAttributeName "=\"" << (Get##propertyName() ? "true" : "false") << "\"";
38
41#define vtkMRMLWriteXMLStringMacro(xmlAttributeName, propertyName) \
42 if (Get##propertyName() != nullptr) \
43 { \
44 xmlWriteOutputStream << " " #xmlAttributeName "=\""; \
45 xmlWriteOutputStream << vtkMRMLNode::XMLAttributeEncodeString(Get##propertyName()); \
46 xmlWriteOutputStream << "\""; \
47 }
48
50#define vtkMRMLWriteXMLStdStringMacro(xmlAttributeName, propertyName) \
51 xmlWriteOutputStream << " " #xmlAttributeName "=\"" << vtkMRMLNode::XMLAttributeEncodeString(Get##propertyName().c_str()) << "\"";
52
55#define vtkMRMLWriteXMLEnumMacro(xmlAttributeName, propertyName) \
56 xmlWriteOutputStream << " " #xmlAttributeName "=\""; \
57 if (Get##propertyName##AsString(Get##propertyName()) != nullptr) \
58 { \
59 xmlWriteOutputStream << vtkMRMLNode::XMLAttributeEncodeString(Get##propertyName##AsString(Get##propertyName())); \
60 } \
61 xmlWriteOutputStream << "\"";
62
64#define vtkMRMLWriteXMLIntMacro(xmlAttributeName, propertyName) xmlWriteOutputStream << " " #xmlAttributeName "=\"" << Get##propertyName() << "\"";
65
67#define vtkMRMLWriteXMLFloatMacro(xmlAttributeName, propertyName) xmlWriteOutputStream << " " #xmlAttributeName "=\"" << Get##propertyName() << "\"";
68
70#define vtkMRMLWriteXMLVectorMacro(xmlAttributeName, propertyName, vectorType, vectorSize) \
71 { \
72 xmlWriteOutputStream << " " #xmlAttributeName "=\""; \
73 vectorType* vectorPtr = Get##propertyName(); \
74 if (vectorPtr != nullptr) \
75 { \
76 for (int i = 0; i < vectorSize; i++) \
77 { \
78 if (i > 0) \
79 { \
80 xmlWriteOutputStream << " "; \
81 } \
82 xmlWriteOutputStream << vectorPtr[i]; \
83 } \
84 } \
85 xmlWriteOutputStream << "\""; \
86 }
87
89#define vtkMRMLWriteXMLStdFloatVectorMacro(xmlAttributeName, propertyName, vectorType) \
90 { \
91 xmlWriteOutputStream << " " #xmlAttributeName "=\""; \
92 vectorType vector = Get##propertyName(); \
93 for (vectorType::iterator it = vector.begin(); it != vector.end(); it++) \
94 { \
95 xmlWriteOutputStream << *it; \
96 xmlWriteOutputStream << " "; \
97 } \
98 xmlWriteOutputStream << "\""; \
99 }
100
102#define vtkMRMLWriteXMLStdIntVectorMacro(xmlAttributeName, propertyName, vectorType) \
103 { \
104 xmlWriteOutputStream << " " #xmlAttributeName "=\""; \
105 vectorType vector = Get##propertyName(); \
106 for (vectorType::iterator it = vector.begin(); it != vector.end(); it++) \
107 { \
108 xmlWriteOutputStream << *it; \
109 xmlWriteOutputStream << " "; \
110 } \
111 xmlWriteOutputStream << "\""; \
112 }
113
115#define vtkMRMLWriteXMLStdStringVectorMacro(xmlAttributeName, propertyName, vectorType) \
116 { \
117 xmlWriteOutputStream << " " #xmlAttributeName "=\""; \
118 vectorType<std::string> vector = Get##propertyName(); \
119 for (vectorType<std::string>::iterator it = vector.begin(); it != vector.end(); it++) \
120 { \
121 if (it != vector.begin()) \
122 { \
123 xmlWriteOutputStream << ";"; \
124 } \
125 std::string attributeValue = *it; \
126 vtksys::SystemTools::ReplaceString(attributeValue, "%", "%25"); \
127 vtksys::SystemTools::ReplaceString(attributeValue, ";", "%3B"); \
128 xmlWriteOutputStream << vtkMRMLNode::XMLAttributeEncodeString(attributeValue); \
129 } \
130 xmlWriteOutputStream << "\""; \
131 }
132
134#define vtkMRMLWriteXMLMatrix4x4Macro(xmlAttributeName, propertyName) \
135 { \
136 xmlWriteOutputStream << " " #xmlAttributeName "=\""; \
137 vtkMatrix4x4* matrix = this->Get##propertyName(); \
138 for (int row = 0; row < 4; row++) \
139 { \
140 for (int col = 0; col < 4; col++) \
141 { \
142 of << matrix->GetElement(row, col); \
143 if (!(row == 3 && col == 3)) \
144 { \
145 of << " "; \
146 } \
147 } \
148 } \
149 xmlWriteOutputStream << "\""; \
150 }
151
153
154//----------------------------------------------------------------------------
162
165#define vtkMRMLReadXMLBeginMacro(atts) \
166 { \
167 const char* xmlReadAttName; \
168 const char* xmlReadAttValue; \
169 const char** xmlReadAtts = atts; \
170 while (*xmlReadAtts != nullptr) \
171 { \
172 xmlReadAttName = *(xmlReadAtts++); \
173 xmlReadAttValue = *(xmlReadAtts++); \
174 if (xmlReadAttValue == nullptr) \
175 { \
176 break; \
177 }
178
180#define vtkMRMLReadXMLEndMacro() \
181 } \
182 } \
183 ;
184
186#define vtkMRMLReadXMLBooleanMacro(xmlAttributeName, propertyName) \
187 if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
188 { \
189 this->Set##propertyName(strcmp(xmlReadAttValue, "true") ? false : true); \
190 }
191
194#define vtkMRMLReadXMLStringMacro(xmlAttributeName, propertyName) \
195 if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
196 { \
197 this->Set##propertyName(xmlReadAttValue); \
198 }
199
202#define vtkMRMLReadXMLStdStringMacro(xmlAttributeName, propertyName) \
203 if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
204 { \
205 this->Set##propertyName(xmlReadAttValue); \
206 }
207
211#define vtkMRMLReadXMLEnumMacro(xmlAttributeName, propertyName) \
212 if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
213 { \
214 int propertyValue = this->Get##propertyName##FromString(xmlReadAttValue); \
215 if (propertyValue >= 0) \
216 { \
217 this->Set##propertyName(propertyValue); \
218 } \
219 else \
220 { \
221 vtkErrorMacro("Failed to read " #xmlAttributeName " attribute value from string '" << xmlReadAttValue << "'"); \
222 } \
223 }
224
226#define vtkMRMLReadXMLIntMacro(xmlAttributeName, propertyName) \
227 if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
228 { \
229 vtkVariant variantValue(xmlReadAttValue); \
230 bool valid = false; \
231 int intValue = variantValue.ToInt(&valid); \
232 if (valid) \
233 { \
234 this->Set##propertyName(intValue); \
235 } \
236 else \
237 { \
238 vtkErrorMacro("Failed to read " #xmlAttributeName " attribute value from string '" << xmlReadAttValue << "': integer expected"); \
239 } \
240 }
241
243#define vtkMRMLReadXMLFloatMacro(xmlAttributeName, propertyName) \
244 if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
245 { \
246 vtkVariant variantValue(xmlReadAttValue); \
247 bool valid = false; \
248 double scalarValue = variantValue.ToDouble(&valid); \
249 if (valid) \
250 { \
251 this->Set##propertyName(scalarValue); \
252 } \
253 else \
254 { \
255 vtkErrorMacro("Failed to read " #xmlAttributeName " attribute value from string '" << xmlReadAttValue << "': float expected"); \
256 } \
257 }
258
260#define vtkMRMLReadXMLVectorMacro(xmlAttributeName, propertyName, vectorType, vectorSize) \
261 if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
262 { \
263 vectorType vectorValue[vectorSize] = { 0 }; \
264 std::stringstream ss; \
265 ss << xmlReadAttValue; \
266 for (int i = 0; i < vectorSize; i++) \
267 { \
268 vectorType val; \
269 ss >> val; \
270 vectorValue[i] = val; \
271 } \
272 this->Set##propertyName(vectorValue); \
273 }
274
276#define vtkMRMLReadXMLStdFloatVectorMacro(xmlAttributeName, propertyName, vectorType) \
277 if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
278 { \
279 vectorType vector; \
280 std::string valueString(xmlReadAttValue); \
281 size_t separatorPosition = valueString.find(" "); \
282 while (separatorPosition != std::string::npos) \
283 { \
284 std::string attributeValue = valueString.substr(0, separatorPosition); \
285 vtkVariant variantValue(attributeValue); \
286 bool valid = false; \
287 vectorType::value_type scalarValue = variantValue.ToDouble(&valid); \
288 if (valid) \
289 { \
290 vector.insert(vector.end(), scalarValue); \
291 } \
292 valueString = valueString.substr(separatorPosition + 1); \
293 separatorPosition = valueString.find(" "); \
294 } \
295 this->Set##propertyName(vector); \
296 }
297
299#define vtkMRMLReadXMLStdIntVectorMacro(xmlAttributeName, propertyName, vectorType) \
300 if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
301 { \
302 vectorType vector; \
303 std::string valueString(xmlReadAttValue); \
304 size_t separatorPosition = valueString.find(" "); \
305 while (separatorPosition != std::string::npos) \
306 { \
307 std::string attributeValue = valueString.substr(0, separatorPosition); \
308 vtkVariant variantValue(attributeValue); \
309 bool valid = false; \
310 vectorType::value_type scalarValue = variantValue.ToInt(&valid); \
311 if (valid) \
312 { \
313 vector.insert(vector.end(), scalarValue); \
314 } \
315 valueString = valueString.substr(separatorPosition + 1); \
316 separatorPosition = valueString.find(" "); \
317 } \
318 this->Set##propertyName(vector); \
319 }
320
322#define vtkMRMLReadXMLStdStringVectorMacro(xmlAttributeName, propertyName, vectorType) \
323 if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
324 { \
325 vectorType<std::string> attributeValues; \
326 std::string valueString(xmlReadAttValue); \
327 std::vector<std::string> splitXmlReadAttValue = vtksys::SystemTools::SplitString(valueString, ';'); \
328 for (std::string attributeValue : splitXmlReadAttValue) \
329 { \
330 vtksys::SystemTools::ReplaceString(attributeValue, "%3B", ";"); \
331 vtksys::SystemTools::ReplaceString(attributeValue, "%25", "%"); \
332 attributeValues.emplace_back(attributeValue); \
333 } \
334 this->Set##propertyName(attributeValues); \
335 }
336
340#define vtkMRMLReadXMLOwnedMatrix4x4Macro(xmlAttributeName, propertyName) \
341 if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
342 { \
343 vtkNew<vtkMatrix4x4> matrix; \
344 std::stringstream ss; \
345 double val; \
346 ss << xmlReadAttValue; \
347 for (int row = 0; row < 4; row++) \
348 { \
349 for (int col = 0; col < 4; col++) \
350 { \
351 ss >> val; \
352 matrix->SetElement(row, col, val); \
353 } \
354 } \
355 this->Get##propertyName()->DeepCopy(matrix); \
356 }
357
359
360//----------------------------------------------------------------------------
367
370#define vtkMRMLCopyBeginMacro(sourceNode) \
371 { \
372 vtkMRMLNode* copySourceNode = this->SafeDownCast(sourceNode); \
373 if (copySourceNode != nullptr) \
374 {
375
377#define vtkMRMLCopyEndMacro() \
378 } \
379 else \
380 { \
381 vtkErrorMacro("Copy failed: invalid source node"); \
382 } \
383 }
384
386#define vtkMRMLCopyBooleanMacro(propertyName) this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
387
389#define vtkMRMLCopyStringMacro(propertyName) this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
390
392#define vtkMRMLCopyStdStringMacro(propertyName) this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
393
395#define vtkMRMLCopyIntMacro(propertyName) this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
396
398#define vtkMRMLCopyEnumMacro(propertyName) this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
399
401#define vtkMRMLCopyFloatMacro(propertyName) this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
402
404#define vtkMRMLCopyVectorMacro(propertyName, vectorType, vectorSize) \
405 { \
406 /* Currently, vectorType and vectorSize is not essential, but in the future */ \
407 /* this information may be used more. */ \
408 vectorType* sourceVector = this->SafeDownCast(copySourceNode)->Get##propertyName(); \
409 if (sourceVector != nullptr) \
410 { \
411 this->Set##propertyName(sourceVector); \
412 } \
413 else \
414 { \
415 vtkErrorMacro("Failed to copy " #propertyName " attribute value: source node returned NULL"); \
416 } \
417 }
418
420#define vtkMRMLCopyStdFloatVectorMacro(propertyName) this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
421
423#define vtkMRMLCopyStdIntVectorMacro(propertyName) this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
424
426#define vtkMRMLCopyStdStringVectorMacro(propertyName) this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
427
431#define vtkMRMLCopyOwnedMatrix4x4Macro(propertyName) this->Get##propertyName()->DeepCopy(this->SafeDownCast(copySourceNode)->Get##propertyName());
432
434
435//----------------------------------------------------------------------------
442
446#define vtkMRMLPrintBeginMacro(os, indent) \
447 { \
448 ostream& printOutputStream = os; \
449 vtkIndent printOutputIndent = indent;
450
452#define vtkMRMLPrintEndMacro() }
453
455#define vtkMRMLPrintBooleanMacro(propertyName) printOutputStream << printOutputIndent << #propertyName ": " << (this->Get##propertyName() ? "true" : "false") << "\n";
456
458#define vtkMRMLPrintStringMacro(propertyName) \
459 printOutputStream << printOutputIndent << #propertyName ": " << (this->Get##propertyName() != nullptr ? this->Get##propertyName() : "(none)") << "\n";
460
462#define vtkMRMLPrintStdStringMacro(propertyName) printOutputStream << printOutputIndent << #propertyName ": " << this->Get##propertyName() << "\n";
463
465#define vtkMRMLPrintEnumMacro(propertyName) printOutputStream << printOutputIndent << #propertyName ": " << (Get##propertyName##AsString(Get##propertyName())) << "\n";
466
468#define vtkMRMLPrintIntMacro(propertyName) printOutputStream << printOutputIndent << #propertyName ": " << this->Get##propertyName() << "\n";
469
471#define vtkMRMLPrintFloatMacro(propertyName) printOutputStream << printOutputIndent << #propertyName ": " << this->Get##propertyName() << "\n";
472
475#define vtkMRMLPrintVectorMacro(propertyName, vectorType, vectorSize) \
476 { \
477 printOutputStream << printOutputIndent << #propertyName ": ("; \
478 vectorType* vectorValue = this->Get##propertyName(); \
479 if (vectorValue) \
480 { \
481 for (int i = 0; i < vectorSize; i++) \
482 { \
483 if (i > 0) \
484 { \
485 printOutputStream << ", "; \
486 } \
487 printOutputStream << vectorValue[i]; \
488 } \
489 printOutputStream << ")\n"; \
490 } \
491 }
492
495#define vtkMRMLPrintStdFloatVectorMacro(propertyName, vectorType) \
496 { \
497 printOutputStream << printOutputIndent << #propertyName " : ("; \
498 vectorType vector = this->Get##propertyName(); \
499 for (vectorType::iterator it = vector.begin(); it != vector.end(); it++) \
500 { \
501 if (it != vector.begin()) \
502 { \
503 printOutputStream << ", "; \
504 } \
505 printOutputStream << *it; \
506 } \
507 printOutputStream << ")\n"; \
508 }
509
512#define vtkMRMLPrintStdIntVectorMacro(propertyName, vectorType) \
513 { \
514 printOutputStream << printOutputIndent << #propertyName " : ("; \
515 vectorType vector = this->Get##propertyName(); \
516 for (vectorType::iterator it = vector.begin(); it != vector.end(); it++) \
517 { \
518 if (it != vector.begin()) \
519 { \
520 printOutputStream << ", "; \
521 } \
522 printOutputStream << *it; \
523 } \
524 printOutputStream << ")\n"; \
525 }
526
528#define vtkMRMLPrintStdStringVectorMacro(propertyName, vectorType) \
529 { \
530 printOutputStream << printOutputIndent << #propertyName " : (\""; \
531 vectorType<std::string> vector = this->Get##propertyName(); \
532 for (vectorType<std::string>::iterator it = vector.begin(); it != vector.end(); it++) \
533 { \
534 if (it != vector.begin()) \
535 { \
536 printOutputStream << "\", \""; \
537 } \
538 printOutputStream << *it; \
539 } \
540 printOutputStream << "\")\n"; \
541 }
542
543#define vtkMRMLPrintMatrix4x4Macro(propertyName) \
544 { \
545 vtkMatrix4x4* matrix = this->Get##propertyName(); \
546 printOutputStream << printOutputIndent << #propertyName ":"; \
547 if (matrix) \
548 { \
549 printOutputStream << "\n"; \
550 matrix->PrintSelf(printOutputStream, printOutputIndent.GetNextIndent()); \
551 } \
552 else \
553 { \
554 printOutputStream << " (none)\n"; \
555 } \
556 }
557
558#define vtkMRMLPrintObjectMacro(propertyName) \
559 { \
560 vtkObject* obj = this->Get##propertyName(); \
561 printOutputStream << printOutputIndent << #propertyName ":"; \
562 if (obj) \
563 { \
564 printOutputStream << "\n"; \
565 obj->PrintSelf(printOutputStream, printOutputIndent.GetNextIndent()); \
566 } \
567 else \
568 { \
569 printOutputStream << " (none)\n"; \
570 } \
571 }
572
574
575#endif