changeset 72:e94f177c3653

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 04 Oct 2024 17:25:46 +0200
parents f3bbafc067d0
children 1963619e3e87
files CMakeLists.txt Sources/Plugin.cpp Sources/ResourcesCache.cpp Sources/ResourcesCache.h
diffstat 4 files changed, 214 insertions(+), 130 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Fri Oct 04 17:18:42 2024 +0200
+++ b/CMakeLists.txt	Fri Oct 04 17:25:46 2024 +0200
@@ -253,6 +253,7 @@
 add_library(OrthancSTL SHARED
   Sources/Extent2D.cpp
   Sources/Plugin.cpp
+  Sources/ResourcesCache.cpp
   Sources/STLToolbox.cpp
   Sources/StructurePolygon.cpp
   Sources/StructureSet.cpp
--- a/Sources/Plugin.cpp	Fri Oct 04 17:18:42 2024 +0200
+++ b/Sources/Plugin.cpp	Fri Oct 04 17:25:46 2024 +0200
@@ -30,8 +30,9 @@
 #  error Macro ORTHANC_ENABLE_3DHOP must be defined
 #endif
 
+#include "ResourcesCache.h"
+#include "STLToolbox.h"
 #include "StructureSetGeometry.h"
-#include "STLToolbox.h"
 #include "VTKToolbox.h"
 
 #include <EmbeddedResources.h>
@@ -107,135 +108,6 @@
 #endif
 
 
-// Forward declaration
-void ReadStaticAsset(std::string& target,
-                     const std::string& path);
-
-
-/**
- * As the static assets are gzipped by the "EmbedStaticAssets.py"
- * script, we use a cache to maintain the uncompressed assets in order
- * to avoid multiple gzip decodings.
- **/
-class ResourcesCache : public boost::noncopyable
-{
-public:
-  class IHandler : public boost::noncopyable
-  {
-  public:
-    virtual ~IHandler()
-    {
-    }
-
-    virtual void Apply(const std::string& resource) = 0;
-  };
-
-private:
-  typedef std::map<std::string, std::string*>  Content;
-  
-  boost::shared_mutex  mutex_;
-  Content              content_;
-
-  class RestOutputHandler : public IHandler
-  {
-  private:
-    OrthancPluginRestOutput* output_;
-    std::string              mime_;
-
-  public:
-    RestOutputHandler(OrthancPluginRestOutput* output,
-                      const std::string& mime) :
-      output_(output),
-      mime_(mime)
-    {
-    }
-
-    virtual void Apply(const std::string& resource) ORTHANC_OVERRIDE
-    {
-      OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output_,
-                                resource.c_str(), resource.size(), mime_.c_str());
-    }
-  };
-
-  class StoreResourceIntoString : public IHandler
-  {
-  private:
-    std::string& target_;
-
-  public:
-    StoreResourceIntoString(std::string& target) :
-      target_(target)
-    {
-    }
-
-    virtual void Apply(const std::string& resource) ORTHANC_OVERRIDE
-    {
-      target_ = resource;
-    }
-  };
-
-public:
-  ~ResourcesCache()
-  {
-    for (Content::iterator it = content_.begin(); it != content_.end(); ++it)
-    {
-      assert(it->second != NULL);
-      delete it->second;
-    }
-  }
-
-  void Apply(IHandler& handler,
-             const std::string& path)
-  {
-    {
-      // Check whether the cache already contains the resource
-      boost::shared_lock<boost::shared_mutex> lock(mutex_);
-
-      Content::const_iterator found = content_.find(path);
-    
-      if (found != content_.end())
-      {
-        assert(found->second != NULL);
-        handler.Apply(*found->second);
-        return;
-      }
-    }
-
-    // This resource has not been cached yet
-
-    std::unique_ptr<std::string> item(new std::string);
-    ReadStaticAsset(*item, path);
-    handler.Apply(*item);
-
-    {
-      // Store the resource into the cache
-      boost::unique_lock<boost::shared_mutex> lock(mutex_);
-
-      if (content_.find(path) == content_.end())
-      {
-        content_[path] = item.release();
-      }
-    }
-  }
-
-  void Answer(OrthancPluginRestOutput* output,
-              const std::string& path)
-  {
-    const std::string mime = Orthanc::EnumerationToString(Orthanc::SystemToolbox::AutodetectMimeType(path));
-
-    RestOutputHandler handler(output, mime);
-    Apply(handler, path);
-  }
-
-  void ReadResource(std::string& target,
-                   const std::string& path)
-  {
-    StoreResourceIntoString handler(target);
-    Apply(handler, path);
-  }
-};
-
-
 static ResourcesCache cache_;
 
 void ServeFile(OrthancPluginRestOutput* output,
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sources/ResourcesCache.cpp	Fri Oct 04 17:25:46 2024 +0200
@@ -0,0 +1,138 @@
+/**
+ * SPDX-FileCopyrightText: 2023-2024 Sebastien Jodogne, UCLouvain, Belgium
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+/**
+ * STL plugin for Orthanc
+ * Copyright (C) 2023-2024 Sebastien Jodogne, UCLouvain, 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.
+ *
+ * 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/>.
+ **/
+
+
+#include "ResourcesCache.h"
+
+#include "../Resources/Orthanc/Plugins/OrthancPluginCppWrapper.h"
+
+#include <Compatibility.h>
+#include <SystemToolbox.h>
+
+
+// Forward declaration
+void ReadStaticAsset(std::string& target,
+                     const std::string& path);
+
+
+class ResourcesCache::RestOutputHandler : public IHandler
+{
+private:
+  OrthancPluginRestOutput* output_;
+  std::string              mime_;
+
+public:
+  RestOutputHandler(OrthancPluginRestOutput* output,
+                    const std::string& mime) :
+    output_(output),
+    mime_(mime)
+  {
+  }
+
+  virtual void Apply(const std::string& resource) ORTHANC_OVERRIDE
+  {
+    OrthancPluginAnswerBuffer(OrthancPlugins::GetGlobalContext(), output_,
+                              resource.c_str(), resource.size(), mime_.c_str());
+  }
+};
+
+
+class ResourcesCache::StoreResourceIntoString : public IHandler
+{
+private:
+  std::string& target_;
+
+public:
+  StoreResourceIntoString(std::string& target) :
+    target_(target)
+  {
+  }
+
+  virtual void Apply(const std::string& resource) ORTHANC_OVERRIDE
+  {
+    target_ = resource;
+  }
+};
+
+
+ResourcesCache::~ResourcesCache()
+{
+  for (Content::iterator it = content_.begin(); it != content_.end(); ++it)
+  {
+    assert(it->second != NULL);
+    delete it->second;
+  }
+}
+
+
+void ResourcesCache::Apply(IHandler& handler,
+                           const std::string& path)
+{
+  {
+    // Check whether the cache already contains the resource
+    boost::shared_lock<boost::shared_mutex> lock(mutex_);
+
+    Content::const_iterator found = content_.find(path);
+
+    if (found != content_.end())
+    {
+      assert(found->second != NULL);
+      handler.Apply(*found->second);
+      return;
+    }
+  }
+
+  // This resource has not been cached yet
+
+  std::unique_ptr<std::string> item(new std::string);
+  ReadStaticAsset(*item, path);
+  handler.Apply(*item);
+
+  {
+    // Store the resource into the cache
+    boost::unique_lock<boost::shared_mutex> lock(mutex_);
+
+    if (content_.find(path) == content_.end())
+    {
+      content_[path] = item.release();
+    }
+  }
+}
+
+
+void ResourcesCache::Answer(OrthancPluginRestOutput* output,
+                            const std::string& path)
+{
+  const std::string mime = Orthanc::EnumerationToString(Orthanc::SystemToolbox::AutodetectMimeType(path));
+
+  RestOutputHandler handler(output, mime);
+  Apply(handler, path);
+}
+
+
+void ResourcesCache::ReadResource(std::string& target,
+                                  const std::string& path)
+{
+  StoreResourceIntoString handler(target);
+  Apply(handler, path);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Sources/ResourcesCache.h	Fri Oct 04 17:25:46 2024 +0200
@@ -0,0 +1,73 @@
+/**
+ * SPDX-FileCopyrightText: 2023-2024 Sebastien Jodogne, UCLouvain, Belgium
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+/**
+ * STL plugin for Orthanc
+ * Copyright (C) 2023-2024 Sebastien Jodogne, UCLouvain, 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.
+ *
+ * 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 <orthanc/OrthancCPlugin.h>
+
+#include <boost/noncopyable.hpp>
+#include <boost/thread/shared_mutex.hpp>
+#include <map>
+#include <string>
+
+
+/**
+ * As the static assets are gzipped by the "EmbedStaticAssets.py"
+ * script, we use a cache to maintain the uncompressed assets in order
+ * to avoid multiple gzip decodings.
+ **/
+class ResourcesCache : public boost::noncopyable
+{
+public:
+  class IHandler : public boost::noncopyable
+  {
+  public:
+    virtual ~IHandler()
+    {
+    }
+
+    virtual void Apply(const std::string& resource) = 0;
+  };
+
+private:
+  typedef std::map<std::string, std::string*>  Content;
+
+  boost::shared_mutex  mutex_;
+  Content              content_;
+
+  class RestOutputHandler;
+  class StoreResourceIntoString;
+
+public:
+  ~ResourcesCache();
+
+  void Apply(IHandler& handler,
+             const std::string& path);
+
+  void Answer(OrthancPluginRestOutput* output,
+              const std::string& path);
+
+  void ReadResource(std::string& target,
+                    const std::string& path);
+};