diff 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
line wrap: on
line diff
--- a/Plugins/Samples/Common/DicomDatasetReader.cpp	Fri Dec 16 14:45:40 2016 +0100
+++ b/Plugins/Samples/Common/DicomDatasetReader.cpp	Fri Dec 16 16:22:38 2016 +0100
@@ -68,20 +68,31 @@
   }
 
 
-  DicomDatasetReader::DicomDatasetReader(IDicomDataset* dataset) :  // takes ownership
+  DicomDatasetReader::DicomDatasetReader(const IDicomDataset& dataset) :
     dataset_(dataset)
   {
-    if (dataset == NULL)
+  }
+  
+
+  std::string DicomDatasetReader::GetStringValue(const DicomPath& path,
+                                                 const std::string& defaultValue) const
+  {
+    std::string s;
+    if (dataset_.GetStringValue(s, path))
     {
-      ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
+      return s;
+    }
+    else
+    {
+      return defaultValue;
     }
   }
-  
+
 
   std::string DicomDatasetReader::GetMandatoryStringValue(const DicomPath& path) const
   {
     std::string s;
-    if (dataset_->GetStringValue(s, path))
+    if (dataset_.GetStringValue(s, path))
     {
       return s;
     }
@@ -92,12 +103,22 @@
   }
 
 
-  int DicomDatasetReader::GetIntegerValue(const DicomPath& path)
+  template <typename T>
+  static T GetValueInternal(const IDicomDataset& dataset,
+                            const DicomPath& path)
   {
     try
     {
-      std::string s = StripSpaces(GetMandatoryStringValue(path));
-      return boost::lexical_cast<int>(s);
+      std::string s;
+
+      if (dataset.GetStringValue(s, path))
+      {
+        return boost::lexical_cast<T>(StripSpaces(s));
+      }
+      else
+      {
+        ORTHANC_PLUGINS_THROW_EXCEPTION(InexistentTag);
+      }
     }
     catch (boost::bad_lexical_cast&)
     {
@@ -106,10 +127,16 @@
   }
 
 
-  unsigned int DicomDatasetReader::GetUnsignedIntegerValue(const DicomPath& path)
+  int DicomDatasetReader::GetIntegerValue(const DicomPath& path) const
+  {
+    return GetValueInternal<int>(dataset_, path);
+  }
+
+
+  unsigned int DicomDatasetReader::GetUnsignedIntegerValue(const DicomPath& path) const
   {
     int value = GetIntegerValue(path);
-
+    
     if (value >= 0)
     {
       return static_cast<unsigned int>(value);
@@ -119,4 +146,16 @@
       ORTHANC_PLUGINS_THROW_EXCEPTION(ParameterOutOfRange);
     }
   }
+
+
+  float DicomDatasetReader::GetFloatValue(const DicomPath& path) const
+  {
+    return GetValueInternal<float>(dataset_, path);
+  }
+
+
+  double DicomDatasetReader::GetDoubleValue(const DicomPath& path) const
+  {
+    return GetValueInternal<double>(dataset_, path);
+  }
 }