changeset 223:af049cd66661

removed singleton from DatabaseBackendAdapterV3
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 01 Apr 2021 11:50:36 +0200
parents c8e06b41feec
children 61c309e06797
files Framework/Plugins/DatabaseBackendAdapterV3.cpp
diffstat 1 files changed, 30 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Plugins/DatabaseBackendAdapterV3.cpp	Thu Apr 01 11:16:13 2021 +0200
+++ b/Framework/Plugins/DatabaseBackendAdapterV3.cpp	Thu Apr 01 11:50:36 2021 +0200
@@ -54,6 +54,8 @@
 
 namespace OrthancDatabases
 {
+  static bool isBackendInUse_ = false;  // Only for sanity checks
+  
   class DatabaseBackendAdapterV3::Output : public IDatabaseBackendOutput
   {
   private:
@@ -805,13 +807,25 @@
   
   static OrthancPluginErrorCode DestructDatabase(void* database)
   {
-    // Nothing to delete, as this plugin uses a singleton to store backend
-    if (database == NULL)
+    IndexBackend* backend = reinterpret_cast<IndexBackend*>(database);
+
+    if (backend == NULL)
     {
       return OrthancPluginErrorCode_InternalError;
     }
     else
     {
+      if (isBackendInUse_)
+      {
+        isBackendInUse_ = false;
+      }
+      else
+      {
+        OrthancPluginLogError(backend->GetContext(), "More than one index backend was registered, internal error");
+      }
+      
+      delete backend;
+
       return OrthancPluginErrorCode_Success;
     }
   }
@@ -1755,22 +1769,18 @@
   }
 
     
-  static std::unique_ptr<IndexBackend> backend_;
-
   void DatabaseBackendAdapterV3::Register(IndexBackend* backend)
   {
+    if (isBackendInUse_)
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
+    }
+    
     if (backend == NULL)
     {
       throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
     }
 
-    if (backend_.get() != NULL)
-    {
-      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
-    }
-
-    backend_.reset(backend);
-
     OrthancPluginDatabaseBackendV3 params;
     memset(&params, 0, sizeof(params));
 
@@ -1843,20 +1853,25 @@
     params.setProtectedPatient = SetProtectedPatient;
     params.setResourcesContent = SetResourcesContent;
 
-    OrthancPluginContext* context = backend_->GetContext();
+    OrthancPluginContext* context = backend->GetContext();
  
-    if (OrthancPluginRegisterDatabaseBackendV3(context, &params, sizeof(params), backend_.get()) != OrthancPluginErrorCode_Success)
+    if (OrthancPluginRegisterDatabaseBackendV3(context, &params, sizeof(params), backend) != OrthancPluginErrorCode_Success)
     {
       throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, "Unable to register the database backend");
     }
 
-    backend_->SetOutputFactory(new Factory);
+    backend->SetOutputFactory(new Factory);
+
+    isBackendInUse_ = true;
   }
 
 
   void DatabaseBackendAdapterV3::Finalize()
   {
-    backend_.reset(NULL);
+    if (isBackendInUse_)
+    {
+      fprintf(stderr, "The Orthanc core has not destructed the index backend, internal error\n");
+    }
   }
 }