changeset 2177:11420238f337

ORTHANC_PLUGINS_THROW_EXCEPTION macro
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 25 Nov 2016 12:29:28 +0100
parents fead5549aaa7
children 037e7bc16541
files Plugins/Samples/Common/OrthancPluginCppWrapper.cpp Plugins/Samples/Common/OrthancPluginCppWrapper.h Plugins/Samples/ServeFolders/Plugin.cpp
diffstat 3 files changed, 37 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp	Fri Nov 25 12:07:36 2016 +0100
+++ b/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp	Fri Nov 25 12:29:28 2016 +0100
@@ -36,22 +36,8 @@
 #include <json/writer.h>
 
 
-#if HAS_ORTHANC_EXCEPTION == 1
-#  define THROW_EXCEPTION(code)  throw Orthanc::OrthancException(static_cast<Orthanc::ErrorCode>(code))
-#else
-#  define THROW_EXCEPTION(code)  throw PluginException(code)
-#endif
-
-
-
 namespace OrthancPlugins
 {
-  void ThrowException(OrthancPluginErrorCode code)
-  {
-    THROW_EXCEPTION(code);
-  }
-
-
   const char* GetErrorDescription(OrthancPluginContext* context,
                                   OrthancPluginErrorCode code)
   {
@@ -72,7 +58,7 @@
   {
     if (code != OrthancPluginErrorCode_Success)
     {
-      THROW_EXCEPTION(code);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(code);
     }
   }
 #endif
@@ -85,7 +71,7 @@
       // Prevent using garbage information
       buffer_.data = NULL;
       buffer_.size = 0;
-      THROW_EXCEPTION(code);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(code);
     }
   }
 
@@ -139,7 +125,7 @@
     if (buffer_.data == NULL ||
         buffer_.size == 0)
     {
-      THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
     }
 
     const char* tmp = reinterpret_cast<const char*>(buffer_.data);
@@ -148,7 +134,7 @@
     if (!reader.parse(tmp, tmp + buffer_.size, target))
     {
       OrthancPluginLogError(context_, "Cannot convert some memory buffer to JSON");
-      THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
     }
   }
 
@@ -180,7 +166,7 @@
     }
     else
     {
-      THROW_EXCEPTION(error);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(error);
     }
   }
 
@@ -214,7 +200,7 @@
     }
     else
     {
-      THROW_EXCEPTION(error);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(error);
     }
   }
 
@@ -248,7 +234,7 @@
     }
     else
     {
-      THROW_EXCEPTION(error);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(error);
     }
   }
 
@@ -326,14 +312,14 @@
     if (str_ == NULL)
     {
       OrthancPluginLogError(context_, "Cannot convert an empty memory buffer to JSON");
-      THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
     }
 
     Json::Reader reader;
     if (!reader.parse(str_, target))
     {
       OrthancPluginLogError(context_, "Cannot convert some memory buffer to JSON");
-      THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
     }
   }
   
@@ -346,7 +332,7 @@
     if (str.GetContent() == NULL)
     {
       OrthancPluginLogError(context, "Cannot access the Orthanc configuration");
-      THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
     }
 
     str.ToJson(configuration_);
@@ -354,7 +340,7 @@
     if (configuration_.type() != Json::objectValue)
     {
       OrthancPluginLogError(context, "Unable to read the Orthanc configuration");
-      THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
     }
   }
 
@@ -363,7 +349,7 @@
   {
     if (context_ == NULL)
     {
-      THROW_EXCEPTION(OrthancPluginErrorCode_Plugin);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_Plugin);
     }
     else
     {
@@ -416,7 +402,7 @@
           OrthancPluginLogError(context_, s.c_str());
         }
 
-        THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+        ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
       }
 
       target.configuration_ = configuration_[key];
@@ -442,7 +428,7 @@
         OrthancPluginLogError(context_, s.c_str());
       }
 
-      THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
     }
 
     target = configuration_[key].asString();
@@ -477,7 +463,7 @@
           OrthancPluginLogError(context_, s.c_str());
         }
 
-        THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+        ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
     }
   }
 
@@ -499,7 +485,7 @@
         OrthancPluginLogError(context_, s.c_str());
       }
 
-      THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
     }
     else
     {
@@ -527,7 +513,7 @@
         OrthancPluginLogError(context_, s.c_str());
       }
 
-      THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
     }
 
     target = configuration_[key].asBool();
@@ -566,7 +552,7 @@
           OrthancPluginLogError(context_, s.c_str());
         }
 
-        THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
+        ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
     }
   }
 
@@ -661,7 +647,7 @@
     if (image_ == NULL)
     {
       OrthancPluginLogError(context_, "Trying to access a NULL image");
-      THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
     }
   }
 
@@ -672,7 +658,7 @@
   {
     if (context == NULL)
     {
-      THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
     }
   }
 
@@ -684,7 +670,7 @@
   {
     if (context == NULL)
     {
-      THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
     }
   }
   
@@ -697,7 +683,7 @@
   {
     if (context == NULL)
     {
-      THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
     }
     else
     {
@@ -714,7 +700,7 @@
     if (image_ == NULL)
     {
       OrthancPluginLogError(context_, "Cannot uncompress a PNG image");
-      THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
     }
   }
 
@@ -727,7 +713,7 @@
     if (image_ == NULL)
     {
       OrthancPluginLogError(context_, "Cannot uncompress a JPEG image");
-      THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
     }
   }
 
@@ -741,7 +727,7 @@
     if (image_ == NULL)
     {
       OrthancPluginLogError(context_, "Cannot uncompress a DICOM image");
-      THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
     }
   }
 
@@ -929,7 +915,7 @@
     }
     else
     {
-      THROW_EXCEPTION(error);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(error);
     }
   }
 
--- a/Plugins/Samples/Common/OrthancPluginCppWrapper.h	Fri Nov 25 12:07:36 2016 +0100
+++ b/Plugins/Samples/Common/OrthancPluginCppWrapper.h	Fri Nov 25 12:29:28 2016 +0100
@@ -44,9 +44,13 @@
 
 #if HAS_ORTHANC_EXCEPTION == 1
 #  include "../../../Core/OrthancException.h"
+#  define ORTHANC_PLUGINS_THROW_EXCEPTION(code)  throw ::Orthanc::OrthancException(static_cast<Orthanc::ErrorCode>(code))
+#else
+#  define ORTHANC_PLUGINS_THROW_EXCEPTION(code)  throw ::OrthancPlugins::PluginException(code)
 #endif
 
 
+
 namespace OrthancPlugins
 {
   typedef void (*RestCallback) (OrthancPluginRestOutput* output,
@@ -56,8 +60,6 @@
   const char* GetErrorDescription(OrthancPluginContext* context,
                                   OrthancPluginErrorCode code);
 
-  void ThrowException(OrthancPluginErrorCode code);
-
 #if HAS_ORTHANC_EXCEPTION == 0
   class PluginException
   {
--- a/Plugins/Samples/ServeFolders/Plugin.cpp	Fri Nov 25 12:07:36 2016 +0100
+++ b/Plugins/Samples/ServeFolders/Plugin.cpp	Fri Nov 25 12:29:28 2016 +0100
@@ -202,7 +202,7 @@
       }
       catch (...)
       {
-        OrthancPlugins::ThrowException(OrthancPluginErrorCode_InexistentFile);
+        ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InexistentFile);
       }
 
       boost::posix_time::ptime lastModification = boost::posix_time::from_time_t(fs::last_write_time(path));
@@ -255,7 +255,7 @@
   if (folders.type() != Json::objectValue)
   {
     OrthancPlugins::LogError(context_, "The list of folders to be served is badly formatted (must be a JSON object)");
-    OrthancPlugins::ThrowException(OrthancPluginErrorCode_BadFileFormat);
+    ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
   }
 
   Json::Value::Members members = folders.getMemberNames();
@@ -268,7 +268,7 @@
     {
       OrthancPlugins::LogError(context_, "The folder to be served \"" + *it + 
                                "\" must be associated with a string value (its mapped URI)");
-      OrthancPlugins::ThrowException(OrthancPluginErrorCode_BadFileFormat);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
     }
 
     std::string baseUri = *it;
@@ -289,7 +289,7 @@
     if (baseUri.empty())
     {
       OrthancPlugins::LogError(context_, "The URI of a folder to be served cannot be empty");
-      OrthancPlugins::ThrowException(OrthancPluginErrorCode_BadFileFormat);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
     }
 
     // Check whether the source folder exists and is indeed a directory
@@ -297,7 +297,7 @@
     if (!boost::filesystem::is_directory(folder))
     {
       OrthancPlugins::LogError(context_, "Trying and serve an inexistent folder: " + folder);
-      OrthancPlugins::ThrowException(OrthancPluginErrorCode_InexistentFile);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InexistentFile);
     }
 
     folders_[baseUri] = folder;
@@ -316,7 +316,7 @@
   if (extensions.type() != Json::objectValue)
   {
     OrthancPlugins::LogError(context_, "The list of extensions is badly formatted (must be a JSON object)");
-    OrthancPlugins::ThrowException(OrthancPluginErrorCode_BadFileFormat);
+    ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
   }
 
   Json::Value::Members members = extensions.getMemberNames();
@@ -328,7 +328,7 @@
     {
       OrthancPlugins::LogError(context_, "The file extension \"" + *it + 
                                "\" must be associated with a string value (its MIME type)");
-      OrthancPlugins::ThrowException(OrthancPluginErrorCode_BadFileFormat);
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
     }
 
     const std::string& mime = extensions[*it].asString();