diff Framework/Plugins/DatabaseBackendAdapterV3.cpp @ 222:c8e06b41feec

refactoring registration/finalization of index backend
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 01 Apr 2021 11:16:13 +0200
parents 73cc85f3d9c1
children af049cd66661
line wrap: on
line diff
--- a/Framework/Plugins/DatabaseBackendAdapterV3.cpp	Tue Mar 30 10:40:34 2021 +0200
+++ b/Framework/Plugins/DatabaseBackendAdapterV3.cpp	Thu Apr 01 11:16:13 2021 +0200
@@ -1755,8 +1755,22 @@
   }
 
     
-  void DatabaseBackendAdapterV3::Register(IndexBackend& database)
+  static std::unique_ptr<IndexBackend> backend_;
+
+  void DatabaseBackendAdapterV3::Register(IndexBackend* backend)
   {
+    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));
 
@@ -1829,14 +1843,20 @@
     params.setProtectedPatient = SetProtectedPatient;
     params.setResourcesContent = SetResourcesContent;
 
-    OrthancPluginContext* context = database.GetContext();
+    OrthancPluginContext* context = backend_->GetContext();
  
-    if (OrthancPluginRegisterDatabaseBackendV3(context, &params, sizeof(params), &database) != OrthancPluginErrorCode_Success)
+    if (OrthancPluginRegisterDatabaseBackendV3(context, &params, sizeof(params), backend_.get()) != OrthancPluginErrorCode_Success)
     {
       throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, "Unable to register the database backend");
     }
 
-    database.SetOutputFactory(new Factory);
+    backend_->SetOutputFactory(new Factory);
+  }
+
+
+  void DatabaseBackendAdapterV3::Finalize()
+  {
+    backend_.reset(NULL);
   }
 }