changeset 2176:fead5549aaa7

introduction of HAS_ORTHANC_EXCEPTION to avoid PluginException if not necessary
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 25 Nov 2016 12:07:36 +0100
parents bed8e7ad8bab
children 11420238f337
files CMakeLists.txt Plugins/Samples/Common/OrthancPluginCppWrapper.cpp Plugins/Samples/Common/OrthancPluginCppWrapper.h Plugins/Samples/Common/OrthancPlugins.cmake Plugins/Samples/ServeFolders/CMakeLists.txt Plugins/Samples/ServeFolders/Plugin.cpp
diffstat 6 files changed, 87 insertions(+), 44 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Thu Nov 24 12:58:43 2016 +0100
+++ b/CMakeLists.txt	Fri Nov 25 12:07:36 2016 +0100
@@ -417,6 +417,7 @@
   -DORTHANC_SANDBOXED=0
 
   # Macros for the plugins
+  -DHAS_ORTHANC_EXCEPTION=0
   -DMODALITY_WORKLISTS_VERSION="${ORTHANC_VERSION}"
   -DSERVE_FOLDERS_VERSION="${ORTHANC_VERSION}"
   )
--- a/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp	Thu Nov 24 12:58:43 2016 +0100
+++ b/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp	Fri Nov 25 12:07:36 2016 +0100
@@ -36,11 +36,26 @@
 #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
 {
-  const char* PluginException::GetErrorDescription(OrthancPluginContext* context) const
+  void ThrowException(OrthancPluginErrorCode code)
   {
-    const char* description = OrthancPluginGetErrorDescription(context, code_);
+    THROW_EXCEPTION(code);
+  }
+
+
+  const char* GetErrorDescription(OrthancPluginContext* context,
+                                  OrthancPluginErrorCode code)
+  {
+    const char* description = OrthancPluginGetErrorDescription(context, code);
     if (description)
     {
       return description;
@@ -52,13 +67,15 @@
   }
 
   
+#if HAS_ORTHANC_EXCEPTION == 0
   void PluginException::Check(OrthancPluginErrorCode code)
   {
     if (code != OrthancPluginErrorCode_Success)
     {
-      throw PluginException(code);
+      THROW_EXCEPTION(code);
     }
   }
+#endif
 
 
   void MemoryBuffer::Check(OrthancPluginErrorCode code)
@@ -68,7 +85,7 @@
       // Prevent using garbage information
       buffer_.data = NULL;
       buffer_.size = 0;
-      throw PluginException(code);
+      THROW_EXCEPTION(code);
     }
   }
 
@@ -122,7 +139,7 @@
     if (buffer_.data == NULL ||
         buffer_.size == 0)
     {
-      throw PluginException(OrthancPluginErrorCode_InternalError);
+      THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
     }
 
     const char* tmp = reinterpret_cast<const char*>(buffer_.data);
@@ -131,7 +148,7 @@
     if (!reader.parse(tmp, tmp + buffer_.size, target))
     {
       OrthancPluginLogError(context_, "Cannot convert some memory buffer to JSON");
-      throw PluginException(OrthancPluginErrorCode_BadFileFormat);
+      THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
     }
   }
 
@@ -163,7 +180,7 @@
     }
     else
     {
-      throw PluginException(error);
+      THROW_EXCEPTION(error);
     }
   }
 
@@ -197,7 +214,7 @@
     }
     else
     {
-      throw PluginException(error);
+      THROW_EXCEPTION(error);
     }
   }
 
@@ -231,7 +248,7 @@
     }
     else
     {
-      throw PluginException(error);
+      THROW_EXCEPTION(error);
     }
   }
 
@@ -309,14 +326,14 @@
     if (str_ == NULL)
     {
       OrthancPluginLogError(context_, "Cannot convert an empty memory buffer to JSON");
-      throw PluginException(OrthancPluginErrorCode_InternalError);
+      THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
     }
 
     Json::Reader reader;
     if (!reader.parse(str_, target))
     {
       OrthancPluginLogError(context_, "Cannot convert some memory buffer to JSON");
-      throw PluginException(OrthancPluginErrorCode_BadFileFormat);
+      THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
     }
   }
   
@@ -329,7 +346,7 @@
     if (str.GetContent() == NULL)
     {
       OrthancPluginLogError(context, "Cannot access the Orthanc configuration");
-      throw PluginException(OrthancPluginErrorCode_InternalError);
+      THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
     }
 
     str.ToJson(configuration_);
@@ -337,7 +354,7 @@
     if (configuration_.type() != Json::objectValue)
     {
       OrthancPluginLogError(context, "Unable to read the Orthanc configuration");
-      throw PluginException(OrthancPluginErrorCode_InternalError);
+      THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
     }
   }
 
@@ -346,7 +363,7 @@
   {
     if (context_ == NULL)
     {
-      throw PluginException(OrthancPluginErrorCode_Plugin);
+      THROW_EXCEPTION(OrthancPluginErrorCode_Plugin);
     }
     else
     {
@@ -399,7 +416,7 @@
           OrthancPluginLogError(context_, s.c_str());
         }
 
-        throw PluginException(OrthancPluginErrorCode_BadFileFormat);
+        THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
       }
 
       target.configuration_ = configuration_[key];
@@ -425,7 +442,7 @@
         OrthancPluginLogError(context_, s.c_str());
       }
 
-      throw PluginException(OrthancPluginErrorCode_BadFileFormat);
+      THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
     }
 
     target = configuration_[key].asString();
@@ -460,7 +477,7 @@
           OrthancPluginLogError(context_, s.c_str());
         }
 
-        throw PluginException(OrthancPluginErrorCode_BadFileFormat);
+        THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
     }
   }
 
@@ -482,7 +499,7 @@
         OrthancPluginLogError(context_, s.c_str());
       }
 
-      throw PluginException(OrthancPluginErrorCode_BadFileFormat);
+      THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
     }
     else
     {
@@ -510,7 +527,7 @@
         OrthancPluginLogError(context_, s.c_str());
       }
 
-      throw PluginException(OrthancPluginErrorCode_BadFileFormat);
+      THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
     }
 
     target = configuration_[key].asBool();
@@ -549,7 +566,7 @@
           OrthancPluginLogError(context_, s.c_str());
         }
 
-        throw PluginException(OrthancPluginErrorCode_BadFileFormat);
+        THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
     }
   }
 
@@ -644,7 +661,7 @@
     if (image_ == NULL)
     {
       OrthancPluginLogError(context_, "Trying to access a NULL image");
-      throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange);
+      THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
     }
   }
 
@@ -655,7 +672,7 @@
   {
     if (context == NULL)
     {
-      throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange);
+      THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
     }
   }
 
@@ -667,7 +684,7 @@
   {
     if (context == NULL)
     {
-      throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange);
+      THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
     }
   }
   
@@ -680,7 +697,7 @@
   {
     if (context == NULL)
     {
-      throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange);
+      THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
     }
     else
     {
@@ -697,7 +714,7 @@
     if (image_ == NULL)
     {
       OrthancPluginLogError(context_, "Cannot uncompress a PNG image");
-      throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange);
+      THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
     }
   }
 
@@ -710,7 +727,7 @@
     if (image_ == NULL)
     {
       OrthancPluginLogError(context_, "Cannot uncompress a JPEG image");
-      throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange);
+      THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
     }
   }
 
@@ -724,7 +741,7 @@
     if (image_ == NULL)
     {
       OrthancPluginLogError(context_, "Cannot uncompress a DICOM image");
-      throw PluginException(OrthancPluginErrorCode_ParameterOutOfRange);
+      THROW_EXCEPTION(OrthancPluginErrorCode_ParameterOutOfRange);
     }
   }
 
@@ -912,7 +929,7 @@
     }
     else
     {
-      throw PluginException(error);
+      THROW_EXCEPTION(error);
     }
   }
 
--- a/Plugins/Samples/Common/OrthancPluginCppWrapper.h	Thu Nov 24 12:58:43 2016 +0100
+++ b/Plugins/Samples/Common/OrthancPluginCppWrapper.h	Fri Nov 25 12:07:36 2016 +0100
@@ -37,6 +37,11 @@
 #include <boost/lexical_cast.hpp>
 #include <json/value.h>
 
+#if !defined(HAS_ORTHANC_EXCEPTION)
+#  error The macro HAS_ORTHANC_EXCEPTION must be defined
+#endif
+
+
 #if HAS_ORTHANC_EXCEPTION == 1
 #  include "../../../Core/OrthancException.h"
 #endif
@@ -48,7 +53,12 @@
                                 const char* url,
                                 const OrthancPluginHttpRequest* request);
 
+  const char* GetErrorDescription(OrthancPluginContext* context,
+                                  OrthancPluginErrorCode code);
 
+  void ThrowException(OrthancPluginErrorCode code);
+
+#if HAS_ORTHANC_EXCEPTION == 0
   class PluginException
   {
   private:
@@ -64,10 +74,14 @@
       return code_;
     }
 
-    const char* GetErrorDescription(OrthancPluginContext* context) const;
+    const char* What(OrthancPluginContext* context) const
+    {
+      return ::OrthancPlugins::GetErrorDescription(context, code_);
+    }
 
     static void Check(OrthancPluginErrorCode code);
   };
+#endif
 
 
   class MemoryBuffer : public boost::noncopyable
@@ -408,15 +422,16 @@
         Callback(output, url, request);
         return OrthancPluginErrorCode_Success;
       }
-      catch (OrthancPlugins::PluginException& e)
-      {
-        return e.GetErrorCode();
-      }
 #if HAS_ORTHANC_EXCEPTION == 1
       catch (Orthanc::OrthancException& e)
       {
         return static_cast<OrthancPluginErrorCode>(e.GetErrorCode());
       }
+#else
+      catch (OrthancPlugins::PluginException& e)
+      {
+        return e.GetErrorCode();
+      }
 #endif
       catch (boost::bad_lexical_cast&)
       {
--- a/Plugins/Samples/Common/OrthancPlugins.cmake	Thu Nov 24 12:58:43 2016 +0100
+++ b/Plugins/Samples/Common/OrthancPlugins.cmake	Fri Nov 25 12:07:36 2016 +0100
@@ -27,3 +27,6 @@
 if (MSVC)
   include_directories(${SAMPLES_ROOT}/../../Resources/ThirdParty/VisualStudio/)
 endif()
+
+
+add_definitions(-DORTHANC_HAS_EXCEPTION=0)
--- a/Plugins/Samples/ServeFolders/CMakeLists.txt	Thu Nov 24 12:58:43 2016 +0100
+++ b/Plugins/Samples/ServeFolders/CMakeLists.txt	Fri Nov 25 12:07:36 2016 +0100
@@ -21,6 +21,8 @@
   ${BOOST_SOURCES}
   )
 
+add_definitions(-DHAS_ORTHANC_EXCEPTION=0)
+
 message("Setting the version of the plugin to ${SERVE_FOLDERS_VERSION}")
 add_definitions(-DSERVE_FOLDERS_VERSION="${SERVE_FOLDERS_VERSION}")
 
--- a/Plugins/Samples/ServeFolders/Plugin.cpp	Thu Nov 24 12:58:43 2016 +0100
+++ b/Plugins/Samples/ServeFolders/Plugin.cpp	Fri Nov 25 12:07:36 2016 +0100
@@ -26,6 +26,12 @@
 #include <boost/date_time/posix_time/posix_time.hpp>
 
 
+#if HAS_ORTHANC_EXCEPTION == 1
+#  error The macro HAS_ORTHANC_EXCEPTION must be set to 0 to compile this plugin
+#endif
+
+
+
 static OrthancPluginContext* context_ = NULL;
 static std::map<std::string, std::string> extensions_;
 static std::map<std::string, std::string> folders_;
@@ -194,9 +200,9 @@
       {
         content.ReadFile(path);
       }
-      catch (OrthancPlugins::PluginException&)
+      catch (...)
       {
-        throw OrthancPlugins::PluginException(OrthancPluginErrorCode_InexistentFile);
+        OrthancPlugins::ThrowException(OrthancPluginErrorCode_InexistentFile);
       }
 
       boost::posix_time::ptime lastModification = boost::posix_time::from_time_t(fs::last_write_time(path));
@@ -249,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)");
-    throw OrthancPlugins::PluginException(OrthancPluginErrorCode_BadFileFormat);
+    OrthancPlugins::ThrowException(OrthancPluginErrorCode_BadFileFormat);
   }
 
   Json::Value::Members members = folders.getMemberNames();
@@ -262,7 +268,7 @@
     {
       OrthancPlugins::LogError(context_, "The folder to be served \"" + *it + 
                                "\" must be associated with a string value (its mapped URI)");
-      throw OrthancPlugins::PluginException(OrthancPluginErrorCode_BadFileFormat);
+      OrthancPlugins::ThrowException(OrthancPluginErrorCode_BadFileFormat);
     }
 
     std::string baseUri = *it;
@@ -283,7 +289,7 @@
     if (baseUri.empty())
     {
       OrthancPlugins::LogError(context_, "The URI of a folder to be served cannot be empty");
-      throw OrthancPlugins::PluginException(OrthancPluginErrorCode_BadFileFormat);
+      OrthancPlugins::ThrowException(OrthancPluginErrorCode_BadFileFormat);
     }
 
     // Check whether the source folder exists and is indeed a directory
@@ -291,7 +297,7 @@
     if (!boost::filesystem::is_directory(folder))
     {
       OrthancPlugins::LogError(context_, "Trying and serve an inexistent folder: " + folder);
-      throw OrthancPlugins::PluginException(OrthancPluginErrorCode_InexistentFile);
+      OrthancPlugins::ThrowException(OrthancPluginErrorCode_InexistentFile);
     }
 
     folders_[baseUri] = folder;
@@ -310,7 +316,7 @@
   if (extensions.type() != Json::objectValue)
   {
     OrthancPlugins::LogError(context_, "The list of extensions is badly formatted (must be a JSON object)");
-    throw OrthancPlugins::PluginException(OrthancPluginErrorCode_BadFileFormat);
+    OrthancPlugins::ThrowException(OrthancPluginErrorCode_BadFileFormat);
   }
 
   Json::Value::Members members = extensions.getMemberNames();
@@ -322,7 +328,7 @@
     {
       OrthancPlugins::LogError(context_, "The file extension \"" + *it + 
                                "\" must be associated with a string value (its MIME type)");
-      throw OrthancPlugins::PluginException(OrthancPluginErrorCode_BadFileFormat);
+      OrthancPlugins::ThrowException(OrthancPluginErrorCode_BadFileFormat);
     }
 
     const std::string& mime = extensions[*it].asString();
@@ -428,9 +434,8 @@
     }
     catch (OrthancPlugins::PluginException& e)
     {
-      OrthancPlugins::LogError(context, "Error while initializing the ServeFolders plugin: " + 
-                               std::string(e.GetErrorDescription(context)));
-      return -1;
+      OrthancPlugins::LogError(context_, "Error while initializing the ServeFolders plugin: " + 
+                               std::string(e.What(context_)));
     }
 
     return 0;