changeset 434:ccf3a0a43dac

EnumerationDictionary
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 15 May 2013 14:54:58 +0200
parents aa50783f9550
children 28ba73274919
files Core/EnumerationDictionary.h OrthancServer/DatabaseWrapper.cpp OrthancServer/OrthancRestApi.cpp OrthancServer/ServerEnumerations.cpp OrthancServer/ServerEnumerations.h OrthancServer/ServerIndex.cpp UnitTests/main.cpp
diffstat 7 files changed, 145 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Core/EnumerationDictionary.h	Wed May 15 14:54:58 2013 +0200
@@ -0,0 +1,94 @@
+/**
+ * Orthanc - A Lightweight, RESTful DICOM Store
+ * Copyright (C) 2012-2013 Medical Physics Department, CHU of Liege,
+ * Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * In addition, as a special exception, the copyright holders of this
+ * program give permission to link the code of its release with the
+ * OpenSSL project's "OpenSSL" library (or with modified versions of it
+ * that use the same license as the "OpenSSL" library), and distribute
+ * the linked executables. You must obey the GNU General Public License
+ * in all respects for all of the code used other than "OpenSSL". If you
+ * modify file(s) with this exception, you may extend this exception to
+ * your version of the file(s), but you are not obligated to do so. If
+ * you do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source files
+ * in the program, then also delete it here.
+ * 
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+#pragma once
+
+#include "OrthancException.h"
+
+#include <boost/lexical_cast.hpp>
+#include <string>
+#include <map>
+
+namespace Orthanc
+{
+  namespace Toolbox
+  {
+    template <typename Enumeration>
+    class EnumerationDictionary
+    {
+    private:
+      typedef std::map<Enumeration, std::string>  EnumerationToString;
+      typedef std::map<std::string, Enumeration>  StringToEnumeration;
+
+      EnumerationToString enumerationToString_;
+      StringToEnumeration stringToEnumeration_;
+
+    public:
+      void Add(Enumeration value, const std::string& str)
+      {
+        enumerationToString_[value] = str;
+        stringToEnumeration_[str] = value;
+        stringToEnumeration_[boost::lexical_cast<std::string>(static_cast<int>(value))] = value;
+      }
+
+      Enumeration Translate(const std::string& str) const
+      {
+        typename StringToEnumeration::const_iterator
+          found = stringToEnumeration_.find(str);
+
+        if (found == stringToEnumeration_.end())
+        {
+          throw OrthancException(ErrorCode_InexistentItem);
+        }
+        else
+        {
+          return found->second;
+        }
+      }
+
+      const std::string& Translate(Enumeration e) const
+      {
+        typename EnumerationToString::const_iterator
+          found = enumerationToString_.find(e);
+
+        if (found == enumerationToString_.end())
+        {
+          throw OrthancException(ErrorCode_InexistentItem);
+        }
+        else
+        {
+          return found->second;
+        }
+      }
+    };
+  }
+}
--- a/OrthancServer/DatabaseWrapper.cpp	Wed May 15 14:19:09 2013 +0200
+++ b/OrthancServer/DatabaseWrapper.cpp	Wed May 15 14:54:58 2013 +0200
@@ -551,8 +551,8 @@
 
       Json::Value item = Json::objectValue;
       item["Seq"] = static_cast<int>(seq);
-      item["ChangeType"] = ToString(changeType);
-      item["ResourceType"] = ToString(resourceType);
+      item["ChangeType"] = EnumerationToString(changeType);
+      item["ResourceType"] = EnumerationToString(resourceType);
       item["ID"] = publicId;
       item["Path"] = GetBasePath(resourceType, publicId);
       item["Date"] = date;
@@ -626,7 +626,7 @@
 
       Json::Value item = Json::objectValue;
       item["Seq"] = static_cast<int>(seq);
-      item["ResourceType"] = ToString(resourceType);
+      item["ResourceType"] = EnumerationToString(resourceType);
       item["ID"] = publicId;
       item["Path"] = GetBasePath(resourceType, publicId);
       item["RemoteModality"] = s.ColumnString(3);
--- a/OrthancServer/OrthancRestApi.cpp	Wed May 15 14:19:09 2013 +0200
+++ b/OrthancServer/OrthancRestApi.cpp	Wed May 15 14:54:58 2013 +0200
@@ -851,7 +851,7 @@
       result["Path"] = GetBasePath(ResourceType_Instance, publicId);
     }
 
-    result["Status"] = ToString(status);
+    result["Status"] = EnumerationToString(status);
     call.GetOutput().AnswerJson(result);
   }
 
@@ -1368,7 +1368,7 @@
             throw OrthancException(ErrorCode_InternalError);
         }
 
-        result["Type"] = ToString(resourceType);
+        result["Type"] = EnumerationToString(resourceType);
         result["ID"] = newId;
         result["Path"] = GetBasePath(resourceType, newId);
         result["PatientID"] = modifiedHasher.HashPatient();
--- a/OrthancServer/ServerEnumerations.cpp	Wed May 15 14:19:09 2013 +0200
+++ b/OrthancServer/ServerEnumerations.cpp	Wed May 15 14:54:58 2013 +0200
@@ -35,7 +35,7 @@
 
 namespace Orthanc
 {
-  const char* ToString(ResourceType type)
+  const char* EnumerationToString(ResourceType type)
   {
     switch (type)
     {
@@ -78,7 +78,7 @@
     }
   }
 
-  const char* ToString(SeriesStatus status)
+  const char* EnumerationToString(SeriesStatus status)
   {
     switch (status)
     {
@@ -99,7 +99,7 @@
     }
   }
 
-  const char* ToString(StoreStatus status)
+  const char* EnumerationToString(StoreStatus status)
   {
     switch (status)
     {
@@ -121,7 +121,7 @@
   }
 
 
-  const char* ToString(ChangeType type)
+  const char* EnumerationToString(ChangeType type)
   {
     switch (type)
     {
--- a/OrthancServer/ServerEnumerations.h	Wed May 15 14:19:09 2013 +0200
+++ b/OrthancServer/ServerEnumerations.h	Wed May 15 14:54:58 2013 +0200
@@ -102,13 +102,13 @@
   std::string GetBasePath(ResourceType type,
                           const std::string& publicId);
 
-  const char* ToString(ResourceType type);
+  const char* EnumerationToString(ResourceType type);
 
-  const char* ToString(SeriesStatus status);
+  const char* EnumerationToString(SeriesStatus status);
 
-  const char* ToString(StoreStatus status);
+  const char* EnumerationToString(StoreStatus status);
 
-  const char* ToString(ChangeType type);
+  const char* EnumerationToString(ChangeType type);
 
   ResourceType GetParentResourceType(ResourceType type);
 
--- a/OrthancServer/ServerIndex.cpp	Wed May 15 14:19:09 2013 +0200
+++ b/OrthancServer/ServerIndex.cpp	Wed May 15 14:54:58 2013 +0200
@@ -211,7 +211,7 @@
 
       target["RemainingAncestor"] = Json::Value(Json::objectValue);
       target["RemainingAncestor"]["Path"] = GetBasePath(type, uuid);
-      target["RemainingAncestor"]["Type"] = ToString(type);
+      target["RemainingAncestor"]["Type"] = EnumerationToString(type);
       target["RemainingAncestor"]["ID"] = uuid;
     }
     else
@@ -705,7 +705,7 @@
       case ResourceType_Series:
       {
         result["Type"] = "Series";
-        result["Status"] = ToString(GetSeriesStatus(id));
+        result["Status"] = EnumerationToString(GetSeriesStatus(id));
 
         int i;
         if (db_->GetMetadataAsInteger(i, id, MetadataType_Series_ExpectedNumberOfInstances))
--- a/UnitTests/main.cpp	Wed May 15 14:19:09 2013 +0200
+++ b/UnitTests/main.cpp	Wed May 15 14:54:58 2013 +0200
@@ -1,3 +1,5 @@
+#include "../Core/EnumerationDictionary.h"
+
 #include "gtest/gtest.h"
 
 #include <ctype.h>
@@ -344,6 +346,40 @@
 #endif
 
 
+
+#include "../OrthancServer/ServerEnumerations.h"
+
+TEST(EnumerationDictionary, Simple)
+{
+  Toolbox::EnumerationDictionary<MetadataType>  d;
+
+  ASSERT_THROW(d.Translate("2"), OrthancException);
+  ASSERT_THROW(d.Translate("ReceptionDate"), OrthancException);
+
+  d.Add(MetadataType_Instance_ReceptionDate, "ReceptionDate");
+
+  ASSERT_EQ(MetadataType_Instance_ReceptionDate, d.Translate("ReceptionDate"));
+  ASSERT_EQ(MetadataType_Instance_ReceptionDate, d.Translate("2"));
+  ASSERT_EQ("ReceptionDate", d.Translate(MetadataType_Instance_ReceptionDate));
+}
+
+
+TEST(EnumerationDictionary, ServerEnumerations)
+{
+  ASSERT_STREQ("Patient", EnumerationToString(ResourceType_Patient));
+  ASSERT_STREQ("Study", EnumerationToString(ResourceType_Study));
+  ASSERT_STREQ("Series", EnumerationToString(ResourceType_Series));
+  ASSERT_STREQ("Instance", EnumerationToString(ResourceType_Instance));
+
+  ASSERT_STREQ("ModifiedSeries", EnumerationToString(ChangeType_ModifiedSeries));
+
+  ASSERT_STREQ("Failure", EnumerationToString(StoreStatus_Failure));
+  ASSERT_STREQ("Success", EnumerationToString(StoreStatus_Success));
+
+  ASSERT_STREQ("CompletedSeries", EnumerationToString(ChangeType_CompletedSeries));
+}
+
+
 int main(int argc, char **argv)
 {
   // Initialize Google's logging library.