Slicer  4.10
Slicer is a multi-platform, free and open source software package for visualization and medical image computing
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 
16 
17 //----------------------------------------------------------------------------
25 
28 #define vtkMRMLWriteXMLBeginMacro(of) \
29  { \
30  ostream& xmlWriteOutputStream = of;
31 
33 #define vtkMRMLWriteXMLEndMacro() \
34  }
35 
37 #define vtkMRMLWriteXMLBooleanMacro(xmlAttributeName, propertyName) \
38  xmlWriteOutputStream << " " #xmlAttributeName "=\"" << (Get##propertyName() ? "true" : "false") << "\"";
39 
42 #define vtkMRMLWriteXMLStringMacro(xmlAttributeName, propertyName) \
43  if (Get##propertyName() != NULL) \
44  { \
45  xmlWriteOutputStream << " " #xmlAttributeName "=\""; \
46  xmlWriteOutputStream << vtkMRMLNode::XMLAttributeEncodeString(Get##propertyName()); \
47  xmlWriteOutputStream << "\""; \
48  }
49 
51 #define vtkMRMLWriteXMLStdStringMacro(xmlAttributeName, propertyName) \
52  xmlWriteOutputStream << " " #xmlAttributeName "=\"" << vtkMRMLNode::XMLAttributeEncodeString(Get##propertyName().c_str()) << "\""; \
53 
54 #define vtkMRMLWriteXMLEnumMacro(xmlAttributeName, propertyName) \
57  xmlWriteOutputStream << " " #xmlAttributeName "=\""; \
58  if (Get##propertyName##AsString(Get##propertyName()) != NULL) \
59  { \
60  xmlWriteOutputStream << vtkMRMLNode::XMLAttributeEncodeString(Get##propertyName##AsString(Get##propertyName())); \
61  } \
62  xmlWriteOutputStream << "\"";
63 
65 #define vtkMRMLWriteXMLIntMacro(xmlAttributeName, propertyName) \
66  xmlWriteOutputStream << " " #xmlAttributeName "=\"" << Get##propertyName() << "\"";
67 
69 #define vtkMRMLWriteXMLFloatMacro(xmlAttributeName, propertyName) \
70  xmlWriteOutputStream << " " #xmlAttributeName "=\"" << Get##propertyName() << "\"";
71 
73 #define vtkMRMLWriteXMLVectorMacro(xmlAttributeName, propertyName, vectorType, vectorSize) \
74  { \
75  xmlWriteOutputStream << " " #xmlAttributeName "=\""; \
76  vectorType* vectorPtr = Get##propertyName(); \
77  if (vectorPtr != NULL) \
78  { \
79  for (int i=0; i<vectorSize; i++) \
80  { \
81  if (i > 0) \
82  { \
83  xmlWriteOutputStream << " "; \
84  } \
85  xmlWriteOutputStream << vectorPtr[i]; \
86  } \
87  } \
88  xmlWriteOutputStream << "\""; \
89  }
90 
92 #define vtkMRMLWriteXMLStdFloatVectorMacro(xmlAttributeName, propertyName, vectorType) \
93  { \
94  xmlWriteOutputStream << " " #xmlAttributeName "=\""; \
95  vectorType vector = Get##propertyName(); \
96  for (vectorType::iterator it=vector.begin(); it!=vector.end(); it++) \
97  { \
98  xmlWriteOutputStream << *it; \
99  xmlWriteOutputStream << " "; \
100  } \
101  xmlWriteOutputStream << "\""; \
102  }
103 
105 #define vtkMRMLWriteXMLStdIntVectorMacro(xmlAttributeName, propertyName, vectorType) \
106  { \
107  xmlWriteOutputStream << " " #xmlAttributeName "=\""; \
108  vectorType vector = Get##propertyName(); \
109  for (vectorType::iterator it=vector.begin(); it!=vector.end(); it++) \
110  { \
111  xmlWriteOutputStream << *it; \
112  xmlWriteOutputStream << " "; \
113  } \
114  xmlWriteOutputStream << "\""; \
115  }
116 
118 #define vtkMRMLWriteXMLStdStringVectorMacro(xmlAttributeName, propertyName, vectorType) \
119  { \
120  xmlWriteOutputStream << " " #xmlAttributeName "=\""; \
121  vectorType<std::string> vector = Get##propertyName(); \
122  for (vectorType<std::string>::iterator it=vector.begin(); it!=vector.end(); it++) \
123  { \
124  std::string attributeValue = *it; \
125  vtksys::SystemTools::ReplaceString(attributeValue, "%", "%25"); \
126  vtksys::SystemTools::ReplaceString(attributeValue, ";", "%3B"); \
127  xmlWriteOutputStream << vtkMRMLNode::XMLAttributeEncodeString(attributeValue); \
128  xmlWriteOutputStream << ";"; \
129  } \
130  xmlWriteOutputStream << "\""; \
131  }
132 
134 
135 //----------------------------------------------------------------------------
143 
146 #define vtkMRMLReadXMLBeginMacro(atts) \
147  { \
148  const char* xmlReadAttName; \
149  const char* xmlReadAttValue; \
150  const char** xmlReadAtts = atts; \
151  while (*xmlReadAtts != NULL) \
152  { \
153  xmlReadAttName = *(xmlReadAtts++); \
154  xmlReadAttValue = *(xmlReadAtts++); \
155  if (xmlReadAttValue == NULL) \
156  { \
157  break; \
158  }
159 
161 #define vtkMRMLReadXMLEndMacro() \
162  }};
163 
165 #define vtkMRMLReadXMLBooleanMacro(xmlAttributeName, propertyName) \
166  if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
167  { \
168  this->Set##propertyName(strcmp(xmlReadAttValue,"true") ? false : true); \
169  }
170 
173 #define vtkMRMLReadXMLStringMacro(xmlAttributeName, propertyName) \
174  if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
175  { \
176  this->Set##propertyName(xmlReadAttValue); \
177  }
178 
181 #define vtkMRMLReadXMLStdStringMacro(xmlAttributeName, propertyName) \
182  if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
183  { \
184  this->Set##propertyName(xmlReadAttValue); \
185  }
186 
190 #define vtkMRMLReadXMLEnumMacro(xmlAttributeName, propertyName) \
191  if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
192  { \
193  int propertyValue = this->Get##propertyName##FromString(xmlReadAttValue); \
194  if (propertyValue >= 0) \
195  { \
196  this->Set##propertyName(propertyValue); \
197  } \
198  else \
199  { \
200  vtkErrorMacro("Failed to read #xmlAttributeName attribute value from string '" << xmlReadAttValue << "'"); \
201  } \
202  }
203 
205 #define vtkMRMLReadXMLIntMacro(xmlAttributeName, propertyName) \
206  if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
207  { \
208  vtkVariant variantValue(xmlReadAttValue); \
209  bool valid = false; \
210  int intValue = variantValue.ToInt(&valid); \
211  if (valid) \
212  { \
213  this->Set##propertyName(intValue); \
214  } \
215  else \
216  { \
217  vtkErrorMacro("Failed to read #xmlAttributeName attribute value from string '" << xmlReadAttValue << "': integer expected"); \
218  } \
219  }
220 
222 #define vtkMRMLReadXMLFloatMacro(xmlAttributeName, propertyName) \
223  if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
224  { \
225  vtkVariant variantValue(xmlReadAttValue); \
226  bool valid = false; \
227  double scalarValue = variantValue.ToDouble(&valid); \
228  if (valid) \
229  { \
230  this->Set##propertyName(scalarValue); \
231  } \
232  else \
233  { \
234  vtkErrorMacro("Failed to read #xmlAttributeName attribute value from string '" << xmlReadAttValue << "': float expected"); \
235  } \
236  }
237 
239 #define vtkMRMLReadXMLVectorMacro(xmlAttributeName, propertyName, vectorType, vectorSize) \
240  if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
241  { \
242  vectorType vectorValue[vectorSize] = {0}; \
243  std::stringstream ss; \
244  ss << xmlReadAttValue; \
245  for (int i=0; i<vectorSize; i++) \
246  { \
247  vectorType val; \
248  ss >> val; \
249  vectorValue[i] = val; \
250  } \
251  this->Set##propertyName(vectorValue); \
252  }
253 
255 #define vtkMRMLReadXMLStdFloatVectorMacro(xmlAttributeName, propertyName, vectorType) \
256  if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
257  { \
258  vectorType vector; \
259  std::string valueString(xmlReadAttValue); \
260  size_t separatorPosition = valueString.find(" "); \
261  while(separatorPosition != std::string::npos) \
262  { \
263  std::string attributeValue = valueString.substr(0, separatorPosition); \
264  vtkVariant variantValue(attributeValue); \
265  bool valid = false; \
266  vectorType::value_type scalarValue = variantValue.ToDouble(&valid); \
267  if (valid) \
268  { \
269  vector.insert(vector.end(), scalarValue); \
270  } \
271  valueString = valueString.substr(separatorPosition+1); \
272  separatorPosition = valueString.find(" "); \
273  } \
274  this->Set##propertyName(vector); \
275  }
276 
278 #define vtkMRMLReadXMLStdIntVectorMacro(xmlAttributeName, propertyName, vectorType) \
279  if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
280  { \
281  vectorType vector; \
282  std::string valueString(xmlReadAttValue); \
283  size_t separatorPosition = valueString.find(" "); \
284  while(separatorPosition != std::string::npos) \
285  { \
286  std::string attributeValue = valueString.substr(0, separatorPosition); \
287  vtkVariant variantValue(attributeValue); \
288  bool valid = false; \
289  vectorType::value_type scalarValue = variantValue.ToInt(&valid); \
290  if (valid) \
291  { \
292  vector.insert(vector.end(), scalarValue); \
293  } \
294  valueString = valueString.substr(separatorPosition+1); \
295  separatorPosition = valueString.find(" "); \
296  } \
297  this->Set##propertyName(vector); \
298  }
299 
301 #define vtkMRMLReadXMLStdStringVectorMacro(xmlAttributeName, propertyName, vectorType) \
302  if (!strcmp(xmlReadAttName, #xmlAttributeName)) \
303  { \
304  vectorType<std::string> vector; \
305  std::string valueString(xmlReadAttValue); \
306  size_t separatorPosition = valueString.find(";"); \
307  while(separatorPosition != std::string::npos) \
308  { \
309  std::string attributeValue = valueString.substr(0,separatorPosition); \
310  vtksys::SystemTools::ReplaceString(attributeValue, "%3B", ";"); \
311  vtksys::SystemTools::ReplaceString(attributeValue, "%25", "%"); \
312  vector.insert(vector.end(), attributeValue); \
313  valueString = valueString.substr(separatorPosition+1); \
314  separatorPosition = valueString.find(";"); \
315  } \
316  if (!valueString.empty()) \
317  { \
318  vector.push_back(valueString); \
319  } \
320  this->Set##propertyName(vector); \
321  }
322 
324 
325 //----------------------------------------------------------------------------
332 
335 #define vtkMRMLCopyBeginMacro(sourceNode) \
336  { \
337  vtkMRMLNode* copySourceNode = this->SafeDownCast(sourceNode); \
338  if (copySourceNode != NULL) \
339  {
340 
342 #define vtkMRMLCopyEndMacro() \
343  } \
344  else \
345  { \
346  vtkErrorMacro("Copy failed: invalid source node"); \
347  } \
348  }
349 
351 #define vtkMRMLCopyBooleanMacro(propertyName) \
352  this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
353 
355 #define vtkMRMLCopyStringMacro(propertyName) \
356  this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
357 
359 #define vtkMRMLCopyStdStringMacro(propertyName) \
360  this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
361 
363 #define vtkMRMLCopyIntMacro(propertyName) \
364  this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
365 
367 #define vtkMRMLCopyEnumMacro(propertyName) \
368  this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
369 
371 #define vtkMRMLCopyFloatMacro(propertyName) \
372  this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
373 
375 #define vtkMRMLCopyVectorMacro(propertyName, vectorType, vectorSize) \
376  { \
377  /* Currently, vectorType and vectorSize is not essential, but in the future */ \
378  /* this information may be used more. */ \
379  vectorType* sourceVector = this->SafeDownCast(copySourceNode)->Get##propertyName(); \
380  if (sourceVector != NULL) \
381  { \
382  this->Set##propertyName(sourceVector); \
383  } \
384  else \
385  { \
386  vtkErrorMacro("Failed to copy #xmlAttributeName attribute value: source node returned NULL"); \
387  } \
388  }
389 
391 #define vtkMRMLCopyStdFloatVectorMacro(propertyName) \
392  this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
393 
395 #define vtkMRMLCopyStdIntVectorMacro(propertyName) \
396  this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
397 
399 #define vtkMRMLCopyStdStringVectorMacroMacro(propertyName) \
400  this->Set##propertyName(this->SafeDownCast(copySourceNode)->Get##propertyName());
401 
403 
404 //----------------------------------------------------------------------------
411 
415 #define vtkMRMLPrintBeginMacro(os, indent) \
416  { \
417  ostream& printOutputStream = os; \
418  vtkIndent printOutputIndent = indent;
419 
421 #define vtkMRMLPrintEndMacro() \
422  }
423 
425 #define vtkMRMLPrintBooleanMacro(propertyName) \
426  printOutputStream << printOutputIndent << #propertyName ": " << (this->Get##propertyName() ? "true" : "false") << "\n";
427 
429 #define vtkMRMLPrintStringMacro(propertyName) \
430  printOutputStream << printOutputIndent << #propertyName ": " << (this->Get##propertyName() != NULL ? this->Get##propertyName() : "(none)") << "\n";
431 
433 #define vtkMRMLPrintStdStringMacro(propertyName) \
434  printOutputStream << printOutputIndent << #propertyName ": " << this->Get##propertyName() << "\n";
435 
437 #define vtkMRMLPrintEnumMacro(propertyName) \
438  printOutputStream << printOutputIndent << #propertyName ": " << (Get##propertyName##AsString(Get##propertyName())) << "\n";
439 
441 #define vtkMRMLPrintIntMacro(propertyName) \
442  printOutputStream << printOutputIndent << #propertyName ": " << this->Get##propertyName() << "\n";
443 
445 #define vtkMRMLPrintFloatMacro(propertyName) \
446  printOutputStream << printOutputIndent << #propertyName ": " << this->Get##propertyName() << "\n";
447 
449 #define vtkMRMLPrintVectorMacro(propertyName, vectorType, vectorSize) \
450  { \
451  printOutputStream << printOutputIndent << #propertyName " : ["; \
452  vectorType* vectorValue = this->Get##propertyName(); \
453  if (vectorValue) \
454  { \
455  for (int i=0; i<vectorSize; i++) \
456  { \
457  if (i > 0) \
458  { \
459  printOutputStream << ", "; \
460  } \
461  printOutputStream << vectorValue[i]; \
462  } \
463  printOutputStream << "]\n"; \
464  } \
465  }
466 
468 #define vtkMRMLPrintStdFloatVectorMacro(propertyName, vectorType) \
469  { \
470  printOutputStream << printOutputIndent << #propertyName " : ["; \
471  vectorType vector = this->Get##propertyName(); \
472  for (vectorType::iterator it=vector.begin(); it!=vector.end(); it++) \
473  { \
474  if (it != vector.begin()) \
475  { \
476  printOutputStream << ", "; \
477  } \
478  printOutputStream << *it; \
479  } \
480  printOutputStream << "]\n"; \
481  }
482 
484 #define vtkMRMLPrintStdIntVectorMacro(propertyName, vectorType) \
485  { \
486  printOutputStream << printOutputIndent << #propertyName " : ["; \
487  vectorType vector = this->Get##propertyName(); \
488  for (vectorType::iterator it=vector.begin(); it!=vector.end(); it++) \
489  { \
490  if (it != vector.begin()) \
491  { \
492  printOutputStream << ", "; \
493  } \
494  printOutputStream << *it; \
495  } \
496  printOutputStream << "]\n"; \
497  }
498 
500 #define vtkMRMLPrintStdStringVectorMacro(propertyName, vectorType) \
501  { \
502  printOutputStream << printOutputIndent << #propertyName " : ["; \
503  vectorType<std::string> vector = this->Get##propertyName(); \
504  for (vectorType<std::string>::iterator it=vector.begin(); it!=vector.end(); it++) \
505  { \
506  if (it != vector.begin()) \
507  { \
508  printOutputStream << ", "; \
509  } \
510  printOutputStream << *it; \
511  } \
512  printOutputStream << "]\n"; \
513  }
514 
516 
517 #endif // __vtkMRMLNodePropertyMacros_h