comparison Plugins/Samples/Common/DicomDatasetReader.cpp @ 2238:4f0a9a61d905

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 16 Dec 2016 16:22:38 +0100
parents 403d92d8df83
children df47c45694ed
comparison
equal deleted inserted replaced
2237:403d92d8df83 2238:4f0a9a61d905
66 assert(first <= last); 66 assert(first <= last);
67 return source.substr(first, last - first); 67 return source.substr(first, last - first);
68 } 68 }
69 69
70 70
71 DicomDatasetReader::DicomDatasetReader(IDicomDataset* dataset) : // takes ownership 71 DicomDatasetReader::DicomDatasetReader(const IDicomDataset& dataset) :
72 dataset_(dataset) 72 dataset_(dataset)
73 { 73 {
74 if (dataset == NULL) 74 }
75
76
77 std::string DicomDatasetReader::GetStringValue(const DicomPath& path,
78 const std::string& defaultValue) const
79 {
80 std::string s;
81 if (dataset_.GetStringValue(s, path))
75 { 82 {
76 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); 83 return s;
84 }
85 else
86 {
87 return defaultValue;
77 } 88 }
78 } 89 }
79 90
80 91
81 std::string DicomDatasetReader::GetMandatoryStringValue(const DicomPath& path) const 92 std::string DicomDatasetReader::GetMandatoryStringValue(const DicomPath& path) const
82 { 93 {
83 std::string s; 94 std::string s;
84 if (dataset_->GetStringValue(s, path)) 95 if (dataset_.GetStringValue(s, path))
85 { 96 {
86 return s; 97 return s;
87 } 98 }
88 else 99 else
89 { 100 {
90 ORTHANC_PLUGINS_THROW_EXCEPTION(InexistentTag); 101 ORTHANC_PLUGINS_THROW_EXCEPTION(InexistentTag);
91 } 102 }
92 } 103 }
93 104
94 105
95 int DicomDatasetReader::GetIntegerValue(const DicomPath& path) 106 template <typename T>
107 static T GetValueInternal(const IDicomDataset& dataset,
108 const DicomPath& path)
96 { 109 {
97 try 110 try
98 { 111 {
99 std::string s = StripSpaces(GetMandatoryStringValue(path)); 112 std::string s;
100 return boost::lexical_cast<int>(s); 113
114 if (dataset.GetStringValue(s, path))
115 {
116 return boost::lexical_cast<T>(StripSpaces(s));
117 }
118 else
119 {
120 ORTHANC_PLUGINS_THROW_EXCEPTION(InexistentTag);
121 }
101 } 122 }
102 catch (boost::bad_lexical_cast&) 123 catch (boost::bad_lexical_cast&)
103 { 124 {
104 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat); 125 ORTHANC_PLUGINS_THROW_EXCEPTION(BadFileFormat);
105 } 126 }
106 } 127 }
107 128
108 129
109 unsigned int DicomDatasetReader::GetUnsignedIntegerValue(const DicomPath& path) 130 int DicomDatasetReader::GetIntegerValue(const DicomPath& path) const
131 {
132 return GetValueInternal<int>(dataset_, path);
133 }
134
135
136 unsigned int DicomDatasetReader::GetUnsignedIntegerValue(const DicomPath& path) const
110 { 137 {
111 int value = GetIntegerValue(path); 138 int value = GetIntegerValue(path);
112 139
113 if (value >= 0) 140 if (value >= 0)
114 { 141 {
115 return static_cast<unsigned int>(value); 142 return static_cast<unsigned int>(value);
116 } 143 }
117 else 144 else
118 { 145 {
119 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange); 146 ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
120 } 147 }
121 } 148 }
149
150
151 float DicomDatasetReader::GetFloatValue(const DicomPath& path) const
152 {
153 return GetValueInternal<float>(dataset_, path);
154 }
155
156
157 double DicomDatasetReader::GetDoubleValue(const DicomPath& path) const
158 {
159 return GetValueInternal<double>(dataset_, path);
160 }
122 } 161 }